Skip to content

Commit f4f28c4

Browse files
authored
feat: invaildate the token when change password (#552)
* feat: invaildate the token when change password * fix: replace CN comments. * remove unnecessary comments. * fix: define corrected code. * fix: fix uncorrect args.
1 parent 07f7853 commit f4f28c4

File tree

13 files changed

+948
-736
lines changed

13 files changed

+948
-736
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ require (
2727
github.com/mitchellh/mapstructure v1.5.0
2828
github.com/openimsdk/gomake v0.0.9
2929
github.com/openimsdk/protocol v0.0.63
30-
github.com/openimsdk/tools v0.0.49-alpha.18
30+
github.com/openimsdk/tools v0.0.49-alpha.24
3131
github.com/redis/go-redis/v9 v9.5.1
3232
github.com/spf13/cobra v1.8.0
3333
github.com/spf13/viper v1.18.2

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ github.com/openimsdk/protocol v0.0.63 h1:9DnweZe9nEYDFa4fGTbC9Cqi0gLUdtBhRo1NRP2
191191
github.com/openimsdk/protocol v0.0.63/go.mod h1:OZQA9FR55lseYoN2Ql1XAHYKHJGu7OMNkUbuekrKCM8=
192192
github.com/openimsdk/tools v0.0.49-alpha.18 h1:ARQeCiRmExvtB6XYItegThuV63JGOTxddwhSLHYXd78=
193193
github.com/openimsdk/tools v0.0.49-alpha.18/go.mod h1:g7mkHXYUPi0/8aAX8VPMHpnb3hqdV69Jph+bXOGvvNM=
194+
github.com/openimsdk/tools v0.0.49-alpha.24 h1:lJsqnjTPujnr91LRQ6QmcTliMIa4fMOBSTri6rFz2ek=
195+
github.com/openimsdk/tools v0.0.49-alpha.24/go.mod h1:g7mkHXYUPi0/8aAX8VPMHpnb3hqdV69Jph+bXOGvvNM=
194196
github.com/pelletier/go-toml/v2 v2.1.0 h1:FnwAJ4oYMvbT/34k9zzHuZNrhlz48GB3/s6at6/MHO4=
195197
github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
196198
github.com/pion/datachannel v1.5.5 h1:10ef4kwdjije+M9d7Xm9im2Y3O6A6ccQb0zcqZcJew8=

internal/api/chat/chat.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,28 @@ func (o *Api) ResetPassword(c *gin.Context) {
164164
}
165165

166166
func (o *Api) ChangePassword(c *gin.Context) {
167-
a2r.Call(chatpb.ChatClient.ChangePassword, o.chatClient, c)
167+
req, err := a2r.ParseRequest[chatpb.ChangePasswordReq](c)
168+
if err != nil {
169+
apiresp.GinError(c, err)
170+
return
171+
}
172+
resp, err := o.chatClient.ChangePassword(c, req)
173+
if err != nil {
174+
apiresp.GinError(c, err)
175+
return
176+
}
177+
178+
imToken, err := o.imApiCaller.ImAdminTokenWithDefaultAdmin(c)
179+
if err != nil {
180+
apiresp.GinError(c, err)
181+
return
182+
}
183+
err = o.imApiCaller.ForceOffLine(mctx.WithApiToken(c, imToken), req.UserID)
184+
if err != nil {
185+
apiresp.GinError(c, err)
186+
return
187+
}
188+
apiresp.GinSuccess(c, resp)
168189
}
169190

170191
// ################## USER ##################

internal/rpc/admin/token.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ package admin
1717
import (
1818
"context"
1919

20-
"github.com/openimsdk/chat/pkg/protocol/admin"
20+
"github.com/openimsdk/chat/pkg/eerrs"
21+
adminpb "github.com/openimsdk/chat/pkg/protocol/admin"
22+
"github.com/openimsdk/tools/log"
23+
"github.com/redis/go-redis/v9"
2124
)
2225

23-
func (o *adminServer) CreateToken(ctx context.Context, req *admin.CreateTokenReq) (*admin.CreateTokenResp, error) {
26+
func (o *adminServer) CreateToken(ctx context.Context, req *adminpb.CreateTokenReq) (*adminpb.CreateTokenResp, error) {
2427
token, err := o.Token.CreateToken(req.UserID, req.UserType)
2528
if err != nil {
2629
return nil, err
@@ -29,26 +32,46 @@ func (o *adminServer) CreateToken(ctx context.Context, req *admin.CreateTokenReq
2932
if err != nil {
3033
return nil, err
3134
}
32-
return &admin.CreateTokenResp{
35+
return &adminpb.CreateTokenResp{
3336
Token: token,
3437
}, nil
3538
}
3639

37-
func (o *adminServer) ParseToken(ctx context.Context, req *admin.ParseTokenReq) (*admin.ParseTokenResp, error) {
40+
func (o *adminServer) ParseToken(ctx context.Context, req *adminpb.ParseTokenReq) (*adminpb.ParseTokenResp, error) {
3841
userID, userType, err := o.Token.GetToken(req.Token)
3942
if err != nil {
4043
return nil, err
4144
}
42-
return &admin.ParseTokenResp{
45+
m, err := o.Database.GetTokens(ctx, userID)
46+
if err != nil && err != redis.Nil {
47+
return nil, err
48+
}
49+
if len(m) == 0 {
50+
return nil, eerrs.ErrTokenNotExist.Wrap()
51+
}
52+
if _, ok := m[req.Token]; !ok {
53+
return nil, eerrs.ErrTokenNotExist.Wrap()
54+
}
55+
56+
return &adminpb.ParseTokenResp{
4357
UserID: userID,
4458
UserType: userType,
4559
}, nil
4660
}
4761

48-
func (o *adminServer) GetUserToken(ctx context.Context, req *admin.GetUserTokenReq) (*admin.GetUserTokenResp, error) {
62+
func (o *adminServer) GetUserToken(ctx context.Context, req *adminpb.GetUserTokenReq) (*adminpb.GetUserTokenResp, error) {
4963
tokensMap, err := o.Database.GetTokens(ctx, req.UserID)
5064
if err != nil {
5165
return nil, err
5266
}
53-
return &admin.GetUserTokenResp{TokensMap: tokensMap}, nil
67+
return &adminpb.GetUserTokenResp{TokensMap: tokensMap}, nil
68+
}
69+
70+
func (o *adminServer) InvalidateToken(ctx context.Context, req *adminpb.InvalidateTokenReq) (*adminpb.InvalidateTokenResp, error) {
71+
err := o.Database.DeleteToken(ctx, req.UserID)
72+
if err != nil && err != redis.Nil {
73+
return nil, err
74+
}
75+
log.ZDebug(ctx, "delete token from redis", "userID", req.UserID)
76+
return &adminpb.InvalidateTokenResp{}, nil
5477
}

internal/rpc/chat/password.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,9 @@ func (o *chatSvr) ChangePassword(ctx context.Context, req *chat.ChangePasswordRe
9999
return nil, err
100100
}
101101
}
102+
if err := o.Admin.InvalidateToken(ctx, req.UserID); err != nil {
103+
return nil, err
104+
}
105+
102106
return &chat.ChangePasswordResp{}, nil
103107
}

pkg/common/db/cache/token.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package cache
1616

1717
import (
1818
"context"
19+
1920
"github.com/openimsdk/tools/utils/stringutil"
2021

2122
"github.com/openimsdk/tools/errs"
@@ -29,6 +30,7 @@ const (
2930
type TokenInterface interface {
3031
AddTokenFlag(ctx context.Context, userID string, token string, flag int) error
3132
GetTokensWithoutError(ctx context.Context, userID string) (map[string]int32, error)
33+
DeleteTokenByUid(ctx context.Context, userID string) error
3234
}
3335

3436
type TokenCacheRedis struct {
@@ -56,3 +58,8 @@ func (t *TokenCacheRedis) GetTokensWithoutError(ctx context.Context, userID stri
5658
}
5759
return mm, nil
5860
}
61+
62+
func (t *TokenCacheRedis) DeleteTokenByUid(ctx context.Context, userID string) error {
63+
key := chatToken + userID
64+
return errs.Wrap(t.rdb.Del(ctx, key).Err())
65+
}

pkg/common/db/database/admin.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ type AdminDatabaseInterface interface {
7676
GetLimitUserLoginIP(ctx context.Context, userID string, ip string) (*admindb.LimitUserLoginIP, error)
7777
CacheToken(ctx context.Context, userID string, token string) error
7878
GetTokens(ctx context.Context, userID string) (map[string]int32, error)
79+
DeleteToken(ctx context.Context, userID string) error
7980
}
8081

8182
func NewAdminDatabase(cli *mongoutil.Client, rdb redis.UniversalClient) (AdminDatabaseInterface, error) {
@@ -331,3 +332,7 @@ func (o *AdminDatabase) CacheToken(ctx context.Context, userID string, token str
331332
func (o *AdminDatabase) GetTokens(ctx context.Context, userID string) (map[string]int32, error) {
332333
return o.cache.GetTokensWithoutError(ctx, userID)
333334
}
335+
336+
func (o *AdminDatabase) DeleteToken(ctx context.Context, userID string) error {
337+
return o.cache.DeleteTokenByUid(ctx, userID)
338+
}

pkg/common/mctx/get.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ package mctx
1717
import (
1818
"context"
1919
"strconv"
20-
21-
2220
"github.com/openimsdk/tools/utils/datautil"
2321

2422
constantpb "github.com/openimsdk/protocol/constant"

pkg/eerrs/predefine.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ var (
3131
ErrForbidden = errs.NewCodeError(20012, "Forbidden")
3232
ErrRefuseFriend = errs.NewCodeError(20013, "RefuseFriend")
3333
ErrEmailAlreadyRegister = errs.NewCodeError(20014, "EmailAlreadyRegister")
34+
35+
ErrTokenNotExist = errs.NewCodeError(20101, "ErrTokenNotExist")
3436
)

0 commit comments

Comments
 (0)