forked from lestrrat-go/jwx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwx_test.go
299 lines (264 loc) · 9 KB
/
jwx_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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package jwx_test
import (
"context"
"crypto/ecdsa"
"crypto/rsa"
"fmt"
"strings"
"testing"
"github.com/lestrrat-go/jwx"
"github.com/lestrrat-go/jwx/internal/jose"
"github.com/lestrrat-go/jwx/internal/json"
"github.com/lestrrat-go/jwx/internal/jwxtest"
"github.com/lestrrat-go/jwx/jwa"
"github.com/lestrrat-go/jwx/jwe"
"github.com/lestrrat-go/jwx/jwk"
"github.com/stretchr/testify/assert"
)
type jsonUnmarshalWrapper struct {
buf []byte
}
func (w jsonUnmarshalWrapper) Decode(v interface{}) error {
return json.Unmarshal(w.buf, v)
}
func TestDecoderSetting(t *testing.T) {
const src = `{"foo": 1}`
for _, useNumber := range []bool{true, false} {
useNumber := useNumber
t.Run(fmt.Sprintf("jwx.WithUseNumber(%t)", useNumber), func(t *testing.T) {
if useNumber {
jwx.DecoderSettings(jwx.WithUseNumber(useNumber))
t.Cleanup(func() {
jwx.DecoderSettings(jwx.WithUseNumber(false))
})
}
// json.NewDecoder must be called AFTER the above jwx.DecoderSettings call
decoders := []struct {
Name string
Decoder interface{ Decode(interface{}) error }
}{
{Name: "Decoder", Decoder: json.NewDecoder(strings.NewReader(src))},
{Name: "Unmarshal", Decoder: jsonUnmarshalWrapper{buf: []byte(src)}},
}
for _, tc := range decoders {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
var m map[string]interface{}
if !assert.NoError(t, tc.Decoder.Decode(&m), `Decode should succeed`) {
return
}
v, ok := m["foo"]
if !assert.True(t, ok, `m["foo"] should exist`) {
return
}
if useNumber {
if !assert.Equal(t, json.Number("1"), v, `v should be a json.Number object`) {
return
}
} else {
if !assert.Equal(t, float64(1), v, `v should be a float64`) {
return
}
}
})
}
})
}
}
// Test compatibility against `jose` tool
func TestJoseCompatibility(t *testing.T) {
if testing.Short() {
t.Logf("Skipped during short tests")
return
}
if !jose.Available() {
t.Logf("`jose` binary not availale, skipping tests")
return
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
t.Run("jwk", func(t *testing.T) {
testcases := []struct {
Name string
Raw interface{}
Template string
VerifyKey func(context.Context, *testing.T, jwk.Key) bool
}{
{
Name: "RSA Private Key (256)",
Raw: rsa.PrivateKey{},
Template: `{"alg": "RS256"}`,
},
{
Name: "RSA Private Key (384)",
Raw: rsa.PrivateKey{},
Template: `{"alg": "RS384"}`,
},
{
Name: "RSA Private Key (512)",
Raw: rsa.PrivateKey{},
Template: `{"alg": "RS512"}`,
},
{
Name: "RSA Private Key with Private Parameters",
Raw: rsa.PrivateKey{},
Template: `{"alg": "RS256", "x-jwx": 1234}`,
VerifyKey: func(ctx context.Context, t *testing.T, key jwk.Key) bool {
m, err := key.AsMap(ctx)
if !assert.NoError(t, err, `key.AsMap() should succeed`) {
return false
}
if !assert.Equal(t, float64(1234), m["x-jwx"], `private parameters should match`) {
return false
}
return true
},
},
}
for _, tc := range testcases {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
keyfile, cleanup, err := jose.GenerateJwk(ctx, t, tc.Template)
if !assert.NoError(t, err, `jose.GenerateJwk should succeed`) {
return
}
defer cleanup()
webkey, err := jwxtest.ParseJwkFile(ctx, keyfile)
if !assert.NoError(t, err, `ParseJwkFile should succeed`) {
return
}
if vk := tc.VerifyKey; vk != nil {
if !vk(ctx, t, webkey) {
return
}
}
if !assert.NoError(t, webkey.Raw(&tc.Raw), `jwk.Raw should succeed`) {
return
}
})
}
})
t.Run("jwe", func(t *testing.T) {
tests := []interopTest{
{jwa.RSA1_5, jwa.A128GCM},
{jwa.RSA1_5, jwa.A128CBC_HS256},
{jwa.RSA1_5, jwa.A256CBC_HS512},
{jwa.RSA_OAEP, jwa.A128GCM},
{jwa.RSA_OAEP, jwa.A128CBC_HS256},
{jwa.RSA_OAEP, jwa.A256CBC_HS512},
{jwa.RSA_OAEP_256, jwa.A128GCM},
{jwa.RSA_OAEP_256, jwa.A128CBC_HS256},
{jwa.RSA_OAEP_256, jwa.A256CBC_HS512},
// {jwa.ECDH_ES, jwa.A256CBC_HS512},
{jwa.ECDH_ES_A128KW, jwa.A128GCM},
// {jwa.ECDH_ES_A128KW, jwa.A128CBC_HS256},
// {jwa.ECDH_ES_A256KW, jwa.A256GCM}, fails intermittently
// {jwa.ECDH_ES_A256KW, jwa.A256CBC_HS512},
{jwa.A128KW, jwa.A128GCM},
{jwa.A128KW, jwa.A128CBC_HS256},
{jwa.A256KW, jwa.A256GCM},
{jwa.A256KW, jwa.A256CBC_HS512},
{jwa.A128GCMKW, jwa.A128GCM},
// {jwa.A128GCMKW, jwa.A128CBC_HS256},
{jwa.A256GCMKW, jwa.A256GCM},
// {jwa.A256GCMKW, jwa.A256CBC_HS512},
{jwa.PBES2_HS256_A128KW, jwa.A128GCM},
{jwa.PBES2_HS256_A128KW, jwa.A128CBC_HS256},
{jwa.PBES2_HS512_A256KW, jwa.A256GCM},
{jwa.PBES2_HS512_A256KW, jwa.A256CBC_HS512},
}
for _, test := range tests {
test := test
t.Run(fmt.Sprintf("%s-%s", test.alg, test.enc),
func(t *testing.T) {
joseInteropTest(ctx, test, t)
})
}
})
}
type interopTest struct {
alg jwa.KeyEncryptionAlgorithm
enc jwa.ContentEncryptionAlgorithm
}
func joseInteropTest(ctx context.Context, spec interopTest, t *testing.T) {
expected := []byte("Lorem ipsum")
// let jose generate a key file
joseJwkFile, joseJwkCleanup, err := jose.GenerateJwk(ctx, t, fmt.Sprintf(`{"alg": "%s"}`, spec.alg))
if !assert.NoError(t, err, `jose.GenerateJwk should succeed`) {
return
}
defer joseJwkCleanup()
// Load the JWK generated by jose
jwxJwk, err := jwxtest.ParseJwkFile(ctx, joseJwkFile)
if !assert.NoError(t, err, `jwxtest.ParseJwkFile should succeed`) {
return
}
t.Run("Parse JWK via jwx", func(t *testing.T) {
switch spec.alg {
case jwa.RSA1_5, jwa.RSA_OAEP, jwa.RSA_OAEP_256:
var rawkey rsa.PrivateKey
if !assert.NoError(t, jwxJwk.Raw(&rawkey), `jwk.Raw should succeed`) {
return
}
case jwa.ECDH_ES, jwa.ECDH_ES_A128KW, jwa.ECDH_ES_A192KW, jwa.ECDH_ES_A256KW:
var rawkey ecdsa.PrivateKey
if !assert.NoError(t, jwxJwk.Raw(&rawkey), `jwk.Raw should succeed`) {
return
}
default:
var rawkey []byte
if !assert.NoError(t, jwxJwk.Raw(&rawkey), `jwk.Raw should succeed`) {
return
}
}
})
t.Run("Encrypt with jose, Decrypt with jwx", func(t *testing.T) {
// let jose encrypt payload using the key file
joseCryptFile, joseCryptCleanup, err := jose.EncryptJwe(ctx, t, expected, joseJwkFile, spec.enc.String(), true)
if !assert.NoError(t, err, `jose.EncryptJwe should succeed`) {
return
}
defer joseCryptCleanup()
jwxtest.DumpFile(t, joseCryptFile)
// let jwx decrypt the jose crypted file
payload, err := jwxtest.DecryptJweFile(ctx, joseCryptFile, spec.alg, joseJwkFile)
if !assert.NoError(t, err, `decryptFile.DecryptJwe should succeed`) {
return
}
if !assert.Equal(t, expected, payload, `decrypted payloads should match`) {
return
}
})
t.Run("Encrypt with jwx, Decrypt with jose", func(t *testing.T) {
jwxCryptFile, jwxCryptCleanup, err := jwxtest.EncryptJweFile(ctx, expected, spec.alg, joseJwkFile, spec.enc, jwa.NoCompress)
if !assert.NoError(t, err, `jwxtest.EncryptJweFile should succeed`) {
return
}
defer jwxCryptCleanup()
payload, err := jose.DecryptJwe(ctx, t, jwxCryptFile, joseJwkFile)
if !assert.NoError(t, err, `jose.DecryptJwe should succeed`) {
return
}
if !assert.Equal(t, expected, payload, `decrypted payloads should match`) {
return
}
})
}
func TestGHIssue230(t *testing.T) {
data := "eyJhbGciOiJFQ0RILUVTIiwiY2xldmlzIjp7InBpbiI6InRhbmciLCJ0YW5nIjp7ImFkdiI6eyJrZXlzIjpbeyJhbGciOiJFQ01SIiwiY3J2IjoiUC01MjEiLCJrZXlfb3BzIjpbImRlcml2ZUtleSJdLCJrdHkiOiJFQyIsIngiOiJBZm5tR2xHRTFHRUZ5NEpUT2tGWmo5ZEhEUmdpVE5IeFBST3hpZDZLdm0xVGRFQkZ3bElsSVB6TG5lTjlnb3h6OUVGYmJLM3BoN0tWZS05aVF4MmxhOVNFIiwieSI6IkFmZGFaTVYzVzk1NE14elQxeXF3MWVaRU9xTFFZZnBXSGczMlJvekhyQjBEYmoxWWV3OVFvTDg1M2Y2aUw2REIyRC1nbEcxSFFsb3czdGRNdFhjN1pSY0IifSx7ImFsZyI6IkVTNTEyIiwiY3J2IjoiUC01MjEiLCJrZXlfb3BzIjpbInZlcmlmeSJdLCJrdHkiOiJFQyIsIngiOiJBR0drcXRPZzZqel9pZnhmVnVWQ01CalVySFhCTGtfS2hIb3lKRkU5NmJucTZKZVVHNFNMZnRrZ2FIYk5WT0U4Q3Mwd0JqR0ZkSWxDbnBmak94RGJfbFBoIiwieSI6IkFLU0laT0JYY1Jfa3RkWjZ6T3F3TGI5SEJzai0yYmRMUmw5dFZVbnVlV2N3aXg5X3NiekliSWx0SE9YUGhBTW9yaUlYMWVyNzc4Unh6Vkg5d0FtaUhGa1kifV19LCJ1cmwiOiJodHRwOi8vbG9jYWxob3N0OjM5NDIxIn19LCJlbmMiOiJBMjU2R0NNIiwiZXBrIjp7ImNydiI6IlAtNTIxIiwia3R5IjoiRUMiLCJ4IjoiQUJMUm9sQWotZFdVdzZLSjg2T3J6d1F6RjlGT09URFZBZnNWNkh0OU0zREhyQ045Q0N6dVJ1b3cwbWp6M3BjZnVCaFpYREpfN0dkdzE0LXdneV9fTFNrYyIsInkiOiJBT3NRMzlKZmFQVGhjc2FZTjhSMVBHXzIwYXZxRU1NRl9fM2RHQmI3c1BqNmktNEJORDVMdkZ3cVpJT1l4SS1kVWlvNzkyOWY1YnE0eEdJY0lGWWtlbllxIn0sImtpZCI6ImhlZmVpNzVqMkp4Sko3REZnSDAxUWlOVmlGayJ9..GH3-8v7wfxEsRnki.wns--EIYTRjM3Tb0HyA.EGn2Gq7PnSVvPaMN0oRi5A"
compactMsg, err := jwe.ParseString(data)
if !assert.NoError(t, err, `jwe.ParseString should succeed`) {
return
}
formatted, err := jose.FmtJwe(context.TODO(), t, []byte(data))
if !assert.NoError(t, err, `jose.FmtJwe should succeed`) {
return
}
jsonMsg, err := jwe.Parse(formatted)
if !assert.NoError(t, err, `jwe.Parse should succeed`) {
return
}
if !assert.Equal(t, compactMsg, jsonMsg, `messages should match`) {
return
}
}