-
Notifications
You must be signed in to change notification settings - Fork 377
/
jwt_spec.rb
513 lines (416 loc) · 18 KB
/
jwt_spec.rb
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
require 'spec_helper'
require 'jwt'
require 'jwt/encode'
require 'jwt/decode'
describe JWT do
let(:payload) { { 'user_id' => '[email protected]' } }
let :data do
data = {
:secret => 'My$ecretK3y',
:rsa_private => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'rsa-2048-private.pem'))),
:rsa_public => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'rsa-2048-public.pem'))),
:wrong_rsa_private => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'rsa-2048-wrong-public.pem'))),
:wrong_rsa_public => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'rsa-2048-wrong-public.pem'))),
'ES256_private' => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'ec256-private.pem'))),
'ES256_public' => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'ec256-public.pem'))),
'ES384_private' => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'ec384-private.pem'))),
'ES384_public' => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'ec384-public.pem'))),
'ES512_private' => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'ec512-private.pem'))),
'ES512_public' => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'ec512-public.pem'))),
'NONE' => 'eyJhbGciOiJub25lIn0.eyJ1c2VyX2lkIjoic29tZUB1c2VyLnRsZCJ9.',
'HS256' => 'eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoic29tZUB1c2VyLnRsZCJ9.kWOVtIOpWcG7JnyJG0qOkTDbOy636XrrQhMm_8JrRQ8',
'HS512256' => 'eyJhbGciOiJIUzUxMjI1NiJ9.eyJ1c2VyX2lkIjoic29tZUB1c2VyLnRsZCJ9.Ds_4ibvf7z4QOBoKntEjDfthy3WJ-3rKMspTEcHE2bA',
'HS384' => 'eyJhbGciOiJIUzM4NCJ9.eyJ1c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-uhRg',
'RS512' => 'eyJhbGciOiJSUzUxMiJ9.eyJ1c2VyX2lkIjoic29tZUB1c2VyLnRsZCJ9.LIIAUEuCkGNdpYguOO5LoW4rZ7ED2POJrB0pmEAAchyTdIK4HKh1jcLxc6KyGwZv40njCgub3y72q6vcQTn7oD0zWFCVQRIDW1911Ii2hRNHuigiPUnrnZh1OQ6z65VZRU6GKs8omoBGU9vrClBU0ODqYE16KxYmE_0n4Xw2h3D_L1LF0IAOtDWKBRDa3QHwZRM9sHsHNsBuD5ye9KzDYN1YALXj64LBfA-DoCKfpVAm9NkRPOyzjR2X2C3TomOSJgqWIVHJucudKDDAZyEbO4RA5pI-UFYy1370p9bRajvtDyoBuLDCzoSkMyQ4L2DnLhx5CbWcnD7Cd3GUmnjjTA',
'ES256' => '',
'ES384' => '',
'ES512' => '',
'PS256' => '',
'PS384' => '',
'PS512' => ''
}
if defined?(RbNaCl)
data.merge!(
'ED25519_private' => RbNaCl::Signatures::Ed25519::SigningKey.new('abcdefghijklmnopqrstuvwxyzABCDEF'),
'ED25519_public' => RbNaCl::Signatures::Ed25519::SigningKey.new('abcdefghijklmnopqrstuvwxyzABCDEF').verify_key,
)
end
data
end
after(:each) do
expect(OpenSSL.errors).to be_empty
end
context 'alg: NONE' do
let(:alg) { 'none' }
let(:encoded_token) { data['NONE'] }
it 'should generate a valid token' do
token = JWT.encode payload, nil, alg
expect(token).to eq encoded_token
end
context 'decoding without verification' do
it 'should decode a valid token' do
jwt_payload, header = JWT.decode encoded_token, nil, false
expect(header['alg']).to eq alg
expect(jwt_payload).to eq payload
end
end
context 'decoding with verification' do
context 'without specifying the none algorithm' do
it 'should fail to decode the token' do
expect do
JWT.decode encoded_token, nil, true
end.to raise_error JWT::IncorrectAlgorithm
end
end
context 'specifying the none algorithm' do
context 'when the claims are valid' do
it 'should decode the token' do
jwt_payload, header = JWT.decode encoded_token, nil, true, { algorithms: 'none' }
expect(header['alg']).to eq 'none'
expect(jwt_payload).to eq payload
end
end
context 'when the claims are invalid' do
let(:encoded_token) { JWT.encode({ exp: 0 }, nil, 'none') }
it 'should fail to decode the token' do
expect do
JWT.decode encoded_token, nil, true
end.to raise_error JWT::DecodeError
end
end
end
end
end
context 'payload validation' do
it 'validates the payload with the ClaimsValidator if the payload is a hash' do
validator = double()
expect(JWT::ClaimsValidator).to receive(:new) { validator }
expect(validator).to receive(:validate!) { true }
payload = {}
JWT.encode payload, "secret", 'HS256'
end
it 'does not validate the payload if it is not present' do
validator = double()
expect(JWT::ClaimsValidator).not_to receive(:new) { validator }
payload = nil
JWT.encode payload, "secret", 'HS256'
end
end
algorithms = %w[HS256 HS384 HS512]
algorithms << 'HS512256' if defined?(RbNaCl)
algorithms.each do |alg|
context "alg: #{alg}" do
it 'should generate a valid token' do
token = JWT.encode payload, data[:secret], alg
expect(token).to eq data[alg]
end
it 'should decode a valid token' do
jwt_payload, header = JWT.decode data[alg], data[:secret], true, algorithm: alg
expect(header['alg']).to eq alg
expect(jwt_payload).to eq payload
end
it 'wrong secret should raise JWT::DecodeError' do
expect do
JWT.decode data[alg], 'wrong_secret', true, algorithm: alg
end.to raise_error JWT::VerificationError
end
it 'wrong secret and verify = false should not raise JWT::DecodeError' do
expect do
JWT.decode data[alg], 'wrong_secret', false
end.not_to raise_error
end
end
end
%w[RS256 RS384 RS512].each do |alg|
context "alg: #{alg}" do
it 'should generate a valid token' do
token = JWT.encode payload, data[:rsa_private], alg
expect(token).to eq data[alg]
end
it 'should decode a valid token' do
jwt_payload, header = JWT.decode data[alg], data[:rsa_public], true, algorithm: alg
expect(header['alg']).to eq alg
expect(jwt_payload).to eq payload
end
it 'should decode a valid token using algorithm hash string key' do
jwt_payload, header = JWT.decode data[alg], data[:rsa_public], true, 'algorithm' => alg
expect(header['alg']).to eq alg
expect(jwt_payload).to eq payload
end
it 'wrong key should raise JWT::DecodeError' do
key = OpenSSL::PKey.read File.read(File.join(CERT_PATH, 'rsa-2048-wrong-public.pem'))
expect do
JWT.decode data[alg], key, true, algorithm: alg
end.to raise_error JWT::DecodeError
end
it 'wrong key and verify = false should not raise JWT::DecodeError' do
key = OpenSSL::PKey.read File.read(File.join(CERT_PATH, 'rsa-2048-wrong-public.pem'))
expect do
JWT.decode data[alg], key, false
end.not_to raise_error
end
end
end
if defined?(RbNaCl)
%w[ED25519].each do |alg|
context "alg: #{alg}" do
before(:each) do
data[alg] = JWT.encode payload, data["#{alg}_private"], alg
end
let(:wrong_key) { OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'ec256-wrong-public.pem'))) }
it 'should generate a valid token' do
jwt_payload, header = JWT.decode data[alg], data["#{alg}_public"], true, algorithm: alg
expect(header['alg']).to eq alg
expect(jwt_payload).to eq payload
end
it 'should decode a valid token' do
jwt_payload, header = JWT.decode data[alg], data["#{alg}_public"], true, algorithm: alg
expect(header['alg']).to eq alg
expect(jwt_payload).to eq payload
end
it 'wrong key should raise JWT::DecodeError' do
expect do
JWT.decode data[alg], wrong_key
end.to raise_error JWT::DecodeError
end
it 'wrong key and verify = false should not raise JWT::DecodeError' do
expect do
JWT.decode data[alg], wrong_key, false
end.not_to raise_error
end
end
end
end
%w[ES256 ES384 ES512].each do |alg|
context "alg: #{alg}" do
before(:each) do
data[alg] = JWT.encode payload, data["#{alg}_private"], alg
end
let(:wrong_key) { OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'ec256-wrong-public.pem'))) }
it 'should generate a valid token' do
jwt_payload, header = JWT.decode data[alg], data["#{alg}_public"], true, algorithm: alg
expect(header['alg']).to eq alg
expect(jwt_payload).to eq payload
end
it 'should decode a valid token' do
jwt_payload, header = JWT.decode data[alg], data["#{alg}_public"], true, algorithm: alg
expect(header['alg']).to eq alg
expect(jwt_payload).to eq payload
end
it 'wrong key should raise JWT::DecodeError' do
expect do
JWT.decode data[alg], wrong_key
end.to raise_error JWT::DecodeError
end
it 'wrong key and verify = false should not raise JWT::DecodeError' do
expect do
JWT.decode data[alg], wrong_key, false
end.not_to raise_error
end
end
end
unless OpenSSL::VERSION >= '2.1'
%w[PS256 PS384 PS512].each do |alg|
context "alg: #{alg}" do
it 'raises error about OpenSSL version' do
expect { JWT.encode payload, data[:rsa_private], alg }.to raise_error(
JWT::RequiredDependencyError,
/You currently have OpenSSL .*. PS support requires >= 2.1/
)
end
end
end
else
%w[PS256 PS384 PS512].each do |alg|
context "alg: #{alg}" do
before(:each) do
data[alg] = JWT.encode payload, data[:rsa_private], alg
end
let(:wrong_key) { data[:wrong_rsa_public] }
it 'should generate a valid token' do
token = data[alg]
header, body, signature = token.split('.')
expect(header).to eql(Base64.strict_encode64({ alg: alg }.to_json))
expect(body).to eql(Base64.strict_encode64(payload.to_json))
# Validate signature is made of up header and body of JWT
translated_alg = alg.gsub('PS', 'sha')
valid_signature = data[:rsa_public].verify_pss(
translated_alg,
JWT::Base64.url_decode(signature),
[header, body].join('.'),
salt_length: :auto,
mgf1_hash: translated_alg
)
expect(valid_signature).to be true
end
it 'should decode a valid token' do
jwt_payload, header = JWT.decode data[alg], data[:rsa_public], true, algorithm: alg
expect(header['alg']).to eq alg
expect(jwt_payload).to eq payload
end
it 'wrong key should raise JWT::DecodeError' do
expect do
JWT.decode data[alg], wrong_key
end.to raise_error JWT::DecodeError
end
it 'wrong key and verify = false should not raise JWT::DecodeError' do
expect do
JWT.decode data[alg], wrong_key, false
end.not_to raise_error
end
end
end
end
context 'Invalid' do
it 'algorithm should raise NotImplementedError' do
expect do
JWT.encode payload, 'secret', 'HS255'
end.to raise_error NotImplementedError
end
it 'raises "No verification key available" error' do
token = JWT.encode({}, 'foo')
expect { JWT.decode(token, nil, true) }.to raise_error(JWT::DecodeError, 'No verification key available')
end
it 'ECDSA curve_name should raise JWT::IncorrectAlgorithm' do
key = OpenSSL::PKey::EC.new 'secp256k1'
key.generate_key
expect do
JWT.encode payload, key, 'ES256'
end.to raise_error JWT::IncorrectAlgorithm
token = JWT.encode payload, data['ES256_private'], 'ES256'
key.private_key = nil
expect do
JWT.decode token, key
end.to raise_error JWT::IncorrectAlgorithm
end
end
context 'Verify' do
context 'algorithm' do
it 'should raise JWT::IncorrectAlgorithm on mismatch' do
token = JWT.encode payload, data[:secret], 'HS256'
expect do
JWT.decode token, data[:secret], true, algorithm: 'HS384'
end.to raise_error JWT::IncorrectAlgorithm
expect do
JWT.decode token, data[:secret], true, algorithm: 'HS256'
end.not_to raise_error
end
it 'should raise JWT::IncorrectAlgorithm on mismatch prior to kid public key network call' do
token = JWT.encode payload, data[:rsa_private], 'RS256'
expect do
JWT.decode(token, nil, true, { algorithms: ['RS384'] }) do |_,_|
# unsuccessful keyfinder public key network call here
end
end.to raise_error JWT::IncorrectAlgorithm
expect do
JWT.decode(token, nil, true, { 'algorithms' => ['RS384'] }) do |_,_|
# unsuccessful keyfinder public key network call here
end
end.to raise_error JWT::IncorrectAlgorithm
end
it 'should raise JWT::IncorrectAlgorithm when algorithms array does not contain algorithm' do
token = JWT.encode payload, data[:secret], 'HS512'
expect do
JWT.decode token, data[:secret], true, algorithms: ['HS384']
end.to raise_error JWT::IncorrectAlgorithm
expect do
JWT.decode token, data[:secret], true, 'algorithms' => ['HS384']
end.to raise_error JWT::IncorrectAlgorithm
expect do
JWT.decode token, data[:secret], true, algorithms: ['HS512', 'HS384']
end.not_to raise_error
expect do
JWT.decode token, data[:secret], true, 'algorithms' => ['HS512', 'HS384']
end.not_to raise_error
end
context 'no algorithm provided' do
it 'should use the default decode algorithm' do
token = JWT.encode payload, data[:rsa_public].to_s
jwt_payload, header = JWT.decode token, data[:rsa_public].to_s
expect(header['alg']).to eq 'HS256'
expect(jwt_payload).to eq payload
end
end
end
context 'issuer claim' do
let(:iss) { 'ruby-jwt-gem' }
let(:invalid_token) { JWT.encode payload, data[:secret] }
let :token do
iss_payload = payload.merge(iss: iss)
JWT.encode iss_payload, data[:secret]
end
it 'if verify_iss is set to false (default option) should not raise JWT::InvalidIssuerError' do
expect do
JWT.decode token, data[:secret], true, iss: iss, algorithm: 'HS256'
end.not_to raise_error
end
end
end
context 'a token with no segments' do
it 'raises JWT::DecodeError' do
expect { JWT.decode('ThisIsNotAValidJWTToken', nil, true) }.to raise_error(JWT::DecodeError, 'Not enough or too many segments')
end
end
context 'a token with not enough segments' do
it 'raises JWT::DecodeError' do
token = JWT.encode('ThisIsNotAValidJWTToken', 'secret').split('.').slice(1,2).join
expect { JWT.decode(token, nil, true) }.to raise_error(JWT::DecodeError, 'Not enough or too many segments')
end
end
context 'a token with not too many segments' do
it 'raises JWT::DecodeError' do
expect { JWT.decode('ThisIsNotAValidJWTToken.second.third.signature', nil, true) }.to raise_error(JWT::DecodeError, 'Not enough or too many segments')
end
end
context 'a token with two segments but does not require verifying' do
it 'raises something else than "Not enough or too many segments"' do
expect { JWT.decode('ThisIsNotAValidJWTToken.second', nil, false) }.to raise_error(JWT::DecodeError, 'Invalid segment encoding')
end
end
context 'Base64' do
it 'urlsafe replace + / with - _' do
allow(Base64).to receive(:encode64) { 'string+with/non+url-safe/characters_' }
expect(JWT::Base64.url_encode('foo')).to eq('string-with_non-url-safe_characters_')
end
end
it 'should not verify token even if the payload has claims' do
head = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9'
load = 'eyJ1c2VyX2lkIjo1NCwiZXhwIjoxNTA0MzkwODA0fQ'
sign = 'Skpi6FfYMbZ-DwW9ocyRIosNMdPMAIWRLYxRO68GTQk'
expect do
JWT.decode([head, load, sign].join('.'), '', false)
end.not_to raise_error
end
it 'should not raise InvalidPayload exception if payload is an array' do
expect do
JWT.encode(['my', 'payload'], 'secret')
end.not_to raise_error
end
it 'should encode string payloads' do
expect do
JWT.encode 'Hello World', 'secret'
end.not_to raise_error
end
context 'when the alg value is given as a header parameter' do
it 'does not override the actual algorithm used' do
headers = JSON.parse(::JWT::Base64.url_decode(JWT.encode('Hello World', 'secret', 'HS256', { alg: 'HS123'}).split('.').first))
expect(headers['alg']).to eq('HS256')
end
it "should generate the same token" do
expect(JWT.encode('Hello World', 'secret', 'HS256', { alg: 'HS256'})).to eq JWT.encode('Hello World', 'secret', 'HS256')
end
end
context 'when hmac algorithm is used without secret key' do
it 'encodes payload' do
payload = { a: 1, b: 'b'}
token = JWT.encode(payload, '', 'HS256')
expect do
token_without_secret = JWT.encode(payload, nil, 'HS256')
expect(token).to eq(token_without_secret)
end.not_to raise_error
end
end
end