descope 1.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 46b46c0f4ea895abb2c02d5728cf7aac05b1f3928cf5b3c605597618d151bfb9
4
- data.tar.gz: dc6763494e5bc8caaf3cde6b40f9f84a8d4d297c34d42db54aac94d192e3633e
3
+ metadata.gz: 575451f7343e4da355f1d936017501a8fb17d7d3d26733c0d585ea575b53ca5c
4
+ data.tar.gz: 5a9098ba2e46568e4a46fd9d194aa003ad359e30af2fb480bd434168faee6e5a
5
5
  SHA512:
6
- metadata.gz: 3cbbe031f32d0fae0044d03e957abd674acc5257a426069082fcb01b097417a2b547255eee816b3b7c9c35b96b9d5a779b46eb561c95cbc31a45b3929e03904a
7
- data.tar.gz: fde06107b8aa690dbb9867b4a028e1bb523dd25f077afbae29262d2a326aa12a0a5c6f1cd5bcbf03c116f3824363433325a9965e6e19b7757a1e579be622e504
6
+ metadata.gz: ae7d126c85d68c34695695cd63aed3da5416889fc40ed04ae49173d43c5f0e84e7b60fca6ecdcbd064d7f9e4ae837623e34519dea9567bebe29c8e6e7726c2f1
7
+ data.tar.gz: 9034f036e866fb7e1ea694da3a9cf97c27ed79005457c1d87f4af4584714ac093f262f24979a3bd7f2659e67e659ce1630a72f20b73755a556eab539d97940cc
@@ -35,7 +35,7 @@ jobs:
35
35
  - name: Checkout Code
36
36
  uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
37
37
 
38
- - uses: ruby/setup-ruby@14594264cd68ce8a2345dd349bc3d138a4ef85c8 # v1.327.0
38
+ - uses: ruby/setup-ruby@651ff0d62c5c79bed6d53ec518387aae20e72c1a # v1.329.0
39
39
  with:
40
40
  bundler-cache: true # Cache bundle based on Gemfile.lock
41
41
 
@@ -51,7 +51,7 @@ jobs:
51
51
  - name: Checkout Code
52
52
  uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
53
53
 
54
- - uses: ruby/setup-ruby@14594264cd68ce8a2345dd349bc3d138a4ef85c8 # v1.327.0
54
+ - uses: ruby/setup-ruby@651ff0d62c5c79bed6d53ec518387aae20e72c1a # v1.329.0
55
55
  with:
56
56
  bundler-cache: true # Cache bundle based on Gemfile.lock
57
57
 
@@ -15,7 +15,7 @@ jobs:
15
15
  ref: main
16
16
  fetch-depth: 0
17
17
 
18
- - uses: ruby/setup-ruby@14594264cd68ce8a2345dd349bc3d138a4ef85c8 # v1.327.0
18
+ - uses: ruby/setup-ruby@651ff0d62c5c79bed6d53ec518387aae20e72c1a # v1.329.0
19
19
  with:
20
20
  # We are not letting this step to run bundle install, we will do it later
21
21
  bundler-cache: false
@@ -1,3 +1,3 @@
1
1
  {
2
- ".": "1.2.0"
2
+ ".": "1.3.0"
3
3
  }
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Change Log
2
2
 
3
+ ## [1.3.0](https://github.com/descope/descope-ruby-sdk/compare/descope/v1.2.0...descope/v1.3.0) (2026-11-09)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * **auth:** refresh cached signing keys and rate limit unknown kid refetch ([#250](https://github.com/descope/descope-ruby-sdk/issues/250)) ([72d586e](https://github.com/descope/descope-ruby-sdk/commit/72d586efd95fd899446ee48a4220a3a1fb7ce347))
9
+
3
10
  ## [1.2.0](https://github.com/descope/descope-ruby-sdk/compare/descope/v1.1.0...descope/v1.2.0) (2026-10-19)
4
11
 
5
12
 
data/Gemfile CHANGED
@@ -4,7 +4,7 @@ source 'https://rubygems.org'
4
4
  gemspec
5
5
 
6
6
  group :development do
7
- gem 'rubocop', '1.91.0', require: false
7
+ gem 'rubocop', '1.92.0', require: false
8
8
  gem 'rubocop-rails', '2.38.0', require: false
9
9
  end
10
10
 
@@ -15,7 +15,7 @@ group :test do
15
15
  gem 'rack-test', '2.2.0', require: false
16
16
  gem 'rotp', '6.3.0', require: false
17
17
  gem 'rspec', '3.13.2', require: false
18
- gem 'selenium-webdriver', '4.49.0', require: false
18
+ gem 'selenium-webdriver', '4.51.0', require: false
19
19
  gem 'simplecov', '0.22.0', require: false
20
20
  gem 'super_diff', '0.19.0', require: false
21
21
  end
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- descope (1.2.0)
4
+ descope (1.3.0)
5
5
  addressable (~> 2.8)
