forked from MicahParks/keyfunc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
given_test.go
192 lines (147 loc) · 5.23 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
package keyfunc_test
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"fmt"
"testing"
"github.com/golang-jwt/jwt/v4"
"github.com/MicahParks/keyfunc"
"github.com/MicahParks/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) {
// Register the signing method.
jwt.RegisterSigningMethod(method.CustomAlg, func() jwt.SigningMethod {
return method.EmptyCustom{}
})
// Create the map of given keys.
givenKeys := make(map[string]keyfunc.GivenKey)
key := addCustom(givenKeys, testKID)
// Use the custom key to create a JWKs.
jwks := keyfunc.NewGiven(givenKeys)
// Create the JWT with the appropriate key ID.
token := jwt.New(method.EmptyCustom{})
token.Header[algAttribute] = method.CustomAlg
token.Header[kidAttribute] = testKID
// Sign, parse, and validate the JWT.
signParseValidate(t, token, key, jwks)
}
// TestNewGivenKeyECDSA tests that a generated ECDSA key can be added to the JWKs and create a proper jwt.Keyfunc.
func TestNewGivenKeyECDSA(t *testing.T) {
// Create the map of given keys.
givenKeys := make(map[string]keyfunc.GivenKey)
key, err := addECDSA(givenKeys, testKID)
if err != nil {
t.Errorf(err.Error())
t.FailNow()
}
// Use the RSA public key to create a JWKs.
jwks := keyfunc.NewGiven(givenKeys)
// Create the JWT with the appropriate key ID.
token := jwt.New(jwt.SigningMethodES256)
token.Header[kidAttribute] = testKID
// Sign, parse, and validate the JWT.
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) {
// Create the map of given keys.
givenKeys := make(map[string]keyfunc.GivenKey)
key, err := addHMAC(givenKeys, testKID)
if err != nil {
t.Errorf(err.Error())
t.FailNow()
}
// Use an HMAC secret to create a given JWKs.
jwks := keyfunc.NewGiven(givenKeys)
// Create a JWT with the appropriate key ID.
token := jwt.New(jwt.SigningMethodHS256)
token.Header[kidAttribute] = testKID
// Sign, parse, and validate the JWT.
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) {
// Create the map of given keys.
givenKeys := make(map[string]keyfunc.GivenKey)
key, err := addRSA(givenKeys, testKID)
if err != nil {
t.Errorf(err.Error())
t.FailNow()
}
// Use the RSA public key to create a JWKs.
jwks := keyfunc.NewGiven(givenKeys)
// Create the JWT with the appropriate key ID.
token := jwt.New(jwt.SigningMethodRS256)
token.Header[kidAttribute] = testKID
// Sign, parse, and validate the JWT.
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.NewGivenCustom(key)
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) {
// Create the ECDSA key.
if key, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader); err != nil {
return nil, fmt.Errorf("failed to create ECDSA key: %w", err)
}
// Add the new ECDSA public key to the keys map.
givenKeys[kid] = keyfunc.NewGivenECDSA(&key.PublicKey)
return key, nil
}
// addHMAC creates a new HMAC secret stuff.
func addHMAC(givenKeys map[string]keyfunc.GivenKey, kid string) (secret []byte, err error) {
// Create the HMAC secret.
secret = make([]byte, sha256.BlockSize)
if _, err = rand.Read(secret); err != nil {
return nil, fmt.Errorf("failed to create HMAC secret: %w", err)
}
// Add the new HMAC key to the keys map.
givenKeys[kid] = keyfunc.NewGivenHMAC(secret)
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) {
// Create the RSA key.
if key, err = rsa.GenerateKey(rand.Reader, 2048); err != nil {
return nil, fmt.Errorf("failed to create RSA key: %w", err)
}
// Add the new RSA public key to the keys map.
givenKeys[kid] = keyfunc.NewGivenRSA(&key.PublicKey)
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) {
// Sign the token.
jwtB64, err := token.SignedString(key)
if err != nil {
t.Errorf("Failed to sign the JWT.\nError: %s", err.Error())
t.FailNow()
}
// Parse the JWT using the JWKs.
var parsed *jwt.Token
if parsed, err = jwt.Parse(jwtB64, jwks.Keyfunc); err != nil {
t.Errorf("Failed to parse the JWT.\nError: %s.", err.Error())
t.FailNow()
}
// Confirm the JWT is valid.
if !parsed.Valid {
t.Errorf("The JWT was not valid.")
t.FailNow()
}
}