Files
2022-01-11 10:41:46 +08:00

51 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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(key string, 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(key, 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,
}
}