mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
Merge remote-tracking branch 'origin/errcode' into errcode
This commit is contained in:
commit
a15051bfde
@ -36,6 +36,7 @@ func NewRpcServer(registerIPInConfig string, port int, registerName string, zkSe
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
s.RegisterCenter = zkClient
|
s.RegisterCenter = zkClient
|
||||||
|
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
|||||||
"Open_IM/internal/common/rpc_server"
|
"Open_IM/internal/common/rpc_server"
|
||||||
"Open_IM/pkg/common/config"
|
"Open_IM/pkg/common/config"
|
||||||
"Open_IM/pkg/common/constant"
|
"Open_IM/pkg/common/constant"
|
||||||
|
"Open_IM/pkg/common/db/cache"
|
||||||
"Open_IM/pkg/common/db/controller"
|
"Open_IM/pkg/common/db/controller"
|
||||||
"Open_IM/pkg/common/log"
|
"Open_IM/pkg/common/log"
|
||||||
promePkg "Open_IM/pkg/common/prometheus"
|
promePkg "Open_IM/pkg/common/prometheus"
|
||||||
@ -23,8 +24,11 @@ func NewRpcAuthServer(port int) *rpcAuth {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
var redis cache.RedisClient
|
||||||
|
redis.InitRedis()
|
||||||
return &rpcAuth{
|
return &rpcAuth{
|
||||||
RpcServer: r,
|
RpcServer: r,
|
||||||
|
AuthInterface: controller.NewAuthController(redis.GetClient(), config.Config.TokenPolicy.AccessSecret, config.Config.TokenPolicy.AccessExpire),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,7 +68,7 @@ func (s *rpcAuth) UserToken(ctx context.Context, req *pbAuth.UserTokenReq) (*pbA
|
|||||||
if _, err := check.GetUsersInfo(ctx, req.UserID); err != nil {
|
if _, err := check.GetUsersInfo(ctx, req.UserID); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
token, err := s.CreateToken(ctx, req.UserID, int(req.PlatformID), config.Config.TokenPolicy.AccessExpire)
|
token, err := s.CreateToken(ctx, req.UserID, constant.PlatformIDToName(int(req.PlatformID)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -73,39 +77,41 @@ func (s *rpcAuth) UserToken(ctx context.Context, req *pbAuth.UserTokenReq) (*pbA
|
|||||||
return &resp, nil
|
return &resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *rpcAuth) parseToken(ctx context.Context, tokensString, operationID string) (claims *tokenverify.Claims, err error) {
|
func (s *rpcAuth) parseToken(ctx context.Context, tokensString string) (claims *tokenverify.Claims, err error) {
|
||||||
claims, err = tokenverify.GetClaimFromToken(tokensString)
|
claims, err = tokenverify.GetClaimFromToken(tokensString)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, utils.Wrap(err, "")
|
return nil, utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
m, err := s.GetTokens(ctx, claims.UID, claims.Platform)
|
m, err := s.GetTokensWithoutError(ctx, claims.UID, claims.Platform)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if len(m) == 0 {
|
||||||
|
return nil, constant.ErrTokenNotExist.Wrap()
|
||||||
|
}
|
||||||
if v, ok := m[tokensString]; ok {
|
if v, ok := m[tokensString]; ok {
|
||||||
switch v {
|
switch v {
|
||||||
case constant.NormalToken:
|
case constant.NormalToken:
|
||||||
return claims, nil
|
return claims, nil
|
||||||
case constant.KickedToken:
|
case constant.KickedToken:
|
||||||
return nil, utils.Wrap(constant.ErrTokenKicked, "this token has been kicked by other same terminal ")
|
return nil, constant.ErrTokenKicked.Wrap()
|
||||||
default:
|
default:
|
||||||
return nil, utils.Wrap(constant.ErrTokenUnknown, "")
|
return nil, utils.Wrap(constant.ErrTokenUnknown, "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, utils.Wrap(constant.ErrTokenNotExist, "redis token map not find")
|
return nil, constant.ErrTokenNotExist.Wrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *rpcAuth) ParseToken(ctx context.Context, req *pbAuth.ParseTokenReq) (*pbAuth.ParseTokenResp, error) {
|
func (s *rpcAuth) ParseToken(ctx context.Context, req *pbAuth.ParseTokenReq) (resp *pbAuth.ParseTokenResp, err error) {
|
||||||
resp := pbAuth.ParseTokenResp{}
|
resp = &pbAuth.ParseTokenResp{}
|
||||||
claims, err := s.parseToken(ctx, req.Token, req.OperationID)
|
claims, err := s.parseToken(ctx, req.Token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
resp.UserID = claims.UID
|
resp.UserID = claims.UID
|
||||||
resp.Platform = claims.Platform
|
resp.Platform = claims.Platform
|
||||||
resp.ExpireTimeSeconds = claims.ExpiresAt.Unix()
|
resp.ExpireTimeSeconds = claims.ExpiresAt.Unix()
|
||||||
return &resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *rpcAuth) ForceLogout(ctx context.Context, req *pbAuth.ForceLogoutReq) (*pbAuth.ForceLogoutResp, error) {
|
func (s *rpcAuth) ForceLogout(ctx context.Context, req *pbAuth.ForceLogoutReq) (*pbAuth.ForceLogoutResp, error) {
|
||||||
|
@ -84,7 +84,7 @@ func DbToPbGroupRequest(m *relation.GroupRequestModel, user *open_im_sdk.PublicU
|
|||||||
func DbToPbGroupAbstractInfo(groupID string, groupMemberNumber int32, groupMemberListHash uint64) *pbGroup.GroupAbstractInfo {
|
func DbToPbGroupAbstractInfo(groupID string, groupMemberNumber int32, groupMemberListHash uint64) *pbGroup.GroupAbstractInfo {
|
||||||
return &pbGroup.GroupAbstractInfo{
|
return &pbGroup.GroupAbstractInfo{
|
||||||
GroupID: groupID,
|
GroupID: groupID,
|
||||||
GroupMemberNumber: groupMemberNumber,
|
GroupMemberNumber: uint32(groupMemberNumber),
|
||||||
GroupMemberListHash: groupMemberListHash,
|
GroupMemberListHash: groupMemberListHash,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -665,15 +665,6 @@ func initConfig(config interface{}, configName, configPath string) {
|
|||||||
func InitConfig(configPath string) {
|
func InitConfig(configPath string) {
|
||||||
initConfig(&Config, "config.yaml", configPath)
|
initConfig(&Config, "config.yaml", configPath)
|
||||||
initConfig(&UsualConfig, "usualConfig.yaml", configPath)
|
initConfig(&UsualConfig, "usualConfig.yaml", configPath)
|
||||||
if Config.Etcd.UserName == "" {
|
|
||||||
Config.Etcd.UserName = UsualConfig.Etcd.UserName
|
|
||||||
}
|
|
||||||
if Config.Etcd.Password == "" {
|
|
||||||
Config.Etcd.Password = UsualConfig.Etcd.Password
|
|
||||||
}
|
|
||||||
if Config.Etcd.Secret == "" {
|
|
||||||
Config.Etcd.Secret = UsualConfig.Etcd.Secret
|
|
||||||
}
|
|
||||||
|
|
||||||
if Config.Mysql.DBUserName == "" {
|
if Config.Mysql.DBUserName == "" {
|
||||||
Config.Mysql.DBUserName = UsualConfig.Mysql.DBUserName
|
Config.Mysql.DBUserName = UsualConfig.Mysql.DBUserName
|
||||||
|
14
pkg/common/db/cache/redis.go
vendored
14
pkg/common/db/cache/redis.go
vendored
@ -23,7 +23,7 @@ const (
|
|||||||
userIncrSeq = "REDIS_USER_INCR_SEQ:" // user incr seq
|
userIncrSeq = "REDIS_USER_INCR_SEQ:" // user incr seq
|
||||||
appleDeviceToken = "DEVICE_TOKEN"
|
appleDeviceToken = "DEVICE_TOKEN"
|
||||||
userMinSeq = "REDIS_USER_MIN_SEQ:"
|
userMinSeq = "REDIS_USER_MIN_SEQ:"
|
||||||
uidPidToken = "UID_PID_TOKEN_STATUS:"
|
|
||||||
getuiToken = "GETUI_TOKEN"
|
getuiToken = "GETUI_TOKEN"
|
||||||
getuiTaskID = "GETUI_TASK_ID"
|
getuiTaskID = "GETUI_TASK_ID"
|
||||||
messageCache = "MESSAGE_CACHE:"
|
messageCache = "MESSAGE_CACHE:"
|
||||||
@ -94,33 +94,33 @@ func NewRedisClient(rdb redis.UniversalClient) *RedisClient {
|
|||||||
return &RedisClient{rdb: rdb}
|
return &RedisClient{rdb: rdb}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Perform seq auto-increment operation of user messages
|
// Perform seq auto-increment operation of user messages
|
||||||
func (r *RedisClient) IncrUserSeq(uid string) (uint64, error) {
|
func (r *RedisClient) IncrUserSeq(uid string) (uint64, error) {
|
||||||
key := userIncrSeq + uid
|
key := userIncrSeq + uid
|
||||||
seq, err := r.rdb.Incr(context.Background(), key).Result()
|
seq, err := r.rdb.Incr(context.Background(), key).Result()
|
||||||
return uint64(seq), err
|
return uint64(seq), err
|
||||||
}
|
}
|
||||||
|
|
||||||
//Get the largest Seq
|
// Get the largest Seq
|
||||||
func (r *RedisClient) GetUserMaxSeq(uid string) (uint64, error) {
|
func (r *RedisClient) GetUserMaxSeq(uid string) (uint64, error) {
|
||||||
key := userIncrSeq + uid
|
key := userIncrSeq + uid
|
||||||
seq, err := r.rdb.Get(context.Background(), key).Result()
|
seq, err := r.rdb.Get(context.Background(), key).Result()
|
||||||
return uint64(utils.StringToInt(seq)), err
|
return uint64(utils.StringToInt(seq)), err
|
||||||
}
|
}
|
||||||
|
|
||||||
//set the largest Seq
|
// set the largest Seq
|
||||||
func (r *RedisClient) SetUserMaxSeq(uid string, maxSeq uint64) error {
|
func (r *RedisClient) SetUserMaxSeq(uid string, maxSeq uint64) error {
|
||||||
key := userIncrSeq + uid
|
key := userIncrSeq + uid
|
||||||
return r.rdb.Set(context.Background(), key, maxSeq, 0).Err()
|
return r.rdb.Set(context.Background(), key, maxSeq, 0).Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
//Set the user's minimum seq
|
// Set the user's minimum seq
|
||||||
func (r *RedisClient) SetUserMinSeq(uid string, minSeq uint32) (err error) {
|
func (r *RedisClient) SetUserMinSeq(uid string, minSeq uint32) (err error) {
|
||||||
key := userMinSeq + uid
|
key := userMinSeq + uid
|
||||||
return r.rdb.Set(context.Background(), key, minSeq, 0).Err()
|
return r.rdb.Set(context.Background(), key, minSeq, 0).Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
//Get the smallest Seq
|
// Get the smallest Seq
|
||||||
func (r *RedisClient) GetUserMinSeq(uid string) (uint64, error) {
|
func (r *RedisClient) GetUserMinSeq(uid string) (uint64, error) {
|
||||||
key := userMinSeq + uid
|
key := userMinSeq + uid
|
||||||
seq, err := r.rdb.Get(context.Background(), key).Result()
|
seq, err := r.rdb.Get(context.Background(), key).Result()
|
||||||
@ -159,7 +159,7 @@ func (r *RedisClient) SetGroupMinSeq(groupID string, minSeq uint32) error {
|
|||||||
return r.rdb.Set(context.Background(), key, minSeq, 0).Err()
|
return r.rdb.Set(context.Background(), key, minSeq, 0).Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
//Store userid and platform class to redis
|
// Store userid and platform class to redis
|
||||||
func (r *RedisClient) AddTokenFlag(userID string, platformID int, token string, flag int) error {
|
func (r *RedisClient) AddTokenFlag(userID string, platformID int, token string, flag int) error {
|
||||||
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID)
|
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID)
|
||||||
log2.NewDebug("", "add token key is ", key)
|
log2.NewDebug("", "add token key is ", key)
|
||||||
|
75
pkg/common/db/cache/token.go
vendored
Normal file
75
pkg/common/db/cache/token.go
vendored
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Open_IM/pkg/common/constant"
|
||||||
|
"Open_IM/pkg/common/tokenverify"
|
||||||
|
"Open_IM/pkg/utils"
|
||||||
|
"context"
|
||||||
|
go_redis "github.com/go-redis/redis/v8"
|
||||||
|
"github.com/golang-jwt/jwt/v4"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
uidPidToken = "UID_PID_TOKEN_STATUS:"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Token interface {
|
||||||
|
//结果为空 不返回错误
|
||||||
|
GetTokensWithoutError(ctx context.Context, userID, platform string) (map[string]int, error)
|
||||||
|
//创建token
|
||||||
|
CreateToken(ctx context.Context, userID string, platformID int) (string, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type TokenRedis struct {
|
||||||
|
RedisClient *RedisClient
|
||||||
|
AccessSecret string
|
||||||
|
AccessExpire int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTokenRedis(redisClient *RedisClient, accessSecret string, accessExpire int64) *TokenRedis {
|
||||||
|
return &TokenRedis{redisClient, accessSecret, accessExpire}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 结果为空 不返回错误
|
||||||
|
func (t *TokenRedis) GetTokensWithoutError(ctx context.Context, userID, platform string) (map[string]int, error) {
|
||||||
|
key := uidPidToken + userID + ":" + platform
|
||||||
|
m, err := t.RedisClient.GetClient().HGetAll(context.Background(), key).Result()
|
||||||
|
if err != nil && err == go_redis.Nil {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
mm := make(map[string]int)
|
||||||
|
for k, v := range m {
|
||||||
|
mm[k] = utils.StringToInt(v)
|
||||||
|
}
|
||||||
|
return mm, utils.Wrap(err, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建token
|
||||||
|
func (t *TokenRedis) CreateToken(ctx context.Context, userID string, platform string) (string, error) {
|
||||||
|
tokens, err := t.GetTokensWithoutError(ctx, userID, platform)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
var deleteTokenKey []string
|
||||||
|
for k, v := range tokens {
|
||||||
|
_, err = tokenverify.GetClaimFromToken(k)
|
||||||
|
if err != nil || v != constant.NormalToken {
|
||||||
|
deleteTokenKey = append(deleteTokenKey, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(deleteTokenKey) != 0 {
|
||||||
|
key := uidPidToken + userID + ":" + platform
|
||||||
|
err := t.RedisClient.GetClient().HDel(context.Background(), key, deleteTokenKey...).Err()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
claims := tokenverify.BuildClaims(userID, platform, t.AccessExpire)
|
||||||
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||||
|
tokenString, err := token.SignedString([]byte(t.AccessSecret))
|
||||||
|
if err != nil {
|
||||||
|
return "", utils.Wrap(err, "")
|
||||||
|
}
|
||||||
|
key := uidPidToken + userID + ":" + platform
|
||||||
|
return "", utils.Wrap(t.RedisClient.GetClient().HSet(context.Background(), key, tokenString, constant.NormalToken).Err(), "")
|
||||||
|
}
|
@ -1,9 +1,34 @@
|
|||||||
package controller
|
package controller
|
||||||
|
|
||||||
import "context"
|
import (
|
||||||
|
"Open_IM/pkg/common/db/cache"
|
||||||
|
"context"
|
||||||
|
"github.com/go-redis/redis/v8"
|
||||||
|
)
|
||||||
|
|
||||||
type AuthInterface interface {
|
type AuthInterface interface {
|
||||||
GetTokens(ctx context.Context, userID, platform string) (map[string]int, error)
|
//结果为空 不返回错误
|
||||||
DeleteToken(ctx context.Context, userID, platform string) error
|
GetTokensWithoutError(ctx context.Context, userID, platform string) (map[string]int, error)
|
||||||
CreateToken(ctx context.Context, userID string, platformID int, ttl int64) (string, error)
|
|
||||||
|
//创建token
|
||||||
|
CreateToken(ctx context.Context, userID string, platform string) (string, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type AuthController struct {
|
||||||
|
database *cache.TokenRedis
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAuthController(rdb redis.UniversalClient, accessSecret string, accessExpire int64) *AuthController {
|
||||||
|
cache.NewRedisClient(rdb)
|
||||||
|
return &AuthController{database: cache.NewTokenRedis(cache.NewRedisClient(rdb), accessSecret, accessExpire)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 结果为空 不返回错误
|
||||||
|
func (a *AuthController) GetTokensWithoutError(ctx context.Context, userID, platform string) (map[string]int, error) {
|
||||||
|
return a.database.GetTokensWithoutError(ctx, userID, platform)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建token
|
||||||
|
func (a *AuthController) CreateToken(ctx context.Context, userID string, platform string) (string, error) {
|
||||||
|
return a.database.CreateToken(ctx, userID, platform)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user