6
6
  jwt (~> 2.7)
7
7
  rest-client (~> 2.1)
@@ -60,7 +60,7 @@ GEM
60
60
  parallel (1.28.0)
61
61
  parallel_tests (5.5.0)
62
62
  parallel
63
- parser (3.3.12.0)
63
+ parser (3.3.13.0)
64
64
  ast (~> 2.4.1)
65
65
  racc
66
66
  patience_diff (1.2.0)
@@ -79,7 +79,6 @@ GEM
79
79
  mime-types (>= 1.16, < 4.0)
80
80
  netrc (~> 0.8)
81
81
  retryable (3.0.5)
82
- rexml (3.4.4)
83
82
  rotp (6.3.0)
84
83
  rspec (3.13.2)
85
84
  rspec-core (~> 3.13.0)
@@ -94,14 +93,14 @@ GEM
94
93
  diff-lcs (>= 1.2.0, < 2.0)
95
94
  rspec-support (~> 3.13.0)
96
95
  rspec-support (3.13.6)
97
- rubocop (1.91.0)
96
+ rubocop (1.92.0)
98
97
  json (>= 2.3)
99
98
  language_server-protocol (~> 3.17.0.2)
100
99
  lint_roller (~> 1.1.0)
101
100
  parallel (>= 1.10)
102
101
  parser (>= 3.3.0.2)
103
102
  rainbow (>= 2.2.2, < 4.0)
104
- regexp_parser (>= 2.9.3, < 3.0)
103
+ regexp_parser (>= 2.11.3, < 3.0)
105
104
  rubocop-ast (>= 1.49.0, < 2.0)
106
105
  ruby-progressbar (~> 1.7)
107
106
  unicode-display_width (>= 2.4.0, < 4.0)
@@ -117,10 +116,9 @@ GEM
117
116
  ruby-progressbar (1.13.0)
118
117
  rubyzip (2.4.1)
119
118
  securerandom (0.4.1)
120
- selenium-webdriver (4.49.0)
119
+ selenium-webdriver (4.51.0)
121
120
  base64 (~> 0.2)
122
121
  logger (~> 1.4)
123
- rexml (~> 3.2, >= 3.2.5)
124
122
  rubyzip (>= 1.2.2, < 4.0)
125
123
  websocket (~> 1.0)
126
124
  simplecov (0.22.0)
@@ -153,9 +151,9 @@ DEPENDENCIES
153
151
  rack-test (= 2.2.0)
154
152
  rotp (= 6.3.0)
155
153
  rspec (= 3.13.2)
156
- rubocop (= 1.91.0)
154
+ rubocop (= 1.92.0)
157
155
  rubocop-rails (= 2.38.0)
158
- selenium-webdriver (= 4.49.0)
156
+ selenium-webdriver (= 4.51.0)
159
157
  simplecov (= 0.22.0)
160
158
  super_diff (= 0.19.0)
161
159
 
@@ -27,6 +27,10 @@ module Descope
27
27
 
28
28
 
29
29
  ALGORITHM_KEY = 'alg'
30
+ PUBLIC_KEYS_TTL = 600 # seconds since the last successful fetch
31
+ PUBLIC_KEYS_REFETCH_COOLDOWN = 30 # seconds between fetches triggered by an unknown kid
32
+ PUBLIC_KEYS_MAX_STALE = 3600 # seconds since the last successful fetch after which cached keys are not used
33
+ private_constant :PUBLIC_KEYS_TTL, :PUBLIC_KEYS_REFETCH_COOLDOWN, :PUBLIC_KEYS_MAX_STALE
30
34
 
31
35
  def generate_jwt_response(response_body: nil, refresh_cookie: nil, audience: nil)
32
36
  if response_body.nil? || response_body.empty?
@@ -188,12 +192,9 @@ module Descope
188
192
  raise AuthException.new('Token header is missing property: kid', code: 500) if kid.nil?
189
193
 
190
194
  found_key = nil
195
+ seen_fetches = @public_keys_fetches
191
196
  @mlock.synchronize do
192
- if @public_keys.nil? || @public_keys == {} || @public_keys.to_s.empty? || @public_keys[kid].nil?
193
- @logger.debug 'fetching public keys'
194
- # fetch keys from /v2/keys and set them in @public_keys
195
- fetch_public_keys
196
- end
197
+ refresh_public_keys(kid, seen_fetches)
197
198
 
198
199
  found_key = @public_keys[kid]
199
200
  @logger.debug "found_key: #{found_key}"
@@ -356,16 +357,72 @@ module Descope
356
357
  raise AuthException.new("Unable to fetch public keys. #{response}", code: 500)
357
358
  end
358
359
 
