Skip to content

Commit

Permalink
fix: admin token cache (#613)
Browse files Browse the repository at this point in the history
* fix: admin token cache

* fix: admin token cache
  • Loading branch information
icey-yu authored and OpenIM-Robot committed Nov 25, 2024
1 parent 0849ee4 commit a8f2a3d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 48 deletions.
10 changes: 5 additions & 5 deletions internal/api/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (o *Api) AdminUpdateInfo(c *gin.Context) {
}

imAdminUserID := o.GetDefaultIMAdminUserID()
imToken, err := o.imApiCaller.GetAdminToken(c, imAdminUserID)
imToken, err := o.imApiCaller.GetAdminTokenCache(c, imAdminUserID)
if err != nil {
log.ZError(c, "AdminUpdateInfo ImAdminTokenWithDefaultAdmin", err, "imAdminUserID", imAdminUserID)
return
Expand Down Expand Up @@ -193,7 +193,7 @@ func (o *Api) AddDefaultGroup(c *gin.Context) {
apiresp.GinError(c, err)
return
}
imToken, err := o.imApiCaller.GetAdminToken(c, o.GetDefaultIMAdminUserID())
imToken, err := o.imApiCaller.ImAdminTokenWithDefaultAdmin(c)
if err != nil {
apiresp.GinError(c, err)
return
Expand Down Expand Up @@ -241,7 +241,7 @@ func (o *Api) SearchDefaultGroup(c *gin.Context) {
Groups: make([]*sdkws.GroupInfo, 0, len(searchResp.GroupIDs)),
}
if len(searchResp.GroupIDs) > 0 {
imToken, err := o.imApiCaller.GetAdminToken(c, o.GetDefaultIMAdminUserID())
imToken, err := o.imApiCaller.ImAdminTokenWithDefaultAdmin(c)
if err != nil {
apiresp.GinError(c, err)
return
Expand Down Expand Up @@ -323,7 +323,7 @@ func (o *Api) BlockUser(c *gin.Context) {
apiresp.GinError(c, err)
return
}
imToken, err := o.imApiCaller.GetAdminToken(c, o.GetDefaultIMAdminUserID())
imToken, err := o.imApiCaller.ImAdminTokenWithDefaultAdmin(c)
if err != nil {
apiresp.GinError(c, err)
return
Expand Down Expand Up @@ -382,7 +382,7 @@ func (o *Api) NewUserCount(c *gin.Context) {
apiresp.GinError(c, err)
return
}
imToken, err := o.imApiCaller.GetAdminToken(c, o.GetDefaultIMAdminUserID())
imToken, err := o.imApiCaller.ImAdminTokenWithDefaultAdmin(c)
if err != nil {
apiresp.GinError(c, err)
return
Expand Down
16 changes: 2 additions & 14 deletions internal/api/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (

"github.com/gin-gonic/gin"
"github.com/openimsdk/chat/pkg/common/apistruct"
"github.com/openimsdk/chat/pkg/common/constant"
"github.com/openimsdk/chat/pkg/common/imapi"
"github.com/openimsdk/chat/pkg/common/mctx"
"github.com/openimsdk/chat/pkg/protocol/admin"
Expand Down Expand Up @@ -235,20 +234,9 @@ func (o *Api) UpdateUserInfo(c *gin.Context) {
apiresp.GinError(c, err)
return
}
opUserType, err := mctx.GetUserType(c)
if err != nil {
apiresp.GinError(c, err)
return
}

var imToken string
if opUserType == constant.NormalUser {
imToken, err = o.imApiCaller.ImAdminTokenWithDefaultAdmin(c)
} else if opUserType == constant.AdminUser {
imToken, err = o.imApiCaller.GetAdminToken(c, o.GetDefaultIMAdminUserID())
} else {
apiresp.GinError(c, errs.ErrArgs.WrapMsg("opUserType unknown"))
return
}
imToken, err = o.imApiCaller.ImAdminTokenWithDefaultAdmin(c)
if err != nil {
apiresp.GinError(c, err)
return
Expand Down
37 changes: 25 additions & 12 deletions pkg/common/imapi/caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type CallerInterface interface {
ImAdminTokenWithDefaultAdmin(ctx context.Context) (string, error)
ImportFriend(ctx context.Context, ownerUserID string, friendUserID []string) error
GetUserToken(ctx context.Context, userID string, platform int32) (string, error)
GetAdminToken(ctx context.Context, userID string) (string, error)
GetAdminTokenCache(ctx context.Context, userID string) (string, error)
InviteToGroup(ctx context.Context, userID string, groupIDs []string) error
UpdateUserInfo(ctx context.Context, userID string, nickName string, faceURL string) error
ForceOffLine(ctx context.Context, userID string) error
Expand All @@ -31,20 +31,26 @@ type CallerInterface interface {
AccountCheckSingle(ctx context.Context, userID string) (bool, error)
}

type authToken struct {
token string
timeout time.Time
}

type Caller struct {
imApi string
imSecret string
defaultIMUserID string
token string
timeout time.Time
lock sync.Mutex
tokenCache map[string]*authToken
lock sync.RWMutex
}

func New(imApi string, imSecret string, defaultIMUserID string) CallerInterface {
return &Caller{
imApi: imApi,
imSecret: imSecret,
defaultIMUserID: defaultIMUserID,
tokenCache: make(map[string]*authToken),
lock: sync.RWMutex{},
}
}

Expand All @@ -60,25 +66,32 @@ func (c *Caller) ImportFriend(ctx context.Context, ownerUserID string, friendUse
}

func (c *Caller) ImAdminTokenWithDefaultAdmin(ctx context.Context) (string, error) {
if c.token == "" || c.timeout.Before(time.Now()) {
return c.GetAdminTokenCache(ctx, c.defaultIMUserID)
}

func (c *Caller) GetAdminTokenCache(ctx context.Context, userID string) (string, error) {
c.lock.RLock()
t, ok := c.tokenCache[userID]
c.lock.RUnlock()
if !ok || t.timeout.Before(time.Now()) {
c.lock.Lock()
if c.token == "" || c.timeout.Before(time.Now()) {
userID := c.defaultIMUserID
token, err := c.GetAdminToken(ctx, userID)
t, ok = c.tokenCache[userID]
if !ok || t.timeout.Before(time.Now()) {
token, err := c.getAdminTokenServer(ctx, userID)
if err != nil {
log.ZError(ctx, "get im admin token", err, "userID", userID)
return "", err
}
log.ZDebug(ctx, "get im admin token", "userID", userID)
c.token = token
c.timeout = time.Now().Add(time.Minute * 5)
t = &authToken{token: token, timeout: time.Now().Add(time.Minute * 5)}
c.tokenCache[userID] = t
}
c.lock.Unlock()
}
return c.token, nil
return t.token, nil
}

func (c *Caller) GetAdminToken(ctx context.Context, userID string) (string, error) {
func (c *Caller) getAdminTokenServer(ctx context.Context, userID string) (string, error) {
resp, err := getAdminToken.Call(ctx, c.imApi, &auth.GetAdminTokenReq{
Secret: c.imSecret,
UserID: userID,
Expand Down
17 changes: 1 addition & 16 deletions pkg/common/tokenverify/token_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,7 @@ func (t *Token) GetToken(token string) (string, int32, error) {
return userID, userType, nil
}

func (t *Token) GetExpire(token string) time.Time {
val, err := jwt.ParseWithClaims(token, &claims{}, t.secret())
if err != nil {
return time.Time{}
}
c, ok := val.Claims.(*claims)
if !ok {
return time.Time{}
}
if c.ExpiresAt == nil {
return time.Time{}
}
return c.ExpiresAt.Time
}

//func (t *Token) GetAdminToken(token string) (string, error) {
//func (t *Token) GetAdminTokenCache(token string) (string, error) {
// userID, userType, err := getToken(token)
// if err != nil {
// return "", err
Expand Down
2 changes: 1 addition & 1 deletion tools/check-component/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func CheckRedis(ctx context.Context, config *config.Redis) error {

func CheckOpenIM(ctx context.Context, apiURL, secret, adminUserID string) error {
imAPI := imapi.New(apiURL, secret, adminUserID)
_, err := imAPI.GetAdminToken(mcontext.SetOperationID(ctx, "CheckOpenIM"+idutil.OperationIDGenerator()), adminUserID)
_, err := imAPI.GetAdminTokenCache(mcontext.SetOperationID(ctx, "CheckOpenIM"+idutil.OperationIDGenerator()), adminUserID)
return err
}

Expand Down

0 comments on commit a8f2a3d

Please sign in to comment.