/
jwt.go
105 lines (86 loc) · 2.68 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
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
97
98
99
100
101
102
103
104
105
// 主要是配合jwt来生成用户登录token
package app
import (
"context"
"errors"
"fmt"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"github.com/FirYuen/scaffold/pkg/conf"
)
var (
// ErrMissingHeader means the `Authorization` header was empty.
ErrMissingHeader = errors.New("the length of the `Authorization` header is zero")
)
// Payload is the data of the JSON web token.
type Payload struct {
UserID uint64
}
// secretFunc validates the secret format.
func secretFunc(secret string) jwt.Keyfunc {
return func(token *jwt.Token) (interface{}, error) {
// Make sure the `alg` is what we except.
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, jwt.ErrSignatureInvalid
}
return []byte(secret), nil
}
}
// Parse validates the token with the specified secret,
// and returns the payloads if the token was valid.
func Parse(tokenString string, secret string) (*Payload, error) {
// Parse the token.
token, err := jwt.Parse(tokenString, secretFunc(secret))
if err != nil {
return nil, err
}
// Read the token if it's valid.
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
payloads := &Payload{}
payloads.UserID = uint64(claims["user_id"].(float64))
return payloads, nil
}
// Other errors.
return nil, err
}
// ParseRequest gets the token from the header and
// pass it to the Parse function to parses the token.
func ParseRequest(c *gin.Context) (*Payload, error) {
header := c.Request.Header.Get("Authorization")
// Load the jwt secret from config
secret := conf.Conf.App.JwtSecret
if len(header) == 0 {
return &Payload{}, ErrMissingHeader
}
var t string
// Parse the header to get the token part.
_, err := fmt.Sscanf(header, "Bearer %s", &t)
if err != nil {
fmt.Printf("fmt.Sscanf err: %+v", err)
}
return Parse(t, secret)
}
// Sign signs the payload with the specified secret.
// The token content.
// iss: (Issuer)签发者
// iat: (Issued At)签发时间,用Unix时间戳表示
// exp: (Expiration Time)过期时间,用Unix时间戳表示
// aud: (Audience)接收该JWT的一方
// sub: (Subject)该JWT的主题
// nbf: (Not Before)不要早于这个时间
// jti: (JWT ID)用于标识JWT的唯一ID
func Sign(ctx context.Context, payload map[string]interface{}, secret string, timeout int64) (tokenString string, err error) {
now := time.Now().Unix()
claims := make(jwt.MapClaims)
claims["nbf"] = now
claims["iat"] = now
claims["exp"] = now + timeout
for k, v := range payload {
claims[k] = v
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
// Sign the token with the specified secret.
tokenString, err = token.SignedString([]byte(secret))
return
}