Class: Ruze::Gigya

Inherits:
Object
  • Object
show all
Defined in:
lib/ruze/gigya.rb

Constant Summary collapse

BASE_URL =
'https://accounts.eu1.gigya.com'.freeze
SOCIALIZE_URL =
'https://socialize.eu1.gigya.com'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email, password, device: Device.new) ⇒ Gigya

Returns a new instance of Gigya.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
# File 'lib/ruze/gigya.rb', line 11

def initialize(email, password, device: Device.new)
  raise ArgumentError unless email.is_a?(String) && password.is_a?(String)

  @email = email
  @password = password
  @device = device
end

Instance Attribute Details

#deviceObject (readonly)

Returns the value of attribute device.



18
19
20
# File 'lib/ruze/gigya.rb', line 18

def device
  @device
end

#emailObject (readonly)

Returns the value of attribute email.



18
19
20
# File 'lib/ruze/gigya.rb', line 18

def email
  @email
end

#passwordObject (readonly)

Returns the value of attribute password.



18
19
20
# File 'lib/ruze/gigya.rb', line 18

def password
  @password
end

Instance Method Details

#jwtObject



27
28
29
30
31
32
33
34
35
# File 'lib/ruze/gigya.rb', line 27

def jwt
  @jwt ||= dig_from post(
    "#{BASE_URL}/accounts.getJWT",
    { 'apiKey'      => api_key,
      'login_token' => session_cookie_value,
      'fields'      => 'data.personId,data.gigyaDataCenter',
      'expiration'  => 900 }
  ), label: 'jwt', keys: %w[id_token]
end

#person_idObject



20
21
22
23
24
25
# File 'lib/ruze/gigya.rb', line 20

def person_id
  @person_id ||= dig_from post(
    "#{BASE_URL}/accounts.getAccountInfo",
    { 'apiKey' => api_key, 'login_token' => session_cookie_value }
  ), label: 'person_id', keys: %w[data personId]
end

#request_verification_codeObject

Triggers the email verification code and returns the obfuscated address it was sent to. Returns nil when no verification is needed – either the account has no 2FA or this device is already trusted.



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ruze/gigya.rb', line 61

def request_verification_code
  ids = device_ids
  reg_token = (ids)
  return nil unless reg_token

  assertion = init_tfa(reg_token, ids)
  mail = first_email(assertion, ids)
  phv_token = send_verification_code(mail['id'], assertion, ids)

  @bootstrap = { ids: ids, reg_token: reg_token, assertion: assertion, phv_token: phv_token }
  mail['obfuscated']
end

Logs in using the trusted device and returns the session cookie value. Raises TwoFactorRequired when Renault demands a fresh 2FA verification.



39
40
41
# File 'lib/ruze/gigya.rb', line 39

def session_cookie_value
  @session_cookie_value ||= 
end

#verify_code(code) ⇒ Object

Completes the 2FA flow with the emailed code, finalizes the device as trusted (remembered for ~30 days) and persists it.

Raises:



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ruze/gigya.rb', line 76

def verify_code(code)
  raise Error, 'Call request_verification_code before verify_code' unless @bootstrap

  ids = @bootstrap[:ids]
  provider_assertion = complete_verification(code, ids)
  finalize_tfa(provider_assertion, ids)

  @session_cookie_value = (ids)
  device.save(gmid: ids[:gmid], ucid: ids[:ucid])
  @session_cookie_value
end