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, #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.

Parameters:

  • options (Hash)
  • options[:client_id] (Hash)

    a customizable set of options

  • options[:client_secret] (Hash)

    a customizable set of options

  • options[:redirect_uri] (Hash)

    a customizable set of options

  • options[:test_mode] (Hash)

    a customizable set of options

  • options[:mode] (Hash)

    a customizable set of options



22
23
24
25
26
27
28
29
30
# File 'lib/smartcar/auth_client.rb', line 22

def initialize(options)
  options[:redirect_uri] ||= get_config('SMARTCAR_REDIRECT_URI')
  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.

Parameters:

  • code (String)

    This is the code that is returned after user

  • options (Hash) (defaults to: {})
  • options[:flags] (Hash)

    a customizable set of options

Returns:

  • (Hash)

    Hash of token, refresh token, expiry info and token type



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/smartcar/auth_client.rb', line 71

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

Parameters:

  • token (String)

    refresh_token received during token exchange

  • options (Hash) (defaults to: {})
  • options[:flags] (Hash)

    a customizable set of options

Returns:

  • (Hash)

    Hash of token, refresh token, expiry info and token type



89
90
91
92
93
94
95
96
97
98
# File 'lib/smartcar/auth_client.rb', line 89

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

Parameters:

  • expires_at (Number)

    expires_at as time since epoch

Returns:

  • (Boolean)


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.

Parameters:

  • scope (Array<String>)

    Array of permissions that specify what the user can access EXAMPLE : ['read_odometer', 'read_vehicle_info', 'required:read_location']

  • options (Hash) (defaults to: {})
  • options[:force_prompt] (Hash)

    a customizable set of options

  • options[:single_select] (Hash)

    a customizable set of options

  • options[:state] (Hash)

    a customizable set of options

  • options[:make_bypass] (Hash)

    a customizable set of options

  • options[:flags] (Hash)

    a customizable set of options

Returns:

  • (String)

    Authorization URL string



58
59
60
61
62
# File 'lib/smartcar/auth_client.rb', line 58

def get_auth_url(scope, options = {})
  initialize_auth_parameters(scope, options)
  add_single_select_options(options[:single_select])
  connect_client.auth_code.authorize_url(@auth_parameters)
end