mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 10:58:35 +08:00
64 lines
1.3 KiB
Go
64 lines
1.3 KiB
Go
package ctx
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/dgrijalva/jwt-go"
|
|
"pandax/base/config"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
jwtSecret = []byte(config.Conf.Jwt.Key)
|
|
)
|
|
|
|
type Claims struct {
|
|
UserId int64
|
|
UserName string
|
|
RoleId int64
|
|
RoleKey string
|
|
DeptId int64
|
|
PostId int64
|
|
jwt.StandardClaims
|
|
}
|
|
|
|
func CreateToken(claims Claims) (string, error) {
|
|
|
|
tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
token, err := tokenClaims.SignedString(jwtSecret)
|
|
|
|
return token, err
|
|
}
|
|
|
|
func ParseToken(token string) (*Claims, error) {
|
|
tokenClaims, err := jwt.ParseWithClaims(token, &Claims{}, func(token *jwt.Token) (interface{}, error) {
|
|
return jwtSecret, nil
|
|
})
|
|
|
|
if tokenClaims != nil {
|
|
if claims, ok := tokenClaims.Claims.(*Claims); ok && tokenClaims.Valid {
|
|
return claims, nil
|
|
}
|
|
}
|
|
|
|
return nil, err
|
|
}
|
|
|
|
// 更新token
|
|
func RefreshToken(tokenString string) (string, error) {
|
|
jwt.TimeFunc = func() time.Time {
|
|
return time.Unix(0, 0)
|
|
}
|
|
token, err := jwt.ParseWithClaims(tokenString, &Claims{}, func(token *jwt.Token) (interface{}, error) {
|
|
return jwtSecret, nil
|
|
})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if claims, ok := token.Claims.(*Claims); ok && token.Valid {
|
|
jwt.TimeFunc = time.Now
|
|
claims.StandardClaims.ExpiresAt = time.Now().Add(1 * time.Hour).Unix()
|
|
return CreateToken(*claims)
|
|
}
|
|
return "", errors.New("Couldn't handle this token:")
|
|
}
|