lib/nubank_sdk/auth.rb in nubank_sdk-0.4.1 vs lib/nubank_sdk/auth.rb in nubank_sdk-0.5.0

- old
+ new

@@ -2,25 +2,20 @@ module NubankSdk class Auth attr_reader :refresh_token, :refresh_before, :access_token - def initialize(cpf:, device_id:, key: nil, api_routes: nil, adapter: nil) + def initialize(cpf:, device_id: nil, api_routes: nil, connection_adapter: nil) @cpf = cpf - @device_id = device_id - @key = key || generate_key + @device_id = device_id || generate_device_id @api_routes = api_routes || NubankSdk::ApiRoutes.new - @adapter = adapter + @connection_adapter = connection_adapter end - def api_routes - @api_routes - end - def certificate - @certificate ||= NubankSdk::Certificate.new(@cpf, @key) + @certificate ||= NubankSdk::Certificate.new(@cpf) end def authenticate_with_certificate(password) token_url = @api_routes.entrypoint(path: :app, entrypoint: :token) response = ssl_connection.post(token_url, token_payload(password)) @@ -49,11 +44,11 @@ 'encrypted-code': @encrypted_code }) ) response_data = Client.get_body(response) - certificate.process_decoded response_data[:certificate] + certificate.process_decoded(key, response_data[:certificate]) end private def parse_authenticate_headers(header_content) @@ -72,11 +67,11 @@ def payload(password) { login: @cpf, password: password, - public_key: @key.public_key.to_pem, + public_key: key.public_key.to_pem, device_id: @device_id, model: "NubankSdk Client (#@device_id)", } end @@ -108,30 +103,38 @@ end def find_url(keys, list) links_keys = list.keys - keys.each do |key| - return list[key]['href'] if links_keys.include?(key) + keys.each do |url_key| + return list[url_key]['href'] if links_keys.include?(url_key) end '' end - def prepare_connections + def prepare_default_connection uri, @gen_certificate_path = @api_routes.entrypoint( path: :app, entrypoint: :gen_certificate, type: :splitted ) - Client::HTTP.new(uri, @adapter) + Client::HTTP.new(uri, @connection_adapter) end def default_connection - @default_connection ||= prepare_connections + @default_connection ||= prepare_default_connection end def ssl_connection - @ssl_connection ||= Client::HTTPS.new(certificate.encoded, @adapter) + @ssl_connection ||= Client::HTTPS.new(certificate.encoded, @connection_adapter) + end + + def key + @key ||= generate_key + end + + def generate_device_id + SecureRandom.uuid.split('-').last end end end