|
|
|
@ -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,15 +56,19 @@ 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
|
|
|
|
|
}
|
|
|
|
|
var deleteTokenKey []string
|
|
|
|
|
var kickedTokenKey []string
|
|
|
|
|
for k, v := range tokens {
|
|
|
|
|
_, err = tokenverify.GetClaimFromToken(k, authverify.Secret(a.accessSecret))
|
|
|
|
|
t, err := tokenverify.GetClaimFromToken(k, authverify.Secret(a.accessSecret))
|
|
|
|
|
if err != nil || v != constant.NormalToken {
|
|
|
|
|
deleteTokenKey = append(deleteTokenKey, k)
|
|
|
|
|
} else if a.checkKickToken(ctx, platformID, t) {
|
|
|
|
|
kickedTokenKey = append(kickedTokenKey, k)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if len(deleteTokenKey) != 0 {
|
|
|
|
@ -72,6 +77,14 @@ func (a *authDatabase) CreateToken(ctx context.Context, userID string, platformI
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if len(kickedTokenKey) != 0 {
|
|
|
|
|
for _, k := range kickedTokenKey {
|
|
|
|
|
err := a.cache.SetTokenFlagEx(ctx, userID, platformID, k, constant.KickedToken)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
claims := tokenverify.BuildClaims(userID, platformID, a.accessExpire)
|
|
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
|
|
@ -85,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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|