-
Notifications
You must be signed in to change notification settings - Fork 1
/
jwt.go
62 lines (52 loc) · 1.42 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
package jwt
import (
"time"
"github.com/dgrijalva/jwt-go"
)
type ClaimsOut struct {
UserId int `json:"user_id"`
LoginName string `json:"login_name"`
}
type Claims struct {
ClaimsOut
jwtSecret []byte
issuer string
subject string
jwt.StandardClaims
}
func NewClaims(userId int, loginName string) *Claims {
return &Claims{ClaimsOut: ClaimsOut{UserId: userId, LoginName: loginName},
jwtSecret: []byte("https://www.github.com/cilidm/toolbox"), issuer: "127.0.0.1", subject: "user-token"}
}
func (c *Claims) SetJwtSecret(secret string) *Claims {
c.jwtSecret = []byte(secret)
return c
}
func (c *Claims) SetSubject(subject string) *Claims {
c.subject = subject
return c
}
func (c *Claims) SetIssuer(issuer string) *Claims {
c.issuer = issuer
return c
}
func (c *Claims) Generate() (token string, err error) {
expireTime := time.Now().Add(24 * time.Hour)
c.ExpiresAt = expireTime.Unix()
c.IssuedAt = time.Now().Unix()
c.Issuer = c.issuer
c.Subject = c.subject
tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, c)
return tokenClaims.SignedString(c.jwtSecret)
}
func (c *Claims) Parse(token string) (*ClaimsOut, error) {
tokenClaims, err := jwt.ParseWithClaims(token, &Claims{}, func(token *jwt.Token) (interface{}, error) {
return c.jwtSecret, nil
})
if tokenClaims != nil {
if claims, ok := tokenClaims.Claims.(*Claims); ok && tokenClaims.Valid {
return &claims.ClaimsOut, nil
}
}
return nil, err
}