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