|
163 | 163 | end |
164 | 164 |
|
165 | 165 | %w[ES256 ES384 ES512 ES256K].each do |alg| |
| 166 | + before do |
| 167 | + skip 'OpenSSL gem missing RSA-PSS support' unless OpenSSL::PKey::RSA.method_defined?(:sign_pss) |
| 168 | + end |
| 169 | + |
166 | 170 | context "alg: #{alg}" do |
167 | 171 | before(:each) do |
168 | 172 | data[alg] = JWT.encode(payload, data["#{alg}_private"], alg) |
|
198 | 202 | end |
199 | 203 | end |
200 | 204 |
|
201 | | - if Gem::Version.new(OpenSSL::VERSION) >= Gem::Version.new('2.1') |
202 | | - %w[PS256 PS384 PS512].each do |alg| |
203 | | - context "alg: #{alg}" do |
204 | | - before(:each) do |
205 | | - data[alg] = JWT.encode payload, data[:rsa_private], alg |
206 | | - end |
| 205 | + %w[PS256 PS384 PS512].each do |alg| |
| 206 | + context "alg: #{alg}" do |
| 207 | + before(:each) do |
| 208 | + data[alg] = JWT.encode payload, data[:rsa_private], alg |
| 209 | + end |
207 | 210 |
|
208 | | - let(:wrong_key) { data[:wrong_rsa_public] } |
| 211 | + let(:wrong_key) { data[:wrong_rsa_public] } |
209 | 212 |
|
210 | | - it 'should generate a valid token' do |
211 | | - token = data[alg] |
| 213 | + it 'should generate a valid token' do |
| 214 | + token = data[alg] |
212 | 215 |
|
213 | | - header, body, signature = token.split('.') |
| 216 | + header, body, signature = token.split('.') |
214 | 217 |
|
215 | | - expect(header).to eql(Base64.strict_encode64({ alg: alg }.to_json)) |
216 | | - expect(body).to eql(Base64.strict_encode64(payload.to_json)) |
| 218 | + expect(header).to eql(Base64.strict_encode64({ alg: alg }.to_json)) |
| 219 | + expect(body).to eql(Base64.strict_encode64(payload.to_json)) |
217 | 220 |
|
218 | | - # Validate signature is made of up header and body of JWT |
219 | | - translated_alg = alg.gsub('PS', 'sha') |
220 | | - valid_signature = data[:rsa_public].verify_pss( |
221 | | - translated_alg, |
222 | | - JWT::Base64.url_decode(signature), |
223 | | - [header, body].join('.'), |
224 | | - salt_length: :auto, |
225 | | - mgf1_hash: translated_alg |
226 | | - ) |
227 | | - expect(valid_signature).to be true |
228 | | - end |
229 | | - |
230 | | - it 'should decode a valid token' do |
231 | | - jwt_payload, header = JWT.decode data[alg], data[:rsa_public], true, algorithm: alg |
| 221 | + # Validate signature is made of up header and body of JWT |
| 222 | + translated_alg = alg.gsub('PS', 'sha') |
| 223 | + valid_signature = data[:rsa_public].verify_pss( |
| 224 | + translated_alg, |
| 225 | + JWT::Base64.url_decode(signature), |
| 226 | + [header, body].join('.'), |
| 227 | + salt_length: :auto, |
| 228 | + mgf1_hash: translated_alg |
| 229 | + ) |
| 230 | + expect(valid_signature).to be true |
| 231 | + end |
232 | 232 |
|
233 | | - expect(header['alg']).to eq alg |
234 | | - expect(jwt_payload).to eq payload |
235 | | - end |
| 233 | + it 'should decode a valid token' do |
| 234 | + jwt_payload, header = JWT.decode data[alg], data[:rsa_public], true, algorithm: alg |
236 | 235 |
|
237 | | - it 'wrong key should raise JWT::DecodeError' do |
238 | | - expect do |
239 | | - JWT.decode data[alg], wrong_key |
240 | | - end.to raise_error JWT::DecodeError |
241 | | - end |
| 236 | + expect(header['alg']).to eq alg |
| 237 | + expect(jwt_payload).to eq payload |
| 238 | + end |
242 | 239 |
|
243 | | - it 'wrong key and verify = false should not raise JWT::DecodeError' do |
244 | | - expect do |
245 | | - JWT.decode data[alg], wrong_key, false |
246 | | - end.not_to raise_error |
247 | | - end |
| 240 | + it 'wrong key should raise JWT::DecodeError' do |
| 241 | + expect do |
| 242 | + JWT.decode data[alg], wrong_key |
| 243 | + end.to raise_error JWT::DecodeError |
248 | 244 | end |
249 | | - end |
250 | | - else |
251 | | - %w[PS256 PS384 PS512].each do |alg| |
252 | | - context "alg: #{alg}" do |
253 | | - it 'raises error about OpenSSL version' do |
254 | | - expect { JWT.encode payload, data[:rsa_private], alg }.to raise_error( |
255 | | - JWT::RequiredDependencyError, |
256 | | - /You currently have OpenSSL .*. PS support requires >= 2.1/ |
257 | | - ) |
258 | | - end |
| 245 | + |
| 246 | + it 'wrong key and verify = false should not raise JWT::DecodeError' do |
| 247 | + expect do |
| 248 | + JWT.decode data[alg], wrong_key, false |
| 249 | + end.not_to raise_error |
259 | 250 | end |
260 | 251 | end |
261 | 252 | end |
|
766 | 757 | let(:token) { JWT.encode(payload, 'secret', 'HS256') } |
767 | 758 |
|
768 | 759 | it 'starts trying with the algorithm referred in the header' do |
769 | | - expect(JWT::JWA::Rsa).not_to receive(:verify) |
| 760 | + allow(JWT::JWA::Rsa).to receive(:verify) |
770 | 761 | JWT.decode(token, 'secret', true, algorithm: %w[RS512 HS256]) |
| 762 | + expect(JWT::JWA::Rsa).not_to have_received(:verify) |
771 | 763 | end |
772 | 764 | end |
773 | 765 |
|
|
0 commit comments