Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
mo3et authored Aug 2, 2024
2 parents 60eacb6 + 2e7893e commit 55f1802
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 13 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) {
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
2 changes: 1 addition & 1 deletion livekit/livekit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ rtc:
use_external_ip: true
enable_loopback_candidate: false
keys:
APIftrpEkL9x2pa: 23ztfSqsfQ8hKkHzHTl3Z4bvaxro0snjk5jwbp5p6Q3
APIGPW3gnFTzqHH: 23ztfSqsfQ8hKkHzHTl3Z4bvaxro0snjk5jwbp5p6Q3
2 changes: 1 addition & 1 deletion pkg/common/config/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.7.0
v1.8.0
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 55f1802

Please sign in to comment.