Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/json/jwk/set/fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module JSON
class JWK
class Set
module Fetcher
class MalformedJWKSet < JWT::Exception; end
class UnexpectedFormat < JWT::Exception; end

class Cache
def fetch(cache_key, options = {})
Expand Down Expand Up @@ -73,7 +73,10 @@ def self.fetch(jwks_uri, kid:, auto_detect: true, **options)
end
)

raise MalformedJWKSet, "Malformed JWK Set: #{parsed_jwks.inspect}" unless parsed_jwks.is_a?(Hash) && parsed_jwks['keys'].is_a?(Array)
unless parsed_jwks.is_a?(Hash) && parsed_jwks['keys'].is_a?(Array)
cache.delete(cache_key, options)
raise UnexpectedFormat
end

jwks = Set.new(parsed_jwks)
cache.delete(cache_key, options) if jwks[kid].blank?
Expand Down
6 changes: 3 additions & 3 deletions spec/json/jwk/set/fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def fetch(cache_key, options = {})
end
end

def delete(cache_key)
def delete(cache_key, options = nil)
# ignore
end
end
Expand Down Expand Up @@ -112,13 +112,13 @@ def delete(cache_key)
end

context "when the JWKS uri returns a structure that's not a valid JWK Set" do
it "raises a JSON::JWK::Set::Fetcher::MalformedJWKSet error" do
it "raises a JSON::JWK::Set::Fetcher::UnexpectedFormat error" do
stub_request(:get, jwks_uri).to_return(
status: 200,
body: '"hello there"' # Note that this is valid JSON, but not a valid JWK Set
)

expect { subject }.to raise_error(JSON::JWK::Set::Fetcher::MalformedJWKSet)
expect { subject }.to raise_error(JSON::JWK::Set::Fetcher::UnexpectedFormat)
end
end

Expand Down