Skip to content

Commit

Permalink
refactor: add GetAdminToken interface. (#2684)
Browse files Browse the repository at this point in the history
* refactor: add GetAdminToken interface.

* update config.
  • Loading branch information
mo3et authored Sep 29, 2024
1 parent 765fa17 commit 5452741
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/gorilla/websocket v1.5.1
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/mitchellh/mapstructure v1.5.0
github.com/openimsdk/protocol v0.0.72-alpha.38
github.com/openimsdk/protocol v0.0.72
github.com/openimsdk/tools v0.0.50-alpha.15
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.18.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ github.com/onsi/gomega v1.25.0 h1:Vw7br2PCDYijJHSfBOWhov+8cAnUf8MfMaIOV323l6Y=
github.com/onsi/gomega v1.25.0/go.mod h1:r+zV744Re+DiYCIPRlYOTxn0YkOLcAnW8k1xXdMPGhM=
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.72-alpha.38 h1:v0GLgS9cNm627NSG3B2k2VF2AMoo90DSKixxiBMKPS4=
github.com/openimsdk/protocol v0.0.72-alpha.38/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.15 h1:HV9aKZ4vvCZCGG4wFDsgUONkkdJeCcrFNn3BT52nUVQ=
github.com/openimsdk/tools v0.0.50-alpha.15/go.mod h1:h1cYmfyaVtgFbKmb1Cfsl8XwUOMTt8ubVUQrdGtsUh4=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
Expand Down
4 changes: 2 additions & 2 deletions internal/api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func NewAuthApi(client rpcclient.Auth) AuthApi {
return AuthApi(client)
}

func (o *AuthApi) UserToken(c *gin.Context) {
a2r.Call(auth.AuthClient.UserToken, o.Client, c)
func (o *AuthApi) GetAdminToken(c *gin.Context) {
a2r.Call(auth.AuthClient.GetAdminToken, o.Client, c)
}

func (o *AuthApi) GetUserToken(c *gin.Context) {
Expand Down
4 changes: 2 additions & 2 deletions internal/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func newGinRouter(disCov discovery.SvcDiscoveryRegistry, config *Config) *gin.En
authRouterGroup := r.Group("/auth")
{
a := NewAuthApi(*authRpc)
authRouterGroup.POST("/user_token", a.UserToken)
authRouterGroup.POST("/get_admin_token", a.GetAdminToken)
authRouterGroup.POST("/get_user_token", a.GetUserToken)
authRouterGroup.POST("/parse_token", a.ParseToken)
authRouterGroup.POST("/force_logout", a.ForceLogout)
Expand Down Expand Up @@ -288,6 +288,6 @@ func GinParseToken(authRPC *rpcclient.Auth) gin.HandlerFunc {

// Whitelist api not parse token
var Whitelist = []string{
"/auth/user_token",
"/auth/get_admin_token",
"/auth/parse_token",
}
4 changes: 2 additions & 2 deletions internal/rpc/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ func Start(ctx context.Context, config *Config, client discovery.SvcDiscoveryReg
return nil
}

func (s *authServer) UserToken(ctx context.Context, req *pbauth.UserTokenReq) (*pbauth.UserTokenResp, error) {
resp := pbauth.UserTokenResp{}
func (s *authServer) GetAdminToken(ctx context.Context, req *pbauth.GetAdminTokenReq) (*pbauth.GetAdminTokenResp, error) {
resp := pbauth.GetAdminTokenResp{}
if req.Secret != s.config.Share.Secret {
return nil, errs.ErrNoPermission.WrapMsg("secret invalid")
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/common/cmd/msg_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
package cmd

import (
"math"
"testing"

"github.com/openimsdk/protocol/auth"
"github.com/openimsdk/tools/apiresp"
"github.com/openimsdk/tools/utils/jsonutil"
"github.com/stretchr/testify/mock"
"go.mongodb.org/mongo-driver/bson/primitive"
"math"
"testing"
)

// MockRootCmd is a mock type for the RootCmd type
Expand All @@ -39,7 +40,7 @@ func TestName(t *testing.T) {
ErrCode: 1234,
ErrMsg: "test",
ErrDlt: "4567",
Data: &auth.UserTokenResp{
Data: &auth.GetUserTokenResp{
Token: "1234567",
ExpireTimeSeconds: math.MaxInt64,
},
Expand All @@ -51,7 +52,7 @@ func TestName(t *testing.T) {
t.Log(string(data))

var rReso apiresp.ApiResponse
rReso.Data = &auth.UserTokenResp{}
rReso.Data = &auth.GetUserTokenResp{}

if err := jsonutil.JsonUnmarshal(data, &rReso); err != nil {
panic(err)
Expand Down
8 changes: 4 additions & 4 deletions tools/url2im/pkg/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ func (a *Api) apiPost(ctx context.Context, path string, req any, resp any) error
return nil
}

func (a *Api) GetToken(ctx context.Context) (string, error) {
req := auth.UserTokenReq{
func (a *Api) GetAdminToken(ctx context.Context) (string, error) {
req := auth.GetAdminTokenReq{
UserID: a.UserID,
Secret: a.Secret,
}
var resp auth.UserTokenResp
if err := a.apiPost(ctx, "/auth/user_token", &req, &resp); err != nil {
var resp auth.GetAdminTokenResp
if err := a.apiPost(ctx, "/auth/get_admin_token", &req, &resp); err != nil {
return "", err
}
return resp.Token, nil
Expand Down
5 changes: 3 additions & 2 deletions tools/url2im/pkg/manage.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/openimsdk/tools/errs"
"io"
"log"
"net/http"
Expand All @@ -34,6 +33,8 @@ import (
"sync/atomic"
"time"

"github.com/openimsdk/tools/errs"

"github.com/openimsdk/protocol/third"
)

Expand Down Expand Up @@ -95,7 +96,7 @@ func (m *Manage) Run() error {
}
var err error
ctx := context.WithValue(m.ctx, "operationID", fmt.Sprintf("%s_init", m.prefix))
m.api.Token, err = m.api.GetToken(ctx)
m.api.Token, err = m.api.GetAdminToken(ctx)
if err != nil {
return err
}
Expand Down

0 comments on commit 5452741

Please sign in to comment.