Skip to content

Commit

Permalink
fix:create token can set expire time (#553)
Browse files Browse the repository at this point in the history
Co-authored-by: skiffer-git <[email protected]>
  • Loading branch information
icey-yu and skiffer-git authored Aug 2, 2024
1 parent 8581365 commit 2e7893e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
7 changes: 4 additions & 3 deletions internal/rpc/admin/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ import (
"github.com/redis/go-redis/v9"
)

func (o *adminServer) CreateToken(ctx context.Context, req *adminpb.CreateTokenReq) (*adminpb.CreateTokenResp, error) {
token, err := o.Token.CreateToken(req.UserID, req.UserType)
func (o *adminServer) CreateToken(ctx context.Context, req *admin.CreateTokenReq) (*admin.CreateTokenResp, error) {

Check failure on line 26 in internal/rpc/admin/token.go

View workflow job for this annotation

GitHub Actions / Execute OpenIM Script On Linux (arm64)

undefined: admin

Check failure on line 26 in internal/rpc/admin/token.go

View workflow job for this annotation

GitHub Actions / Execute OpenIM Script On Linux (arm64)

undefined: admin

Check failure on line 26 in internal/rpc/admin/token.go

View workflow job for this annotation

GitHub Actions / Execute OpenIM Script On Linux (arm64)

undefined: admin

Check failure on line 26 in internal/rpc/admin/token.go

View workflow job for this annotation

GitHub Actions / Execute OpenIM Script On Linux (arm64)

undefined: admin

Check failure on line 26 in internal/rpc/admin/token.go

View workflow job for this annotation

GitHub Actions / Execute OpenIM Script On Linux (armv7)

undefined: admin

Check failure on line 26 in internal/rpc/admin/token.go

View workflow job for this annotation

GitHub Actions / Execute OpenIM Script On Linux (armv7)

undefined: admin

Check failure on line 26 in internal/rpc/admin/token.go

View workflow job for this annotation

GitHub Actions / Execute OpenIM Script On Linux (armv7)

undefined: admin

Check failure on line 26 in internal/rpc/admin/token.go

View workflow job for this annotation

GitHub Actions / Execute OpenIM Script On Linux (armv7)

undefined: admin

Check failure on line 26 in internal/rpc/admin/token.go

View workflow job for this annotation

GitHub Actions / Execute OpenIM Script On Linux (amd64)

undefined: admin

Check failure on line 26 in internal/rpc/admin/token.go

View workflow job for this annotation

GitHub Actions / Execute OpenIM Script On Linux (amd64)

undefined: admin

Check failure on line 26 in internal/rpc/admin/token.go

View workflow job for this annotation

GitHub Actions / Execute OpenIM Script On Linux (amd64)

undefined: admin

Check failure on line 26 in internal/rpc/admin/token.go

View workflow job for this annotation

GitHub Actions / Execute OpenIM Script On Linux (amd64)

undefined: admin
token, expire, err := o.Token.CreateToken(req.UserID, req.UserType)

if err != nil {
return nil, err
}
err = o.Database.CacheToken(ctx, req.UserID, token)
err = o.Database.CacheToken(ctx, req.UserID, token, expire)
if err != nil {
return nil, err
}
Expand Down
21 changes: 20 additions & 1 deletion pkg/common/db/cache/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"

"github.com/openimsdk/tools/utils/stringutil"
"time"

"github.com/openimsdk/tools/errs"
"github.com/redis/go-redis/v9"
Expand All @@ -29,12 +30,14 @@ const (

type TokenInterface interface {
AddTokenFlag(ctx context.Context, userID string, token string, flag int) error
AddTokenFlagNXEx(ctx context.Context, userID string, token string, flag int, expire time.Duration) (bool, error)
GetTokensWithoutError(ctx context.Context, userID string) (map[string]int32, error)
DeleteTokenByUid(ctx context.Context, userID string) error
}

type TokenCacheRedis struct {
rdb redis.UniversalClient
rdb redis.UniversalClient
accessExpire int64
}

func NewTokenInterface(rdb redis.UniversalClient) *TokenCacheRedis {
Expand All @@ -46,6 +49,22 @@ func (t *TokenCacheRedis) AddTokenFlag(ctx context.Context, userID string, token
return errs.Wrap(t.rdb.HSet(ctx, key, token, flag).Err())
}

func (t *TokenCacheRedis) AddTokenFlagNXEx(ctx context.Context, userID string, token string, flag int, expire time.Duration) (bool, error) {
key := chatToken + userID
isSet, err := t.rdb.HSetNX(ctx, key, token, flag).Result()
if err != nil {
return false, errs.Wrap(err)
}
if !isSet {
// key already exists
return false, nil
}
if err = t.rdb.Expire(ctx, key, expire).Err(); err != nil {
return false, errs.Wrap(err)
}
return isSet, nil
}

func (t *TokenCacheRedis) GetTokensWithoutError(ctx context.Context, userID string) (map[string]int32, error) {
key := chatToken + userID
m, err := t.rdb.HGetAll(ctx, key).Result()
Expand Down
17 changes: 14 additions & 3 deletions pkg/common/db/database/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package database

import (
"context"
"time"

"github.com/openimsdk/chat/pkg/common/db/cache"
"github.com/openimsdk/protocol/constant"
Expand Down Expand Up @@ -74,7 +75,7 @@ type AdminDatabaseInterface interface {
DelUserLimitLogin(ctx context.Context, ms []*admindb.LimitUserLoginIP) error
CountLimitUserLoginIP(ctx context.Context, userID string) (uint32, error)
GetLimitUserLoginIP(ctx context.Context, userID string, ip string) (*admindb.LimitUserLoginIP, error)
CacheToken(ctx context.Context, userID string, token string) error
CacheToken(ctx context.Context, userID string, token string, expire time.Duration) error
GetTokens(ctx context.Context, userID string) (map[string]int32, error)
DeleteToken(ctx context.Context, userID string) error
}
Expand Down Expand Up @@ -325,8 +326,18 @@ func (o *AdminDatabase) GetLimitUserLoginIP(ctx context.Context, userID string,
return o.limitUserLoginIP.Take(ctx, userID, ip)
}

func (o *AdminDatabase) CacheToken(ctx context.Context, userID string, token string) error {
return o.cache.AddTokenFlag(ctx, userID, token, constant.NormalToken)
func (o *AdminDatabase) CacheToken(ctx context.Context, userID string, token string, expire time.Duration) error {
isSet, err := o.cache.AddTokenFlagNXEx(ctx, userID, token, constant.NormalToken, expire)
if err != nil {
return err
}
if !isSet {
// already exists, update
if err = o.cache.AddTokenFlag(ctx, userID, token, constant.NormalToken); err != nil {
return err
}
}
return nil
}

func (o *AdminDatabase) GetTokens(ctx context.Context, userID string) (map[string]int32, error) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/common/tokenverify/token_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ func (t *Token) getToken(str string) (string, int32, error) {
}
}

func (t *Token) CreateToken(UserID string, userType int32) (string, error) {
func (t *Token) CreateToken(UserID string, userType int32) (string, time.Duration, error) {
if !(userType == TokenUser || userType == TokenAdmin) {
return "", errs.ErrTokenUnknown.WrapMsg("token type unknown")
return "", 0, errs.ErrTokenUnknown.WrapMsg("token type unknown")
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, t.buildClaims(UserID, userType))
str, err := token.SignedString([]byte(t.Secret))
if err != nil {
return "", errs.Wrap(err)
return "", 0, errs.Wrap(err)
}
return str, nil
return str, t.Expires, nil
}

func (t *Token) GetToken(token string) (string, int32, error) {
Expand Down

0 comments on commit 2e7893e

Please sign in to comment.