nubank_sdk 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8e5b9021cf033b5ec98a45bdd97701e6acb0d4e7f7ea12d2fe8247e91fb24dd7
4
- data.tar.gz: 9faacb99367c3fd68c1d93dc343a2dac43efd90f2c27291d6ab67cdb478fcc8a
3
+ metadata.gz: b37684fbfd690e5b972c3626514ef706766af1279c9c1f4049310f94f31e90b9
4
+ data.tar.gz: 6052b2f3a8cc2d879960414d415898b9c5bbc6eb66a7aafad7003788d38696f7
5
5
  SHA512:
6
- metadata.gz: 7afd83c07054626508d65dc3b028a09aaf07f0625f81cf7f4b2f53b121fc34d43a89f269a6cedc9a3e064af09f29e5e68b3a2f79a5ecacb845d34bf1576d23d7
7
- data.tar.gz: c99a7db8b4c2fcb29a7331009dcd2cd2bc3c9a4256153e5fc7c188dac3689179607c52fafe8d234d66295158dce2a2c2989e17388b7a2ca4f609c85dca513ac0
6
+ metadata.gz: e659147f1a8eb81e4442a4268c915e3391e337e44a81a59c7faecbcf4e9eaaf0de4e1abc2650de1e91aa5226d680b58b5b19b277e13b2bbc0fd3999076dcdaab
7
+ data.tar.gz: e362fd35b8ab55e31ec48740369570b964b56990bfe51940a61ea03d5f22518bd08b8cb7fa919b6265a1dbc184271a09557352f3844b44b719ee2f12501d4b31
@@ -2,10 +2,14 @@
2
2
  "cSpell.words": [
3
3
  "faraday",
4
4
  "nubank",
5
- "Nubank"
5
+ "Nubank",
6
+ "pkcs"
6
7
  ],
7
8
  "conventionalCommits.scopes": [
9
+ "api routes",
8
10
  "certificates",
11
+ "auth",
12
+ "client",
9
13
  "version"
10
14
  ]
11
15
  }
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nubank_sdk (0.3.0)
4
+ nubank_sdk (0.4.0)
5
5
  faraday (~> 0.15.0)
6
6
  json (~> 2.1.0)
7
7
 
@@ -51,10 +51,7 @@ module NubankSdk
51
51
  end
52
52
 
53
53
  def connection
54
- @connection ||= Faraday.new(url: DISCOVERY_URI) do |faraday|
55
- faraday.adapter(*@connection_adapter) if @connection_adapter
56
- faraday.adapter Faraday.default_adapter unless @connection_adapter
57
- end
54
+ @connection ||= Client::HTTP.new(DISCOVERY_URI, @connection_adapter)
58
55
  end
59
56
  end
60
57
  end
