Class: JWKSCache

Inherits:
Object
  • Object
show all
Defined in:
lib/clerk/jwks_cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(lifetime) ⇒ JWKSCache

Returns a new instance of JWKSCache.



2
3
4
5
6
7
# File 'lib/clerk/jwks_cache.rb', line 2

def initialize(lifetime)
  @lifetime = lifetime
  @jwks = nil
  @last_update = nil
  @lock = Concurrent::ReadWriteLock.new
end

Instance Method Details

#fetch(sdk, force_refresh: false, kid_not_found: false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/clerk/jwks_cache.rb', line 9

def fetch(sdk, force_refresh: false, kid_not_found: false)
  should_refresh = @lock.with_read_lock do
    @jwks.nil? || @last_update.nil? || force_refresh ||
      (Time.now.to_i-@last_update > @lifetime) ||
      (kid_not_found && Time.now.to_i-@last_update > 300)
  end

  if should_refresh
    @lock.with_write_lock do
      @last_update = Time.now.to_i

      @jwks = begin
        sdk.jwks.all["keys"]
      rescue Clerk::Errors::Base
        nil
      end
    end
  end

  @lock.with_read_lock do
    @jwks
  end
end