Class: Smartcar::AuthClient

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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(options)
  options[:redirect_uri] ||= get_config('SMARTCAR_REDIRECT_URI', { nullable: true })
  options[:client_id] ||= get_config('SMARTCAR_CLIENT_ID')
  options[:client_secret] ||= get_config('SMARTCAR_CLIENT_SECRET')
  options[:auth_origin] = ENV['SMARTCAR_AUTH_ORIGIN'] || AUTH_ORIGIN
  options[:connect_origin] = ENV['SMARTCAR_CONNECT_ORIGIN'] || CONNECT_ORIGIN
  options[:mode] = determine_mode(options[:test_mode], options[:mode]) || 'live'
  super
end

Instance Attribute Details

#auth_originObject (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_idObject (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_secretObject (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_originObject (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

#flagsObject (readonly)

Returns the value of attribute flags.



9
10
11
# File 'lib/smartcar/auth_client.rb', line 9

def flags
  @flags
end

#modeObject (readonly)

Returns the value of attribute mode.



9
10
11
# File 'lib/smartcar/auth_client.rb', line 9

def mode
  @mode
end

#redirect_uriObject (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

#scopeObject (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, options = {})
  set_token_url(options[: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, options = {})
  set_token_url(options[: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_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. 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_or_options = {}, options = {})
  scope = nil
  if scope_or_options.is_a?(Array)
    scope = scope_or_options
    options ||= {}
  elsif scope_or_options.is_a?(Hash)
    options = scope_or_options
  end

  initialize_auth_parameters(scope, options)
  add_single_select_options(options[:single_select])
  connect_client.auth_code.authorize_url(@auth_parameters)
end