-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
70 lines (63 loc) · 1.71 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
61
62
63
64
65
66
67
68
69
70
package account
import (
"time"
"github.com/dgrijalva/jwt-go"
)
type Claims struct {
jwt.StandardClaims
MemberId string `json:"member_id"`
AccountPlat string `json:"account_plat"`
PhonePlat int32 `json:"phone_plat"`
DeviceId string `json:"device_id"`
Channel int64 `json:"channel"`
}
// 生成 jwt token
func GenerateJwtToken(secret string, duration time.Duration, memberId string, channel int64, accountPlat string, phonePlat int32, deviceID string) (string, error) {
nowTime := time.Now()
expireTime := nowTime.Add(duration)
claims := Claims{
jwt.StandardClaims{
ExpiresAt: expireTime.Unix(),
Issuer: "gira",
},
memberId,
accountPlat,
phonePlat,
deviceID,
channel,
}
tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
token, err := tokenClaims.SignedString([]byte(secret))
return token, err
}
// 生成 jwt refresh token
func GenerateJwtRefreshToken(secret string, duration time.Duration) (string, error) {
nowTime := time.Now()
expireTime := nowTime.Add(duration)
claims := Claims{
jwt.StandardClaims{
ExpiresAt: expireTime.Unix(),
Issuer: "gira",
},
"", // MemberId
"", // AccountPlat
0, // PhonePlat
"", // DeviceId
0, // Channel
}
tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
token, err := tokenClaims.SignedString([]byte(secret))
return token, err
}
// 解析 jwt token
func ParseJwtToken(token string, secret string) (*Claims, error) {
tokenClaims, err := jwt.ParseWithClaims(token, &Claims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(secret), nil
})
if tokenClaims != nil {
if claims, ok := tokenClaims.Claims.(*Claims); ok && tokenClaims.Valid {
return claims, nil
}
}
return nil, err
}