Class: JWT::JWK::KeyFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/jwt/jwk/key_finder.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ KeyFinder

Returns a new instance of KeyFinder.



6
7
8
9
10
11
12
13
14
15
# File 'lib/jwt/jwk/key_finder.rb', line 6

def initialize(options)
  @allow_nil_kid = options[:allow_nil_kid]
  jwks_or_loader = options[:jwks]

  @jwks_loader = if jwks_or_loader.respond_to?(:call)
                   jwks_or_loader
                 else
                   ->(_options) { jwks_or_loader }
                 end
end

Instance Method Details

#key_for(kid) ⇒ Object

Raises:



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/jwt/jwk/key_finder.rb', line 17

def key_for(kid)
  raise ::JWT::DecodeError, 'No key id (kid) found from token headers' unless kid || @allow_nil_kid
  raise ::JWT::DecodeError, 'Invalid type for kid header parameter' unless kid.nil? || kid.is_a?(String)

  jwk = resolve_key(kid)

  raise ::JWT::DecodeError, 'No keys found in jwks' unless @jwks.any?
  raise ::JWT::DecodeError, "Could not find public key for kid #{kid}" unless jwk

  jwk.verify_key
end