-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
73 lines (64 loc) · 1.98 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
package jwt
import (
"time"
jwtgo "github.com/dgrijalva/jwt-go"
"github.com/pkg/errors"
)
// TokenCreateValidator describes the JWT operations.
type TokenCreateValidator interface {
Create(payload interface{}) (string, error)
Validate(string) (interface{}, error)
}
// jwt represents a Token implementation of TokenCreateValidator interface.
type jwt struct {
// secret describes a secret key to use with the token
secret string
expiresAt time.Duration
}
// New creates new JWT object given a secret value
func New(secret string) TokenCreateValidator {
return &jwt{
secret: secret,
// Default expires time to one month
expiresAt: time.Hour * 24 * 7 * 4,
}
}
// NewWithExpiresAt creates new JWT object with an expires time
func NewWithExpiresAt(secret string, expiresAt int) TokenCreateValidator {
return &jwt{
secret: secret,
expiresAt: time.Hour * time.Duration(expiresAt),
}
}
// Create creates, signs, and encodes a JWT token using the HMAC signing method
func (t jwt) Create(data interface{}) (string, error) {
// create a signer for HS256
token := jwtgo.New(jwtgo.GetSigningMethod("HS256"))
// set our claims
token.Claims = &customClaim{
data,
jwtgo.StandardClaims{
ExpiresAt: time.Now().Add(t.expiresAt).Unix(),
},
}
// create a token string
return token.SignedString([]byte(t.secret))
}
// customClaim represents the user custom payload to store into a token.
type customClaim struct {
// Data stores the user data
Data interface{} `json:"data"`
jwtgo.StandardClaims
}
// Validate validates tokens using a secret key and custom claim.
// it returns an error in case the token is malformed or expired.
func (t jwt) Validate(secret string) (interface{}, error) {
token, err := jwtgo.ParseWithClaims(secret, &customClaim{}, func(token *jwtgo.Token) (interface{}, error) {
return []byte(t.secret), nil
})
if err != nil {
return nil, errors.Wrap(err, "could not parse token with custom claims")
}
claims, _ := token.Claims.(*customClaim)
return claims.Data, nil
}