Initial commit
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package define
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"stat/common/log"
|
||||
"stat/common/utils"
|
||||
"stat/statg"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ActType = string
|
||||
|
||||
const (
|
||||
Admin ActType = "admin"
|
||||
Channel ActType = "channel"
|
||||
District ActType = "district"
|
||||
ActTypeNone ActType = ""
|
||||
)
|
||||
|
||||
type Claims struct {
|
||||
Type ActType
|
||||
Name string `json:"name"`
|
||||
Role string `json:"role"`
|
||||
CID string `json:"cid"`
|
||||
ShowMagic string `json:"showMagic"`
|
||||
}
|
||||
|
||||
type claimsWithExp struct {
|
||||
Claims
|
||||
Exp int64 `json:"exp"`
|
||||
}
|
||||
|
||||
var (
|
||||
NoToken = fmt.Errorf("no token")
|
||||
RepeatLogin = fmt.Errorf("repeat login")
|
||||
)
|
||||
|
||||
const (
|
||||
WebTokenExpire = 10 * 24 * time.Hour
|
||||
)
|
||||
|
||||
func GetTokenSecret() string {
|
||||
return statg.Conf.JwtKey
|
||||
}
|
||||
|
||||
func GenToken(claims *Claims) (string, error) {
|
||||
c := claimsWithExp{Claims: *claims, Exp: time.Now().Add(WebTokenExpire).Unix()}
|
||||
secret := GetTokenSecret()
|
||||
args, _ := utils.JSONStruct2Map(c)
|
||||
token, err := utils.CreateToken(secret, args)
|
||||
if err != nil {
|
||||
log.Error("auth web GenToken error", log.Any("claims", claims), log.E(err))
|
||||
}
|
||||
return token, err
|
||||
}
|
||||
|
||||
func ParseToken(token string) (*claimsWithExp, error) {
|
||||
secret := GetTokenSecret()
|
||||
claims, err := utils.ParseToken(secret, token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c := claimsWithExp{}
|
||||
err = utils.Map2JSONStruct(&c, claims)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &c, nil
|
||||
}
|
||||
Reference in New Issue
Block a user