Class: NubankSdk::Auth

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

Overview

Auth method to connect with the nubank api

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



19
20
21
22
23
24
25
26
27
# File 'lib/nubank_sdk/auth.rb', line 19

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 || ApiRoutes.new

  @connection_adapter = connection_adapter
  @p_key = OpenSSL::PKey::RSA.new 2048
  @encrypted_code = ''
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



10
11
12
# File 'lib/nubank_sdk/auth.rb', line 10

def access_token
  @access_token
end

#encrypted_codeObject

Returns the value of attribute encrypted_code.



10
11
12
# File 'lib/nubank_sdk/auth.rb', line 10

def encrypted_code
  @encrypted_code
end

#p_keyObject

Returns the value of attribute p_key.



10
11
12
# File 'lib/nubank_sdk/auth.rb', line 10

def p_key
  @p_key
end

#refresh_beforeObject

Returns the value of attribute refresh_before.



10
11
12
# File 'lib/nubank_sdk/auth.rb', line 10

def refresh_before
  @refresh_before
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



10
11
12
# File 'lib/nubank_sdk/auth.rb', line 10

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:



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/nubank_sdk/auth.rb', line 43

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, @refresh_before, @access_token = response_hash.values_at(:refresh_token, :refresh_before,
                                                                           :access_token)

  update_api_routes(response_hash[:_links])
end

#certificateNubankSdk::Certificate

Return the instance of user certificate

Returns:



33
34
35
# File 'lib/nubank_sdk/auth.rb', line 33

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

#exchange_certs(email_code, password, custom_encryption = nil) ⇒ File

Verify communication with the nubank api

Returns:

  • (File)

    the certificate file

Raises:



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/nubank_sdk/auth.rb', line 74

def exchange_certs(email_code, password, custom_encryption = nil)
  device_authorization_encrypted_code = custom_encryption || @encrypted_code || ''
  raise Errors::InvalidEncryptedCode if device_authorization_encrypted_code.empty?

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

  response_data = Client.get_body(response)
  certificate.process_decoded(@p_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



61
62
63
64
65
66
67
68
# File 'lib/nubank_sdk/auth.rb', line 61

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
end