Files

52 lines
1.2 KiB
Go
Raw Normal View History

2021-11-24 09:59:29 +08:00
package service
import (
2021-11-24 10:50:09 +08:00
"SciencesServer/app/logic"
2021-11-24 09:59:29 +08:00
"SciencesServer/config"
cache2 "SciencesServer/serve/cache"
"SciencesServer/utils"
2021-11-24 10:50:09 +08:00
"encoding/json"
2021-11-24 09:59:29 +08:00
"errors"
"fmt"
"time"
)
type AuthToken struct {
Token string `json:"token"`
}
2022-01-04 11:59:58 +08:00
func (this *AuthToken) Auth(key string, session logic.ISession) error {
2021-11-24 09:59:29 +08:00
tokenInfo := utils.JWTDecrypt(this.Token)
if tokenInfo == nil || len(tokenInfo) <= 0 {
2021-11-24 10:50:09 +08:00
return errors.New("登陆错误Token无效")
2021-11-24 09:59:29 +08:00
}
expTimestamp := utils.StringToInt64(fmt.Sprintf("%v", tokenInfo["exp"]))
expTime := time.Unix(expTimestamp, 0)
ok := expTime.After(time.Now())
if !ok {
2021-11-24 10:50:09 +08:00
return errors.New("登陆错误Token过期")
2021-11-24 09:59:29 +08:00
}
2022-01-04 11:59:58 +08:00
cache, _ := cache2.Cache.HGet(key, fmt.Sprintf("%v", tokenInfo[config.TokenForUID]))
2021-11-24 09:59:29 +08:00
if cache == "" {
2021-11-24 10:50:09 +08:00
return errors.New("登陆错误,用户未登录或已退出")
2021-11-24 09:59:29 +08:00
}
2021-11-24 10:50:09 +08:00
if err := json.Unmarshal([]byte(cache), session); err != nil {
return err
2021-11-24 09:59:29 +08:00
}
2022-01-17 09:57:57 +08:00
if (config.SystemConfig[config.SysMultipleLogin] == "0" || config.SystemConfig[config.SysMultipleLogin] == "") &&
session.GetToken() != this.Token {
2021-11-24 10:50:09 +08:00
return errors.New("登陆错误,已在其他地方登录!")
}
return nil
2021-11-24 09:59:29 +08:00
}
func NewAuthToken(token string) *AuthToken {
return &AuthToken{
Token: token,
}
}