forked from pocketbase/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 15
/
jwt.go
112 lines (87 loc) · 2.93 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
108
109
110
111
112
package security
import (
"errors"
"os"
"time"
"github.com/golang-jwt/jwt/v4"
)
// ParseUnverifiedJWT parses JWT and returns its claims
// but DOES NOT verify the signature.
//
// It verifies only the exp, iat and nbf claims.
func ParseUnverifiedJWT(token string) (jwt.MapClaims, error) {
claims := jwt.MapClaims{}
parser := &jwt.Parser{}
_, _, err := parser.ParseUnverified(token, claims)
if err == nil {
err = claims.Valid()
}
return claims, err
}
// // ParseJWT verifies and parses JWT and returns its claims.
// func ParseJWT(token string, verificationKey string) (jwt.MapClaims, error) {
// parser := jwt.NewParser(jwt.WithValidMethods([]string{"HS256"}))
// parsedToken, err := parser.Parse(token, func(t *jwt.Token) (any, error) {
// return []byte(verificationKey), nil
// })
// if err != nil {
// return nil, err
// }
// if claims, ok := parsedToken.Claims.(jwt.MapClaims); ok && parsedToken.Valid {
// return claims, nil
// }
// return nil, errors.New("Unable to parse token.")
// }
// ParseJWT verifies and parses JWT and returns its claims.
func ParseJWT(token string, oldVerificationKey string) (jwt.MapClaims, error) {
parser := jwt.NewParser(jwt.WithValidMethods([]string{"RS256"}))
publicKey, err := jwt.ParseRSAPublicKeyFromPEM([]byte(os.Getenv("JWT_PUBLIC_KEY")))
if err != nil {
return nil, err
}
parsedToken, err := parser.Parse(token, func(t *jwt.Token) (any, error) {
return publicKey, nil
})
if err != nil {
return nil, err
}
if claims, ok := parsedToken.Claims.(jwt.MapClaims); ok && parsedToken.Valid {
return claims, nil
}
return nil, errors.New("Unable to parse token.")
}
// // NewJWT generates and returns new HS256 signed JWT.
// func NewJWT(payload jwt.MapClaims, signingKey string, secondsDuration int64) (string, error) {
// seconds := time.Duration(secondsDuration) * time.Second
// claims := jwt.MapClaims{
// "exp": time.Now().Add(seconds).Unix(),
// }
// for k, v := range payload {
// claims[k] = v
// }
// return jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString([]byte(signingKey))
// }
// NewJWT generates and returns new HS256 signed JWT.
func NewJWT(payload jwt.MapClaims, oldSigninKey string, secondsDuration int64) (string, error) {
seconds := time.Duration(secondsDuration) * time.Second
claims := jwt.MapClaims{
"exp": time.Now().Add(seconds).Unix(),
}
for k, v := range payload {
claims[k] = v
}
privateKey, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(os.Getenv("JWT_PRIVATE_KEY")))
if err != nil {
return "", err
}
return jwt.NewWithClaims(jwt.SigningMethodRS256, claims).SignedString(privateKey)
}
// Deprecated:
// Consider replacing with NewJWT().
//
// NewToken is a legacy alias for NewJWT that generates a HS256 signed JWT.
func NewToken(payload jwt.MapClaims, signingKey string, secondsDuration int64) (string, error) {
//
// return NewJWT(payload, signingKey, secondsDuration)
return NewJWT(payload, signingKey, secondsDuration)
}