-
-
Notifications
You must be signed in to change notification settings - Fork 364
/
jwt.go
199 lines (167 loc) · 5.34 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0
// Package jwt is able to generate and validate json web tokens.
// Follows https://tools.ietf.org/html/draft-ietf-oauth-json-web-token-32
package jwt
import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"crypto/sha256"
"strings"
"github.com/go-jose/go-jose/v3"
"github.com/ory/x/errorsx"
"github.com/pkg/errors"
)
type Signer interface {
Generate(ctx context.Context, claims MapClaims, header Mapper) (string, string, error)
Validate(ctx context.Context, token string) (string, error)
Hash(ctx context.Context, in []byte) ([]byte, error)
Decode(ctx context.Context, token string) (*Token, error)
GetSignature(ctx context.Context, token string) (string, error)
GetSigningMethodLength(ctx context.Context) int
}
var SHA256HashSize = crypto.SHA256.Size()
type GetPrivateKeyFunc func(ctx context.Context) (interface{}, error)
// DefaultSigner is responsible for generating and validating JWT challenges
type DefaultSigner struct {
GetPrivateKey GetPrivateKeyFunc
}
// Generate generates a new authorize code or returns an error. set secret
func (j *DefaultSigner) Generate(ctx context.Context, claims MapClaims, header Mapper) (string, string, error) {
key, err := j.GetPrivateKey(ctx)
if err != nil {
return "", "", err
}
switch t := key.(type) {
case *jose.JSONWebKey:
return generateToken(claims, header, jose.SignatureAlgorithm(t.Algorithm), t)
case jose.JSONWebKey:
return generateToken(claims, header, jose.SignatureAlgorithm(t.Algorithm), t)
case *rsa.PrivateKey:
return generateToken(claims, header, jose.RS256, t)
case *ecdsa.PrivateKey:
return generateToken(claims, header, jose.ES256, t)
case jose.OpaqueSigner:
switch tt := t.Public().Key.(type) {
case *rsa.PrivateKey:
alg := jose.RS256
if len(t.Algs()) > 0 {
alg = t.Algs()[0]
}
return generateToken(claims, header, alg, t)
case *ecdsa.PrivateKey:
alg := jose.ES256
if len(t.Algs()) > 0 {
alg = t.Algs()[0]
}
return generateToken(claims, header, alg, t)
default:
return "", "", errors.Errorf("unsupported private / public key pairs: %T, %T", t, tt)
}
default:
return "", "", errors.Errorf("unsupported private key type: %T", t)
}
}
// Validate validates a token and returns its signature or an error if the token is not valid.
func (j *DefaultSigner) Validate(ctx context.Context, token string) (string, error) {
key, err := j.GetPrivateKey(ctx)
if err != nil {
return "", err
}
if t, ok := key.(*jose.JSONWebKey); ok {
key = t.Key
}
switch t := key.(type) {
case *rsa.PrivateKey:
return validateToken(token, t.PublicKey)
case *ecdsa.PrivateKey:
return validateToken(token, t.PublicKey)
case jose.OpaqueSigner:
return validateToken(token, t.Public().Key)
default:
return "", errors.New("Unable to validate token. Invalid PrivateKey type")
}
}
// Decode will decode a JWT token
func (j *DefaultSigner) Decode(ctx context.Context, token string) (*Token, error) {
key, err := j.GetPrivateKey(ctx)
if err != nil {
return nil, err
}
if t, ok := key.(*jose.JSONWebKey); ok {
key = t.Key
}
switch t := key.(type) {
case *rsa.PrivateKey:
return decodeToken(token, t.PublicKey)
case *ecdsa.PrivateKey:
return decodeToken(token, t.PublicKey)
case jose.OpaqueSigner:
return decodeToken(token, t.Public().Key)
default:
return nil, errors.New("Unable to decode token. Invalid PrivateKey type")
}
}
// GetSignature will return the signature of a token
func (j *DefaultSigner) GetSignature(ctx context.Context, token string) (string, error) {
return getTokenSignature(token)
}
// Hash will return a given hash based on the byte input or an error upon fail
func (j *DefaultSigner) Hash(ctx context.Context, in []byte) ([]byte, error) {
return hashSHA256(in)
}
// GetSigningMethodLength will return the length of the signing method
func (j *DefaultSigner) GetSigningMethodLength(ctx context.Context) int {
return SHA256HashSize
}
func generateToken(claims MapClaims, header Mapper, signingMethod jose.SignatureAlgorithm, privateKey interface{}) (rawToken string, sig string, err error) {
if header == nil || claims == nil {
err = errors.New("either claims or header is nil")
return
}
token := NewWithClaims(signingMethod, claims)
token.Header = assign(token.Header, header.ToMap())
rawToken, err = token.SignedString(privateKey)
if err != nil {
return
}
sig, err = getTokenSignature(rawToken)
return
}
func decodeToken(token string, verificationKey interface{}) (*Token, error) {
keyFunc := func(*Token) (interface{}, error) { return verificationKey, nil }
return ParseWithClaims(token, MapClaims{}, keyFunc)
}
func validateToken(tokenStr string, verificationKey interface{}) (string, error) {
_, err := decodeToken(tokenStr, verificationKey)
if err != nil {
return "", err
}
return getTokenSignature(tokenStr)
}
func getTokenSignature(token string) (string, error) {
split := strings.Split(token, ".")
if len(split) != 3 {
return "", errors.New("header, body and signature must all be set")
}
return split[2], nil
}
func hashSHA256(in []byte) ([]byte, error) {
hash := sha256.New()
_, err := hash.Write(in)
if err != nil {
return []byte{}, errorsx.WithStack(err)
}
return hash.Sum([]byte{}), nil
}
func assign(a, b map[string]interface{}) map[string]interface{} {
for k, w := range b {
if _, ok := a[k]; ok {
continue
}
a[k] = w
}
return a
}