-
Notifications
You must be signed in to change notification settings - Fork 1
/
jwt_rs256.go
64 lines (51 loc) · 1.59 KB
/
jwt_rs256.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
package signature_go
import (
"bytes"
"context"
"crypto/rsa"
"encoding/json"
"fmt"
"github.com/golang-jwt/jwt/v5"
"io"
)
type JwtSignRS256Hmac struct {
privateKey *rsa.PrivateKey
publicKey *rsa.PublicKey
}
func NewJwtSignRS256Hmac(privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey) *JwtSignRS256Hmac {
return &JwtSignRS256Hmac{privateKey: privateKey, publicKey: publicKey}
}
func (j JwtSignRS256Hmac) Sign(ctx context.Context, payload *jwt.MapClaims) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodRS256, payload)
return token.SignedString(j.privateKey)
}
func (j JwtSignRS256Hmac) Validate(ctx context.Context, tokenStr string) (jwt.MapClaims, error) {
token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (interface{}, error) {
// Don't forget to validate the alg is what you expect:
if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
return nil, fmt.Errorf("JwtSignRS256Hmac.Validate unexpected signing method: %v", token.Header["alg"])
}
return j.publicKey, nil
})
if token == nil || err != nil {
return nil, err
}
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
return claims, nil
}
return nil, err
}
func (j JwtSignRS256Hmac) ValidateReturnBytes(ctx context.Context, token string) ([]byte, error) {
claims, err := j.Validate(ctx, token)
if err != nil {
return nil, err
}
return json.Marshal(claims)
}
func (j JwtSignRS256Hmac) ValidateReturnReader(ctx context.Context, token string) (io.Reader, error) {
b, err := j.ValidateReturnBytes(ctx, token)
if err != nil {
return nil, err
}
return bytes.NewReader(b), err
}