-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
59 lines (52 loc) · 1.27 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
package jwt
import (
"github.com/dgrijalva/jwt-go"
"time"
)
type JWT struct {
SigningKey []byte
}
func NewJWT(secretKey string) *JWT {
return &JWT{
[]byte(secretKey),
}
}
// CreateToken 创建一个token
func (j *JWT) CreateToken(data string, timeout int64) (string, error) {
claims := &jwt.StandardClaims{
Issuer: "TimeToken",
Subject: data,
}
if timeout > 0 {
claims.ExpiresAt = time.Now().Add(time.Second * time.Duration(timeout)).Unix()
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString(j.SigningKey)
}
// ParseToken 解析 token
func (j *JWT) ParseToken(token string) (string, error) {
var err error
var claims jwt.StandardClaims
_, err = jwt.ParseWithClaims(token, &claims, func(token *jwt.Token) (interface{}, error) {
return j.SigningKey, nil
})
if err != nil {
return "", err
}
err = claims.Valid()
if err != nil {
return "", err
} else {
return claims.Subject, err
}
}
// MakeTimeoutToken 创建一个会过期的token
func MakeTimeoutToken(data string, secretKey string, timeout int64) (string, error) {
j := NewJWT(secretKey)
return j.CreateToken(data, timeout)
}
// ParseToken 解析 token
func ParseToken(secretKey, token string) (string, error) {
j := NewJWT(secretKey)
return j.ParseToken(token)
}