-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
executable file
·50 lines (39 loc) · 970 Bytes
/
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
package ddd
import (
"fmt"
"time"
"github.com/golang-jwt/jwt"
)
var hmacSecret []byte = []byte("abcdefghijklmnopqrstuvwxyz")
func SignJWTClaims(
email string,
) string {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"email": email,
"nbf": time.Date(2015, 10, 10, 12, 0, 0, 0, time.UTC).Unix(),
})
tokenString, _ := token.SignedString(hmacSecret)
return tokenString
}
func ParseJWTClaims(
tokenString string,
) string {
token, err := jwt.Parse(tokenString, 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 hmacSecret, nil
})
if err != nil {
// invalid token
return ""
}
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
if _, ok := claims["email"]; !ok {
// email doesn't exist
return ""
}
return claims["email"].(string)
}
return ""
}