Class: ResoTransport::Authentication::FetchTokenAuth

Inherits:
AuthStrategy
  • Object
show all
Defined in:
lib/reso_transport/authentication/fetch_token_auth.rb

Instance Attribute Summary collapse

Attributes inherited from AuthStrategy

#access

Instance Method Summary collapse

Methods inherited from AuthStrategy

#ensure_valid_access!, #reset

Constructor Details

#initialize(options) ⇒ FetchTokenAuth

Returns a new instance of FetchTokenAuth.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/reso_transport/authentication/fetch_token_auth.rb', line 6

def initialize(options)
  @grant_type    = options.fetch(:grant_type, "client_credentials")
  @scope         = options.fetch(:scope, "api")
  @client_id     = options.fetch(:client_id)
  @client_secret = options.fetch(:client_secret)
  @endpoint      = options.fetch(:endpoint)

  @connection = Faraday.new(@endpoint) do |faraday|
    faraday.request  :url_encoded
    faraday.response :logger, ResoTransport.configuration.logger if ResoTransport.configuration.logger
    faraday.adapter Faraday.default_adapter
    faraday.basic_auth @client_id, @client_secret
  end
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



4
5
6
# File 'lib/reso_transport/authentication/fetch_token_auth.rb', line 4

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



4
5
6
# File 'lib/reso_transport/authentication/fetch_token_auth.rb', line 4

def client_secret
  @client_secret
end

#connectionObject (readonly)

Returns the value of attribute connection.



4
5
6
# File 'lib/reso_transport/authentication/fetch_token_auth.rb', line 4

def connection
  @connection
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



4
5
6
# File 'lib/reso_transport/authentication/fetch_token_auth.rb', line 4

def endpoint
  @endpoint
end

#grant_typeObject (readonly)

Returns the value of attribute grant_type.



4
5
6
# File 'lib/reso_transport/authentication/fetch_token_auth.rb', line 4

def grant_type
  @grant_type
end

#scopeObject (readonly)

Returns the value of attribute scope.



4
5
6
# File 'lib/reso_transport/authentication/fetch_token_auth.rb', line 4

def scope
  @scope
end

Instance Method Details

#authenticateObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/reso_transport/authentication/fetch_token_auth.rb', line 21

def authenticate
  response = connection.post nil, auth_params
  json = JSON.parse response.body

  unless response.success?
    message = "#{response.reason_phrase}: #{json['error'] || response.body}"
    raise ResoTransport::AccessDenied, response: response, message: message
  end

  Access.new({
    access_token: json.fetch('access_token'),
    expires_in: json.fetch('expires_in', 1 << (1.size * 8 - 2) - 1),
    token_type: json.fetch('token_type', "Bearer")
  })
end