359
- jwkeys_wrapper = response
360
- jwkeys = jwkeys_wrapper['keys']
361
- @public_keys = {}
362
-
363
- jwkeys.each do |key|
360
+ public_keys = {}
361
+ response['keys'].each do |key|
364
362
  loaded_kid, pub_key, alg = validate_and_load_public_key(key)
365
- @public_keys[loaded_kid] = [pub_key, alg]
363
+ public_keys[loaded_kid] = [pub_key, alg]
366
364
  rescue AuthException
367
365
  nil
368
366
  end
367
+ raise AuthException.new("Unable to fetch public keys. #{response}", code: 500) if public_keys.empty?
368
+
369
+ @public_keys = public_keys.merge(static_public_keys)
370
+ end
371
+
372
+ # The user-provided public key, kept alongside fetched keys and never expired.
373
+ def static_public_keys
374
+ return {} if @public_key.nil?
375
+
376
+ @static_public_keys ||= begin
377
+ kid, pub_key, alg = validate_and_load_public_key(@public_key)
378
+ { kid => [pub_key, alg] }
379
+ end
380
+ end
381
+
382
+ # An unknown kid refetches the whole key set at most once per PUBLIC_KEYS_REFETCH_COOLDOWN since the last
383
+ # successful fetch, so a failed fetch is retried on the next lookup. A cached kid refetches once the set is
384
+ # older than PUBLIC_KEYS_TTL, skipping the fetch for PUBLIC_KEYS_REFETCH_COOLDOWN after a failed attempt.
385
+ # On failure the last good key set is kept, but it is not used once older than PUBLIC_KEYS_MAX_STALE: a
386
+ # lookup then fails unless a fetch succeeds, and fetches are spaced PUBLIC_KEYS_REFETCH_COOLDOWN after a
387
+ # failed attempt. A caller that waited on @mlock while another caller fetched uses that fetch's outcome.
388
+ # The static public key is never fetched or expired. Caller holds @mlock.
389
+ def refresh_public_keys(kid, seen_fetches)
390
+ @public_keys ||= {}
391
+ if static_public_keys.key?(kid)
392
+ @public_keys[kid] ||= static_public_keys[kid]
393
+ return
394
+ end
395
+
396
+ now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
397
+ fetched = @public_keys_fetched_at
398
+ stale = fetched && now - fetched >= PUBLIC_KEYS_MAX_STALE
399
+ attempt_public_keys_fetch(kid, now, fetched, stale) if seen_fetches == @public_keys_fetches
400
+ error = @public_keys_fetch_error
401
+ raise error if error && (@public_keys[kid].nil? || stale)
402
+ end
403
+
404
+ def attempt_public_keys_fetch(kid, now, fetched, stale)
405
+ attempted = @public_keys_fetch_attempted_at
406
+ retry_wait = attempted && now - attempted < PUBLIC_KEYS_REFETCH_COOLDOWN
407
+ if stale
408
+ raise AuthException.new('Unable to validate public key. Public key not found.', code: 500) if retry_wait
409
+ elsif @public_keys[kid].nil?
410
+ return if fetched && now - fetched < PUBLIC_KEYS_REFETCH_COOLDOWN
411
+ elsif (fetched && now - fetched < PUBLIC_KEYS_TTL) || retry_wait
412
+ return
413
+ end
414
+
415
+ @logger.debug 'fetching public keys'
416
+ @public_keys_fetch_attempted_at = now
417
+ begin
418
+ fetch_public_keys
419
+ @public_keys_fetched_at = now
420
+ @public_keys_fetch_error = nil
421
+ rescue StandardError => e
422
+ @logger.warn "Unable to fetch public keys: #{e.message}"
423
+ @public_keys_fetch_error = e
424
+ end
425
+ @public_keys_fetches = @public_keys_fetches.to_i + 1
369
426
  end
370
427
 
371
428
  # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity
@@ -2,6 +2,6 @@
2
2
 
3
3
  # Current version of gem
4
4
  module Descope
5
- VERSION = '1.2.0'
5
+ VERSION = '1.3.0'
6
6
  SDK_VERSION = '1.0.0'
7
7
  end
@@ -3,7 +3,7 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe Descope::Api::V1::Auth do
6
- before(:all) do
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)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: descope
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Descope Inc.
@@ -372,9 +372,9 @@ licenses:
372
372
  - MIT
373
373
  metadata:
374
374
  bug_tracker_uri: https://github.com/descope/descope-ruby-sdk/issues
375
- changelog_uri: https://github.com/descope/descope-ruby-sdk/releases/tag/1.2.0
375
+ changelog_uri: https://github.com/descope/descope-ruby-sdk/releases/tag/1.3.0
376
376
  documentation_uri: https://docs.descope.com
377
- source_code_uri: https://github.com/descope/descope-ruby-sdk/tree/1.2.0
377
+ source_code_uri: https://github.com/descope/descope-ruby-sdk/tree/1.3.0
378
378
  rdoc_options: []
379
379
  require_paths:
380
380
  - lib