jwt 1.5.4 → 2.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -13
- data/AUTHORS +119 -0
- data/CHANGELOG.md +812 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/CONTRIBUTING.md +99 -0
- data/README.md +400 -79
- data/lib/jwt/algos/algo_wrapper.rb +30 -0
- data/lib/jwt/algos/ecdsa.rb +62 -0
- data/lib/jwt/algos/eddsa.rb +33 -0
- data/lib/jwt/algos/hmac.rb +73 -0
- data/lib/jwt/algos/hmac_rbnacl.rb +53 -0
- data/lib/jwt/algos/hmac_rbnacl_fixed.rb +52 -0
- data/lib/jwt/algos/none.rb +19 -0
- data/lib/jwt/algos/ps.rb +41 -0
- data/lib/jwt/algos/rsa.rb +21 -0
- data/lib/jwt/algos/unsupported.rb +19 -0
- data/lib/jwt/algos.rb +67 -0
- data/lib/jwt/base64.rb +19 -0
- data/lib/jwt/claims_validator.rb +37 -0
- data/lib/jwt/configuration/container.rb +21 -0
- data/lib/jwt/configuration/decode_configuration.rb +46 -0
- data/lib/jwt/configuration/jwk_configuration.rb +27 -0
- data/lib/jwt/configuration.rb +15 -0
- data/lib/jwt/decode.rb +141 -29
- data/lib/jwt/encode.rb +79 -0
- data/lib/jwt/error.rb +10 -0
- data/lib/jwt/json.rb +11 -9
- data/lib/jwt/jwk/ec.rb +236 -0
- data/lib/jwt/jwk/hmac.rb +103 -0
- data/lib/jwt/jwk/key_base.rb +55 -0
- data/lib/jwt/jwk/key_finder.rb +46 -0
- data/lib/jwt/jwk/kid_as_key_digest.rb +15 -0
- data/lib/jwt/jwk/okp_rbnacl.rb +110 -0
- data/lib/jwt/jwk/rsa.rb +203 -0
- data/lib/jwt/jwk/set.rb +80 -0
- data/lib/jwt/jwk/thumbprint.rb +26 -0
- data/lib/jwt/jwk.rb +55 -0
- data/lib/jwt/security_utils.rb +32 -0
- data/lib/jwt/verify.rb +59 -44
- data/lib/jwt/version.rb +25 -4
- data/lib/jwt/x5c_key_finder.rb +55 -0
- data/lib/jwt.rb +16 -162
- data/ruby-jwt.gemspec +19 -9
- metadata +64 -97
- data/.codeclimate.yml +0 -20
- data/.gitignore +0 -6
- data/.rspec +0 -2
- data/.rubocop.yml +0 -2
- data/.travis.yml +0 -13
- data/Gemfile +0 -4
- data/Manifest +0 -8
- data/Rakefile +0 -1
- data/spec/fixtures/certs/ec256-private.pem +0 -8
- data/spec/fixtures/certs/ec256-public.pem +0 -4
- data/spec/fixtures/certs/ec256-wrong-private.pem +0 -8
- data/spec/fixtures/certs/ec256-wrong-public.pem +0 -4
- data/spec/fixtures/certs/ec384-private.pem +0 -9
- data/spec/fixtures/certs/ec384-public.pem +0 -5
- data/spec/fixtures/certs/ec384-wrong-private.pem +0 -9
- data/spec/fixtures/certs/ec384-wrong-public.pem +0 -5
- data/spec/fixtures/certs/ec512-private.pem +0 -10
- data/spec/fixtures/certs/ec512-public.pem +0 -6
- data/spec/fixtures/certs/ec512-wrong-private.pem +0 -10
- data/spec/fixtures/certs/ec512-wrong-public.pem +0 -6
- data/spec/fixtures/certs/rsa-1024-private.pem +0 -15
- data/spec/fixtures/certs/rsa-1024-public.pem +0 -6
- data/spec/fixtures/certs/rsa-2048-private.pem +0 -27
- data/spec/fixtures/certs/rsa-2048-public.pem +0 -9
- data/spec/fixtures/certs/rsa-2048-wrong-private.pem +0 -27
- data/spec/fixtures/certs/rsa-2048-wrong-public.pem +0 -9
- data/spec/fixtures/certs/rsa-4096-private.pem +0 -51
- data/spec/fixtures/certs/rsa-4096-public.pem +0 -14
- data/spec/jwt/verify_spec.rb +0 -175
- data/spec/jwt_spec.rb +0 -232
- data/spec/spec_helper.rb +0 -31
data/spec/jwt/verify_spec.rb
DELETED
@@ -1,175 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'jwt/verify'
|
3
|
-
|
4
|
-
module JWT
|
5
|
-
RSpec.describe Verify do
|
6
|
-
let(:base_payload) { { 'user_id' => 'some@user.tld' } }
|
7
|
-
let(:options) { { leeway: 0} }
|
8
|
-
|
9
|
-
context '.verify_aud(payload, options)' do
|
10
|
-
let(:scalar_aud) { 'ruby-jwt-audience' }
|
11
|
-
let(:array_aud) { %w(ruby-jwt-aud test-aud ruby-ruby-ruby) }
|
12
|
-
let(:scalar_payload) { base_payload.merge('aud' => scalar_aud) }
|
13
|
-
let(:array_payload) { base_payload.merge('aud' => array_aud) }
|
14
|
-
|
15
|
-
it 'must raise JWT::InvalidAudError when the singular audience does not match' do
|
16
|
-
expect do
|
17
|
-
Verify.verify_aud(scalar_payload, options.merge(aud: 'no-match'))
|
18
|
-
end.to raise_error JWT::InvalidAudError
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'must raise JWT::InvalidAudError when the payload has an array and none match the supplied value' do
|
22
|
-
expect do
|
23
|
-
Verify.verify_aud(array_payload, options.merge(aud: 'no-match'))
|
24
|
-
end.to raise_error JWT::InvalidAudError
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'must raise JWT::InvalidAudError when the singular audience does not match and the options aud key is a string' do
|
28
|
-
expect do
|
29
|
-
Verify.verify_aud(scalar_payload, options.merge('aud' => 'no-match'))
|
30
|
-
end.to raise_error JWT::InvalidAudError
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'must allow a matching singular audience to pass' do
|
34
|
-
Verify.verify_aud(scalar_payload, options.merge(aud: scalar_aud))
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'must allow a matching audence to pass when the options key is a string' do
|
38
|
-
Verify.verify_aud(scalar_payload, options.merge('aud' => scalar_aud))
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'must allow an array with any value matching the one in the options' do
|
42
|
-
Verify.verify_aud(array_payload, options.merge(aud: array_aud.first))
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'must allow an array with any value matching the one in the options with a string options key' do
|
46
|
-
Verify.verify_aud(array_payload, options.merge('aud' => array_aud.first))
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
context '.verify_expiration(payload, options)' do
|
51
|
-
let(:leeway) { 10 }
|
52
|
-
let(:payload) { base_payload.merge('exp' => (Time.now.to_i - 5)) }
|
53
|
-
|
54
|
-
it 'must raise JWT::ExpiredSignature when the token has expired' do
|
55
|
-
expect do
|
56
|
-
Verify.verify_expiration(payload, options)
|
57
|
-
end.to raise_error JWT::ExpiredSignature
|
58
|
-
end
|
59
|
-
|
60
|
-
it 'must allow some leeway in the expiration when configured' do
|
61
|
-
Verify.verify_expiration(payload, options.merge(leeway: 10))
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
context '.verify_iat(payload, options)' do
|
66
|
-
let(:iat) { Time.now.to_f }
|
67
|
-
let(:payload) { base_payload.merge('iat' => iat) }
|
68
|
-
|
69
|
-
it 'must allow a valid iat' do
|
70
|
-
Verify.verify_iat(payload, options)
|
71
|
-
end
|
72
|
-
|
73
|
-
it 'must allow configured leeway' do
|
74
|
-
Verify.verify_iat(payload.merge('iat' => (iat + 60)), options.merge(leeway: 70))
|
75
|
-
end
|
76
|
-
|
77
|
-
it 'must properly handle integer times' do
|
78
|
-
Verify.verify_iat(payload.merge('iat' => Time.now.to_i), options)
|
79
|
-
end
|
80
|
-
|
81
|
-
it 'must raise JWT::InvalidIatError when the iat value is not Numeric' do
|
82
|
-
expect do
|
83
|
-
Verify.verify_iat(payload.merge('iat' => 'not a number'), options)
|
84
|
-
end.to raise_error JWT::InvalidIatError
|
85
|
-
end
|
86
|
-
|
87
|
-
it 'must raise JWT::InvalidIatError when the iat value is in the future' do
|
88
|
-
expect do
|
89
|
-
Verify.verify_iat(payload.merge('iat' => (iat + 120)), options)
|
90
|
-
end.to raise_error JWT::InvalidIatError
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
context '.verify_iss(payload, options)' do
|
95
|
-
let(:iss) { 'ruby-jwt-gem' }
|
96
|
-
let(:payload) { base_payload.merge('iss' => iss) }
|
97
|
-
|
98
|
-
let(:invalid_token) { JWT.encode base_payload, payload[:secret] }
|
99
|
-
|
100
|
-
it 'must raise JWT::InvalidIssuerError when the configured issuer does not match the payload issuer' do
|
101
|
-
expect do
|
102
|
-
Verify.verify_iss(payload, options.merge(iss: 'mismatched-issuer'))
|
103
|
-
end.to raise_error JWT::InvalidIssuerError
|
104
|
-
end
|
105
|
-
|
106
|
-
it 'must raise JWT::InvalidIssuerError when the payload does not include an issuer' do
|
107
|
-
expect do
|
108
|
-
Verify.verify_iss(base_payload, options.merge(iss: iss))
|
109
|
-
end.to raise_error(JWT::InvalidIssuerError, /received <none>/)
|
110
|
-
end
|
111
|
-
|
112
|
-
it 'must allow a matching issuer to pass' do
|
113
|
-
Verify.verify_iss(payload, options.merge(iss: iss))
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
context '.verify_jti(payload, options)' do
|
118
|
-
let(:payload) { base_payload.merge('jti' => 'some-random-uuid-or-whatever') }
|
119
|
-
|
120
|
-
it 'must allow any jti when the verfy_jti key in the options is truthy but not a proc' do
|
121
|
-
Verify.verify_jti(payload, options.merge(verify_jti: true))
|
122
|
-
end
|
123
|
-
|
124
|
-
it 'must raise JWT::InvalidJtiError when the jti is missing' do
|
125
|
-
expect do
|
126
|
-
Verify.verify_jti(base_payload, options)
|
127
|
-
end.to raise_error JWT::InvalidJtiError, /missing/i
|
128
|
-
end
|
129
|
-
|
130
|
-
it 'must raise JWT::InvalidJtiError when the jti is an empty string' do
|
131
|
-
expect do
|
132
|
-
Verify.verify_jti(base_payload.merge('jti' => ' '), options)
|
133
|
-
end.to raise_error JWT::InvalidJtiError, /missing/i
|
134
|
-
end
|
135
|
-
|
136
|
-
it 'must raise JWT::InvalidJtiError when verify_jti proc returns false' do
|
137
|
-
expect do
|
138
|
-
Verify.verify_jti(payload, options.merge(verify_jti: ->(jti) { false }))
|
139
|
-
end.to raise_error JWT::InvalidJtiError, /invalid/i
|
140
|
-
end
|
141
|
-
|
142
|
-
it 'true proc should not raise JWT::InvalidJtiError' do
|
143
|
-
Verify.verify_jti(payload, options.merge(verify_jti: ->(jti) { true }))
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
context '.verify_not_before(payload, options)' do
|
148
|
-
let(:payload) { base_payload.merge('nbf' => (Time.now.to_i + 5)) }
|
149
|
-
|
150
|
-
it 'must raise JWT::ImmatureSignature when the nbf in the payload is in the future' do
|
151
|
-
expect do
|
152
|
-
Verify.verify_not_before(payload, options)
|
153
|
-
end.to raise_error JWT::ImmatureSignature
|
154
|
-
end
|
155
|
-
|
156
|
-
it 'must allow some leeway in the token age when configured' do
|
157
|
-
Verify.verify_not_before(payload, options.merge(leeway: 10))
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
context '.verify_sub(payload, options)' do
|
162
|
-
let(:sub) { 'ruby jwt subject' }
|
163
|
-
|
164
|
-
it 'must raise JWT::InvalidSubError when the subjects do not match' do
|
165
|
-
expect do
|
166
|
-
Verify.verify_sub(base_payload.merge('sub' => 'not-a-match'), options.merge(sub: sub))
|
167
|
-
end.to raise_error JWT::InvalidSubError
|
168
|
-
end
|
169
|
-
|
170
|
-
it 'must allow a matching sub' do
|
171
|
-
Verify.verify_sub(base_payload.merge('sub' => sub), options.merge(sub: sub))
|
172
|
-
end
|
173
|
-
end
|
174
|
-
end
|
175
|
-
end
|
data/spec/jwt_spec.rb
DELETED
@@ -1,232 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'jwt'
|
3
|
-
require 'jwt/decode'
|
4
|
-
|
5
|
-
describe JWT do
|
6
|
-
let(:payload) { { 'user_id' => 'some@user.tld' } }
|
7
|
-
|
8
|
-
let :data do
|
9
|
-
{
|
10
|
-
:secret => 'My$ecretK3y',
|
11
|
-
:rsa_private => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'rsa-2048-private.pem'))),
|
12
|
-
:rsa_public => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'rsa-2048-public.pem'))),
|
13
|
-
:wrong_rsa_private => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'rsa-2048-wrong-public.pem'))),
|
14
|
-
:wrong_rsa_public => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'rsa-2048-wrong-public.pem'))),
|
15
|
-
'ES256_private' => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'ec256-private.pem'))),
|
16
|
-
'ES256_public' => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'ec256-public.pem'))),
|
17
|
-
'ES384_private' => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'ec384-private.pem'))),
|
18
|
-
'ES384_public' => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'ec384-public.pem'))),
|
19
|
-
'ES512_private' => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'ec512-private.pem'))),
|
20
|
-
'ES512_public' => OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'ec512-public.pem'))),
|
21
|
-
'NONE' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJ1c2VyX2lkIjoic29tZUB1c2VyLnRsZCJ9.',
|
22
|
-
'HS256' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoic29tZUB1c2VyLnRsZCJ9.tCGvlClld0lbQ3NZaH8y53n5RSBr3zlS4Oy5bXqvzZQ',
|
23
|
-
'HS384' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzM4NCJ9.eyJ1c2VyX2lkIjoic29tZUB1c2VyLnRsZCJ9.sj1gc01SawlJSrPZgmveifJ8CzZRYAWjejWm4FRaGaAISESJ9Ncf12fCz2vHrITm',
|
24
|
-
'HS512' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJ1c2VyX2lkIjoic29tZUB1c2VyLnRsZCJ9.isjhsWMZpRQOWw6LKtlY4L6tMDNkLr0qZ3bQe_xRFXWhzVvJlkclTbLVa1J6Dlj2WyZ_I1jEobTaFMDoXPzwWg',
|
25
|
-
'RS256' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1c2VyX2lkIjoic29tZUB1c2VyLnRsZCJ9.u82QrhjZTtwve5akvfWS_4LPywbkb1Yp0nUwZJWtTW0ID7dY9rRiQF5KGj2UDLZotqRlUjyNQgE_hB5BBzICDQdCjQHQoYWE5n_D2wV4PMu7Qg3FVKoBFbf8ee6irodu10fgYxpUIZtvbWw52_6k6A9IoSLSzx_lCcxoVGdW90dUuKhBcZkDtY5WNuQg7MiDthupSL1-V4Y1jmT_7o8tLNGFiocyZfGNw4yGpEOGNvD5WePNit0xsnbj6dEquovUvSFKsMaQXp2PVDEkLOiLMcyk0RrHqrHw2eNSCquWTH8PhX5Up-CVmjQM5zF9ibkaiq8NyPtsy-7rgtbyVMqXBQ',
|
26
|
-
'RS384' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzM4NCJ9.eyJ1c2VyX2lkIjoic29tZUB1c2VyLnRsZCJ9.2_jPwOsUWJ-3r6lXMdJGPdhLNJQSSEmY2mrDXCwNJk-2YhMIqKAzJJCbyso_A1hS7BVkXmHt54RCcNJXroZBOgmGavCcYTPMaT6sCvVVvJJ_wn7jzKHNAJfL5nWeynTQIBWmL-m_v9QpZAgPALdeqjPRv4JHePZm23kvrUgQOxef2ldXv1l6IB3zfF72uEbk9T5pKBvgeeeQ46xm_HtkpXqMdqcTHawUXeXhuiWxuWfy9pAvhm8ivxwJhiQ15-sQNBlS9lG1_gQz1xaZ_Ou_n1nhNfGwpK5HeS0AgmqsqyCOvaGHeAuAOPZ_dSC3cFKu2AP7kc6_AKBgwJzh4agkXg',
|
27
|
-
'RS512' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzUxMiJ9.eyJ1c2VyX2lkIjoic29tZUB1c2VyLnRsZCJ9.abwof7BqTvuLkN69OhEuFTP7vjGzfvAvooQdwIRne_a88MsjCq31n4UPvyIlY9_8u69rpU79RbMsrq_UZ6L85zP83EcyYI-HOfFZgYDAL3DJ7biBD99JTzyOsH_2i_E6yCkevjEX6uL_Am_C7jpWyePJQkYzTFni6mW4W1T9UobiVGA1tIZ-XOJDPHHxZkGu6W8lKW0UCsr9Ge2SCSlTs_LDSOa34gqMC5GP89unhLqSMqEMJ_Nm6Rj0rnmk87wBZM-b04LLteWuEU59QDNa4nMTjfXW74U4hX9n5EECDPQdQMecgxlUbFunAfZaoNzP4m7H4vux2FzYkjkXhdqnnw',
|
28
|
-
'ES256' => '',
|
29
|
-
'ES384' => '',
|
30
|
-
'ES512' => ''
|
31
|
-
}
|
32
|
-
end
|
33
|
-
|
34
|
-
after(:each) do
|
35
|
-
expect(OpenSSL.errors).to be_empty
|
36
|
-
end
|
37
|
-
|
38
|
-
context 'alg: NONE' do
|
39
|
-
let(:alg) { 'none' }
|
40
|
-
|
41
|
-
it 'should generate a valid token' do
|
42
|
-
token = JWT.encode payload, nil, alg
|
43
|
-
|
44
|
-
expect(token).to eq data['NONE']
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'should decode a valid token' do
|
48
|
-
jwt_payload, header = JWT.decode data['NONE'], nil, false
|
49
|
-
|
50
|
-
expect(header['alg']).to eq alg
|
51
|
-
expect(jwt_payload).to eq payload
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
%w(HS256 HS384 HS512).each do |alg|
|
56
|
-
context "alg: #{alg}" do
|
57
|
-
it 'should generate a valid token' do
|
58
|
-
token = JWT.encode payload, data[:secret], alg
|
59
|
-
|
60
|
-
expect(token).to eq data[alg]
|
61
|
-
end
|
62
|
-
|
63
|
-
it 'should decode a valid token' do
|
64
|
-
jwt_payload, header = JWT.decode data[alg], data[:secret]
|
65
|
-
|
66
|
-
expect(header['alg']).to eq alg
|
67
|
-
expect(jwt_payload).to eq payload
|
68
|
-
end
|
69
|
-
|
70
|
-
it 'wrong secret should raise JWT::DecodeError' do
|
71
|
-
expect do
|
72
|
-
JWT.decode data[alg], 'wrong_secret'
|
73
|
-
end.to raise_error JWT::DecodeError
|
74
|
-
end
|
75
|
-
|
76
|
-
it 'wrong secret and verify = false should not raise JWT::DecodeError' do
|
77
|
-
expect do
|
78
|
-
JWT.decode data[alg], 'wrong_secret', false
|
79
|
-
end.not_to raise_error
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
%w(RS256 RS384 RS512).each do |alg|
|
85
|
-
context "alg: #{alg}" do
|
86
|
-
it 'should generate a valid token' do
|
87
|
-
token = JWT.encode payload, data[:rsa_private], alg
|
88
|
-
|
89
|
-
expect(token).to eq data[alg]
|
90
|
-
end
|
91
|
-
|
92
|
-
it 'should decode a valid token' do
|
93
|
-
jwt_payload, header = JWT.decode data[alg], data[:rsa_public]
|
94
|
-
|
95
|
-
expect(header['alg']).to eq alg
|
96
|
-
expect(jwt_payload).to eq payload
|
97
|
-
end
|
98
|
-
|
99
|
-
it 'wrong key should raise JWT::DecodeError' do
|
100
|
-
key = OpenSSL::PKey.read File.read(File.join(CERT_PATH, 'rsa-2048-wrong-public.pem'))
|
101
|
-
|
102
|
-
expect do
|
103
|
-
JWT.decode data[alg], key
|
104
|
-
end.to raise_error JWT::DecodeError
|
105
|
-
end
|
106
|
-
|
107
|
-
it 'wrong key and verify = false should not raise JWT::DecodeError' do
|
108
|
-
key = OpenSSL::PKey.read File.read(File.join(CERT_PATH, 'rsa-2048-wrong-public.pem'))
|
109
|
-
|
110
|
-
expect do
|
111
|
-
JWT.decode data[alg], key, false
|
112
|
-
end.not_to raise_error
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
%w(ES256 ES384 ES512).each do |alg|
|
118
|
-
context "alg: #{alg}" do
|
119
|
-
before(:each) do
|
120
|
-
data[alg] = JWT.encode payload, data["#{alg}_private"], alg
|
121
|
-
end
|
122
|
-
|
123
|
-
let(:wrong_key) { OpenSSL::PKey.read(File.read(File.join(CERT_PATH, 'ec256-wrong-public.pem'))) }
|
124
|
-
|
125
|
-
it 'should generate a valid token' do
|
126
|
-
jwt_payload, header = JWT.decode data[alg], data["#{alg}_public"]
|
127
|
-
|
128
|
-
expect(header['alg']).to eq alg
|
129
|
-
expect(jwt_payload).to eq payload
|
130
|
-
end
|
131
|
-
|
132
|
-
it 'should decode a valid token' do
|
133
|
-
jwt_payload, header = JWT.decode data[alg], data["#{alg}_public"]
|
134
|
-
|
135
|
-
expect(header['alg']).to eq alg
|
136
|
-
expect(jwt_payload).to eq payload
|
137
|
-
end
|
138
|
-
|
139
|
-
it 'wrong key should raise JWT::DecodeError' do
|
140
|
-
expect do
|
141
|
-
JWT.decode data[alg], wrong_key
|
142
|
-
end.to raise_error JWT::DecodeError
|
143
|
-
end
|
144
|
-
|
145
|
-
it 'wrong key and verify = false should not raise JWT::DecodeError' do
|
146
|
-
expect do
|
147
|
-
JWT.decode data[alg], wrong_key, false
|
148
|
-
end.not_to raise_error
|
149
|
-
end
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
|
-
context 'Invalid' do
|
154
|
-
it 'algorithm should raise NotImplementedError' do
|
155
|
-
expect do
|
156
|
-
JWT.encode payload, 'secret', 'HS255'
|
157
|
-
end.to raise_error NotImplementedError
|
158
|
-
end
|
159
|
-
|
160
|
-
it 'ECDSA curve_name should raise JWT::IncorrectAlgorithm' do
|
161
|
-
key = OpenSSL::PKey::EC.new 'secp256k1'
|
162
|
-
key.generate_key
|
163
|
-
|
164
|
-
expect do
|
165
|
-
JWT.encode payload, key, 'ES256'
|
166
|
-
end.to raise_error JWT::IncorrectAlgorithm
|
167
|
-
|
168
|
-
token = JWT.encode payload, data['ES256_private'], 'ES256'
|
169
|
-
key.private_key = nil
|
170
|
-
|
171
|
-
expect do
|
172
|
-
JWT.decode token, key
|
173
|
-
end.to raise_error JWT::IncorrectAlgorithm
|
174
|
-
end
|
175
|
-
end
|
176
|
-
|
177
|
-
context 'Verify' do
|
178
|
-
context 'algorithm' do
|
179
|
-
it 'should raise JWT::IncorrectAlgorithm on missmatch' do
|
180
|
-
token = JWT.encode payload, data[:secret], 'HS512'
|
181
|
-
|
182
|
-
expect do
|
183
|
-
JWT.decode token, data[:secret], true, algorithm: 'HS384'
|
184
|
-
end.to raise_error JWT::IncorrectAlgorithm
|
185
|
-
|
186
|
-
expect do
|
187
|
-
JWT.decode token, data[:secret], true, algorithm: 'HS512'
|
188
|
-
end.not_to raise_error
|
189
|
-
end
|
190
|
-
end
|
191
|
-
|
192
|
-
context 'issuer claim' do
|
193
|
-
let(:iss) { 'ruby-jwt-gem' }
|
194
|
-
let(:invalid_token) { JWT.encode payload, data[:secret] }
|
195
|
-
|
196
|
-
let :token do
|
197
|
-
iss_payload = payload.merge(iss: iss)
|
198
|
-
JWT.encode iss_payload, data[:secret]
|
199
|
-
end
|
200
|
-
|
201
|
-
it 'if verify_iss is set to false (default option) should not raise JWT::InvalidIssuerError' do
|
202
|
-
expect do
|
203
|
-
JWT.decode token, data[:secret], true, iss: iss
|
204
|
-
end.not_to raise_error
|
205
|
-
end
|
206
|
-
end
|
207
|
-
end
|
208
|
-
|
209
|
-
context 'Base64' do
|
210
|
-
it 'urlsafe replace + / with - _' do
|
211
|
-
allow(Base64).to receive(:encode64) { 'string+with/non+url-safe/characters_' }
|
212
|
-
expect(JWT.base64url_encode('foo')).to eq('string-with_non-url-safe_characters_')
|
213
|
-
end
|
214
|
-
end
|
215
|
-
|
216
|
-
describe 'secure comparison' do
|
217
|
-
it 'returns true if strings are equal' do
|
218
|
-
expect(JWT.secure_compare('Foo', 'Foo')).to eq true
|
219
|
-
end
|
220
|
-
|
221
|
-
it 'returns false if either input is nil or empty' do
|
222
|
-
[nil, ''].each do |bad|
|
223
|
-
expect(JWT.secure_compare(bad, 'Foo')).to eq false
|
224
|
-
expect(JWT.secure_compare('Foo', bad)).to eq false
|
225
|
-
end
|
226
|
-
end
|
227
|
-
|
228
|
-
it 'retuns false if the strings are different' do
|
229
|
-
expect(JWT.secure_compare('Foo', 'Bar')).to eq false
|
230
|
-
end
|
231
|
-
end
|
232
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
require 'rspec'
|
2
|
-
require 'simplecov'
|
3
|
-
require 'simplecov-json'
|
4
|
-
require 'codeclimate-test-reporter'
|
5
|
-
|
6
|
-
SimpleCov.configure do
|
7
|
-
root File.join(File.dirname(__FILE__), '..')
|
8
|
-
project_name 'Ruby JWT - Ruby JSON Web Token implementation'
|
9
|
-
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
|
10
|
-
SimpleCov::Formatter::HTMLFormatter,
|
11
|
-
SimpleCov::Formatter::JSONFormatter
|
12
|
-
])
|
13
|
-
|
14
|
-
add_filter 'spec'
|
15
|
-
end
|
16
|
-
|
17
|
-
SimpleCov.start if ENV['COVERAGE']
|
18
|
-
CodeClimate::TestReporter.start if ENV['CODECLIMATE_REPO_TOKEN']
|
19
|
-
|
20
|
-
CERT_PATH = File.join(File.dirname(__FILE__), 'fixtures', 'certs')
|
21
|
-
|
22
|
-
RSpec.configure do |config|
|
23
|
-
config.expect_with :rspec do |c|
|
24
|
-
c.syntax = [:should, :expect]
|
25
|
-
end
|
26
|
-
|
27
|
-
config.run_all_when_everything_filtered = true
|
28
|
-
config.filter_run :focus
|
29
|
-
|
30
|
-
config.order = 'random'
|
31
|
-
end
|