mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-06 10:05:58 +08:00
Merge branch 'errcode' of github.com:OpenIMSDK/Open-IM-Server into errcode
This commit is contained in:
@@ -118,6 +118,7 @@ const (
|
||||
ExpiredToken = 3
|
||||
|
||||
//MultiTerminalLogin
|
||||
DefalutNotKick = 0
|
||||
//Full-end login, but the same end is mutually exclusive
|
||||
AllLoginButSameTermKick = 1
|
||||
//Only one of the endpoints can log in
|
||||
|
||||
@@ -57,7 +57,7 @@ var PlatformName2ID = map[string]int{
|
||||
IPadPlatformStr: IPadPlatformID,
|
||||
AdminPlatformStr: AdminPlatformID,
|
||||
}
|
||||
var Platform2class = map[string]string{
|
||||
var PlatformName2class = map[string]string{
|
||||
IOSPlatformStr: TerminalMobile,
|
||||
AndroidPlatformStr: TerminalMobile,
|
||||
MiniWebPlatformStr: WebPlatformStr,
|
||||
@@ -66,6 +66,15 @@ var Platform2class = map[string]string{
|
||||
OSXPlatformStr: TerminalPC,
|
||||
LinuxPlatformStr: TerminalPC,
|
||||
}
|
||||
var PlatformID2class = map[int]string{
|
||||
IOSPlatformID: TerminalMobile,
|
||||
AndroidPlatformID: TerminalMobile,
|
||||
MiniWebPlatformID: WebPlatformStr,
|
||||
WebPlatformID: WebPlatformStr,
|
||||
WindowsPlatformID: TerminalPC,
|
||||
OSXPlatformID: TerminalPC,
|
||||
LinuxPlatformID: TerminalPC,
|
||||
}
|
||||
|
||||
func PlatformIDToName(num int) string {
|
||||
return PlatformID2Name[num]
|
||||
@@ -74,5 +83,8 @@ func PlatformNameToID(name string) int {
|
||||
return PlatformName2ID[name]
|
||||
}
|
||||
func PlatformNameToClass(name string) string {
|
||||
return Platform2class[name]
|
||||
return PlatformName2class[name]
|
||||
}
|
||||
func PlatformIDToClass(num int) string {
|
||||
return PlatformID2class[num]
|
||||
}
|
||||
|
||||
Vendored
+9
-9
@@ -88,9 +88,9 @@ type MsgModel interface {
|
||||
SeqCache
|
||||
thirdCache
|
||||
AddTokenFlag(ctx context.Context, userID string, platformID int, token string, flag int) error
|
||||
GetTokensWithoutError(ctx context.Context, userID, platformID string) (map[string]int, error)
|
||||
SetTokenMapByUidPid(ctx context.Context, userID string, platform string, m map[string]int) error
|
||||
DeleteTokenByUidPid(ctx context.Context, userID string, platform string, fields []string) error
|
||||
GetTokensWithoutError(ctx context.Context, userID string, platformID int) (map[string]int, error)
|
||||
SetTokenMapByUidPid(ctx context.Context, userID string, platformID int, m map[string]int) error
|
||||
DeleteTokenByUidPid(ctx context.Context, userID string, platformID int, fields []string) error
|
||||
GetMessagesBySeq(ctx context.Context, conversationID string, seqs []int64) (seqMsg []*sdkws.MsgData, failedSeqList []int64, err error)
|
||||
SetMessageToCache(ctx context.Context, conversationID string, msgs []*sdkws.MsgData) (int, error)
|
||||
UserDeleteMsgs(ctx context.Context, conversationID string, seqs []int64, userID string) error
|
||||
@@ -260,8 +260,8 @@ func (c *msgCache) AddTokenFlag(ctx context.Context, userID string, platformID i
|
||||
return errs.Wrap(c.rdb.HSet(ctx, key, token, flag).Err())
|
||||
}
|
||||
|
||||
func (c *msgCache) GetTokensWithoutError(ctx context.Context, userID, platformID string) (map[string]int, error) {
|
||||
key := uidPidToken + userID + ":" + platformID
|
||||
func (c *msgCache) GetTokensWithoutError(ctx context.Context, userID string, platformID int) (map[string]int, error) {
|
||||
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID)
|
||||
m, err := c.rdb.HGetAll(ctx, key).Result()
|
||||
if err != nil {
|
||||
return nil, errs.Wrap(err)
|
||||
@@ -273,8 +273,8 @@ func (c *msgCache) GetTokensWithoutError(ctx context.Context, userID, platformID
|
||||
return mm, nil
|
||||
}
|
||||
|
||||
func (c *msgCache) SetTokenMapByUidPid(ctx context.Context, userID string, platform string, m map[string]int) error {
|
||||
key := uidPidToken + userID + ":" + platform
|
||||
func (c *msgCache) SetTokenMapByUidPid(ctx context.Context, userID string, platform int, m map[string]int) error {
|
||||
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platform)
|
||||
mm := make(map[string]interface{})
|
||||
for k, v := range m {
|
||||
mm[k] = v
|
||||
@@ -282,8 +282,8 @@ func (c *msgCache) SetTokenMapByUidPid(ctx context.Context, userID string, platf
|
||||
return errs.Wrap(c.rdb.HSet(ctx, key, mm).Err())
|
||||
}
|
||||
|
||||
func (c *msgCache) DeleteTokenByUidPid(ctx context.Context, userID string, platform string, fields []string) error {
|
||||
key := uidPidToken + userID + ":" + platform
|
||||
func (c *msgCache) DeleteTokenByUidPid(ctx context.Context, userID string, platform int, fields []string) error {
|
||||
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platform)
|
||||
return errs.Wrap(c.rdb.HDel(ctx, key, fields...).Err())
|
||||
}
|
||||
|
||||
|
||||
@@ -12,9 +12,9 @@ import (
|
||||
|
||||
type AuthDatabase interface {
|
||||
//结果为空 不返回错误
|
||||
GetTokensWithoutError(ctx context.Context, userID, platform string) (map[string]int, error)
|
||||
GetTokensWithoutError(ctx context.Context, userID string, platformID int) (map[string]int, error)
|
||||
//创建token
|
||||
CreateToken(ctx context.Context, userID string, platform string) (string, error)
|
||||
CreateToken(ctx context.Context, userID string, platformID int) (string, error)
|
||||
}
|
||||
|
||||
type authDatabase struct {
|
||||
@@ -29,13 +29,13 @@ func NewAuthDatabase(cache cache.MsgModel, accessSecret string, accessExpire int
|
||||
}
|
||||
|
||||
// 结果为空 不返回错误
|
||||
func (a *authDatabase) GetTokensWithoutError(ctx context.Context, userID, platform string) (map[string]int, error) {
|
||||
return a.cache.GetTokensWithoutError(ctx, userID, platform)
|
||||
func (a *authDatabase) GetTokensWithoutError(ctx context.Context, userID string, platformID int) (map[string]int, error) {
|
||||
return a.cache.GetTokensWithoutError(ctx, userID, platformID)
|
||||
}
|
||||
|
||||
// 创建token
|
||||
func (a *authDatabase) CreateToken(ctx context.Context, userID string, platform string) (string, error) {
|
||||
tokens, err := a.cache.GetTokensWithoutError(ctx, userID, platform)
|
||||
func (a *authDatabase) CreateToken(ctx context.Context, userID string, platformID int) (string, error) {
|
||||
tokens, err := a.cache.GetTokensWithoutError(ctx, userID, platformID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -47,16 +47,16 @@ func (a *authDatabase) CreateToken(ctx context.Context, userID string, platform
|
||||
}
|
||||
}
|
||||
if len(deleteTokenKey) != 0 {
|
||||
err := a.cache.DeleteTokenByUidPid(ctx, userID, platform, deleteTokenKey)
|
||||
err := a.cache.DeleteTokenByUidPid(ctx, userID, platformID, deleteTokenKey)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
claims := tokenverify.BuildClaims(userID, platform, a.accessExpire)
|
||||
claims := tokenverify.BuildClaims(userID, platformID, a.accessExpire)
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
tokenString, err := token.SignedString([]byte(a.accessSecret))
|
||||
if err != nil {
|
||||
return "", utils.Wrap(err, "")
|
||||
}
|
||||
return tokenString, a.cache.AddTokenFlag(ctx, userID, constant.PlatformNameToID(platform), tokenString, constant.NormalToken)
|
||||
return tokenString, a.cache.AddTokenFlag(ctx, userID, platformID, tokenString, constant.NormalToken)
|
||||
}
|
||||
|
||||
@@ -128,7 +128,7 @@ func GinParseToken(rdb redis.UniversalClient) gin.HandlerFunc {
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
m, err := dataBase.GetTokensWithoutError(c, claims.UID, claims.Platform)
|
||||
m, err := dataBase.GetTokensWithoutError(c, claims.UserID, claims.PlatformID)
|
||||
if err != nil {
|
||||
log.ZWarn(c, "cache get token error", errs.ErrTokenNotExist.Wrap())
|
||||
apiresp.GinError(c, errs.ErrTokenNotExist.Wrap())
|
||||
@@ -155,9 +155,12 @@ func GinParseToken(rdb redis.UniversalClient) gin.HandlerFunc {
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
} else {
|
||||
apiresp.GinError(c, errs.ErrTokenNotExist.Wrap())
|
||||
return
|
||||
}
|
||||
c.Set(constant.OpUserPlatform, claims.Platform)
|
||||
c.Set(constant.OpUserID, claims.UID)
|
||||
c.Set(constant.OpUserPlatform, constant.PlatformIDToName(claims.PlatformID))
|
||||
c.Set(constant.OpUserID, claims.UserID)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,27 +4,25 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mcontext"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
|
||||
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Claims struct {
|
||||
UID string
|
||||
Platform string //login platform
|
||||
UserID string
|
||||
PlatformID int //login platform
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
func BuildClaims(uid, platform string, ttl int64) Claims {
|
||||
func BuildClaims(uid string, platformID int, ttl int64) Claims {
|
||||
now := time.Now()
|
||||
before := now.Add(-time.Minute * 5)
|
||||
return Claims{
|
||||
UID: uid,
|
||||
Platform: platform,
|
||||
UserID: uid,
|
||||
PlatformID: platformID,
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
ExpiresAt: jwt.NewNumericDate(now.Add(time.Duration(ttl*24) * time.Hour)), //Expiration time
|
||||
IssuedAt: jwt.NewNumericDate(now), //Issuing time
|
||||
@@ -90,24 +88,16 @@ func ParseRedisInterfaceToken(redisToken interface{}) (*Claims, error) {
|
||||
func IsManagerUserID(opUserID string) bool {
|
||||
return utils.IsContain(opUserID, config.Config.Manager.AppManagerUid)
|
||||
}
|
||||
func WsVerifyToken(token, userID, platformID string) error {
|
||||
platformIDInt, err := strconv.Atoi(platformID)
|
||||
if err != nil {
|
||||
return errs.ErrArgs.Wrap(fmt.Sprintf("platformID %s is not int", platformID))
|
||||
}
|
||||
platform := constant.PlatformIDToName(platformIDInt)
|
||||
if platform == "" {
|
||||
return errs.ErrArgs.Wrap(fmt.Sprintf("platformID %s is not exist", platformID))
|
||||
}
|
||||
func WsVerifyToken(token, userID string, platformID int) error {
|
||||
claim, err := GetClaimFromToken(token)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if claim.UID != userID {
|
||||
return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token uid %s != userID %s", claim.UID, userID))
|
||||
if claim.UserID != userID {
|
||||
return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token uid %s != userID %s", claim.UserID, userID))
|
||||
}
|
||||
if claim.Platform != platform {
|
||||
return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token platform %s != %s", claim.Platform, platform))
|
||||
if claim.PlatformID != platformID {
|
||||
return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token platform %d != %d", claim.PlatformID, platformID))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user