Class: ResoTransport::Authentication::FetchTokenAuth
- Inherits:
-
AuthStrategy
- Object
- AuthStrategy
- ResoTransport::Authentication::FetchTokenAuth
- Defined in:
- lib/reso_transport/authentication/fetch_token_auth.rb
Instance Attribute Summary collapse
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
readonly
Returns the value of attribute client_secret.
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#grant_type ⇒ Object
readonly
Returns the value of attribute grant_type.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Attributes inherited from AuthStrategy
Instance Method Summary collapse
- #authenticate ⇒ Object
- #connection ⇒ Object
-
#initialize(options) ⇒ FetchTokenAuth
constructor
A new instance of FetchTokenAuth.
- #request ⇒ Object
Methods inherited from AuthStrategy
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() super() @grant_type = .fetch(:grant_type, 'client_credentials') @scope = .fetch(:scope, 'api') @client_id = .fetch(:client_id) @client_secret = .fetch(:client_secret) @endpoint = .fetch(:endpoint) @username = .fetch(:username, nil) @password = .fetch(:password, nil) @request = nil end |
Instance Attribute Details
#client_id ⇒ Object (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_secret ⇒ Object (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 |
#endpoint ⇒ Object (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_type ⇒ Object (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 |
#password ⇒ Object (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 |
#scope ⇒ Object (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 |
#username ⇒ Object (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
#authenticate ⇒ Object
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 |
#connection ⇒ Object
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 |
#request ⇒ Object
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 |