-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
96 lines (82 loc) · 2.11 KB
/
token.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package xjwt
import (
"errors"
"github.com/chenxingyuu/gin_template/config"
"github.com/chenxingyuu/gin_template/internal/app/model"
"github.com/dgrijalva/jwt-go"
"time"
)
type TokenType uint8
const (
Refresh TokenType = iota
Access
)
type Claims struct {
UserID int64 `json:"user_id"`
Username string `json:"username"`
TokenType TokenType `json:"token_type"`
jwt.StandardClaims
}
// New 生成 JWT
func New(claims Claims) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
signedToken, err := token.SignedString(config.Jwt.GetSecretKey())
if err != nil {
return "", err
}
return signedToken, nil
}
func NewAccess(user *model.User) (string, error) {
return New(Claims{
UserID: int64(user.ID),
Username: user.Username,
TokenType: Access,
StandardClaims: jwt.StandardClaims{
ExpiresAt: time.Now().Add(config.Jwt.GetAccessExp()).Unix(),
IssuedAt: time.Now().Unix(),
},
})
}
func NewRefresh(user *model.User) (string, error) {
return New(Claims{
UserID: int64(user.ID),
Username: user.Username,
TokenType: Refresh,
StandardClaims: jwt.StandardClaims{
ExpiresAt: time.Now().Add(config.Jwt.GetRefreshExp()).Unix(),
IssuedAt: time.Now().Unix(),
},
})
}
func Validate(t string) (*Claims, error) {
token, err := jwt.ParseWithClaims(t, &Claims{}, func(token *jwt.Token) (interface{}, error) {
return config.Jwt.GetSecretKey(), nil
})
if err != nil {
var ve *jwt.ValidationError
if errors.As(err, &ve) {
if ve.Errors&jwt.ValidationErrorExpired != 0 {
return nil, errors.New("token已过期")
}
}
return nil, err
}
if claims, ok := token.Claims.(*Claims); ok && token.Valid {
return claims, nil
}
return nil, errors.New("无效的Token")
}
func ValidateAccess(t string) (*Claims, error) {
claims, err := Validate(t)
if err == nil && claims.TokenType == Access {
return claims, nil
}
return nil, errors.New("token类型错误")
}
func ValidateRefresh(t string) (*Claims, error) {
claims, err := Validate(t)
if err == nil && claims.TokenType == Refresh {
return claims, nil
}
return nil, errors.New("token类型错误")
}