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_or_options = {}, 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, #build_v3_response, #convert_path_to_attribute, #convert_to_ostruct_recursively, #deep_transform_keys_to_snake_case, #determine_mode, #get_config, #handle_error, #json_to_ostruct, #parse_date_safely, #process_batch_response, #stringify_params, #to_snake_case
Constructor Details
#initialize(options) ⇒ Smartcar::AuthClient
Constructor for a client object
application settings. The given URL must exactly match one of the registered URLs. This parameter is optional and should normally be set within the Smartcar Dashboard. Launch Smartcar Connect in test mode. Should be one of test, live or simulated.
24 25 26 27 28 29 30 31 32 |
# File 'lib/smartcar/auth_client.rb', line 24 def initialize() [:redirect_uri] ||= get_config('SMARTCAR_REDIRECT_URI', { nullable: true }) [: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.
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/smartcar/auth_client.rb', line 85 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
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/smartcar/auth_client.rb', line 103 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
118 119 120 |
# File 'lib/smartcar/auth_client.rb', line 118 def expired?(expires_at) OAuth2::AccessToken.from_hash(auth_client, { expires_at: expires_at }).expired? end |
#get_auth_url(scope_or_options = {}, options = {}) ⇒ String
Generate the OAuth authorization URL.
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_selectlimits 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. is used to aggregate analytics across Connect sessions for each vehicle owner.
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/smartcar/auth_client.rb', line 64 def get_auth_url( = {}, = {}) scope = nil if .is_a?(Array) scope = ||= {} elsif .is_a?(Hash) = end initialize_auth_parameters(scope, ) ([:single_select]) connect_client.auth_code.(@auth_parameters) end |