Class: Warden::Auth0::Strategy

Inherits:
Strategies::Base
  • Object
show all
Extended by:
Dry::Configurable
Defined in:
lib/warden/auth0/strategy.rb

Overview

Warden strategy to authenticate a user through a JWT token in the request header (see Warden::Auth0.config.token_header).

Configure issuer, aud, algorithm on the strategy before adding to Warden.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.connectionObject



129
130
131
132
133
# File 'lib/warden/auth0/strategy.rb', line 129

def self.connection
  Faraday.new(request: { timeout: 5 }, ssl: { verify: config.verify_ssl }) do |conn|
    conn.response :json
  end
end

.fetch_jwks(jwks_url) ⇒ Object

Fetches JWKS from the given URL.



119
120
121
122
123
124
125
126
127
# File 'lib/warden/auth0/strategy.rb', line 119

def self.fetch_jwks(jwks_url)
  puts "Fetching JWKS from #{jwks_url}"
  raise 'No url provided for fetching jwks' if jwks_url.nil?
  jwks_response = self.connection.get(jwks_url).body
  jwks = JWT::JWK::Set.new(jwks_response)
  jwks.select { |key| key[:use] == 'sig' }
rescue StandardError => e
  raise "Failed to fetch JWKS: #{e.message}"
end

Instance Method Details

#authenticate!Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/warden/auth0/strategy.rb', line 31

def authenticate!
  raise Errors::WrongIssuer, 'wrong issuer' unless issuer_claim_valid?
  raise Errors::WrongAud, 'wrong audience' unless aud_claim_valid?

  resolver_method = "#{scope}_resolver"
  raise "unimplemented resolver #{resolver_method}" unless respond_to?(resolver_method)

  user = send(resolver_method, decoded_token)
  raise Warden::Auth0::Errors::NilUser, 'nil user' unless user

  success!(user)
rescue JWT::DecodeError => e
  puts "Failing to authenticate with #{e.message}"
  fail!(e.message)
end

#store?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/warden/auth0/strategy.rb', line 27

def store?
  false
end

#valid?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/warden/auth0/strategy.rb', line 23

def valid?
  token_exists? && issuer_claim_valid? && aud_claim_valid?
end