Class: Youlend::Auth

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/youlend/auth.rb

Constant Summary collapse

AUTH_URLS =
{
  production: 'https://youlend.eu.auth0.com',
  development: 'https://youlend-stag.eu.auth0.com'
}.freeze
AUDIENCES =
%i[prequalification onboarding].freeze
DEFAULT_AUDIENCE =
:prequalification

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAuth

Returns a new instance of Auth.



20
21
22
# File 'lib/youlend/auth.rb', line 20

def initialize
  @configuration = Youlend.configuration
end

Class Method Details

.request_token(audience = DEFAULT_AUDIENCE) ⇒ Object



24
25
26
# File 'lib/youlend/auth.rb', line 24

def self.request_token(audience = DEFAULT_AUDIENCE)
  new.request_token(audience)
end

Instance Method Details

#request_token(audience = DEFAULT_AUDIENCE) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/youlend/auth.rb', line 28

def request_token(audience = DEFAULT_AUDIENCE)
  raise 'Invalid Audience' unless AUDIENCES.include?(audience.to_sym)

  params = {
    grant_type: 'client_credentials',
    client_id: client_id,
    client_secret: client_secret,
    audience: "#{Youlend.configuration.domain}/#{audience}"
  }

  result = adapter.post('/oauth/token', params.to_json)

  json = result.body

  raise json[:error_description] if json[:error]

  @configuration.tokens[audience] = json[:access_token] unless json[:error]
end