-
Notifications
You must be signed in to change notification settings - Fork 1
/
jwt_sha256.go
61 lines (48 loc) · 1.4 KB
/
jwt_sha256.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
package signature_go
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/golang-jwt/jwt/v5"
"io"
)
type JwtSignSHA256Hmac struct {
secret []byte
}
func NewJwtSignSHA256Hmac(secret string) *JwtSignSHA256Hmac {
return &JwtSignSHA256Hmac{secret: []byte(secret)}
}
func (j JwtSignSHA256Hmac) Sign(ctx context.Context, payload *jwt.MapClaims) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, payload)
return token.SignedString(j.secret)
}
func (j JwtSignSHA256Hmac) Validate(ctx context.Context, tokenStr string) (jwt.MapClaims, error) {
token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return j.secret, 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 JwtSignSHA256Hmac) 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 JwtSignSHA256Hmac) 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
}