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.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/reso_transport/authentication/fetch_token_auth.rb', line 12

def initialize(options)
  super()

  @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)
  @username      = options.fetch(:username, nil)
  @password      = options.fetch(:password, nil)
  @request       = nil
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

#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

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
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

#usernameObject (readonly)

Returns the value of attribute username.



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

def username
  @username
end

Instance Method Details

#authenticateObject

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/reso_transport/authentication/fetch_token_auth.rb', line 34

def authenticate
  response = connection.post(nil, auth_params { |req| @request = req })
  json = JSON.parse response.body

  raise AccessDenied.new(request, response, 'token') unless response.success?

  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

#connectionObject



25
26
27
28
29
30
31
32
# File 'lib/reso_transport/authentication/fetch_token_auth.rb', line 25

def connection
  @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

#requestObject



47
48
49
50
51
# File 'lib/reso_transport/authentication/fetch_token_auth.rb', line 47

def request
  return @request.to_h if @request.respond_to? :to_h

  {}
end