Skip to content

Commit

Permalink
refactor: update userToken to getAdminToken (#727)
Browse files Browse the repository at this point in the history
  • Loading branch information
mo3et authored Sep 29, 2024
1 parent cded399 commit 1aa0bee
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 28 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require golang.org/x/net v0.22.0 // indirect

require (
github.com/google/go-cmp v0.6.0
github.com/openimsdk/protocol v0.0.72-alpha.24
github.com/openimsdk/protocol v0.0.72
github.com/openimsdk/tools v0.0.50-alpha.14
github.com/patrickmn/go-cache v2.1.0+incompatible
golang.org/x/image v0.15.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205Ah
github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/openimsdk/protocol v0.0.72-alpha.24 h1:1Dl8TEZVXhdmve+ewoLkJa2wbFBIHqPgjvr9u/J66JM=
github.com/openimsdk/protocol v0.0.72-alpha.24/go.mod h1:OZQA9FR55lseYoN2Ql1XAHYKHJGu7OMNkUbuekrKCM8=
github.com/openimsdk/protocol v0.0.72 h1:K+vslwaR7lDXyBzb07UuEQITaqsgighz7NyXVIWsu6A=
github.com/openimsdk/protocol v0.0.72/go.mod h1:OZQA9FR55lseYoN2Ql1XAHYKHJGu7OMNkUbuekrKCM8=
github.com/openimsdk/tools v0.0.50-alpha.14 h1:7mqOwJWHcG181KvkmU+oJn9ZpYNoUykt9WtuOQg4sDU=
github.com/openimsdk/tools v0.0.50-alpha.14/go.mod h1:h1cYmfyaVtgFbKmb1Cfsl8XwUOMTt8ubVUQrdGtsUh4=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
Expand Down
6 changes: 3 additions & 3 deletions integration_test/internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ func (m *MetaManager) GetSecret() string {
}

func (m *MetaManager) GetAdminToken(userID string, platformID int32) (string, error) {
req := authPB.UserTokenReq{PlatformID: platformID, UserID: userID, Secret: m.secret}
resp := authPB.UserTokenResp{}
err := m.PostWithCtx(api.UsersToken.Route(), &req, &resp)
req := authPB.GetAdminTokenReq{UserID: userID, Secret: m.secret}
resp := authPB.GetAdminTokenResp{}
err := m.PostWithCtx(api.GetAdminToken.Route(), &req, &resp)
if err != nil {
return "", err
}
Expand Down
6 changes: 3 additions & 3 deletions integration_test/internal/manager/user_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package manager

import (
"context"
"time"

"github.com/openimsdk/openim-sdk-core/v3/integration_test/internal/config"
"github.com/openimsdk/openim-sdk-core/v3/integration_test/internal/pkg/decorator"
"github.com/openimsdk/openim-sdk-core/v3/integration_test/internal/pkg/progress"
Expand All @@ -17,7 +19,6 @@ import (
userPB "github.com/openimsdk/protocol/user"
"github.com/openimsdk/tools/log"
"github.com/openimsdk/tools/mcontext"
"time"
)

type TestUserManager struct {
Expand Down Expand Up @@ -53,8 +54,7 @@ func (t *TestUserManager) registerUsers(ctx context.Context, userIDs ...string)
end = len(users)
}
if err := t.PostWithCtx(api.UserRegister.Route(), &userPB.UserRegisterReq{
Secret: t.GetSecret(),
Users: users[i:end],
Users: users[i:end],
}, nil); err != nil {
return err
}
Expand Down
24 changes: 17 additions & 7 deletions msgtest/module/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/openimsdk/openim-sdk-core/v3/pkg/api"
"io"
"net/http"
"time"

"github.com/openimsdk/openim-sdk-core/v3/pkg/api"

"github.com/openimsdk/openim-sdk-core/v3/pkg/network"
"github.com/openimsdk/openim-sdk-core/v3/pkg/sdkerrs"
"github.com/openimsdk/openim-sdk-core/v3/pkg/utils"
authPB "github.com/openimsdk/protocol/auth"
"github.com/openimsdk/protocol/constant"
"github.com/openimsdk/protocol/msg"
"github.com/openimsdk/tools/errs"
"github.com/openimsdk/tools/log"
Expand Down Expand Up @@ -115,18 +115,28 @@ func (m *MetaManager) buildCtx() context.Context {
return mcontext.NewCtx(utils.OperationIDGenerator())
}

func (m *MetaManager) getToken(userID string, platformID int32) (string, error) {
req := authPB.UserTokenReq{PlatformID: platformID, UserID: userID, Secret: m.secret}
resp := authPB.UserTokenResp{}
err := m.postWithCtx(api.UsersToken.Route(), &req, &resp)
func (m *MetaManager) getAdminToken(userID string) (string, error) {
req := authPB.GetAdminTokenReq{UserID: userID, Secret: m.secret}
resp := authPB.GetAdminTokenResp{}
err := m.postWithCtx(api.GetAdminToken.Route(), &req, &resp)
if err != nil {
return "", err
}
return resp.Token, nil
}

func (m *MetaManager) getUserToken(userID string, platform int32) (string, error) {
req := authPB.GetUserTokenReq{UserID: userID, PlatformID: platform}
resp := authPB.GetUserTokenResp{}
err := m.postWithCtx(api.GetUsersToken.Route(), &req, &resp)
if err != nil {
return "", err
}
return resp.Token, nil
}

func (m *MetaManager) initToken() error {
token, err := m.getToken(m.managerUserID, constant.AdminPlatformID)
token, err := m.getAdminToken(m.managerUserID)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions msgtest/module/user_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package module

import (
"fmt"

"github.com/openimsdk/openim-sdk-core/v3/pkg/api"
"github.com/openimsdk/protocol/sdkws"
userPB "github.com/openimsdk/protocol/user"
Expand Down Expand Up @@ -37,11 +38,10 @@ func (t *TestUserManager) RegisterUsers(userIDs ...string) error {
users = append(users, &sdkws.UserInfo{UserID: userID, Nickname: userID})
}
return t.postWithCtx(api.UserRegister.Route(), &userPB.UserRegisterReq{
Secret: t.secret,
Users: users,
Users: users,
}, nil)
}

func (t *TestUserManager) GetToken(userID string, platformID int32) (string, error) {
return t.getToken(userID, platformID)
return t.getUserToken(userID, platformID)
}
4 changes: 2 additions & 2 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ var (
)

var (
UsersToken = newApi[auth.UserTokenReq, auth.UserTokenResp]("/auth/user_token")
GetUsersToken = newApi[auth.UserTokenReq, auth.UserTokenResp]("/auth/get_user_token")
GetAdminToken = newApi[auth.GetAdminTokenReq, auth.GetAdminTokenResp]("/auth/get_admin_token")
GetUsersToken = newApi[auth.GetUserTokenReq, auth.GetUserTokenResp]("/auth/get_user_token")
)

var (
Expand Down
13 changes: 6 additions & 7 deletions test/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func init() {
}
ctx = open_im_sdk.UserForSDK.Context()
ctx = ccontext.WithOperationID(ctx, "initOperationID_"+strconv.Itoa(int(time.Now().UnixMilli())))
token, err := GetUserToken(ctx, UserID, PlatformID, Secret)
token, err := GetAdminToken(ctx, UserID, Secret)
if err != nil {
panic(err)
}
Expand All @@ -77,11 +77,10 @@ func getConf(APIADDR, WSADDR string) sdk_struct.IMConfig {
return cf
}

func GetUserToken(ctx context.Context, userID string, platformID int32, secret string) (string, error) {
req := &auth.UserTokenReq{
UserID: userID,
PlatformID: platformID,
Secret: secret,
func GetAdminToken(ctx context.Context, userID string, secret string) (string, error) {
req := &auth.GetAdminTokenReq{
UserID: userID,
Secret: secret,
}
return api.ExtractField(ctx, api.UsersToken.Invoke, req, (*auth.UserTokenResp).GetToken)
return api.ExtractField(ctx, api.GetAdminToken.Invoke, req, (*auth.GetAdminTokenResp).GetToken)
}

0 comments on commit 1aa0bee

Please sign in to comment.