Class: HoninClient::JwksCache

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

Defined Under Namespace

Classes: Static

Constant Summary collapse

TTL =
3600

Instance Method Summary collapse

Constructor Details

#initialize(jwks_uri) ⇒ JwksCache

Returns a new instance of JwksCache.



34
35
36
37
38
# File 'lib/honin/client/jwks_cache.rb', line 34

def initialize(jwks_uri)
  @jwks_uri = jwks_uri
  @keys = nil
  @fetched_at = nil
end

Instance Method Details

#fetchObject



40
41
42
43
# File 'lib/honin/client/jwks_cache.rb', line 40

def fetch
  refresh! if stale?
  @keys
end

#refresh!Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/honin/client/jwks_cache.rb', line 45

def refresh!
  response = Net::HTTP.get_response(URI.parse(@jwks_uri))
  unless response.is_a?(Net::HTTPSuccess)
    raise Error, "Failed to fetch JWKS from #{@jwks_uri}: HTTP #{response.code}"
  end
  body = JSON.parse(response.body)
  @keys = JWT::JWK::Set.new(body["keys"] || [])
  @fetched_at = Time.now
  @keys
rescue JSON::ParserError => e
  raise Error, "Invalid JWKS response from #{@jwks_uri}: #{e.message}"
end