Class: NubankSdk::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/nubank_sdk/auth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cpf:, device_id: nil, api_routes: nil, connection_adapter: nil) ⇒ Auth

Auth method to connect with the nubank api

Parameters:

  • cpf (String)

    the cpf to authenticate

  • device_id (String) (defaults to: nil)

    the device id to authenticate

  • api_routes (NubankSdk::ApiRoutes) (defaults to: nil)

    the api routes to connect

  • adapter ([Symbol, Faraday::Adapter::Test::Stubs])

    the adapter to connect



12
13
14
15
16
17
18
# File 'lib/nubank_sdk/auth.rb', line 12

def initialize(cpf:, device_id: nil, api_routes: nil, connection_adapter: nil)
  @cpf = cpf
  @device_id = device_id || generate_device_id
  @api_routes = api_routes || NubankSdk::ApiRoutes.new

  @connection_adapter = connection_adapter
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



3
4
5
# File 'lib/nubank_sdk/auth.rb', line 3

def access_token
  @access_token
end

#refresh_beforeObject (readonly)

Returns the value of attribute refresh_before.



3
4
5
# File 'lib/nubank_sdk/auth.rb', line 3

def refresh_before
  @refresh_before
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



3
4
5
# File 'lib/nubank_sdk/auth.rb', line 3

def refresh_token
  @refresh_token
end

Instance Method Details

#authenticate_with_certificate(password) ⇒ NubankSdk::ApiRoutes

Authenticate with the nubank api to get a new access token

Parameters:

  • password (String)

    the password to authenticate

Returns:



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/nubank_sdk/auth.rb', line 34

def authenticate_with_certificate(password)
  token_url = @api_routes.entrypoint(path: :app, entrypoint: :token)
  response = ssl_connection.post(token_url, token_payload(password))

  response_hash = Client.get_body(response)

  @refresh_token = response_hash[:refresh_token]
  @refresh_before = response_hash[:refresh_before]
  @access_token = response_hash[:access_token]

  update_api_routes(response_hash[:_links])
end

#certificateNubankSdk::Certificate

Return the instance of user certificate

Returns:



24
25
26
# File 'lib/nubank_sdk/auth.rb', line 24

def certificate
  @certificate ||= NubankSdk::Certificate.new(@cpf)
end

#exchange_certs(email_code, password) ⇒ File

Verify communication with the nubank api

Returns:

  • (File)

    the certificate file



66
67
68
69
70
71
72
73
74
75
# File 'lib/nubank_sdk/auth.rb', line 66

def exchange_certs(email_code, password)
  response = default_connection.post(@gen_certificate_path, payload(password).merge({
      code: email_code,
      'encrypted-code': @encrypted_code
    })
  )

  response_data = Client.get_body(response)
  certificate.process_decoded(key, response_data[:certificate])
end

#request_email_code(password) ⇒ String

Request to nubank api to generate a new certificate

Parameters:

  • password (String)

    the password to authenticate

Returns:

  • (String)

    email was has been received the code



53
54
55
56
57
58
59
60
# File 'lib/nubank_sdk/auth.rb', line 53

def request_email_code(password)
  response = default_connection.post(@gen_certificate_path, payload(password))

  response_parsed = parse_authenticate_headers(response.headers['WWW-Authenticate'])
  @encrypted_code = response_parsed[:device_authorization_encrypted_code]

  response_parsed[:sent_to]
end