-
Notifications
You must be signed in to change notification settings - Fork 2
/
jwt.go
60 lines (54 loc) · 1.59 KB
/
jwt.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
/**
* @author: yangchangjia
* @email 1320259466@qq.com
* @date: 2023/7/13 6:13 PM
* @desc: about the role of class.
*/
package jwts
import (
"github.com/golang-jwt/jwt"
"time"
)
type CustomClaims struct {
UserInfo interface{}
jwt.StandardClaims
}
func GenerateToken(userInfo interface{}, secret string, issuer string, audience string, expiredMinutes int64) (string, error) {
hmacSampleSecret := []byte(secret)
token := jwt.New(jwt.SigningMethodHS256)
nowTime := time.Now().Unix()
token.Claims = CustomClaims{
UserInfo: userInfo,
StandardClaims: jwt.StandardClaims{
NotBefore: nowTime,
ExpiresAt: nowTime + expiredMinutes*60,
Issuer: issuer,
Audience: audience,
},
}
tokenString, err := token.SignedString(hmacSampleSecret)
return tokenString, err
}
func ParseToken(tokenString string, secret string) (*CustomClaims, error) {
var hmacSampleSecret = []byte(secret)
token, err := jwt.ParseWithClaims(tokenString, &CustomClaims{}, func(t *jwt.Token) (interface{}, error) {
return hmacSampleSecret, nil
})
if err != nil {
return nil, err
}
claims := token.Claims.(*CustomClaims)
return claims, nil
}
func RefreshToken(tokenString string, secret string, addMinutes int64) (string, error) {
var hmacSampleSecret = []byte(secret)
token, err := jwt.ParseWithClaims(tokenString, &CustomClaims{}, func(t *jwt.Token) (interface{}, error) {
return hmacSampleSecret, nil
})
if err != nil {
return "", err
}
token.Claims.(*CustomClaims).ExpiresAt = time.Now().Unix() + addMinutes*60
newTokenString, err := token.SignedString(hmacSampleSecret)
return newTokenString, err
}