forked from MicahParks/keyfunc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
given_test.go
249 lines (194 loc) · 7.21 KB
/
given_test.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package keyfunc_test
import (
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"errors"
"fmt"
"testing"
"github.com/golang-jwt/jwt"
keyfunc "github.com/pmishchenko-ua/jwt-keyfunc"
"github.com/pmishchenko-ua/jwt-keyfunc/examples/custom/method"
)
const (
// algAttribute is the JSON attribute for the JWT encryption algorithm.
algAttribute = "alg"
// kidAttribute is the JSON attribute for the Key ID.
kidAttribute = "kid"
// testKID is the testing KID.
testKID = "testkid"
)
// TestNewGivenCustom tests that a custom jwt.SigningMethod can be used to create a JWKS and a proper jwt.Keyfunc.
func TestNewGivenCustom(t *testing.T) {
jwt.RegisterSigningMethod(method.CustomAlgHeader, func() jwt.SigningMethod {
return method.EmptyCustom{}
})
givenKeys := make(map[string]keyfunc.GivenKey)
key := addCustom(givenKeys, testKID)
jwks := keyfunc.NewGiven(givenKeys)
token := jwt.New(method.EmptyCustom{})
token.Header[algAttribute] = method.CustomAlgHeader
token.Header[kidAttribute] = testKID
signParseValidate(t, token, key, jwks)
}
// TestNewGivenCustomAlg tests that a custom jwt.SigningMethod can be used to create a JWKS and a proper jwt.Keyfunc.
func TestNewGivenCustomAlg(t *testing.T) {
jwt.RegisterSigningMethod(method.CustomAlgHeader, func() jwt.SigningMethod {
return method.EmptyCustom{}
})
const key = "test-key"
givenKeys := make(map[string]keyfunc.GivenKey)
givenKeys[testKID] = keyfunc.NewGivenCustomWithOptions(key, keyfunc.GivenKeyOptions{
Algorithm: method.CustomAlgHeader,
})
jwks := keyfunc.NewGiven(givenKeys)
token := jwt.New(method.EmptyCustom{})
token.Header[algAttribute] = method.CustomAlgHeader
token.Header[kidAttribute] = testKID
signParseValidate(t, token, key, jwks)
}
// TestNewGivenCustomAlg_NegativeCase tests that a custom jwt.SigningMethod can be used to create
// a JWKS and a proper jwt.Keyfunc and that a token with a non-matching algorithm will be rejected.
func TestNewGivenCustomAlg_NegativeCase(t *testing.T) {
jwt.RegisterSigningMethod(method.CustomAlgHeader, func() jwt.SigningMethod {
return method.EmptyCustom{}
})
const key = jwt.UnsafeAllowNoneSignatureType // Allow the "none" JWT "alg" header value for golang-jwt.
givenKeys := make(map[string]keyfunc.GivenKey)
givenKeys[testKID] = keyfunc.NewGivenCustomWithOptions(key, keyfunc.GivenKeyOptions{
Algorithm: method.CustomAlgHeader,
})
jwks := keyfunc.NewGiven(givenKeys)
token := jwt.New(method.EmptyCustom{})
token.Header[algAttribute] = jwt.SigningMethodNone.Alg()
token.Header[kidAttribute] = testKID
jwtB64, err := token.SignedString(key)
if err != nil {
t.Fatalf(logFmt, "Failed to sign the JWT.", err)
}
parsed, err := jwt.NewParser().Parse(jwtB64, jwks.Keyfunc)
if !errors.Is(err, keyfunc.ErrNoMatchingKey) {
t.Fatalf("Failed to return ErrNoMatchingKey: %v.", err)
}
if parsed.Valid {
t.Fatalf("The JWT was valid.")
}
}
// TestNewGivenKeyECDSA tests that a generated ECDSA key can be added to the JWKS and create a proper jwt.Keyfunc.
func TestNewGivenKeyECDSA(t *testing.T) {
givenKeys := make(map[string]keyfunc.GivenKey)
key, err := addECDSA(givenKeys, testKID)
if err != nil {
t.Fatalf(err.Error())
}
jwks := keyfunc.NewGiven(givenKeys)
token := jwt.New(jwt.SigningMethodES256)
token.Header[kidAttribute] = testKID
signParseValidate(t, token, key, jwks)
}
// TestNewGivenKeyEdDSA tests that a generated EdDSA key can be added to the JWKS and create a proper jwt.Keyfunc.
func TestNewGivenKeyEdDSA(t *testing.T) {
givenKeys := make(map[string]keyfunc.GivenKey)
key, err := addEdDSA(givenKeys, testKID)
if err != nil {
t.Fatalf(err.Error())
}
jwks := keyfunc.NewGiven(givenKeys)
token := jwt.New(jwt.SigningMethodEdDSA)
token.Header[kidAttribute] = testKID
signParseValidate(t, token, key, jwks)
}
// TestNewGivenKeyHMAC tests that a generated HMAC key can be added to a JWKS and create a proper jwt.Keyfunc.
func TestNewGivenKeyHMAC(t *testing.T) {
givenKeys := make(map[string]keyfunc.GivenKey)
key, err := addHMAC(givenKeys, testKID)
if err != nil {
t.Fatalf(err.Error())
}
jwks := keyfunc.NewGiven(givenKeys)
token := jwt.New(jwt.SigningMethodHS256)
token.Header[kidAttribute] = testKID
signParseValidate(t, token, key, jwks)
}
// TestNewGivenKeyRSA tests that a generated RSA key can be added to the JWKS and create a proper jwt.Keyfunc.
func TestNewGivenKeyRSA(t *testing.T) {
givenKeys := make(map[string]keyfunc.GivenKey)
key, err := addRSA(givenKeys, testKID)
if err != nil {
t.Fatalf(err.Error())
}
jwks := keyfunc.NewGiven(givenKeys)
token := jwt.New(jwt.SigningMethodRS256)
token.Header[kidAttribute] = testKID
signParseValidate(t, token, key, jwks)
}
// addCustom adds a new key wto the given keys map. The new key is using a test jwt.SigningMethod.
func addCustom(givenKeys map[string]keyfunc.GivenKey, kid string) (key string) {
key = ""
givenKeys[kid] = keyfunc.NewGivenCustomWithOptions(key, keyfunc.GivenKeyOptions{
Algorithm: method.CustomAlgHeader,
})
return key
}
// addECDSA adds a new ECDSA key to the given keys map.
func addECDSA(givenKeys map[string]keyfunc.GivenKey, kid string) (key *ecdsa.PrivateKey, err error) {
key, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, fmt.Errorf("failed to create ECDSA key: %w", err)
}
givenKeys[kid] = keyfunc.NewGivenECDSACustomWithOptions(&key.PublicKey, keyfunc.GivenKeyOptions{
Algorithm: jwt.SigningMethodES256.Alg(),
})
return key, nil
}
// addEdDSA adds a new EdDSA key to the given keys map.
func addEdDSA(givenKeys map[string]keyfunc.GivenKey, kid string) (key ed25519.PrivateKey, err error) {
pub, key, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
return nil, fmt.Errorf("failed to create ECDSA key: %w", err)
}
givenKeys[kid] = keyfunc.NewGivenEdDSACustomWithOptions(pub, keyfunc.GivenKeyOptions{
Algorithm: jwt.SigningMethodEdDSA.Alg(),
})
return key, nil
}
// addHMAC creates a new HMAC secret stuff.
func addHMAC(givenKeys map[string]keyfunc.GivenKey, kid string) (secret []byte, err error) {
secret = make([]byte, sha256.BlockSize)
_, err = rand.Read(secret)
if err != nil {
return nil, fmt.Errorf("failed to create HMAC secret: %w", err)
}
givenKeys[kid] = keyfunc.NewGivenHMACCustomWithOptions(secret, keyfunc.GivenKeyOptions{
Algorithm: jwt.SigningMethodHS256.Alg(),
})
return secret, nil
}
// addRSA adds a new RSA key to the given keys map.
func addRSA(givenKeys map[string]keyfunc.GivenKey, kid string) (key *rsa.PrivateKey, err error) {
key, err = rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, fmt.Errorf("failed to create RSA key: %w", err)
}
givenKeys[kid] = keyfunc.NewGivenRSACustomWithOptions(&key.PublicKey, keyfunc.GivenKeyOptions{
Algorithm: jwt.SigningMethodRS256.Alg(),
})
return key, nil
}
// signParseValidate signs the JWT, parses it using the given JWKS, then validates it.
func signParseValidate(t *testing.T, token *jwt.Token, key interface{}, jwks *keyfunc.JWKS) {
jwtB64, err := token.SignedString(key)
if err != nil {
t.Fatalf(logFmt, "Failed to sign the JWT.", err)
}
parsed, err := jwt.Parse(jwtB64, jwks.Keyfunc)
if err != nil {
t.Fatalf(logFmt, "Failed to parse the JWT.", err)
}
if !parsed.Valid {
t.Fatalf("The JWT was not valid.")
}
}