-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwtRS256.go
84 lines (74 loc) · 1.99 KB
/
jwtRS256.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
package jwt
import (
"crypto/rsa"
"github.com/cockroachdb/errors"
"github.com/go-jose/go-jose/v3"
"github.com/justdomepaul/toolbox/config"
"os"
"time"
)
// NewRS256JWT method
func NewRS256JWT(rsaPrivateKey string) (*RS256JWT, error) {
rsaKey, err := parseRSAPrivateKeyFromPEM([]byte(rsaPrivateKey))
if err != nil {
return nil, errors.Wrap(err, ErrParsePrivateKey.Error())
}
sig, err := newSigner(
jose.SigningKey{Algorithm: jose.RS256, Key: rsaKey},
(&jose.SignerOptions{}).WithType("JWT"),
)
if err != nil {
return nil, err
}
return &RS256JWT{
SigningKey: rsaKey,
Sig: sig,
}, nil
}
func NewRS256JWTFromOptions(option config.JWT) (*RS256JWT, error) {
if option.RsaPrivateKeyPath == "" && option.RsaPrivateKey == "" {
return nil, ErrNoKey
}
key := option.RsaPrivateKey
if option.RsaPrivateKeyPath != "" {
if result, err := os.ReadFile(option.RsaPrivateKeyPath); err != nil {
return nil, err
} else {
key = string(result)
}
}
return NewRS256JWT(key)
}
// RS256JWT type
type RS256JWT struct {
SigningKey *rsa.PrivateKey
Sig jose.Signer
}
// GenerateToken method
func (r RS256JWT) GenerateToken(claims IJWTClaims) (token string, err error) {
return signed(r.Sig).Claims(claims).CompactSerialize()
}
// Validate method
func (r RS256JWT) Validate(raw string) error {
tok, errParse := parseSigned(raw)
if errParse != nil {
return errParse
}
return tok.Claims(r.SigningKey.Public())
}
// VerifyToken method
func (r RS256JWT) VerifyToken(token string, claims IJWTClaims) (err error) {
return parseRSRaw(r.SigningKey, token, claims)
}
// RefreshToken method
func (r RS256JWT) RefreshToken(token string, claims IJWTClaims, duration time.Duration) (string, error) {
errParse := parseRSRaw(r.SigningKey, token, claims)
if errParse != nil && errParse != ErrTokenExpired {
return "", errParse
}
if errors.Is(errParse, ErrTokenExpired) {
claims.(IJWTExpire).ExpiresAfter(duration)
return signed(r.Sig).Claims(claims).CompactSerialize()
}
return token, nil
}