@@ -0,0 +1,135 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NubankSdk
4
+ class Auth
5
+ attr_reader :refresh_token, :refresh_before, :access_token
6
+
7
+ def initialize(cpf:, device_id:, key: nil, api_routes: nil, adapter: nil)
8
+ @cpf = cpf
9
+ @device_id = device_id
10
+ @key = key || generate_key
11
+ @api_routes = api_routes || NubankSdk::ApiRoutes.new
12
+
13
+ @adapter = adapter
14
+ end
15
+
16
+ def certificate
17
+ @certificate ||= NubankSdk::Certificate.new(@cpf, @key)
18
+ end
19
+
20
+ def authenticate_with_certificate(password)
21
+ token_url = @api_routes.entrypoint(path: :app, entrypoint: :token)
22
+ response = ssl_connection.post(token_url, token_payload(password))
23
+
24
+ response_hash = Client.get_body(response)
25
+
26
+ @refresh_token = response_hash[:refresh_token]
27
+ @refresh_before = response_hash[:refresh_before]
28
+ @access_token = response_hash[:access_token]
29
+
30
+ update_api_routes(response_hash[:_links])
31
+ end
32
+
33
+ def request_email_code(password)
34
+ response = default_connection.post(@gen_certificate_path, payload(password))
35
+
36
+ response_parsed = parse_authenticate_headers(response.headers['WWW-Authenticate'])
37
+ @encrypted_code = response_parsed[:device_authorization_encrypted_code]
38
+
39
+ response_parsed[:sent_to]
40
+ end
41
+
42
+ def exchange_certs(email_code, password)
43
+ response = default_connection.post(@gen_certificate_path, payload(password).merge({
44
+ code: email_code,
45
+ 'encrypted-code': @encrypted_code
46
+ })
47
+ )
48
+
49
+ response_data = Client.get_body(response)
50
+ certificate.process_decoded response_data[:certificate]
51
+ end
52
+
53
+ private
54
+
55
+ def parse_authenticate_headers(header_content)
56
+ chunks = header_content.split(',')
57
+ parsed = {}
58
+
59
+ chunks.each do |chunk|
60
+ key, value = chunk.split('=')
61
+ key = key.strip().gsub(' ', '_').gsub('-', '_').to_sym
62
+ value = value.gsub('"', '')
63
+ parsed[key] = value
64
+ end
65
+
66
+ parsed
67
+ end
68
+
69
+ def payload(password)
70
+ {
71
+ login: @cpf,
72
+ password: password,
73
+ public_key: @key.public_key.to_pem,
74
+ device_id: @device_id,
75
+ model: "NubankSdk Client (#@device_id)",
76
+ }
77
+ end
78
+
79
+ def token_payload(password)
80
+ {
81
+ 'grant_type': 'password',
82
+ 'client_id': 'legacy_client_id',
83
+ 'client_secret': 'legacy_client_secret',
84
+ 'login': @cpf,
85
+ 'password': password
86
+ }
87
+ end
88
+
89
+ def generate_key
90
+ OpenSSL::PKey::RSA.new 2048
91
+ end
92
+
93
+ def update_api_routes(links)
94
+ feed_url_keys = ['events', 'magnitude']
95
+ bills_url_keys = ['bills_summary']
96
+ customer_url_keys = ['customer']
97
+ account_url_keys = ['account']
98
+
99
+ @api_routes.add_entrypoint(:ssl, :revoke_token, links[:revoke_token][:href])
100
+ @api_routes.add_entrypoint(:ssl, :query, links[:ghostflame][:href])
101
+ @api_routes.add_entrypoint(:ssl, :feed, find_url(feed_url_keys, links))
102
+ @api_routes.add_entrypoint(:ssl, :bills, find_url(bills_url_keys, links))
103
+ @api_routes.add_entrypoint(:ssl, :customer, find_url(customer_url_keys, links))
104
+ @api_routes.add_entrypoint(:ssl, :account, find_url(account_url_keys, links))
105
+ @api_routes
106
+ end
107
+
108
+ def find_url(keys, list)
109
+ links_keys = list.keys
110
+
111
+ keys.each do |key|
112
+ return list[key]['href'] if links_keys.include?(key)
113
+ end
114
+ ''
115
+ end
116
+
117
+ def prepare_connections
118
+ uri, @gen_certificate_path = @api_routes.entrypoint(
119
+ path: :app,
120
+ entrypoint: :gen_certificate,
121
+ type: :splitted
122
+ )
123
+
124
+ Client::HTTP.new(uri, @adapter)
125
+ end
126
+
127
+ def default_connection
128
+ @default_connection ||= prepare_connections
129
+ end
130
+
131
+ def ssl_connection
132
+ @ssl_connection ||= Client::HTTPS.new(certificate.encoded, @adapter)
133
+ end
134
+ end
135
+ end
@@ -1,11 +1,51 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'faraday'
3
4
  require 'json'
4
5
 
5
6
  module NubankSdk
6
- class Client
7
+ module Client
7
8
  def self.get_body(response)
8
9
  JSON.parse(response.body, symbolize_names: true)
9
10
  end
11
+
12
+ class HTTP
13
+ def initialize(base_url, adapter = nil)
14
+ @connection = Faraday.new(url: base_url) do |faraday|
15
+ faraday.adapter *adapter if adapter
16
+ faraday.adapter Faraday.default_adapter unless adapter
17
+ end
18
+ end
19
+
20
+ def post(path, body)
21
+ @connection.post(path) do |req|
22
+ req.headers['Content-Type'] = 'application/json'
23
+ req.body = body.to_json
24
+ end
25
+ end
26
+
27
+ def get(path)
28
+ @connection.get(path)
29
+ end
30
+ end
31
+
32
+ class HTTPS
33
+ def initialize(certificate, adapter = nil)
34
+ client_cert = OpenSSL::X509::Certificate.new(certificate.certificate),
35
+ client_key = OpenSSL::PKey::RSA.new(certificate.key)
36
+
37
+ @connection = Faraday.new(ssl: { client_cert: client_cert, client_key: client_key}) do |faraday|
38
+ faraday.adapter *adapter if adapter
39
+ faraday.adapter Faraday.default_adapter unless adapter
40
+ end
41
+ end
42
+
43
+ def post(url, body)
44
+ @connection.post(url) do |req|
45
+ req.headers['Content-Type'] = 'application/json'
46
+ req.body = body.to_json
47
+ end
48
+ end
49
+ end
10
50
  end
11
51
  end
@@ -1,3 +1,3 @@
1
1
  module NubankSdk
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/lib/nubank_sdk.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require "nubank_sdk/api_routes"
2
+ require "nubank_sdk/auth"
2
3
  require "nubank_sdk/certificate"
4
+ require "nubank_sdk/client"
3
5
  require "nubank_sdk/version"
4
6
 
5
7
  module NubankSdk
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nubank_sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Viserion77
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-22 00:00:00.000000000 Z
11
+ date: 2022-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -101,6 +101,7 @@ files:
101
101
  - certificates/.gitkeep
102
102
  - lib/nubank_sdk.rb
103
103
  - lib/nubank_sdk/api_routes.rb
104
+ - lib/nubank_sdk/auth.rb
104
105
  - lib/nubank_sdk/certificate.rb
105
106
  - lib/nubank_sdk/client.rb
106
107
  - lib/nubank_sdk/version.rb