Module: JSON::JWK::Set::Fetcher

Defined in:
lib/json/jwk/set/fetcher.rb

Defined Under Namespace

Classes: Cache

Class Method Summary collapse

Class Method Details

.cacheObject



56
57
58
# File 'lib/json/jwk/set/fetcher.rb', line 56

def self.cache
  @@cache
end

.cache=(cache) ⇒ Object



53
54
55
# File 'lib/json/jwk/set/fetcher.rb', line 53

def self.cache=(cache)
  @@cache = cache
end

.debug(&block) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/json/jwk/set/fetcher.rb', line 31

def self.debug(&block)
  original = self.debugging?
  debug!
  yield
ensure
  self.debugging = original
end

.debug!Object



28
29
30
# File 'lib/json/jwk/set/fetcher.rb', line 28

def self.debug!
  self.debugging = true
end

.debugging=(boolean) ⇒ Object



25
26
27
# File 'lib/json/jwk/set/fetcher.rb', line 25

def self.debugging=(boolean)
  @@debugging = boolean
end

.debugging?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/json/jwk/set/fetcher.rb', line 22

def self.debugging?
  @@debugging
end

.fetch(jwks_uri, kid:, auto_detect: true, **options) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/json/jwk/set/fetcher.rb', line 61

def self.fetch(jwks_uri, kid:, auto_detect: true, **options)
  cache_key = [
    'json:jwk:set',
    OpenSSL::Digest::MD5.hexdigest(jwks_uri),
    kid
  ].collect(&:to_s).join(':')

  jwks = Set.new(
    JSON.parse(
      cache.fetch(cache_key, options) do
        http_client.get(jwks_uri).body
      end
    )
  )
  cache.delete(cache_key, options) if jwks[kid].blank?

  if auto_detect
    jwks[kid] or raise KidNotFound
  else
    jwks
  end
end

.http_clientObject



40
41
42
43
44
45
46
47
48
# File 'lib/json/jwk/set/fetcher.rb', line 40

def self.http_client
  Faraday.new(headers: {user_agent: "JSON::JWK::Set::Fetcher #{VERSION}"}) do |faraday|
    faraday.response :raise_error
    faraday.response :follow_redirects
    faraday.response :logger, JSON::JWK::Set::Fetcher.logger if debugging?
    faraday.adapter Faraday.default_adapter
    http_config.try(:call, faraday)
  end
end

.http_config(&block) ⇒ Object



49
50
51
# File 'lib/json/jwk/set/fetcher.rb', line 49

def self.http_config(&block)
  @@http_config ||= block
end

.loggerObject



13
14
15
# File 'lib/json/jwk/set/fetcher.rb', line 13

def self.logger
  @@logger
end

.logger=(logger) ⇒ Object



16
17
18
# File 'lib/json/jwk/set/fetcher.rb', line 16

def self.logger=(logger)
  @@logger = logger
end