-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
107 lines (93 loc) · 2.36 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package auth
import (
"encoding/base64"
"fmt"
"time"
"github.com/golang-jwt/jwt/v5"
)
type Jwt interface {
Generate(payload jwt.MapClaims) (string, error)
Verify(token string) (interface{}, error)
}
type Options struct {
Alg jwt.SigningMethod
Secret string
PrivateKey *string
PublicKey *string
Exp time.Duration
IgnoreExp bool
}
func NewJwt(opt Options) Jwt {
return &JwtImp{
Alg: opt.Alg,
Secret: opt.Secret,
PrivateKey: opt.PrivateKey,
PublicKey: opt.PublicKey,
Exp: opt.Exp,
IgnoreExp: opt.IgnoreExp,
}
}
type JwtImp struct {
Alg jwt.SigningMethod
Secret string
PrivateKey *string
PublicKey *string
Exp time.Duration
IgnoreExp bool
}
func (p *JwtImp) Generate(payload jwt.MapClaims) (string, error) {
payload["iat"] = time.Now().Unix()
payload["exp"] = time.Now().Add(p.Exp).Unix()
claims := jwt.NewWithClaims(p.Alg, payload)
var key interface{}
if p.Alg == jwt.SigningMethodRS256 {
decodedPrivateKey, err := base64.StdEncoding.DecodeString(*p.PrivateKey)
if err != nil {
return "", fmt.Errorf("could not decode key: %w", err)
}
key, err = jwt.ParseRSAPrivateKeyFromPEM(decodedPrivateKey)
if err != nil {
return "", fmt.Errorf("could not parse key: %w", err)
}
} else {
key = p.Secret
}
token, err := claims.SignedString(key)
if err != nil {
return "", err
}
return token, nil
}
func (p *JwtImp) Verify(token string) (interface{}, error) {
var key interface{}
if p.Alg == jwt.SigningMethodRS256 {
decodedPublicKey, err := base64.StdEncoding.DecodeString(*p.PublicKey)
if err != nil {
return nil, fmt.Errorf("could not decode: %w", err)
}
key, err = jwt.ParseRSAPublicKeyFromPEM(decodedPublicKey)
if err != nil {
return "", fmt.Errorf("validate: parse key: %w", err)
}
} else {
key = p.Secret
}
parsedToken, err := jwt.Parse(token, func(t *jwt.Token) (interface{}, error) {
if _, ok := t.Method.(*jwt.SigningMethodRSA); !ok {
return nil, fmt.Errorf("unexpected method: %s", t.Header["alg"])
}
return key, nil
})
if err != nil {
return nil, err
}
claims, ok := parsedToken.Claims.(jwt.MapClaims)
if !ok || !parsedToken.Valid {
return nil, fmt.Errorf("validate: invalid token")
}
exp := claims["exp"].(time.Time)
if !p.IgnoreExp && time.Now().After(exp) {
return nil, fmt.Errorf("validate: token expired")
}
return claims, nil
}