2019-08-22 06:57:44 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-14 19:23:31 -04:00
|
|
|
describe JSONWebToken::RSAToken do
|
2016-05-15 09:52:26 -04:00
|
|
|
let(:rsa_key) do
|
|
|
|
OpenSSL::PKey::RSA.new <<-eos.strip_heredoc
|
|
|
|
-----BEGIN RSA PRIVATE KEY-----
|
|
|
|
MIIBOgIBAAJBAMA5sXIBE0HwgIB40iNidN4PGWzOyLQK0bsdOBNgpEXkDlZBvnak
|
|
|
|
OUgAPF+rME4PB0Yl415DabUI40T5UNmlwxcCAwEAAQJAZtY2pSwIFm3JAXIh0cZZ
|
|
|
|
iXcAfiJ+YzuqinUOS+eW2sBCAEzjcARlU/o6sFQgtsOi4FOMczAd1Yx8UDMXMmrw
|
|
|
|
2QIhAPBgVhJiTF09pdmeFWutCvTJDlFFAQNbrbo2X2x/9WF9AiEAzLgqMKeStSRu
|
|
|
|
H9N16TuDrUoO8R+DPqriCwkKrSHaWyMCIFzMhE4inuKcSywBaLmiG4m3GQzs++Al
|
|
|
|
A6PRG/PSTpQtAiBxtBg6zdf+JC3GH3zt/dA0/10tL4OF2wORfYQghRzyYQIhAL2l
|
|
|
|
0ZQW+yLIZAGrdBFWYEAa52GZosncmzBNlsoTgwE4
|
|
|
|
-----END RSA PRIVATE KEY-----
|
|
|
|
eos
|
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
let(:rsa_token) { described_class.new(nil) }
|
|
|
|
let(:rsa_encoded) { rsa_token.encoded }
|
|
|
|
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
allow_any_instance_of(described_class).to receive(:key).and_return(rsa_key)
|
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
|
|
|
|
context 'token' do
|
|
|
|
context 'for valid key to be validated' do
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
rsa_token['key'] = 'value'
|
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
|
2018-12-29 09:20:04 -05:00
|
|
|
subject { JWT.decode(rsa_encoded, rsa_key, true, { algorithm: 'RS256' }) }
|
2016-05-09 15:06:32 -04:00
|
|
|
|
2017-08-09 05:52:22 -04:00
|
|
|
it { expect {subject}.not_to raise_error }
|
2016-05-09 15:06:32 -04:00
|
|
|
it { expect(subject.first).to include('key' => 'value') }
|
2016-05-15 09:52:26 -04:00
|
|
|
it do
|
|
|
|
expect(subject.second).to eq(
|
|
|
|
"typ" => "JWT",
|
|
|
|
"alg" => "RS256",
|
|
|
|
"kid" => "OGXY:4TR7:FAVO:WEM2:XXEW:E4FP:TKL7:7ACK:TZAF:D54P:SUIA:P3B2")
|
|
|
|
end
|
2016-05-09 15:06:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for invalid key to raise an exception' do
|
2016-05-15 09:52:26 -04:00
|
|
|
let(:new_key) { OpenSSL::PKey::RSA.generate(512) }
|
2018-12-29 09:20:04 -05:00
|
|
|
subject { JWT.decode(rsa_encoded, new_key, true, { algorithm: 'RS256' }) }
|
2016-05-09 15:06:32 -04:00
|
|
|
|
2017-08-09 05:52:22 -04:00
|
|
|
it { expect {subject}.to raise_error(JWT::DecodeError) }
|
2016-05-09 15:06:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|