-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
56 lines (50 loc) · 1.54 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
package jwt
import (
"fmt"
"time"
"github.com/dgrijalva/jwt-go"
)
type jwtContents struct {
CustomClaims interface{} `json:"custom_claims"`
jwt.StandardClaims
}
// New returns a signed JWT string for the given claims
func New(signingKey string, claims interface{}, expireDuration *time.Duration) (string, jwt.StandardClaims, error) {
var standardClaims jwt.StandardClaims
if expireDuration != nil {
standardClaims = jwt.StandardClaims{
ExpiresAt: time.Now().UTC().Add(*expireDuration).Unix(),
}
}
contents := jwtContents{
CustomClaims: claims,
StandardClaims: standardClaims,
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, contents)
str, err := token.SignedString([]byte(signingKey))
return str, standardClaims, err
}
// Read returns the interface of custom claims from within the token
func Read(signingKey, jwtString string) (map[string]interface{}, error) {
token, err := jwt.Parse(jwtString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return []byte(signingKey), nil
})
if err != nil {
return nil, err
}
if !token.Valid {
return nil, invalidJwtError("claim validation failed")
}
if claims, ok := token.Claims.(jwt.MapClaims); ok {
if custom, ok := claims["custom_claims"].(map[string]interface{}); ok {
return custom, nil
}
}
return nil, invalidJwtError("could not read claims")
}
func invalidJwtError(err string) error {
return fmt.Errorf("invalid jwt: %s", err)
}