-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgiven_test.go
160 lines (124 loc) · 4.26 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
package keyfunc_test
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"fmt"
"testing"
"github.com/dgrijalva/jwt-go"
"github.com/MicahParks/compatibility-keyfunc"
"github.com/MicahParks/compatibility-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.CustomAlg, 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.CustomAlg
token.Header[kidAttribute] = testKID
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) {
givenKeys := make(map[string]keyfunc.GivenKey)
key, err := addECDSA(givenKeys, testKID)
if err != nil {
t.Errorf(err.Error())
t.FailNow()
}
jwks := keyfunc.NewGiven(givenKeys)
token := jwt.New(jwt.SigningMethodES256)
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.Errorf(err.Error())
t.FailNow()
}
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.Errorf(err.Error())
t.FailNow()
}
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.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) {
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.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) {
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.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) {
key, err = rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, fmt.Errorf("failed to create RSA key: %w", err)
}
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) {
jwtB64, err := token.SignedString(key)
if err != nil {
t.Errorf("Failed to sign the JWT.\nError: %s", err.Error())
t.FailNow()
}
parsed, err := jwt.Parse(jwtB64, jwks.KeyfuncLegacy)
if err != nil {
t.Errorf("Failed to parse the JWT.\nError: %s.", err.Error())
t.FailNow()
}
if !parsed.Valid {
t.Errorf("The JWT was not valid.")
t.FailNow()
}
}