forked from jwt/ruby-jwt
/
rsa_spec.rb
105 lines (90 loc) · 3.43 KB
/
rsa_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
# frozen_string_literal: true
require_relative '../spec_helper'
require 'jwt'
describe JWT::JWK::RSA do
let(:rsa_key) { OpenSSL::PKey::RSA.new(2048) }
describe '.new' do
subject { described_class.new(keypair) }
context 'when a keypair with both keys given' do
let(:keypair) { rsa_key }
it 'creates an instance of the class' do
expect(subject).to be_a described_class
expect(subject.private?).to eq true
end
end
context 'when a keypair with only public key is given' do
let(:keypair) { rsa_key.public_key }
it 'creates an instance of the class' do
expect(subject).to be_a described_class
expect(subject.private?).to eq false
end
end
end
describe '#export' do
subject { described_class.new(keypair).export }
context 'when keypair with private key is exported' do
let(:keypair) { rsa_key }
it 'returns a hash with the public parts of the key' do
expect(subject).to be_a Hash
expect(subject).to include(:kty, :n, :e, :kid)
expect(subject).not_to include(:d, :p, :dp, :dq, :qi)
end
end
context 'when keypair with public key is exported' do
let(:keypair) { rsa_key.public_key }
it 'returns a hash with the public parts of the key' do
expect(subject).to be_a Hash
expect(subject).to include(:kty, :n, :e, :kid)
expect(subject).not_to include(:d, :p, :dp, :dq, :qi)
end
end
context 'when unsupported keypair is given' do
let(:keypair) { 'key' }
it 'raises an error' do
expect { subject }.to raise_error(ArgumentError, 'keypair must be of type OpenSSL::PKey::RSA')
end
end
context 'when private key is requested' do
subject { described_class.new(keypair).export(include_private: true) }
let(:keypair) { rsa_key }
it 'returns a hash with the public AND private parts of the key' do
expect(subject).to be_a Hash
expect(subject).to include(:kty, :n, :e, :kid, :d, :p, :q, :dp, :dq, :qi)
end
end
end
describe '.import' do
subject { described_class.import(params) }
let(:exported_key) { described_class.new(rsa_key).export }
context 'when keypair is imported with symbol keys' do
let(:params) { {e: exported_key[:e], n: exported_key[:n]} }
it 'returns a hash with the public parts of the key' do
expect(subject).to be_a described_class
expect(subject.private?).to eq false
expect(subject.export).to eq(exported_key)
end
end
context 'when keypair is imported with string keys from JSON' do
let(:params) { {'e' => exported_key[:e], 'n' => exported_key[:n]} }
it 'returns a hash with the public parts of the key' do
expect(subject).to be_a described_class
expect(subject.private?).to eq false
expect(subject.export).to eq(exported_key)
end
end
context 'when private key is included in the data' do
let(:exported_key) { described_class.new(rsa_key).export(include_private: true) }
let(:params) { exported_key }
it 'creates a complete keypair' do
expect(subject).to be_a described_class
expect(subject.private?).to eq true
end
end
context 'when jwk_data is given without e and/or n' do
let(:params) { { kty: "RSA" } }
it 'raises an error' do
expect { subject }.to raise_error(JWT::JWKError, "Key format is invalid for RSA")
end
end
end
end