firebase_id_token 2.5.2 → 4.0.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 +4 -4
- data/.github/workflows/test.yml +28 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +71 -1
- data/Gemfile +7 -0
- data/README.md +97 -56
- data/firebase_id_token.gemspec +14 -12
- data/lib/firebase_id_token/certificates/active_support.rb +92 -0
- data/lib/firebase_id_token/certificates/redis.rb +46 -0
- data/lib/firebase_id_token/certificates.rb +69 -60
- data/lib/firebase_id_token/configuration.rb +10 -3
- data/lib/firebase_id_token/exceptions/unsupported_cache_operation_error.rb +11 -0
- data/lib/firebase_id_token/signature.rb +30 -13
- data/lib/firebase_id_token/testing/certificates.rb +1 -1
- data/lib/firebase_id_token/version.rb +1 -1
- data/lib/firebase_id_token.rb +8 -6
- data/spec/firebase_id_token/certificates/active_support_spec.rb +43 -0
- data/spec/firebase_id_token/certificates/redis_spec.rb +39 -0
- data/spec/firebase_id_token/certificates_session_cookie_spec.rb +49 -0
- data/spec/firebase_id_token/configuration_spec.rb +0 -6
- data/spec/firebase_id_token/signature_session_cookie_spec.rb +65 -0
- data/spec/firebase_id_token/signature_spec.rb +15 -1
- data/spec/firebase_id_token/signature_test_spec.rb +4 -0
- data/spec/firebase_id_token_spec.rb +1 -1
- data/spec/spec_helper.rb +4 -0
- data/spec/support/certificates_shared_examples.rb +196 -0
- metadata +81 -60
- data/CODE_OF_CONDUCT.md +0 -74
- data/spec/firebase_id_token/certificates_spec.rb +0 -164
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
module FirebaseIdToken
|
|
4
|
-
describe Certificates do
|
|
5
|
-
let (:redis) { Redis::Namespace.new 'firebase_id_token', redis: Redis.new }
|
|
6
|
-
let (:certs) { File.read('spec/fixtures/files/certificates.json') }
|
|
7
|
-
let (:cache) { 'public, max-age=19302, must-revalidate, no-transform' }
|
|
8
|
-
let (:low_cache) { 'public, max-age=2160, must-revalidate, no-transform' }
|
|
9
|
-
let (:kid) { JSON.parse(certs).first[0] }
|
|
10
|
-
let (:expires_in) { (DateTime.now + (5/24r)).to_s }
|
|
11
|
-
let (:response) { double }
|
|
12
|
-
|
|
13
|
-
let (:mock_response) {
|
|
14
|
-
allow(response).to receive(:code) { 200 }
|
|
15
|
-
allow(response).to receive(:headers) { { 'cache-control' => cache } }
|
|
16
|
-
allow(response).to receive(:body) { certs }
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
let(:mock_request) {
|
|
20
|
-
mock_response
|
|
21
|
-
allow(HTTParty).to receive(:get).
|
|
22
|
-
with(an_instance_of(String)) { response }
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
before :each do
|
|
26
|
-
redis.del 'certificates'
|
|
27
|
-
mock_request
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
describe '#request' do
|
|
31
|
-
it 'requests certificates when Redis database is empty' do
|
|
32
|
-
expect(HTTParty).to receive(:get).
|
|
33
|
-
with(FirebaseIdToken::Certificates::URL)
|
|
34
|
-
described_class.request
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
it 'does not requests certificates when Redis database is written' do
|
|
38
|
-
expect(HTTParty).to receive(:get).
|
|
39
|
-
with(FirebaseIdToken::Certificates::URL).once
|
|
40
|
-
2.times { described_class.request }
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
describe '#request!' do
|
|
45
|
-
it 'always requests certificates' do
|
|
46
|
-
expect(HTTParty).to receive(:get).
|
|
47
|
-
with(FirebaseIdToken::Certificates::URL).twice
|
|
48
|
-
2.times { described_class.request! }
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
it 'sets the certificate expiration time as Redis TTL' do
|
|
52
|
-
described_class.request!
|
|
53
|
-
expect(redis.ttl('certificates')).to be > 3600
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
it 'raises a error when certificates expires in less than 1 hour' do
|
|
57
|
-
allow(response).to receive(:headers) {{'cache-control' => low_cache}}
|
|
58
|
-
expect{ described_class.request! }.
|
|
59
|
-
to raise_error(Exceptions::CertificatesTtlError)
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
it 'raises a error when HTTP response code is other than 200' do
|
|
63
|
-
allow(response).to receive(:code) { 401 }
|
|
64
|
-
expect{ described_class.request! }.
|
|
65
|
-
to raise_error(Exceptions::CertificatesRequestError)
|
|
66
|
-
end
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
describe '#request_anyway' do
|
|
70
|
-
it 'also requests certificates' do
|
|
71
|
-
expect(HTTParty).to receive(:get).
|
|
72
|
-
with(FirebaseIdToken::Certificates::URL)
|
|
73
|
-
|
|
74
|
-
described_class.request_anyway
|
|
75
|
-
end
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
describe '.present?' do
|
|
79
|
-
it 'returns false when Redis database is empty' do
|
|
80
|
-
expect(described_class.present?).to be(false)
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
it 'returns true when Redis database is written' do
|
|
84
|
-
described_class.request
|
|
85
|
-
expect(described_class.present?).to be(true)
|
|
86
|
-
end
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
describe '.all' do
|
|
90
|
-
context 'before requesting certificates' do
|
|
91
|
-
it 'returns a empty Array' do
|
|
92
|
-
expect(described_class.all).to eq([])
|
|
93
|
-
end
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
context 'after requesting certificates' do
|
|
97
|
-
it 'returns a array of hashes: String keys' do
|
|
98
|
-
described_class.request
|
|
99
|
-
expect(described_class.all.first.keys[0]).to be_a(String)
|
|
100
|
-
end
|
|
101
|
-
|
|
102
|
-
it 'returns a array of hashes: OpenSSL::X509::Certificate values' do
|
|
103
|
-
described_class.request
|
|
104
|
-
expect(described_class.all.first.values[0]).
|
|
105
|
-
to be_a(OpenSSL::X509::Certificate)
|
|
106
|
-
end
|
|
107
|
-
end
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
describe '.find' do
|
|
111
|
-
context 'without certificates in Redis database' do
|
|
112
|
-
it 'raises a exception' do
|
|
113
|
-
expect{ described_class.find(kid)}.
|
|
114
|
-
to raise_error(Exceptions::NoCertificatesError)
|
|
115
|
-
end
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
context 'with certificates in Redis database' do
|
|
119
|
-
it 'returns a OpenSSL::X509::Certificate when it finds the kid' do
|
|
120
|
-
described_class.request
|
|
121
|
-
expect(described_class.find(kid)).to be_a(OpenSSL::X509::Certificate)
|
|
122
|
-
end
|
|
123
|
-
|
|
124
|
-
it 'returns nil when it can not find the kid' do
|
|
125
|
-
described_class.request
|
|
126
|
-
expect(described_class.find('')).to be(nil)
|
|
127
|
-
end
|
|
128
|
-
end
|
|
129
|
-
end
|
|
130
|
-
|
|
131
|
-
describe '.find!' do
|
|
132
|
-
context 'without certificates in Redis database' do
|
|
133
|
-
it 'raises a exception' do
|
|
134
|
-
expect{ described_class.find!(kid)}.
|
|
135
|
-
to raise_error(Exceptions::NoCertificatesError)
|
|
136
|
-
end
|
|
137
|
-
end
|
|
138
|
-
context 'with certificates in Redis database' do
|
|
139
|
-
it 'returns a OpenSSL::X509::Certificate when it finds the kid' do
|
|
140
|
-
described_class.request
|
|
141
|
-
expect(described_class.find!(kid)).to be_a(OpenSSL::X509::Certificate)
|
|
142
|
-
end
|
|
143
|
-
|
|
144
|
-
it 'raises a CertificateNotFound error when it can not find the kid' do
|
|
145
|
-
described_class.request
|
|
146
|
-
expect { described_class.find!('') }
|
|
147
|
-
.to raise_error(Exceptions::CertificateNotFound, /Unable to find/)
|
|
148
|
-
end
|
|
149
|
-
end
|
|
150
|
-
|
|
151
|
-
end
|
|
152
|
-
|
|
153
|
-
describe '.ttl' do
|
|
154
|
-
it 'returns a positive number when has certificates in Redis' do
|
|
155
|
-
described_class.request
|
|
156
|
-
expect(described_class.ttl).to be > 0
|
|
157
|
-
end
|
|
158
|
-
|
|
159
|
-
it 'returns zero when has no certificates in Redis' do
|
|
160
|
-
expect(described_class.ttl).to eq(0)
|
|
161
|
-
end
|
|
162
|
-
end
|
|
163
|
-
end
|
|
164
|
-
end
|