Skip to content

Commit

Permalink
refactor: update token logic. (#575)
Browse files Browse the repository at this point in the history
* update dockerfile contents.

* update field.

* refactor: update token logic.

* remove unused ci.
  • Loading branch information
mo3et authored Sep 29, 2024
1 parent 189c094 commit e5878ab
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/chatci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
# name: openim
strategy:
matrix:
arch: [arm64, armv7, amd64]
arch: [amd64]

steps:
- uses: actions/checkout@v3
Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/uuid v1.6.0
github.com/jinzhu/copier v0.4.0 // indirect
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
google.golang.org/grpc v1.62.1
google.golang.org/protobuf v1.33.0
Expand All @@ -26,8 +25,8 @@ require (
github.com/livekit/protocol v1.10.1
github.com/mitchellh/mapstructure v1.5.0
github.com/openimsdk/gomake v0.0.14-alpha.5
github.com/openimsdk/protocol v0.0.63
github.com/openimsdk/tools v0.0.49-alpha.24
github.com/openimsdk/protocol v0.0.72-alpha.38
github.com/openimsdk/tools v0.0.50-alpha.15
github.com/redis/go-redis/v9 v9.5.1
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OS
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/openimsdk/gomake v0.0.14-alpha.5 h1:VY9c5x515lTfmdhhPjMvR3BBRrRquAUCFsz7t7vbv7Y=
github.com/openimsdk/gomake v0.0.14-alpha.5/go.mod h1:PndCozNc2IsQIciyn9mvEblYWZwJmAI+06z94EY+csI=
github.com/openimsdk/protocol v0.0.63 h1:9DnweZe9nEYDFa4fGTbC9Cqi0gLUdtBhRo1NRP2X3WQ=
github.com/openimsdk/protocol v0.0.63/go.mod h1:OZQA9FR55lseYoN2Ql1XAHYKHJGu7OMNkUbuekrKCM8=
github.com/openimsdk/tools v0.0.49-alpha.24 h1:lJsqnjTPujnr91LRQ6QmcTliMIa4fMOBSTri6rFz2ek=
github.com/openimsdk/tools v0.0.49-alpha.24/go.mod h1:g7mkHXYUPi0/8aAX8VPMHpnb3hqdV69Jph+bXOGvvNM=
github.com/openimsdk/protocol v0.0.72-alpha.38 h1:v0GLgS9cNm627NSG3B2k2VF2AMoo90DSKixxiBMKPS4=
github.com/openimsdk/protocol v0.0.72-alpha.38/go.mod h1:OZQA9FR55lseYoN2Ql1XAHYKHJGu7OMNkUbuekrKCM8=
github.com/openimsdk/tools v0.0.50-alpha.15 h1:HV9aKZ4vvCZCGG4wFDsgUONkkdJeCcrFNn3BT52nUVQ=
github.com/openimsdk/tools v0.0.50-alpha.15/go.mod h1:h1cYmfyaVtgFbKmb1Cfsl8XwUOMTt8ubVUQrdGtsUh4=
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
github.com/pion/datachannel v1.5.5 h1:10ef4kwdjije+M9d7Xm9im2Y3O6A6ccQb0zcqZcJew8=
Expand Down
12 changes: 6 additions & 6 deletions internal/api/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (o *Api) AdminLogin(c *gin.Context) {
return
}
imAdminUserID := o.GetDefaultIMAdminUserID()
imToken, err := o.imApiCaller.UserToken(c, imAdminUserID, constant.AdminPlatformID)
imToken, err := o.imApiCaller.AdminToken(c, imAdminUserID)
if err != nil {
apiresp.GinError(c, err)
return
Expand Down Expand Up @@ -127,7 +127,7 @@ func (o *Api) AdminUpdateInfo(c *gin.Context) {
}

imAdminUserID := o.GetDefaultIMAdminUserID()
imToken, err := o.imApiCaller.UserToken(c, imAdminUserID, constant.AdminPlatformID)
imToken, err := o.imApiCaller.AdminToken(c, imAdminUserID)
if err != nil {
log.ZError(c, "AdminUpdateInfo ImAdminTokenWithDefaultAdmin", err, "imAdminUserID", imAdminUserID)
return
Expand Down Expand Up @@ -207,7 +207,7 @@ func (o *Api) AddDefaultGroup(c *gin.Context) {
apiresp.GinError(c, err)
return
}
imToken, err := o.imApiCaller.UserToken(c, o.GetDefaultIMAdminUserID(), constant.AdminPlatformID)
imToken, err := o.imApiCaller.AdminToken(c, o.GetDefaultIMAdminUserID())
if err != nil {
apiresp.GinError(c, err)
return
Expand Down Expand Up @@ -255,7 +255,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.UserToken(c, o.GetDefaultIMAdminUserID(), constant.AdminPlatformID)
imToken, err := o.imApiCaller.AdminToken(c, o.GetDefaultIMAdminUserID())
if err != nil {
apiresp.GinError(c, err)
return
Expand Down Expand Up @@ -337,7 +337,7 @@ func (o *Api) BlockUser(c *gin.Context) {
apiresp.GinError(c, err)
return
}
imToken, err := o.imApiCaller.UserToken(c, o.GetDefaultIMAdminUserID(), constant.AdminPlatformID)
imToken, err := o.imApiCaller.AdminToken(c, o.GetDefaultIMAdminUserID())
if err != nil {
apiresp.GinError(c, err)
return
Expand Down Expand Up @@ -396,7 +396,7 @@ func (o *Api) NewUserCount(c *gin.Context) {
apiresp.GinError(c, err)
return
}
imToken, err := o.imApiCaller.UserToken(c, o.GetDefaultIMAdminUserID(), constant.AdminPlatformID)
imToken, err := o.imApiCaller.AdminToken(c, o.GetDefaultIMAdminUserID())
if err != nil {
apiresp.GinError(c, err)
return
Expand Down
13 changes: 10 additions & 3 deletions internal/api/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (o *Api) RegisterUser(c *gin.Context) {
}
var resp apistruct.UserRegisterResp
if req.AutoLogin {
resp.ImToken, err = o.imApiCaller.UserToken(c, respRegisterUser.UserID, req.Platform)
resp.ImToken, err = o.imApiCaller.GetUserToken(apiCtx, respRegisterUser.UserID, req.Platform)
if err != nil {
apiresp.GinError(c, err)
return
Expand Down Expand Up @@ -174,7 +174,14 @@ func (o *Api) Login(c *gin.Context) {
apiresp.GinError(c, err)
return
}
imToken, err := o.imApiCaller.UserToken(c, resp.UserID, req.Platform)
adminToken, err := o.imApiCaller.ImAdminTokenWithDefaultAdmin(c)
if err != nil {
apiresp.GinError(c, err)
return
}
apiCtx := mctx.WithApiToken(c, adminToken)

imToken, err := o.imApiCaller.GetUserToken(apiCtx, resp.UserID, req.Platform)
if err != nil {
apiresp.GinError(c, err)
return
Expand Down Expand Up @@ -237,7 +244,7 @@ func (o *Api) UpdateUserInfo(c *gin.Context) {
if opUserType == constant.NormalUser {
imToken, err = o.imApiCaller.ImAdminTokenWithDefaultAdmin(c)
} else if opUserType == constant.AdminUser {
imToken, err = o.imApiCaller.UserToken(c, o.GetDefaultIMAdminUserID(), constantpb.AdminPlatformID)
imToken, err = o.imApiCaller.AdminToken(c, o.GetDefaultIMAdminUserID())
} else {
apiresp.GinError(c, errs.ErrArgs.WrapMsg("opUserType unknown"))
return
Expand Down
8 changes: 6 additions & 2 deletions pkg/common/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ package cmd

import (
"fmt"
"github.com/openimsdk/chat/pkg/common/config"
"path/filepath"

"github.com/openimsdk/chat/pkg/common/config"

"github.com/openimsdk/tools/errs"
"github.com/openimsdk/tools/log"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -117,16 +118,19 @@ func (r *RootCmd) applyOptions(opts ...func(*CmdOpts)) *CmdOpts {
}

func (r *RootCmd) initializeLogger(cmdOpts *CmdOpts) error {
err := log.InitFromConfig(
err := log.InitLoggerFromConfig(
cmdOpts.loggerPrefixName,
r.processName,
"",
"",
r.log.RemainLogLevel,
r.log.IsStdout,
r.log.IsJson,
r.log.StorageLocation,
r.log.RemainRotationCount,
r.log.RotationTime,
config.Version,
false,
)
if err != nil {
return errs.Wrap(err)
Expand Down
7 changes: 4 additions & 3 deletions pkg/common/imapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@ package imapi

import (
"github.com/openimsdk/protocol/auth"
"github.com/openimsdk/protocol/friend"
"github.com/openimsdk/protocol/group"
"github.com/openimsdk/protocol/relation"
"github.com/openimsdk/protocol/user"
)

// im caller.
var (
importFriend = NewApiCaller[friend.ImportFriendReq, friend.ImportFriendResp]("/friend/import_friend")
importFriend = NewApiCaller[relation.ImportFriendReq, relation.ImportFriendResp]("/friend/import_friend")
userToken = NewApiCaller[auth.UserTokenReq, auth.UserTokenResp]("/auth/user_token")
getuserToken = NewApiCaller[auth.GetUserTokenReq, auth.GetUserTokenResp]("/auth/get_user_token")
inviteToGroup = NewApiCaller[group.InviteUserToGroupReq, group.InviteUserToGroupResp]("/group/invite_user_to_group")
updateUserInfo = NewApiCaller[user.UpdateUserInfoReq, user.UpdateUserInfoResp]("/user/update_user_info")
registerUser = NewApiCaller[user.UserRegisterReq, user.UserRegisterResp]("/user/user_register")
forceOffLine = NewApiCaller[auth.ForceLogoutReq, auth.ForceLogoutResp]("/auth/force_logout")
getGroupsInfo = NewApiCaller[group.GetGroupsInfoReq, group.GetGroupsInfoResp]("/group/get_groups_info")
registerUserCount = NewApiCaller[user.UserRegisterCountReq, user.UserRegisterCountResp]("/statistics/user/register")
friendUserIDs = NewApiCaller[friend.GetFriendIDsReq, friend.GetFriendIDsResp]("/friend/get_friend_id")
friendUserIDs = NewApiCaller[relation.GetFriendIDsReq, relation.GetFriendIDsResp]("/friend/get_friend_id")
accountCheck = NewApiCaller[user.AccountCheckReq, user.AccountCheckResp]("/user/account_check")
)
31 changes: 21 additions & 10 deletions pkg/common/imapi/caller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@ import (
"github.com/openimsdk/tools/log"

"github.com/openimsdk/protocol/auth"
"github.com/openimsdk/protocol/constant"
constantpb "github.com/openimsdk/protocol/constant"
"github.com/openimsdk/protocol/friend"
"github.com/openimsdk/protocol/group"
"github.com/openimsdk/protocol/relation"
"github.com/openimsdk/protocol/sdkws"
"github.com/openimsdk/protocol/user"
)

type CallerInterface interface {
ImAdminTokenWithDefaultAdmin(ctx context.Context) (string, error)
ImportFriend(ctx context.Context, ownerUserID string, friendUserID []string) error
UserToken(ctx context.Context, userID string, platform int32) (string, error)
GetUserToken(ctx context.Context, userID string, platform int32) (string, error)
AdminToken(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 Down Expand Up @@ -65,7 +67,7 @@ func (c *Caller) ImportFriend(ctx context.Context, ownerUserID string, friendUse
if len(friendUserIDs) == 0 {
return nil
}
_, err := importFriend.Call(ctx, c.imApi, &friend.ImportFriendReq{
_, err := importFriend.Call(ctx, c.imApi, &relation.ImportFriendReq{
OwnerUserID: ownerUserID,
FriendUserIDs: friendUserIDs,
})
Expand All @@ -77,7 +79,7 @@ func (c *Caller) ImAdminTokenWithDefaultAdmin(ctx context.Context) (string, erro
defer c.lock.Unlock()
if c.token == "" || c.timeout.Before(time.Now()) {
userID := c.defaultIMUserID
token, err := c.UserToken(ctx, userID, constantpb.AdminPlatformID)
token, err := c.AdminToken(ctx, userID)
if err != nil {
log.ZError(ctx, "get im admin token", err, "userID", userID)
return "", err
Expand All @@ -89,9 +91,19 @@ func (c *Caller) ImAdminTokenWithDefaultAdmin(ctx context.Context) (string, erro
return c.token, nil
}

func (c *Caller) UserToken(ctx context.Context, userID string, platformID int32) (string, error) {
func (c *Caller) AdminToken(ctx context.Context, userID string) (string, error) {
resp, err := userToken.Call(ctx, c.imApi, &auth.UserTokenReq{
Secret: c.imSecret,
Secret: c.imSecret,
UserID: userID,
})
if err != nil {
return "", err
}
return resp.Token, nil
}

func (c *Caller) GetUserToken(ctx context.Context, userID string, platformID int32) (string, error) {
resp, err := getuserToken.Call(ctx, c.imApi, &auth.GetUserTokenReq{
PlatformID: platformID,
UserID: userID,
})
Expand Down Expand Up @@ -123,8 +135,7 @@ func (c *Caller) UpdateUserInfo(ctx context.Context, userID string, nickName str

func (c *Caller) RegisterUser(ctx context.Context, users []*sdkws.UserInfo) error {
_, err := registerUser.Call(ctx, c.imApi, &user.UserRegisterReq{
Secret: c.imSecret,
Users: users,
Users: users,
})
return err
}
Expand Down Expand Up @@ -161,7 +172,7 @@ func (c *Caller) UserRegisterCount(ctx context.Context, start int64, end int64)
}

func (c *Caller) FriendUserIDs(ctx context.Context, userID string) ([]string, error) {
resp, err := friendUserIDs.Call(ctx, c.imApi, &friend.GetFriendIDsReq{UserID: userID})
resp, err := friendUserIDs.Call(ctx, c.imApi, &relation.GetFriendIDsReq{UserID: userID})
if err != nil {
return nil, err
}
Expand All @@ -174,7 +185,7 @@ func (c *Caller) AccountCheckSingle(ctx context.Context, userID string) (bool, e
if err != nil {
return false, err
}
if resp.Results[0].AccountStatus == "registered" {
if resp.Results[0].AccountStatus == constant.Registered {
return false, eerrs.ErrAccountAlreadyRegister.Wrap()
}
return true, nil
Expand Down
3 changes: 1 addition & 2 deletions tools/check-component/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/openimsdk/chat/pkg/common/cmd"
"github.com/openimsdk/chat/pkg/common/config"
"github.com/openimsdk/chat/pkg/common/imapi"
constantpb "github.com/openimsdk/protocol/constant"
"github.com/openimsdk/tools/db/mongoutil"
"github.com/openimsdk/tools/db/redisutil"
"github.com/openimsdk/tools/discovery/etcd"
Expand Down Expand Up @@ -58,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.UserToken(mcontext.SetOperationID(ctx, "CheckOpenIM"+idutil.OperationIDGenerator()), adminUserID, constantpb.AdminPlatformID)
_, err := imAPI.AdminToken(mcontext.SetOperationID(ctx, "CheckOpenIM"+idutil.OperationIDGenerator()), adminUserID)
return err
}

Expand Down

0 comments on commit e5878ab

Please sign in to comment.