Class: Auth0::Mixins::Validation::Algorithm::RS256

Inherits:
JWTAlgorithm
  • Object
show all
Includes:
HTTPProxy
Defined in:
lib/auth0/mixins/validation.rb

Overview

Represents the RS256 algorithm, which rely on public key certificates.

Constant Summary collapse

@@cache =
Zache.new.freeze

Instance Attribute Summary

Attributes included from HTTPProxy

#base_uri, #headers, #timeout

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HTTPProxy

#add_headers, #call, #safe_parse_json, #url

Constructor Details

#initialize(jwks_url, lifetime) ⇒ RS256

Returns a new instance of RS256.


279
280
281
282
283
284
285
286
# File 'lib/auth0/mixins/validation.rb', line 279

def initialize(jwks_url, lifetime)
  raise Auth0::InvalidParameter, 'Must supply a valid jwks_url' if jwks_url.to_s.empty?
  raise Auth0::InvalidParameter, 'Must supply a valid lifetime' unless lifetime.is_a?(Integer) && lifetime >= 0

  @lifetime = lifetime
  @jwks_url = jwks_url
  @did_fetch_jwks = false
end

Class Method Details

.jwks_url(url, lifetime: 10 * 60) ⇒ RS256

Create a new instance passing the JWK set url.

Parameters:

  • url (string)

    The url where the JWK set is located.

  • lifetime (integer) (defaults to: 10 * 60)

    The lifetime of the JWK set in-memory cache in seconds. Must be a non-negative value. Defaults to *600 seconds* (10 minutes).

Returns:

  • (RS256)

    A new instance.


269
270
271
# File 'lib/auth0/mixins/validation.rb', line 269

def jwks_url(url, lifetime: 10 * 60)
  new url, lifetime
end

.remove_jwksObject

Clear the JWK set cache.


274
275
276
# File 'lib/auth0/mixins/validation.rb', line 274

def remove_jwks
  @@cache.remove(:jwks)
end

Instance Method Details

#fetched_jwks?boolean

Returns whether or not the JWK set was fetched from the url.

Returns:

  • (boolean)

    true if a request to the JWK set url was made, false otherwise.


317
318
319
# File 'lib/auth0/mixins/validation.rb', line 317

def fetched_jwks?
  @did_fetch_jwks
end

#jwks(force: false) ⇒ hash

Fetches the JWK set from the in-memory cache or from the url.

Returns:

  • (hash)

    A JWK set.


296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/auth0/mixins/validation.rb', line 296

def jwks(force: false)
  result = fetch_jwks if force

  if result
    @@cache.put(:jwks, result, lifetime: @lifetime)
    return result
  end

  previous_value = @@cache.last(:jwks)

  @@cache.get(:jwks, lifetime: @lifetime, dirty: true) do
    new_value = fetch_jwks

    raise Auth0::InvalidIdToken, 'Could not fetch the JWK set' unless new_value || previous_value

    new_value || previous_value
  end
end

#namestring

Returns the algorithm name.

Returns:

  • (string)

    The algorithm name.


290
291
292
# File 'lib/auth0/mixins/validation.rb', line 290

def name
  'RS256'
end