firebase_id_token 3.0.0 → 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 +1 -7
- data/CHANGELOG.md +36 -1
- data/Gemfile +7 -0
- data/README.md +57 -28
- data/firebase_id_token.gemspec +4 -4
- 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 -2
- 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 +6 -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/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 +1 -0
- data/spec/support/certificates_shared_examples.rb +196 -0
- metadata +24 -15
- data/CODE_OF_CONDUCT.md +0 -74
- data/spec/firebase_id_token/certificates_spec.rb +0 -164
|
@@ -8,7 +8,8 @@ module FirebaseIdToken
|
|
|
8
8
|
let(:mock_certificates) do
|
|
9
9
|
allow(Certificates)
|
|
10
10
|
.to(receive(:find))
|
|
11
|
-
.with(an_instance_of(String), raise_error: raise_certificates_error
|
|
11
|
+
.with(an_instance_of(String), raise_error: raise_certificates_error,
|
|
12
|
+
source: :id_token)
|
|
12
13
|
.and_return(OpenSSL::X509::Certificate.new(jwt['certificate']))
|
|
13
14
|
end
|
|
14
15
|
|
|
@@ -31,6 +32,19 @@ module FirebaseIdToken
|
|
|
31
32
|
it 'returns nil with a invalid key format' do
|
|
32
33
|
expect(described_class.verify('aaa')).to be(nil)
|
|
33
34
|
end
|
|
35
|
+
|
|
36
|
+
it 'returns nil when the token has no sub claim' do
|
|
37
|
+
payload = {
|
|
38
|
+
'iss' => 'https://securetoken.google.com/firebase-id-token',
|
|
39
|
+
'aud' => 'firebase-id-token',
|
|
40
|
+
'exp' => Time.now.to_i + 3600,
|
|
41
|
+
'iat' => Time.now.to_i - 60
|
|
42
|
+
}
|
|
43
|
+
token = JWT.encode(payload,
|
|
44
|
+
OpenSSL::PKey::RSA.new(jwt['private_key']), 'RS256', kid: 'test')
|
|
45
|
+
|
|
46
|
+
expect(described_class.verify(token)).to be(nil)
|
|
47
|
+
end
|
|
34
48
|
end
|
|
35
49
|
|
|
36
50
|
describe '#verify!' do
|
data/spec/spec_helper.rb
CHANGED
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
# The ActiveSupport store lazily downloads certificates whenever it is
|
|
4
|
+
# instantiated over an empty cache, while the legacy Redis store only
|
|
5
|
+
# downloads them upon an explicit request. Examples whose outcome depends on
|
|
6
|
+
# the empty-cache behavior branch on the `lazy` option.
|
|
7
|
+
shared_examples_for 'a certificate store' do |opts = {}|
|
|
8
|
+
lazy = opts.fetch(:lazy, false)
|
|
9
|
+
|
|
10
|
+
before :each do
|
|
11
|
+
mock_request
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe '#request' do
|
|
15
|
+
it 'requests certificates when the cache is empty' do
|
|
16
|
+
expect(HTTParty).to receive(:get).
|
|
17
|
+
with(FirebaseIdToken::Certificates::URL)
|
|
18
|
+
described_class.request
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it 'does not requests certificates when the cache is written' do
|
|
22
|
+
expect(HTTParty).to receive(:get).
|
|
23
|
+
with(FirebaseIdToken::Certificates::URL).once
|
|
24
|
+
2.times { described_class.request }
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
describe '#request!' do
|
|
29
|
+
if lazy
|
|
30
|
+
it 'always requests certificates' do
|
|
31
|
+
# The store lazily loads the empty cache once before the first
|
|
32
|
+
# explicit request, hence the extra call.
|
|
33
|
+
expect(HTTParty).to receive(:get).
|
|
34
|
+
with(FirebaseIdToken::Certificates::URL).exactly(3).times
|
|
35
|
+
2.times { described_class.request! }
|
|
36
|
+
end
|
|
37
|
+
else
|
|
38
|
+
it 'always requests certificates' do
|
|
39
|
+
expect(HTTParty).to receive(:get).
|
|
40
|
+
with(FirebaseIdToken::Certificates::URL).twice
|
|
41
|
+
2.times { described_class.request! }
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it 'sets the certificate expiration time as the cache TTL' do
|
|
46
|
+
described_class.request!
|
|
47
|
+
expect(described_class.ttl).to be > 3600
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
it 'raises a error when certificates expires in less than 1 hour' do
|
|
51
|
+
allow(response).to receive(:headers) {{'cache-control' => low_cache}}
|
|
52
|
+
expect{ described_class.request! }.
|
|
53
|
+
to raise_error(FirebaseIdToken::Exceptions::CertificatesTtlError)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
it 'raises a error when HTTP response code is other than 200' do
|
|
57
|
+
allow(response).to receive(:code) { 401 }
|
|
58
|
+
expect{ described_class.request! }.
|
|
59
|
+
to raise_error(FirebaseIdToken::Exceptions::CertificatesRequestError)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
describe '#request_anyway' do
|
|
64
|
+
if lazy
|
|
65
|
+
it 'also requests certificates' do
|
|
66
|
+
expect(HTTParty).to receive(:get).
|
|
67
|
+
with(FirebaseIdToken::Certificates::URL).twice
|
|
68
|
+
described_class.request_anyway
|
|
69
|
+
end
|
|
70
|
+
else
|
|
71
|
+
it 'also requests certificates' do
|
|
72
|
+
expect(HTTParty).to receive(:get).
|
|
73
|
+
with(FirebaseIdToken::Certificates::URL)
|
|
74
|
+
described_class.request_anyway
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
describe '.present?' do
|
|
80
|
+
if lazy
|
|
81
|
+
it 'returns true when the cache is empty, lazily fetching' do
|
|
82
|
+
expect(described_class.present?).to be(true)
|
|
83
|
+
end
|
|
84
|
+
else
|
|
85
|
+
it 'returns false when the cache is empty' do
|
|
86
|
+
expect(described_class.present?).to be(false)
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
it 'returns true when the cache is written' do
|
|
91
|
+
described_class.request
|
|
92
|
+
expect(described_class.present?).to be(true)
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
describe '.all' do
|
|
97
|
+
context 'before requesting certificates' do
|
|
98
|
+
if lazy
|
|
99
|
+
it 'lazily fetches and returns the certificates' do
|
|
100
|
+
expect(described_class.all.first.values[0]).
|
|
101
|
+
to be_a(OpenSSL::X509::Certificate)
|
|
102
|
+
end
|
|
103
|
+
else
|
|
104
|
+
it 'returns a empty Array' do
|
|
105
|
+
expect(described_class.all).to eq([])
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
context 'after requesting certificates' do
|
|
111
|
+
it 'returns a array of hashes: String keys' do
|
|
112
|
+
described_class.request
|
|
113
|
+
expect(described_class.all.first.keys[0]).to be_a(String)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
it 'returns a array of hashes: OpenSSL::X509::Certificate values' do
|
|
117
|
+
described_class.request
|
|
118
|
+
expect(described_class.all.first.values[0]).
|
|
119
|
+
to be_a(OpenSSL::X509::Certificate)
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
describe '.find' do
|
|
125
|
+
context 'without certificates in the cache' do
|
|
126
|
+
if lazy
|
|
127
|
+
it 'lazily fetches certificates and finds the kid' do
|
|
128
|
+
expect(described_class.find(kid)).to be_a(OpenSSL::X509::Certificate)
|
|
129
|
+
end
|
|
130
|
+
else
|
|
131
|
+
it 'raises a exception' do
|
|
132
|
+
expect{ described_class.find(kid)}.
|
|
133
|
+
to raise_error(FirebaseIdToken::Exceptions::NoCertificatesError)
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
context 'with certificates in the cache' 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 'returns nil when it can not find the kid' do
|
|
145
|
+
described_class.request
|
|
146
|
+
expect(described_class.find('')).to be(nil)
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
describe '.find!' do
|
|
152
|
+
context 'without certificates in the cache' do
|
|
153
|
+
if lazy
|
|
154
|
+
it 'lazily fetches certificates and finds the kid' do
|
|
155
|
+
expect(described_class.find!(kid)).to be_a(OpenSSL::X509::Certificate)
|
|
156
|
+
end
|
|
157
|
+
else
|
|
158
|
+
it 'raises a exception' do
|
|
159
|
+
expect{ described_class.find!(kid)}.
|
|
160
|
+
to raise_error(FirebaseIdToken::Exceptions::NoCertificatesError)
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
context 'with certificates in the cache' do
|
|
166
|
+
it 'returns a OpenSSL::X509::Certificate when it finds the kid' do
|
|
167
|
+
described_class.request
|
|
168
|
+
expect(described_class.find!(kid)).to be_a(OpenSSL::X509::Certificate)
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
it 'raises a CertificateNotFound error when it can not find the kid' do
|
|
172
|
+
described_class.request
|
|
173
|
+
expect { described_class.find!('') }
|
|
174
|
+
.to raise_error(FirebaseIdToken::Exceptions::CertificateNotFound, /Unable to find/)
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
describe '.ttl' do
|
|
181
|
+
it 'returns a positive number when has certificates in the cache' do
|
|
182
|
+
described_class.request
|
|
183
|
+
expect(described_class.ttl).to be > 0
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
if lazy
|
|
187
|
+
it 'returns a positive number when the cache is empty, lazily fetching' do
|
|
188
|
+
expect(described_class.ttl).to be > 0
|
|
189
|
+
end
|
|
190
|
+
else
|
|
191
|
+
it 'returns zero when has no certificates in the cache' do
|
|
192
|
+
expect(described_class.ttl).to eq(0)
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: firebase_id_token
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 4.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Fernando Schuindt
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: exe
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: bundler
|
|
@@ -130,7 +129,7 @@ dependencies:
|
|
|
130
129
|
- - ">="
|
|
131
130
|
- !ruby/object:Gem::Version
|
|
132
131
|
version: 5.0.6
|
|
133
|
-
type: :
|
|
132
|
+
type: :development
|
|
134
133
|
prerelease: false
|
|
135
134
|
version_requirements: !ruby/object:Gem::Requirement
|
|
136
135
|
requirements:
|
|
@@ -147,7 +146,7 @@ dependencies:
|
|
|
147
146
|
- - "~>"
|
|
148
147
|
- !ruby/object:Gem::Version
|
|
149
148
|
version: '1.10'
|
|
150
|
-
type: :
|
|
149
|
+
type: :development
|
|
151
150
|
prerelease: false
|
|
152
151
|
version_requirements: !ruby/object:Gem::Requirement
|
|
153
152
|
requirements:
|
|
@@ -158,16 +157,22 @@ dependencies:
|
|
|
158
157
|
name: httparty
|
|
159
158
|
requirement: !ruby/object:Gem::Requirement
|
|
160
159
|
requirements:
|
|
161
|
-
- - "
|
|
160
|
+
- - ">="
|
|
161
|
+
- !ruby/object:Gem::Version
|
|
162
|
+
version: '0.21'
|
|
163
|
+
- - "<"
|
|
162
164
|
- !ruby/object:Gem::Version
|
|
163
|
-
version:
|
|
165
|
+
version: '1.0'
|
|
164
166
|
type: :runtime
|
|
165
167
|
prerelease: false
|
|
166
168
|
version_requirements: !ruby/object:Gem::Requirement
|
|
167
169
|
requirements:
|
|
168
|
-
- - "
|
|
170
|
+
- - ">="
|
|
171
|
+
- !ruby/object:Gem::Version
|
|
172
|
+
version: '0.21'
|
|
173
|
+
- - "<"
|
|
169
174
|
- !ruby/object:Gem::Version
|
|
170
|
-
version:
|
|
175
|
+
version: '1.0'
|
|
171
176
|
- !ruby/object:Gem::Dependency
|
|
172
177
|
name: jwt
|
|
173
178
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -222,7 +227,7 @@ dependencies:
|
|
|
222
227
|
- - ">="
|
|
223
228
|
- !ruby/object:Gem::Version
|
|
224
229
|
version: 2.6.3
|
|
225
|
-
description: A Ruby gem to verify the signature of Firebase ID Tokens. It uses
|
|
230
|
+
description: A Ruby gem to verify the signature of Firebase ID Tokens. It uses ActiveSupport::Cache
|
|
226
231
|
to store Google's x509 certificates and manage their expiration time, so you don't
|
|
227
232
|
need to request Google's API in every execution and can access it as fast as reading
|
|
228
233
|
from memory.
|
|
@@ -239,7 +244,6 @@ files:
|
|
|
239
244
|
- ".travis.yml"
|
|
240
245
|
- ".yardopts"
|
|
241
246
|
- CHANGELOG.md
|
|
242
|
-
- CODE_OF_CONDUCT.md
|
|
243
247
|
- Gemfile
|
|
244
248
|
- LICENSE.txt
|
|
245
249
|
- README.md
|
|
@@ -250,16 +254,22 @@ files:
|
|
|
250
254
|
- firebase_id_token.gemspec
|
|
251
255
|
- lib/firebase_id_token.rb
|
|
252
256
|
- lib/firebase_id_token/certificates.rb
|
|
257
|
+
- lib/firebase_id_token/certificates/active_support.rb
|
|
258
|
+
- lib/firebase_id_token/certificates/redis.rb
|
|
253
259
|
- lib/firebase_id_token/configuration.rb
|
|
254
260
|
- lib/firebase_id_token/exceptions/certificate_not_found.rb
|
|
255
261
|
- lib/firebase_id_token/exceptions/certificates_request_error.rb
|
|
256
262
|
- lib/firebase_id_token/exceptions/certificates_ttl_error.rb
|
|
257
263
|
- lib/firebase_id_token/exceptions/no_certificates_error.rb
|
|
264
|
+
- lib/firebase_id_token/exceptions/unsupported_cache_operation_error.rb
|
|
258
265
|
- lib/firebase_id_token/signature.rb
|
|
259
266
|
- lib/firebase_id_token/testing/certificates.rb
|
|
260
267
|
- lib/firebase_id_token/version.rb
|
|
261
|
-
- spec/firebase_id_token/
|
|
268
|
+
- spec/firebase_id_token/certificates/active_support_spec.rb
|
|
269
|
+
- spec/firebase_id_token/certificates/redis_spec.rb
|
|
270
|
+
- spec/firebase_id_token/certificates_session_cookie_spec.rb
|
|
262
271
|
- spec/firebase_id_token/configuration_spec.rb
|
|
272
|
+
- spec/firebase_id_token/signature_session_cookie_spec.rb
|
|
263
273
|
- spec/firebase_id_token/signature_spec.rb
|
|
264
274
|
- spec/firebase_id_token/signature_test_spec.rb
|
|
265
275
|
- spec/firebase_id_token_spec.rb
|
|
@@ -268,11 +278,11 @@ files:
|
|
|
268
278
|
- spec/fixtures/files/jwt.json
|
|
269
279
|
- spec/fixtures/files/payload.json
|
|
270
280
|
- spec/spec_helper.rb
|
|
281
|
+
- spec/support/certificates_shared_examples.rb
|
|
271
282
|
homepage: https://github.com/fschuindt/firebase_id_token
|
|
272
283
|
licenses:
|
|
273
284
|
- MIT
|
|
274
285
|
metadata: {}
|
|
275
|
-
post_install_message:
|
|
276
286
|
rdoc_options: []
|
|
277
287
|
require_paths:
|
|
278
288
|
- lib
|
|
@@ -287,8 +297,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
287
297
|
- !ruby/object:Gem::Version
|
|
288
298
|
version: '0'
|
|
289
299
|
requirements: []
|
|
290
|
-
rubygems_version:
|
|
291
|
-
signing_key:
|
|
300
|
+
rubygems_version: 4.0.3
|
|
292
301
|
specification_version: 4
|
|
293
302
|
summary: A Firebase ID Token verifier.
|
|
294
303
|
test_files: []
|
data/CODE_OF_CONDUCT.md
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
# Contributor Covenant Code of Conduct
|
|
2
|
-
|
|
3
|
-
## Our Pledge
|
|
4
|
-
|
|
5
|
-
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
-
contributors and maintainers pledge to making participation in our project and
|
|
7
|
-
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
-
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
-
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
-
orientation.
|
|
11
|
-
|
|
12
|
-
## Our Standards
|
|
13
|
-
|
|
14
|
-
Examples of behavior that contributes to creating a positive environment
|
|
15
|
-
include:
|
|
16
|
-
|
|
17
|
-
* Using welcoming and inclusive language
|
|
18
|
-
* Being respectful of differing viewpoints and experiences
|
|
19
|
-
* Gracefully accepting constructive criticism
|
|
20
|
-
* Focusing on what is best for the community
|
|
21
|
-
* Showing empathy towards other community members
|
|
22
|
-
|
|
23
|
-
Examples of unacceptable behavior by participants include:
|
|
24
|
-
|
|
25
|
-
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
-
advances
|
|
27
|
-
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
-
* Public or private harassment
|
|
29
|
-
* Publishing others' private information, such as a physical or electronic
|
|
30
|
-
address, without explicit permission
|
|
31
|
-
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
-
professional setting
|
|
33
|
-
|
|
34
|
-
## Our Responsibilities
|
|
35
|
-
|
|
36
|
-
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
-
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
-
response to any instances of unacceptable behavior.
|
|
39
|
-
|
|
40
|
-
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
-
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
-
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
-
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
-
threatening, offensive, or harmful.
|
|
45
|
-
|
|
46
|
-
## Scope
|
|
47
|
-
|
|
48
|
-
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
-
when an individual is representing the project or its community. Examples of
|
|
50
|
-
representing a project or community include using an official project e-mail
|
|
51
|
-
address, posting via an official social media account, or acting as an appointed
|
|
52
|
-
representative at an online or offline event. Representation of a project may be
|
|
53
|
-
further defined and clarified by project maintainers.
|
|
54
|
-
|
|
55
|
-
## Enforcement
|
|
56
|
-
|
|
57
|
-
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
-
reported by contacting the project team at f.schuindtcs@gmail.com. All
|
|
59
|
-
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
-
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
-
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
-
Further details of specific enforcement policies may be posted separately.
|
|
63
|
-
|
|
64
|
-
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
-
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
-
members of the project's leadership.
|
|
67
|
-
|
|
68
|
-
## Attribution
|
|
69
|
-
|
|
70
|
-
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
-
available at [http://contributor-covenant.org/version/1/4][version]
|
|
72
|
-
|
|
73
|
-
[homepage]: http://contributor-covenant.org
|
|
74
|
-
[version]: http://contributor-covenant.org/version/1/4/
|
|
@@ -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
|