|
|
|
@ -35,13 +35,14 @@ type AuthDatabase interface {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type authDatabase struct {
|
|
|
|
|
cache cache.TokenModel
|
|
|
|
|
accessSecret string
|
|
|
|
|
accessExpire int64
|
|
|
|
|
cache cache.TokenModel
|
|
|
|
|
accessSecret string
|
|
|
|
|
accessExpire int64
|
|
|
|
|
multiLoginPolicy int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewAuthDatabase(cache cache.TokenModel, accessSecret string, accessExpire int64) AuthDatabase {
|
|
|
|
|
return &authDatabase{cache: cache, accessSecret: accessSecret, accessExpire: accessExpire}
|
|
|
|
|
func NewAuthDatabase(cache cache.TokenModel, accessSecret string, accessExpire int64, policy int) AuthDatabase {
|
|
|
|
|
return &authDatabase{cache: cache, accessSecret: accessSecret, accessExpire: accessExpire, multiLoginPolicy: policy}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If the result is empty.
|
|
|
|
@ -55,6 +56,7 @@ func (a *authDatabase) SetTokenMapByUidPid(ctx context.Context, userID string, p
|
|
|
|
|
|
|
|
|
|
// Create Token.
|
|
|
|
|
func (a *authDatabase) CreateToken(ctx context.Context, userID string, platformID int) (string, error) {
|
|
|
|
|
// todo: get all platform token
|
|
|
|
|
tokens, err := a.cache.GetTokensWithoutError(ctx, userID, platformID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
@ -65,7 +67,7 @@ func (a *authDatabase) CreateToken(ctx context.Context, userID string, platformI
|
|
|
|
|
t, err := tokenverify.GetClaimFromToken(k, authverify.Secret(a.accessSecret))
|
|
|
|
|
if err != nil || v != constant.NormalToken {
|
|
|
|
|
deleteTokenKey = append(deleteTokenKey, k)
|
|
|
|
|
} else if t.UserID == userID && t.PlatformID == platformID {
|
|
|
|
|
} else if a.checkKickToken(ctx, platformID, t) {
|
|
|
|
|
kickedTokenKey = append(kickedTokenKey, k)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -96,3 +98,23 @@ func (a *authDatabase) CreateToken(ctx context.Context, userID string, platformI
|
|
|
|
|
}
|
|
|
|
|
return tokenString, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *authDatabase) checkKickToken(ctx context.Context, platformID int, token *tokenverify.Claims) bool {
|
|
|
|
|
switch a.multiLoginPolicy {
|
|
|
|
|
case constant.DefalutNotKick:
|
|
|
|
|
return false
|
|
|
|
|
case constant.PCAndOther:
|
|
|
|
|
if constant.PlatformIDToClass(platformID) == constant.TerminalPC ||
|
|
|
|
|
constant.PlatformIDToClass(token.PlatformID) == constant.TerminalPC {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
case constant.AllLoginButSameTermKick:
|
|
|
|
|
if platformID == token.PlatformID {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
default:
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|