Module: PQCrypto::JWT::JWKS

Defined in:
lib/pq_crypto/jwt/jwks.rb

Class Method Summary collapse

Class Method Details

.find(jwks, kid: nil, alg: nil) ⇒ Object



16
17
18
19
20
21
# File 'lib/pq_crypto/jwt/jwks.rb', line 16

def find(jwks, kid: nil, alg: nil)
  keys_from(jwks).find do |key|
    (kid.nil? || value_for(key, "kid") == kid) &&
      (alg.nil? || value_for(key, "alg") == alg)
  end
end

.from_keys(public_keys, kids: nil) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/pq_crypto/jwt/jwks.rb', line 8

def from_keys(public_keys, kids: nil)
  keys = Array(public_keys).each_with_index.map do |public_key, index|
    kid = kids&.fetch(index, nil)
    PQCrypto::JWT::JWK.from_public_key(public_key, kid: kid)
  end
  { "keys" => keys }.freeze
end

.loader(jwks_hash_or_callable) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pq_crypto/jwt/jwks.rb', line 23

def loader(jwks_hash_or_callable)
  cached = nil
  lambda do |options = {}|
    if jwks_hash_or_callable.respond_to?(:call)
      cached = nil if options && options[:invalidate]
      cached ||= jwks_hash_or_callable.call(options || {})
    else
      cached = nil if options && options[:invalidate]
      cached ||= jwks_hash_or_callable
    end
  end
end