Class: Smartcar::AuthClient
- Inherits:
-
Object
- Object
- Smartcar::AuthClient
- Includes:
- Utils
- Defined in:
- lib/smartcar/auth_client.rb
Overview
AuthClient class to take care of the Oauth 2.0 with Smartcar APIs
Instance Attribute Summary collapse
-
#auth_origin ⇒ Object
readonly
Returns the value of attribute auth_origin.
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
readonly
Returns the value of attribute client_secret.
-
#connect_origin ⇒ Object
readonly
Returns the value of attribute connect_origin.
-
#flags ⇒ Object
readonly
Returns the value of attribute flags.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#redirect_uri ⇒ Object
readonly
Returns the value of attribute redirect_uri.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Instance Method Summary collapse
-
#exchange_code(code, options = {}) ⇒ Hash
Generates the tokens hash using the code returned in oauth process.
-
#exchange_refresh_token(token, options = {}) ⇒ Hash
Refreshing the access token.
-
#expired?(expires_at) ⇒ Boolean
Checks if token is expired using Oauth2 classes.
-
#get_auth_url(scope, options = {}) ⇒ String
Generate the OAuth authorization URL.
-
#initialize(options) ⇒ Smartcar::AuthClient
constructor
Constructor for a client object.
Methods included from Utils
#build_aliases, #build_error, #build_meta, #build_response, #convert_path_to_attribute, #determine_mode, #get_config, #handle_error, #json_to_ostruct, #process_batch_response, #stringify_params
Constructor Details
#initialize(options) ⇒ Smartcar::AuthClient
Constructor for a client object
Launch Smartcar Connect in test mode. Should be one of test, live or simulated.
22 23 24 25 26 27 28 29 30 |
# File 'lib/smartcar/auth_client.rb', line 22 def initialize() [:redirect_uri] ||= get_config('SMARTCAR_REDIRECT_URI') [:client_id] ||= get_config('SMARTCAR_CLIENT_ID') [:client_secret] ||= get_config('SMARTCAR_CLIENT_SECRET') [:auth_origin] = ENV['SMARTCAR_AUTH_ORIGIN'] || AUTH_ORIGIN [:connect_origin] = ENV['SMARTCAR_CONNECT_ORIGIN'] || CONNECT_ORIGIN [:mode] = determine_mode([:test_mode], [:mode]) || 'live' super end |
Instance Attribute Details
#auth_origin ⇒ Object (readonly)
Returns the value of attribute auth_origin.
9 10 11 |
# File 'lib/smartcar/auth_client.rb', line 9 def auth_origin @auth_origin end |
#client_id ⇒ Object (readonly)
Returns the value of attribute client_id.
9 10 11 |
# File 'lib/smartcar/auth_client.rb', line 9 def client_id @client_id end |
#client_secret ⇒ Object (readonly)
Returns the value of attribute client_secret.
9 10 11 |
# File 'lib/smartcar/auth_client.rb', line 9 def client_secret @client_secret end |
#connect_origin ⇒ Object (readonly)
Returns the value of attribute connect_origin.
9 10 11 |
# File 'lib/smartcar/auth_client.rb', line 9 def connect_origin @connect_origin end |
#flags ⇒ Object (readonly)
Returns the value of attribute flags.
9 10 11 |
# File 'lib/smartcar/auth_client.rb', line 9 def flags @flags end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
9 10 11 |
# File 'lib/smartcar/auth_client.rb', line 9 def mode @mode end |
#redirect_uri ⇒ Object (readonly)
Returns the value of attribute redirect_uri.
9 10 11 |
# File 'lib/smartcar/auth_client.rb', line 9 def redirect_uri @redirect_uri end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
9 10 11 |
# File 'lib/smartcar/auth_client.rb', line 9 def scope @scope end |
Instance Method Details
#exchange_code(code, options = {}) ⇒ Hash
Generates the tokens hash using the code returned in oauth process. visits and authorizes on the authorization URL.
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/smartcar/auth_client.rb', line 71 def exchange_code(code, = {}) set_token_url([:flags]) token_hash = auth_client.auth_code .get_token(code, redirect_uri: redirect_uri) .to_hash json_to_ostruct(token_hash) rescue OAuth2::Error => e raise build_error(e.response.status, e.response.body, e.response.headers) end |
#exchange_refresh_token(token, options = {}) ⇒ Hash
Refreshing the access token
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/smartcar/auth_client.rb', line 89 def exchange_refresh_token(token, = {}) set_token_url([:flags]) token_object = OAuth2::AccessToken.from_hash(auth_client, { refresh_token: token }) token_object = token_object.refresh! json_to_ostruct(token_object.to_hash) rescue OAuth2::Error => e raise build_error(e.response.status, e.response.body, e.response.headers) end |
#expired?(expires_at) ⇒ Boolean
Checks if token is expired using Oauth2 classes
104 105 106 |
# File 'lib/smartcar/auth_client.rb', line 104 def expired?(expires_at) OAuth2::AccessToken.from_hash(auth_client, { expires_at: expires_at }).expired? end |
#get_auth_url(scope, options = {}) ⇒ String
Generate the OAuth authorization URL.
For further details refer to https://smartcar.com/docs/guides/scope/
true
will show the permissions approval screen on every authentication
attempt, even if the user has previously consented to the exact scope of
permissions.
behavior of the grant dialog displayed to the user. Object can contain two keys :
- enabled - Boolean value, if set to
true
,single_select
limits the user to selecting only one vehicle. - vin - String vin, if set, Smartcar will only authorize the vehicle with the specified VIN. See the Single Select guide for more information. redirect uri. This parameter may be used for identifying the user who initiated the request. users to bypass the car brand selection screen. For a complete list of supported makes, please see our API Reference documentation.
58 59 60 61 62 |
# File 'lib/smartcar/auth_client.rb', line 58 def get_auth_url(scope, = {}) initialize_auth_parameters(scope, ) ([:single_select]) connect_client.auth_code.(@auth_parameters) end |