Skip to content

Commit

Permalink
fix: admin token cache
Browse files Browse the repository at this point in the history
  • Loading branch information
icey-yu committed Nov 1, 2024
1 parent 276ec1f commit 5688d05
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 33 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
35 changes: 23 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,13 +31,17 @@ 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 {
Expand All @@ -60,25 +64,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
2 changes: 1 addition & 1 deletion pkg/common/tokenverify/token_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (t *Token) GetToken(token string) (string, int32, error) {
return userID, userType, nil
}

//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 5688d05

Please sign in to comment.