-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
50 lines (43 loc) · 1.12 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
package util
import (
"fmt"
"regexp"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/google/uuid"
)
var bearerRegexp = regexp.MustCompile(`^\s*Bearer\s+`)
type JWTClaims struct {
jwt.StandardClaims
UserId int64 `json:"userId"`
}
func EncodeJWT(id int64, issuer, secret string, expiry time.Duration) (string, error) {
issuedAt := time.Now()
expiresAt := issuedAt.Add(expiry)
token, err := jwt.NewWithClaims(jwt.SigningMethodHS256, JWTClaims{
UserId: id,
StandardClaims: jwt.StandardClaims{
Id: uuid.New().String(),
Issuer: issuer,
IssuedAt: issuedAt.Unix(),
ExpiresAt: expiresAt.Unix(),
},
}).SignedString([]byte(secret))
if err != nil {
return "", fmt.Errorf("encode jwt: signed secret: %w", err)
}
return token, nil
}
func DecodeJWT(token, secret string) (*JWTClaims, error) {
claims := JWTClaims{}
parser := jwt.Parser{
SkipClaimsValidation: true,
}
_, err := parser.ParseWithClaims(token, &claims, func(token *jwt.Token) (interface{}, error) {
return []byte(secret), nil
})
if err != nil {
return nil, fmt.Errorf("decode jwt: parse claims: %w", err)
}
return &claims, nil
}