Skip to content

Commit

Permalink
support cache options
Browse files Browse the repository at this point in the history
  • Loading branch information
nov committed Aug 14, 2022
1 parent 7570f61 commit 3a81c85
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/json/jwk/set/fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class JWK
class Set
module Fetcher
class Cache
def fetch(cache_key)
def fetch(cache_key, options = {})
yield
end
end
Expand Down Expand Up @@ -60,7 +60,7 @@ def self.cache
end
self.cache = Cache.new

def self.fetch(jwks_uri, kid:, auto_detect: true)
def self.fetch(jwks_uri, kid:, auto_detect: true, **options)
cache_key = [
'json:jwk:set',
OpenSSL::Digest::MD5.hexdigest(jwks_uri),
Expand All @@ -69,7 +69,7 @@ def self.fetch(jwks_uri, kid:, auto_detect: true)

jwks = Set.new(
JSON.parse(
cache.fetch(cache_key) do
cache.fetch(cache_key, options) do
http_client.get_content(jwks_uri)
end
)
Expand Down
71 changes: 69 additions & 2 deletions spec/json/jwk/set/fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@
class CustomCache
JWKS_URI = 'https://idp.example.com/jwks'

def fetch(kid)
def fetch(cache_key, options = {})
base_key = "json:jwk:set:#{OpenSSL::Digest::MD5.hexdigest JWKS_URI}"
case kid
case cache_key
when "#{base_key}:known"
File.read(File.join(File.dirname(__FILE__), '../../../mock_response/jwks.json'))
else
Expand Down Expand Up @@ -143,6 +143,73 @@ def fetch(kid)
end
end
end

describe 'cache options' do
let(:kid) { 'known' }

shared_context :receive_options_as_hash do
it do
expect_any_instance_of(CustomCache).to receive(:fetch).with(
"json:jwk:set:#{OpenSSL::Digest::MD5.hexdigest CustomCache::JWKS_URI}:#{kid}",
options
).and_return(
File.read(File.join(File.dirname(__FILE__), '../../../mock_response/jwks.json'))
)
subject
end
end
shared_context :receive_options_as_blank_hash do
let(:options) { {} }
it_behaves_like :receive_options_as_hash
end

context 'when cache options not given' do
context 'with auto_detect' do
subject { JSON::JWK::Set::Fetcher.fetch jwks_uri, kid: kid }
it_behaves_like :receive_options_as_blank_hash
end
end

context 'when cache options given' do
let(:options) do
{
force: true,
expires_in: 10.minutes
}
end

context 'with auto_detect' do
subject { JSON::JWK::Set::Fetcher.fetch jwks_uri, kid: kid, auto_detect: true, **options }
it_behaves_like :receive_options_as_hash
end

context 'without auto_detect' do
subject { JSON::JWK::Set::Fetcher.fetch jwks_uri, kid: kid, **options }
it_behaves_like :receive_options_as_hash
end

context 'when kid & auto_detect are included in the given options' do
context 'as hash' do
subject { JSON::JWK::Set::Fetcher.fetch jwks_uri, **options.merge(kid: kid, auto_detect: true) }
it_behaves_like :receive_options_as_hash
end

context 'as keyward args' do
subject { JSON::JWK::Set::Fetcher.fetch jwks_uri, options.merge(kid: kid, auto_detect: true) }

if Gem::Version.create(RUBY_VERSION) >= Gem::Version.create(3.0)
it do
expect do
subject
end.to raise_error ArgumentError
end
else
it_behaves_like :receive_options_as_hash
end
end
end
end
end
end
end
end

0 comments on commit 3a81c85

Please sign in to comment.