48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package service
|
|
|
|
import (
|
|
"ArmedPolice/config"
|
|
cache2 "ArmedPolice/serve/cache"
|
|
"ArmedPolice/utils"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
type AuthToken struct {
|
|
Token string `json:"token"`
|
|
}
|
|
|
|
func (this *AuthToken) Auth() (*Session, error) {
|
|
tokenInfo := utils.JWTDecrypt(this.Token)
|
|
|
|
if tokenInfo == nil || len(tokenInfo) <= 0 {
|
|
return nil, errors.New("Token无效")
|
|
}
|
|
expTimestamp := utils.StringToInt64(fmt.Sprintf("%v", tokenInfo["exp"]))
|
|
expTime := time.Unix(expTimestamp, 0)
|
|
ok := expTime.After(time.Now())
|
|
|
|
if !ok {
|
|
return nil, errors.New("Token过期")
|
|
}
|
|
cache, _ := cache2.Cache.HGet(config.RedisKeyForAccount, fmt.Sprintf("%v", tokenInfo[config.TokenForUID]))
|
|
|
|
if cache == "" {
|
|
return nil, errors.New("用户未登录或已退出")
|
|
}
|
|
session := new(Session)
|
|
_ = session.UnmarshalBinary([]byte(cache))
|
|
|
|
if !config.SettingInfo.MultipleLogin && session.Token != this.Token {
|
|
return nil, errors.New("登录失效,已在其他地方登录!")
|
|
}
|
|
return session, nil
|
|
}
|
|
|
|
func NewAuthToken(token string) *AuthToken {
|
|
return &AuthToken{
|
|
Token: token,
|
|
}
|
|
}
|