descope 1.1.0 → 1.3.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/ci.yaml +4 -4
- data/.github/workflows/publish-gem.yaml +2 -2
- data/.github/workflows/release-please.yaml +1 -1
- data/.release-please-manifest.json +1 -1
- data/.rubocop.yml +1 -1
- data/.rubocop_todo.yml +1 -1
- data/.ruby-version +1 -1
- data/CHANGELOG.md +17 -0
- data/Gemfile +5 -5
- data/Gemfile.lock +33 -35
- data/README.md +233 -1
- data/lib/descope/api/v1/auth/enchantedlink.rb +76 -9
- data/lib/descope/api/v1/auth.rb +68 -11
- data/lib/descope/api/v1/management/access_key.rb +8 -7
- data/lib/descope/api/v1/management/analytics.rb +62 -0
- data/lib/descope/api/v1/management/audit.rb +2 -2
- data/lib/descope/api/v1/management/authz.rb +15 -15
- data/lib/descope/api/v1/management/common.rb +76 -0
- data/lib/descope/api/v1/management/descoper.rb +45 -0
- data/lib/descope/api/v1/management/engine.rb +44 -0
- data/lib/descope/api/v1/management/fga.rb +64 -0
- data/lib/descope/api/v1/management/flow.rb +5 -5
- data/lib/descope/api/v1/management/group.rb +42 -0
- data/lib/descope/api/v1/management/jwt_template.rb +49 -0
- data/lib/descope/api/v1/management/lists.rb +96 -0
- data/lib/descope/api/v1/management/management_key.rb +54 -0
- data/lib/descope/api/v1/management/outbound_app.rb +112 -0
- data/lib/descope/api/v1/management/password.rb +2 -2
- data/lib/descope/api/v1/management/permission.rb +4 -4
- data/lib/descope/api/v1/management/project.rb +5 -5
- data/lib/descope/api/v1/management/role.rb +5 -5
- data/lib/descope/api/v1/management/scim.rb +14 -14
- data/lib/descope/api/v1/management/scope_claim_mapping.rb +34 -0
- data/lib/descope/api/v1/management/sso_application.rb +7 -7
- data/lib/descope/api/v1/management/sso_settings.rb +5 -5
- data/lib/descope/api/v1/management/tenant.rb +8 -6
- data/lib/descope/api/v1/management/third_party_application.rb +190 -0
- data/lib/descope/api/v1/management/user.rb +36 -36
- data/lib/descope/api/v1/management.rb +22 -0
- data/lib/descope/http_client.rb +30 -0
- data/lib/descope/mixins/common.rb +1 -0
- data/lib/descope/mixins/http.rb +23 -16
- data/lib/descope/mixins/initializer.rb +19 -9
- data/lib/descope/mixins/requests.rb +24 -0
- data/lib/descope/mixins.rb +2 -1
- data/lib/descope/version.rb +1 -1
- data/spec/integration/lib.descope/api/v1/management/user_spec.rb +1 -1
- data/spec/lib.descope/api/v1/auth/enchantedlink_spec.rb +163 -1
- data/spec/lib.descope/api/v1/auth_spec.rb +227 -1
- data/spec/lib.descope/api/v1/management/access_key_spec.rb +7 -7
- data/spec/lib.descope/api/v1/management/analytics_spec.rb +82 -0
- data/spec/lib.descope/api/v1/management/audit_spec.rb +2 -2
- data/spec/lib.descope/api/v1/management/authz_spec.rb +16 -16
- data/spec/lib.descope/api/v1/management/descoper_spec.rb +101 -0
- data/spec/lib.descope/api/v1/management/engine_spec.rb +115 -0
- data/spec/lib.descope/api/v1/management/fga_spec.rb +169 -0
- data/spec/lib.descope/api/v1/management/flow_spec.rb +5 -5
- data/spec/lib.descope/api/v1/management/group_spec.rb +72 -0
- data/spec/lib.descope/api/v1/management/jwt_template_spec.rb +103 -0
- data/spec/lib.descope/api/v1/management/lists_spec.rb +285 -0
- data/spec/lib.descope/api/v1/management/management_key_spec.rb +109 -0
- data/spec/lib.descope/api/v1/management/outbound_app_spec.rb +236 -0
- data/spec/lib.descope/api/v1/management/password_spec.rb +2 -2
- data/spec/lib.descope/api/v1/management/permission_spec.rb +4 -4
- data/spec/lib.descope/api/v1/management/project_spec.rb +4 -4
- data/spec/lib.descope/api/v1/management/role_spec.rb +5 -5
- data/spec/lib.descope/api/v1/management/scim_spec.rb +14 -14
- data/spec/lib.descope/api/v1/management/scope_claim_mapping_spec.rb +59 -0
- data/spec/lib.descope/api/v1/management/sso_application_spec.rb +6 -6
- data/spec/lib.descope/api/v1/management/sso_settings_spec.rb +5 -5
- data/spec/lib.descope/api/v1/management/tenant_spec.rb +6 -6
- data/spec/lib.descope/api/v1/management/third_party_application_spec.rb +171 -0
- data/spec/lib.descope/api/v1/management/user_spec.rb +36 -36
- data/spec/lib.descope/auth_management_key_spec.rb +151 -0
- data/spec/support/client_config.rb +1 -0
- data/spec/support/dummy_class.rb +12 -9
- metadata +28 -3
|
@@ -30,7 +30,7 @@ describe Descope::Api::V1::EnchantedLink do
|
|
|
30
30
|
expect(@instance).to receive(:post).with(
|
|
31
31
|
enchanted_link_compose_signin_url,
|
|
32
32
|
request_params,
|
|
33
|
-
|
|
33
|
+
{},
|
|
34
34
|
'refresh_token'
|
|
35
35
|
)
|
|
36
36
|
|
|
@@ -60,6 +60,48 @@ describe Descope::Api::V1::EnchantedLink do
|
|
|
60
60
|
@instance.send(:validate_refresh_token_provided, { mfa: true, stepup: true }, '')
|
|
61
61
|
end.to raise_error(Descope::AuthException, 'Missing refresh token for stepup/mfa')
|
|
62
62
|
end
|
|
63
|
+
|
|
64
|
+
it 'is expected to respond to sign in with phone' do
|
|
65
|
+
expect(@instance).to respond_to(:enchanted_link_sign_in_with_phone)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
it 'is expected to sign in with enchanted link via sms' do
|
|
69
|
+
request_params = {
|
|
70
|
+
loginId: '+1234567890',
|
|
71
|
+
redirectUrl: 'https://some-uri/sms',
|
|
72
|
+
loginOptions: {
|
|
73
|
+
stepup: false,
|
|
74
|
+
customClaims: { 'abc': '123' },
|
|
75
|
+
mfa: false,
|
|
76
|
+
ssoAppId: 'sso-id'
|
|
77
|
+
},
|
|
78
|
+
providerId: 'provider-id',
|
|
79
|
+
templateId: 'template-id'
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
expect(@instance).to receive(:post).with(
|
|
83
|
+
'/v1/auth/enchantedlink/signin/sms',
|
|
84
|
+
request_params,
|
|
85
|
+
{},
|
|
86
|
+
'refresh_token'
|
|
87
|
+
).and_return({ 'linkId' => '1', 'pendingRef' => 'ref', 'maskedPhone' => '+1******890' })
|
|
88
|
+
|
|
89
|
+
res = @instance.enchanted_link_sign_in_with_phone(
|
|
90
|
+
login_id: '+1234567890',
|
|
91
|
+
uri: 'https://some-uri/sms',
|
|
92
|
+
login_options: {
|
|
93
|
+
stepup: false,
|
|
94
|
+
custom_claims: { 'abc': '123' },
|
|
95
|
+
mfa: false,
|
|
96
|
+
sso_app_id: 'sso-id'
|
|
97
|
+
},
|
|
98
|
+
provider_id: 'provider-id',
|
|
99
|
+
template_id: 'template-id',
|
|
100
|
+
refresh_token: 'refresh_token'
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
expect(res['maskedPhone']).to eq('+1******890')
|
|
104
|
+
end
|
|
63
105
|
end
|
|
64
106
|
|
|
65
107
|
context '.sign_up' do
|
|
@@ -88,6 +130,36 @@ describe Descope::Api::V1::EnchantedLink do
|
|
|
88
130
|
)
|
|
89
131
|
end.not_to raise_error
|
|
90
132
|
end
|
|
133
|
+
|
|
134
|
+
it 'is expected to respond to sign up with phone' do
|
|
135
|
+
expect(@instance).to respond_to(:enchanted_link_sign_up_with_phone)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
it 'is expected to sign up with enchanted link via sms' do
|
|
139
|
+
request_params = {
|
|
140
|
+
loginId: '+1234567890',
|
|
141
|
+
redirectUrl: 'https://some-uri/sms',
|
|
142
|
+
user: { loginId: 'user1', phone: '+1234567890' },
|
|
143
|
+
phone: '+1234567890',
|
|
144
|
+
providerId: 'provider-id',
|
|
145
|
+
templateId: 'template-id'
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
expect(@instance).to receive(:post).with(
|
|
149
|
+
'/v1/auth/enchantedlink/signup/sms',
|
|
150
|
+
request_params
|
|
151
|
+
).and_return({ 'linkId' => '1', 'pendingRef' => 'ref', 'maskedPhone' => '+1******890' })
|
|
152
|
+
|
|
153
|
+
res = @instance.enchanted_link_sign_up_with_phone(
|
|
154
|
+
login_id: '+1234567890',
|
|
155
|
+
uri: 'https://some-uri/sms',
|
|
156
|
+
user: { login_id: 'user1', phone: '+1234567890' },
|
|
157
|
+
provider_id: 'provider-id',
|
|
158
|
+
template_id: 'template-id'
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
expect(res['maskedPhone']).to eq('+1******890')
|
|
162
|
+
end
|
|
91
163
|
end
|
|
92
164
|
|
|
93
165
|
context '.sign_up_or_in' do
|
|
@@ -125,6 +197,96 @@ describe Descope::Api::V1::EnchantedLink do
|
|
|
125
197
|
)
|
|
126
198
|
end.not_to raise_error
|
|
127
199
|
end
|
|
200
|
+
|
|
201
|
+
it 'is expected to respond to sign up or in with phone' do
|
|
202
|
+
expect(@instance).to respond_to(:enchanted_link_sign_up_or_in_with_phone)
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
it 'is expected to sign up or in with enchanted link via sms' do
|
|
206
|
+
request_params = {
|
|
207
|
+
loginId: '+1234567890',
|
|
208
|
+
redirectUrl: 'https://some-uri/sms',
|
|
209
|
+
loginOptions: {
|
|
210
|
+
stepup: false,
|
|
211
|
+
customClaims: { 'abc': '123' },
|
|
212
|
+
mfa: false,
|
|
213
|
+
ssoAppId: 'sso-id'
|
|
214
|
+
},
|
|
215
|
+
providerId: 'provider-id',
|
|
216
|
+
templateId: 'template-id'
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
expect(@instance).to receive(:post).with(
|
|
220
|
+
'/v1/auth/enchantedlink/signup-in/sms',
|
|
221
|
+
request_params
|
|
222
|
+
).and_return({ 'linkId' => '1', 'pendingRef' => 'ref', 'maskedPhone' => '+1******890' })
|
|
223
|
+
|
|
224
|
+
res = @instance.enchanted_link_sign_up_or_in_with_phone(
|
|
225
|
+
login_id: '+1234567890',
|
|
226
|
+
uri: 'https://some-uri/sms',
|
|
227
|
+
login_options: {
|
|
228
|
+
stepup: false,
|
|
229
|
+
custom_claims: { 'abc': '123' },
|
|
230
|
+
mfa: false,
|
|
231
|
+
sso_app_id: 'sso-id'
|
|
232
|
+
},
|
|
233
|
+
provider_id: 'provider-id',
|
|
234
|
+
template_id: 'template-id'
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
expect(res['maskedPhone']).to eq('+1******890')
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
context '.update_user_phone' do
|
|
242
|
+
it 'is expected to respond to update user phone' do
|
|
243
|
+
expect(@instance).to respond_to(:enchanted_link_update_user_phone)
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
it 'is expected to update user phone with enchanted link via sms' do
|
|
247
|
+
request_params = {
|
|
248
|
+
loginId: 'test',
|
|
249
|
+
phone: '+1234567890',
|
|
250
|
+
addToLoginIDs: true,
|
|
251
|
+
onMergeUseExisting: true,
|
|
252
|
+
redirectUrl: 'https://some-uri/sms',
|
|
253
|
+
providerId: 'provider-id',
|
|
254
|
+
templateId: 'template-id',
|
|
255
|
+
templateOptions: { 'abc': '123' }
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
expect(@instance).to receive(:post).with(
|
|
259
|
+
'/v1/auth/enchantedlink/update/phone/sms',
|
|
260
|
+
request_params,
|
|
261
|
+
{},
|
|
262
|
+
'refresh_token'
|
|
263
|
+
).and_return({ 'linkId' => '1', 'pendingRef' => 'ref', 'maskedPhone' => '+1******890' })
|
|
264
|
+
|
|
265
|
+
res = @instance.enchanted_link_update_user_phone(
|
|
266
|
+
login_id: 'test',
|
|
267
|
+
phone: '+1234567890',
|
|
268
|
+
uri: 'https://some-uri/sms',
|
|
269
|
+
add_to_login_ids: true,
|
|
270
|
+
on_merge_use_existing: true,
|
|
271
|
+
provider_id: 'provider-id',
|
|
272
|
+
template_id: 'template-id',
|
|
273
|
+
template_options: { 'abc': '123' },
|
|
274
|
+
refresh_token: 'refresh_token'
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
expect(res['maskedPhone']).to eq('+1******890')
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
it 'is expected to raise an error on an invalid phone number' do
|
|
281
|
+
expect do
|
|
282
|
+
@instance.enchanted_link_update_user_phone(
|
|
283
|
+
login_id: 'test',
|
|
284
|
+
phone: 'not-a-phone',
|
|
285
|
+
uri: 'https://some-uri/sms',
|
|
286
|
+
refresh_token: 'refresh_token'
|
|
287
|
+
)
|
|
288
|
+
end.to raise_error(Descope::AuthException)
|
|
289
|
+
end
|
|
128
290
|
end
|
|
129
291
|
|
|
130
292
|
context '.enchanted_link_verify_token' do
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
require 'spec_helper'
|
|
4
4
|
|
|
5
5
|
describe Descope::Api::V1::Auth do
|
|
6
|
-
before
|
|
6
|
+
before do
|
|
7
7
|
dummy_instance = DummyClass.new
|
|
8
8
|
dummy_instance.extend(Descope::Api::V1::Auth)
|
|
9
9
|
dummy_instance.extend(Descope::Api::V1::Session)
|
|
@@ -243,6 +243,232 @@ describe Descope::Api::V1::Auth do
|
|
|
243
243
|
end
|
|
244
244
|
end
|
|
245
245
|
|
|
246
|
+
context 'public key cache' do
|
|
247
|
+
let(:rsa) { OpenSSL::PKey::RSA.generate(2048) }
|
|
248
|
+
let(:jwk) { JWT::JWK.new(rsa, kid: 'kid-1', use: 'sig', alg: ALGORITHM) }
|
|
249
|
+
let(:token) { JWT.encode({ data: 'data' }, rsa, ALGORITHM, kid: 'kid-1') }
|
|
250
|
+
let(:keys_response) { { 'keys' => [jwk.export.transform_keys(&:to_s)] } }
|
|
251
|
+
let(:now) { [0.0] }
|
|
252
|
+
|
|
253
|
+
def token_for(kid)
|
|
254
|
+
JWT.encode({ data: 'data' }, rsa, ALGORITHM, kid:)
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
before do
|
|
258
|
+
allow(Process).to receive(:clock_gettime).with(Process::CLOCK_MONOTONIC) { now[0] }
|
|
259
|
+
@fetches = 0
|
|
260
|
+
@response = keys_response
|
|
261
|
+
allow(@instance).to receive(:token_validation_key) do
|
|
262
|
+
@fetches += 1
|
|
263
|
+
raise Descope::AuthException.new('boom', code: 500) if @response.nil?
|
|
264
|
+
|
|
265
|
+
@response
|
|
266
|
+
end
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
it 'fetches once for the same kid within the TTL' do
|
|
270
|
+
2.times { @instance.send(:validate_token, token) }
|
|
271
|
+
now[0] = 599
|
|
272
|
+
@instance.send(:validate_token, token)
|
|
273
|
+
expect(@fetches).to eq(1)
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
it 'replaces the key set after the TTL and rejects a key that is no longer published' do
|
|
277
|
+
@instance.send(:validate_token, token)
|
|
278
|
+
@response = { 'keys' => [JWT::JWK.new(rsa, kid: 'kid-2', use: 'sig', alg: ALGORITHM).export.transform_keys(&:to_s)] }
|
|
279
|
+
now[0] = 599
|
|
280
|
+
expect { @instance.send(:validate_token, token) }.not_to raise_error
|
|
281
|
+
now[0] = 600
|
|
282
|
+
expect { @instance.send(:validate_token, token) }.to raise_error(Descope::AuthException, /Public key not found/)
|
|
283
|
+
expect(@fetches).to eq(2)
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
it 'keeps the last key set and logs a warning when a successful response has no usable keys' do
|
|
287
|
+
@instance.send(:validate_token, token)
|
|
288
|
+
logger = @instance.instance_variable_get(:@logger)
|
|
289
|
+
allow(logger).to receive(:warn)
|
|
290
|
+
[{ 'keys' => [] }, { 'keys' => nil }, { 'keys' => [{ 'kid' => 'no-alg' }] }].each_with_index do |response, i|
|
|
291
|
+
@response = response
|
|
292
|
+
now[0] = 600 * (i + 1)
|
|
293
|
+
expect { @instance.send(:validate_token, token) }.not_to raise_error
|
|
294
|
+
end
|
|
295
|
+
expect(@instance.instance_variable_get(:@public_keys).keys).to eq(['kid-1'])
|
|
296
|
+
expect(logger).to have_received(:warn).with(/Unable to fetch public keys/).exactly(3).times
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
it 'shares the outcome of a failed fetch with callers that waited on it' do
|
|
300
|
+
started = Queue.new
|
|
301
|
+
release = Queue.new
|
|
302
|
+
allow(@instance).to receive(:token_validation_key) do
|
|
303
|
+
@fetches += 1
|
|
304
|
+
started << true
|
|
305
|
+
release.pop
|
|
306
|
+
raise Descope::AuthException.new('boom', code: 500)
|
|
307
|
+
end
|
|
308
|
+
errors = Concurrent::Array.new
|
|
309
|
+
lookup = lambda do
|
|
310
|
+
Thread.new do
|
|
311
|
+
@instance.send(:validate_token, token)
|
|
312
|
+
rescue Descope::AuthException => e
|
|
313
|
+
errors << e.message
|
|
314
|
+
end
|
|
315
|
+
end
|
|
316
|
+
first = lookup.call
|
|
317
|
+
started.pop
|
|
318
|
+
waiters = Array.new(3) { lookup.call }
|
|
319
|
+
Thread.pass until waiters.all? { |t| t.status == 'sleep' }
|
|
320
|
+
release.close
|
|
321
|
+
[first, *waiters].each(&:join)
|
|
322
|
+
expect(@fetches).to eq(1)
|
|
323
|
+
expect(errors).to eq(['boom'] * 4)
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
it 'refetches for an unknown kid at most once per cooldown after a successful fetch' do
|
|
327
|
+
@instance.send(:validate_token, token)
|
|
328
|
+
now[0] = 100
|
|
329
|
+
3.times do
|
|
330
|
+
expect { @instance.send(:validate_token, token_for('other')) }
|
|
331
|
+
.to raise_error(Descope::AuthException, /Public key not found/)
|
|
332
|
+
end
|
|
333
|
+
expect(@fetches).to eq(2)
|
|
334
|
+
now[0] = 130
|
|
335
|
+
expect { @instance.send(:validate_token, token_for('other')) }.to raise_error(Descope::AuthException)
|
|
336
|
+
expect(@fetches).to eq(3)
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
it 'keeps cached keys usable when a refresh fails and limits retries to the cooldown' do
|
|
340
|
+
@instance.send(:validate_token, token)
|
|
341
|
+
@response = nil
|
|
342
|
+
now[0] = 600
|
|
343
|
+
3.times { expect { @instance.send(:validate_token, token) }.not_to raise_error }
|
|
344
|
+
expect(@fetches).to eq(2)
|
|
345
|
+
now[0] = 630
|
|
346
|
+
expect { @instance.send(:validate_token, token) }.not_to raise_error
|
|
347
|
+
expect(@fetches).to eq(3)
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
it 'stops using cached keys an hour after the last successful fetch while refreshes keep failing' do
|
|
351
|
+
@instance.send(:validate_token, token)
|
|
352
|
+
@response = nil
|
|
353
|
+
now[0] = 3599
|
|
354
|
+
expect { @instance.send(:validate_token, token) }.not_to raise_error
|
|
355
|
+
now[0] = 3600
|
|
356
|
+
expect { @instance.send(:validate_token, token) }.to raise_error(Descope::AuthException, /Public key not found/)
|
|
357
|
+
expect(@fetches).to eq(2)
|
|
358
|
+
now[0] = 3628
|
|
359
|
+
expect { @instance.send(:validate_token, token) }.to raise_error(Descope::AuthException, /Public key not found/)
|
|
360
|
+
expect(@fetches).to eq(2)
|
|
361
|
+
now[0] = 3629
|
|
362
|
+
expect { @instance.send(:validate_token, token) }.to raise_error(Descope::AuthException, 'boom')
|
|
363
|
+
expect(@fetches).to eq(3)
|
|
364
|
+
@response = keys_response
|
|
365
|
+
now[0] = 3660
|
|
366
|
+
expect { @instance.send(:validate_token, token) }.not_to raise_error
|
|
367
|
+
expect(@fetches).to eq(4)
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
it 'spaces fetches 30 seconds after a failed attempt once cached keys are an hour old' do
|
|
371
|
+
@instance.send(:validate_token, token)
|
|
372
|
+
@response = nil
|
|
373
|
+
now[0] = 3600
|
|
374
|
+
expect { @instance.send(:validate_token, token_for('other')) }.to raise_error(Descope::AuthException, 'boom')
|
|
375
|
+
expect(@fetches).to eq(2)
|
|
376
|
+
[3601, 3629].each do |t|
|
|
377
|
+
now[0] = t
|
|
378
|
+
[token_for('other'), token].each do |tok|
|
|
379
|
+
expect { @instance.send(:validate_token, tok) }.to raise_error(Descope::AuthException, /Public key not found/)
|
|
380
|
+
end
|
|
381
|
+
end
|
|
382
|
+
expect(@fetches).to eq(2)
|
|
383
|
+
now[0] = 3630
|
|
384
|
+
expect { @instance.send(:validate_token, token_for('other')) }.to raise_error(Descope::AuthException, 'boom')
|
|
385
|
+
expect(@fetches).to eq(3)
|
|
386
|
+
@response = keys_response
|
|
387
|
+
now[0] = 3660
|
|
388
|
+
expect { @instance.send(:validate_token, token) }.not_to raise_error
|
|
389
|
+
expect(@fetches).to eq(4)
|
|
390
|
+
end
|
|
391
|
+
|
|
392
|
+
it 'keeps cached keys when the refresh response is malformed' do
|
|
393
|
+
@instance.send(:validate_token, token)
|
|
394
|
+
@response = { 'error' => 'nope' }
|
|
395
|
+
now[0] = 600
|
|
396
|
+
expect { @instance.send(:validate_token, token) }.not_to raise_error
|
|
397
|
+
expect(@instance.instance_variable_get(:@public_keys).keys).to eq(['kid-1'])
|
|
398
|
+
end
|
|
399
|
+
|
|
400
|
+
it 'surfaces the fetch error when the key is not cached and the fetch fails' do
|
|
401
|
+
@response = nil
|
|
402
|
+
expect { @instance.send(:validate_token, token) }.to raise_error(Descope::AuthException, 'boom')
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
it 'retries the fetch for an unknown kid right after a failed fetch' do
|
|
406
|
+
@response = nil
|
|
407
|
+
expect { @instance.send(:validate_token, token) }.to raise_error(Descope::AuthException, 'boom')
|
|
408
|
+
@response = keys_response
|
|
409
|
+
now[0] = 1
|
|
410
|
+
expect { @instance.send(:validate_token, token) }.not_to raise_error
|
|
411
|
+
expect(@fetches).to eq(2)
|
|
412
|
+
end
|
|
413
|
+
|
|
414
|
+
it 'does not delay an unknown kid lookup after a failed refresh of a cached key' do
|
|
415
|
+
@instance.send(:validate_token, token)
|
|
416
|
+
@response = nil
|
|
417
|
+
now[0] = 600
|
|
418
|
+
@instance.send(:validate_token, token)
|
|
419
|
+
expect(@fetches).to eq(2)
|
|
420
|
+
now[0] = 601
|
|
421
|
+
expect { @instance.send(:validate_token, token_for('other')) }.to raise_error(Descope::AuthException, 'boom')
|
|
422
|
+
expect(@fetches).to eq(3)
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
it 'does not apply the TTL to a static public key but still fetches for an unknown kid, rate limited' do
|
|
426
|
+
@instance.instance_variable_set(:@public_key, jwk.export.transform_keys(&:to_s))
|
|
427
|
+
@instance.instance_variable_set(:@public_keys, { 'kid-1' => [jwk, ALGORITHM] })
|
|
428
|
+
now[0] = 10_000
|
|
429
|
+
@instance.send(:validate_token, token)
|
|
430
|
+
expect(@fetches).to eq(0)
|
|
431
|
+
2.times { expect { @instance.send(:validate_token, token_for('other')) }.to raise_error(Descope::AuthException) }
|
|
432
|
+
expect(@fetches).to eq(1)
|
|
433
|
+
now[0] = 10_030
|
|
434
|
+
expect { @instance.send(:validate_token, token_for('other')) }.to raise_error(Descope::AuthException)
|
|
435
|
+
expect(@fetches).to eq(2)
|
|
436
|
+
end
|
|
437
|
+
|
|
438
|
+
it 'keeps a static public key alongside fetched keys and replaces only the fetched ones' do
|
|
439
|
+
@instance.instance_variable_set(:@public_key, jwk.export.transform_keys(&:to_s))
|
|
440
|
+
@instance.instance_variable_set(:@public_keys, { 'kid-1' => [jwk, ALGORITHM] })
|
|
441
|
+
fetched_jwk = JWT::JWK.new(rsa, kid: 'kid-2', use: 'sig', alg: ALGORITHM)
|
|
442
|
+
@response = { 'keys' => [fetched_jwk.export.transform_keys(&:to_s)] }
|
|
443
|
+
expect { @instance.send(:validate_token, token_for('kid-2')) }.not_to raise_error
|
|
444
|
+
expect { @instance.send(:validate_token, token) }.not_to raise_error
|
|
445
|
+
@response = { 'keys' => [JWT::JWK.new(rsa, kid: 'kid-3', use: 'sig', alg: ALGORITHM).export.transform_keys(&:to_s)] }
|
|
446
|
+
now[0] = 600
|
|
447
|
+
expect { @instance.send(:validate_token, token_for('kid-3')) }.not_to raise_error
|
|
448
|
+
expect { @instance.send(:validate_token, token_for('kid-2')) }
|
|
449
|
+
.to raise_error(Descope::AuthException, /Public key not found/)
|
|
450
|
+
@response = nil
|
|
451
|
+
now[0] = 10_000
|
|
452
|
+
expect { @instance.send(:validate_token, token) }.not_to raise_error
|
|
453
|
+
expect { @instance.send(:validate_token, token_for('kid-3')) }.to raise_error(Descope::AuthException)
|
|
454
|
+
end
|
|
455
|
+
|
|
456
|
+
it 'restores the static public key without a fetch when the cache was cleared' do
|
|
457
|
+
@instance.instance_variable_set(:@public_key, jwk.export.transform_keys(&:to_s))
|
|
458
|
+
[nil, {}].each do |cleared|
|
|
459
|
+
@instance.instance_variable_set(:@public_keys, cleared)
|
|
460
|
+
expect { @instance.send(:validate_token, token) }.not_to raise_error
|
|
461
|
+
end
|
|
462
|
+
expect(@fetches).to eq(0)
|
|
463
|
+
end
|
|
464
|
+
|
|
465
|
+
it 'fetches keys when the cache was cleared to nil' do
|
|
466
|
+
@instance.instance_variable_set(:@public_keys, nil)
|
|
467
|
+
expect { @instance.send(:validate_token, token) }.not_to raise_error
|
|
468
|
+
expect(@fetches).to eq(1)
|
|
469
|
+
end
|
|
470
|
+
end
|
|
471
|
+
|
|
246
472
|
context '.select_tenant' do
|
|
247
473
|
it 'is expected to respond to select tenant' do
|
|
248
474
|
expect(@instance).to respond_to(:select_tenant)
|
|
@@ -15,7 +15,7 @@ describe Descope::Api::V1::Management::AccessKey do
|
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
it 'is expected to create access key' do
|
|
18
|
-
expect(@instance).to receive(:
|
|
18
|
+
expect(@instance).to receive(:mgmt_post).with(
|
|
19
19
|
ACCESS_KEY_CREATE_PATH, {
|
|
20
20
|
name: 'test',
|
|
21
21
|
expireTime: 0,
|
|
@@ -46,7 +46,7 @@ describe Descope::Api::V1::Management::AccessKey do
|
|
|
46
46
|
end
|
|
47
47
|
|
|
48
48
|
it 'is expected to load an access key' do
|
|
49
|
-
expect(@instance).to receive(:
|
|
49
|
+
expect(@instance).to receive(:mgmt_get).with(
|
|
50
50
|
ACCESS_KEY_LOAD_PATH, { id: '123' }
|
|
51
51
|
)
|
|
52
52
|
expect { @instance.load_access_key('123') }.not_to raise_error
|
|
@@ -59,7 +59,7 @@ describe Descope::Api::V1::Management::AccessKey do
|
|
|
59
59
|
end
|
|
60
60
|
|
|
61
61
|
it 'is expected to search all access keys' do
|
|
62
|
-
expect(@instance).to receive(:
|
|
62
|
+
expect(@instance).to receive(:mgmt_post).with(
|
|
63
63
|
ACCESS_KEYS_SEARCH_PATH, { tenantIds: %w[123 456] }
|
|
64
64
|
)
|
|
65
65
|
expect { @instance.search_all_access_keys(%w[123 456]) }.not_to raise_error
|
|
@@ -72,7 +72,7 @@ describe Descope::Api::V1::Management::AccessKey do
|
|
|
72
72
|
end
|
|
73
73
|
|
|
74
74
|
it 'is expected to update an access keys' do
|
|
75
|
-
expect(@instance).to receive(:
|
|
75
|
+
expect(@instance).to receive(:mgmt_post).with(
|
|
76
76
|
ACCESS_KEY_UPDATE_PATH, { id: '123', name: 'test1' }
|
|
77
77
|
)
|
|
78
78
|
expect { @instance.update_access_key(id: '123', name: 'test1') }.not_to raise_error
|
|
@@ -85,7 +85,7 @@ describe Descope::Api::V1::Management::AccessKey do
|
|
|
85
85
|
end
|
|
86
86
|
|
|
87
87
|
it 'is expected to deactivate an access keys' do
|
|
88
|
-
expect(@instance).to receive(:
|
|
88
|
+
expect(@instance).to receive(:mgmt_post).with(
|
|
89
89
|
ACCESS_KEY_DEACTIVATE_PATH, { id: '123' }
|
|
90
90
|
)
|
|
91
91
|
expect { @instance.deactivate_access_key('123') }.not_to raise_error
|
|
@@ -98,7 +98,7 @@ describe Descope::Api::V1::Management::AccessKey do
|
|
|
98
98
|
end
|
|
99
99
|
|
|
100
100
|
it 'is expected to activate an access keys' do
|
|
101
|
-
expect(@instance).to receive(:
|
|
101
|
+
expect(@instance).to receive(:mgmt_post).with(
|
|
102
102
|
ACCESS_KEY_ACTIVATE_PATH, { id: '123' }
|
|
103
103
|
)
|
|
104
104
|
expect { @instance.activate_access_key('123') }.not_to raise_error
|
|
@@ -111,7 +111,7 @@ describe Descope::Api::V1::Management::AccessKey do
|
|
|
111
111
|
end
|
|
112
112
|
|
|
113
113
|
it 'is expected to delete an access keys' do
|
|
114
|
-
expect(@instance).to receive(:
|
|
114
|
+
expect(@instance).to receive(:mgmt_post).with(
|
|
115
115
|
ACCESS_KEY_DELETE_PATH, { id: '123' }
|
|
116
116
|
)
|
|
117
117
|
expect { @instance.delete_access_key('123') }.not_to raise_error
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
describe Descope::Api::V1::Management::Analytics do
|
|
6
|
+
before(:all) do
|
|
7
|
+
dummy_instance = DummyClass.new
|
|
8
|
+
dummy_instance.extend(Descope::Api::V1::Management::Analytics)
|
|
9
|
+
@instance = dummy_instance
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
context '.search' do
|
|
13
|
+
let(:mock_response) do
|
|
14
|
+
{
|
|
15
|
+
'analytics' => [
|
|
16
|
+
{
|
|
17
|
+
'projectId' => 'abc',
|
|
18
|
+
'action' => 'get',
|
|
19
|
+
'created' => '1234567000',
|
|
20
|
+
'device' => 'mobile',
|
|
21
|
+
'method' => 'post',
|
|
22
|
+
'geo' => 'US',
|
|
23
|
+
'tenant' => 'tenant1',
|
|
24
|
+
'referrer' => 'referrer1',
|
|
25
|
+
'cnt' => '5'
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
}
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
before do
|
|
32
|
+
allow(@instance).to receive(:mgmt_post).with(
|
|
33
|
+
ANALYTICS_SEARCH_PATH,
|
|
34
|
+
{
|
|
35
|
+
from: 1_234_567_000,
|
|
36
|
+
to: 123_456_789_000,
|
|
37
|
+
groupByAction: true,
|
|
38
|
+
groupByDevice: true,
|
|
39
|
+
groupByMethod: true,
|
|
40
|
+
groupByGeo: true,
|
|
41
|
+
groupByTenant: true,
|
|
42
|
+
groupByReferrer: true,
|
|
43
|
+
groupByCreated: true,
|
|
44
|
+
actions: %w[action1 action2],
|
|
45
|
+
excludedActions: %w[exclude1 exclude2],
|
|
46
|
+
devices: %w[Bot Mobile Desktop Tablet Unknown],
|
|
47
|
+
methods: %w[otp totp magiclink oauth saml password],
|
|
48
|
+
geos: %w[US IL],
|
|
49
|
+
tenants: %w[tenant1 tenant2]
|
|
50
|
+
}
|
|
51
|
+
).and_return(mock_response)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it 'should respond to .analytics_search' do
|
|
55
|
+
expect(@instance).to respond_to :analytics_search
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it 'is expected to search analytics and get records' do
|
|
59
|
+
res = nil # define res outside the block
|
|
60
|
+
expect do
|
|
61
|
+
res = @instance.analytics_search(
|
|
62
|
+
from_ts: 1_234_567_000,
|
|
63
|
+
to_ts: 123_456_789_000,
|
|
64
|
+
group_by_action: true,
|
|
65
|
+
group_by_device: true,
|
|
66
|
+
group_by_method: true,
|
|
67
|
+
group_by_geo: true,
|
|
68
|
+
group_by_tenant: true,
|
|
69
|
+
group_by_referrer: true,
|
|
70
|
+
group_by_created: true,
|
|
71
|
+
actions: %w[action1 action2],
|
|
72
|
+
excluded_actions: %w[exclude1 exclude2],
|
|
73
|
+
devices: %w[Bot Mobile Desktop Tablet Unknown],
|
|
74
|
+
methods: %w[otp totp magiclink oauth saml password],
|
|
75
|
+
geos: %w[US IL],
|
|
76
|
+
tenants: %w[tenant1 tenant2]
|
|
77
|
+
)
|
|
78
|
+
end.not_to raise_error
|
|
79
|
+
expect(res['analytics'][0]['projectId']).to eq('abc')
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -31,7 +31,7 @@ describe Descope::Api::V1::Management::Audit do
|
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
before do
|
|
34
|
-
allow(@instance).to receive(:
|
|
34
|
+
allow(@instance).to receive(:mgmt_post).twice.with(
|
|
35
35
|
AUDIT_SEARCH,
|
|
36
36
|
{
|
|
37
37
|
noTenants: true,
|
|
@@ -144,7 +144,7 @@ describe Descope::Api::V1::Management::Audit do
|
|
|
144
144
|
end
|
|
145
145
|
|
|
146
146
|
it 'is expected to create an audit event' do
|
|
147
|
-
expect(@instance).to receive(:
|
|
147
|
+
expect(@instance).to receive(:mgmt_post).with(
|
|
148
148
|
'/v1/mgmt/audit/event',
|
|
149
149
|
{
|
|
150
150
|
action: 'get',
|