nubank_sdk 0.4.1 → 0.5.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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +2 -0
- data/lib/nubank_sdk/account.rb +12 -33
- data/lib/nubank_sdk/auth.rb +20 -17
- data/lib/nubank_sdk/certificate.rb +5 -6
- data/lib/nubank_sdk/client.rb +12 -10
- data/lib/nubank_sdk/user.rb +41 -0
- data/lib/nubank_sdk/version.rb +1 -1
- data/usage_example.rb +5 -5
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ec434c6062ddfb9c5f4f85e39e991c1ebbd95add122bef24232b242172906bdb
|
4
|
+
data.tar.gz: d7c490ff9c027c0aa6e6f8045ea03b3a99a1f4e24a7294db032c9581486cae32
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f8e0d67ef141bd01cbdb7604183708792be2a146a441b09d5581a4be32698d88f7ce87386e71dcc9dbf009139b980958e934dbef6c97c6dc72fb3248e17d528
|
7
|
+
data.tar.gz: 49fbe8d14b4da3f83cb676755633c2174477696178e0925aa5f3a9796c9b459c2f40bdfb9668548bba5ea4aaafb037434f3094606c9650e4ed740402eb21e487
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
data/lib/nubank_sdk/account.rb
CHANGED
@@ -1,43 +1,22 @@
|
|
1
1
|
module NubankSdk
|
2
2
|
class Account
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
@cpf = cpf
|
7
|
-
@device_id = device_id || generate_device_id
|
8
|
-
|
9
|
-
@api_routes = NubankSdk::ApiRoutes.new(connection_adapter: adapter)
|
10
|
-
@adapter = adapter
|
11
|
-
@key = key
|
12
|
-
end
|
13
|
-
|
14
|
-
def auth
|
15
|
-
@auth ||= NubankSdk::Auth.new(
|
16
|
-
cpf: @cpf,
|
17
|
-
key: @key,
|
18
|
-
device_id: @device_id,
|
19
|
-
api_routes: @api_routes,
|
20
|
-
adapter: @adapter
|
21
|
-
)
|
3
|
+
def initialize(connection:, api_routes:)
|
4
|
+
@connection = connection
|
5
|
+
@api_routes = api_routes
|
22
6
|
end
|
23
7
|
|
24
|
-
def
|
25
|
-
query_url =
|
26
|
-
connection = Client::HTTPS.new(auth.certificate.encoded, @adapter)
|
8
|
+
def balance
|
9
|
+
query_url = @api_routes.entrypoint(path: :ssl, entrypoint: :query)
|
27
10
|
|
28
|
-
response = connection.post(
|
29
|
-
|
30
|
-
|
31
|
-
|
11
|
+
response = @connection.post(
|
12
|
+
query_url, {
|
13
|
+
'variables': {},
|
14
|
+
'query': '{viewer {savingsAccount {currentSavingsBalance {netAmount}}}}'
|
15
|
+
}
|
16
|
+
)
|
32
17
|
|
33
|
-
data =
|
18
|
+
data = NubankSdk::Client.get_body(response)
|
34
19
|
data[:data][:viewer][:savingsAccount][:currentSavingsBalance][:netAmount]
|
35
20
|
end
|
36
|
-
|
37
|
-
private
|
38
|
-
|
39
|
-
def generate_device_id
|
40
|
-
SecureRandom.uuid.split('-').last
|
41
|
-
end
|
42
21
|
end
|
43
22
|
end
|
data/lib/nubank_sdk/auth.rb
CHANGED
@@ -4,21 +4,16 @@ module NubankSdk
|
|
4
4
|
class Auth
|
5
5
|
attr_reader :refresh_token, :refresh_before, :access_token
|
6
6
|
|
7
|
-
def initialize(cpf:, device_id
|
7
|
+
def initialize(cpf:, device_id: nil, api_routes: nil, connection_adapter: nil)
|
8
8
|
@cpf = cpf
|
9
|
-
@device_id = device_id
|
10
|
-
@key = key || generate_key
|
9
|
+
@device_id = device_id || generate_device_id
|
11
10
|
@api_routes = api_routes || NubankSdk::ApiRoutes.new
|
12
11
|
|
13
|
-
@
|
14
|
-
end
|
15
|
-
|
16
|
-
def api_routes
|
17
|
-
@api_routes
|
12
|
+
@connection_adapter = connection_adapter
|
18
13
|
end
|
19
14
|
|
20
15
|
def certificate
|
21
|
-
@certificate ||= NubankSdk::Certificate.new(@cpf
|
16
|
+
@certificate ||= NubankSdk::Certificate.new(@cpf)
|
22
17
|
end
|
23
18
|
|
24
19
|
def authenticate_with_certificate(password)
|
@@ -51,7 +46,7 @@ module NubankSdk
|
|
51
46
|
)
|
52
47
|
|
53
48
|
response_data = Client.get_body(response)
|
54
|
-
certificate.process_decoded response_data[:certificate]
|
49
|
+
certificate.process_decoded(key, response_data[:certificate])
|
55
50
|
end
|
56
51
|
|
57
52
|
private
|
@@ -74,7 +69,7 @@ module NubankSdk
|
|
74
69
|
{
|
75
70
|
login: @cpf,
|
76
71
|
password: password,
|
77
|
-
public_key:
|
72
|
+
public_key: key.public_key.to_pem,
|
78
73
|
device_id: @device_id,
|
79
74
|
model: "NubankSdk Client (#@device_id)",
|
80
75
|
}
|
@@ -110,28 +105,36 @@ module NubankSdk
|
|
110
105
|
def find_url(keys, list)
|
111
106
|
links_keys = list.keys
|
112
107
|
|
113
|
-
keys.each do |
|
114
|
-
return list[
|
108
|
+
keys.each do |url_key|
|
109
|
+
return list[url_key]['href'] if links_keys.include?(url_key)
|
115
110
|
end
|
116
111
|
''
|
117
112
|
end
|
118
113
|
|
119
|
-
def
|
114
|
+
def prepare_default_connection
|
120
115
|
uri, @gen_certificate_path = @api_routes.entrypoint(
|
121
116
|
path: :app,
|
122
117
|
entrypoint: :gen_certificate,
|
123
118
|
type: :splitted
|
124
119
|
)
|
125
120
|
|
126
|
-
Client::HTTP.new(uri, @
|
121
|
+
Client::HTTP.new(uri, @connection_adapter)
|
127
122
|
end
|
128
123
|
|
129
124
|
def default_connection
|
130
|
-
@default_connection ||=
|
125
|
+
@default_connection ||= prepare_default_connection
|
131
126
|
end
|
132
127
|
|
133
128
|
def ssl_connection
|
134
|
-
@ssl_connection ||= Client::HTTPS.new(certificate.encoded, @
|
129
|
+
@ssl_connection ||= Client::HTTPS.new(certificate.encoded, @connection_adapter)
|
130
|
+
end
|
131
|
+
|
132
|
+
def key
|
133
|
+
@key ||= generate_key
|
134
|
+
end
|
135
|
+
|
136
|
+
def generate_device_id
|
137
|
+
SecureRandom.uuid.split('-').last
|
135
138
|
end
|
136
139
|
end
|
137
140
|
end
|
@@ -6,15 +6,14 @@ module NubankSdk
|
|
6
6
|
class Certificate
|
7
7
|
FILES_PATH = './certificates/'
|
8
8
|
|
9
|
-
def initialize(cpf
|
9
|
+
def initialize(cpf)
|
10
10
|
@cpf = cpf
|
11
|
-
@key = key
|
12
11
|
end
|
13
12
|
|
14
|
-
def process_decoded(certificate)
|
13
|
+
def process_decoded(key, certificate)
|
15
14
|
encoded = encode certificate
|
16
15
|
|
17
|
-
p12 = create_pkcs12_from encoded
|
16
|
+
p12 = create_pkcs12_from(key, encoded)
|
18
17
|
save p12
|
19
18
|
end
|
20
19
|
|
@@ -34,8 +33,8 @@ module NubankSdk
|
|
34
33
|
end
|
35
34
|
end
|
36
35
|
|
37
|
-
def create_pkcs12_from(certificate)
|
38
|
-
OpenSSL::PKCS12.create("password", "key",
|
36
|
+
def create_pkcs12_from(key, certificate)
|
37
|
+
OpenSSL::PKCS12.create("password", "key", key, certificate)
|
39
38
|
end
|
40
39
|
|
41
40
|
def encode(certificate)
|
data/lib/nubank_sdk/client.rb
CHANGED
@@ -10,10 +10,10 @@ module NubankSdk
|
|
10
10
|
end
|
11
11
|
|
12
12
|
class HTTP
|
13
|
-
def initialize(base_url,
|
13
|
+
def initialize(base_url, connection_adapter = nil)
|
14
14
|
@connection = Faraday.new(url: base_url) do |faraday|
|
15
|
-
faraday.adapter *
|
16
|
-
faraday.adapter Faraday.default_adapter unless
|
15
|
+
faraday.adapter *connection_adapter if connection_adapter
|
16
|
+
faraday.adapter Faraday.default_adapter unless connection_adapter
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
@@ -30,25 +30,27 @@ module NubankSdk
|
|
30
30
|
end
|
31
31
|
|
32
32
|
class HTTPS
|
33
|
-
|
33
|
+
attr_accessor :headers
|
34
|
+
|
35
|
+
def initialize(certificate, connection_adapter = nil)
|
34
36
|
client_cert = OpenSSL::X509::Certificate.new(certificate.certificate)
|
35
37
|
client_key = OpenSSL::PKey::RSA.new(certificate.key)
|
36
38
|
|
37
39
|
@connection = Faraday.new(ssl: { client_cert: client_cert, client_key: client_key}) do |faraday|
|
38
|
-
faraday.adapter *
|
39
|
-
faraday.adapter Faraday.default_adapter unless
|
40
|
+
faraday.adapter *connection_adapter if connection_adapter
|
41
|
+
faraday.adapter Faraday.default_adapter unless connection_adapter
|
40
42
|
end
|
41
43
|
end
|
42
44
|
|
43
|
-
def post(url, body
|
45
|
+
def post(url, body)
|
44
46
|
@connection.post(url) do |req|
|
45
47
|
req.headers['Content-Type'] = 'application/json'
|
46
48
|
req.headers['X-Correlation-Id'] = '772428d8-f0ee-43d6-8093-a13de3c9ce96'
|
47
49
|
req.headers['User-Agent'] = "NubankSdk Client (#{NubankSdk::VERSION})"
|
48
50
|
|
49
|
-
headers.each do |
|
50
|
-
req.headers[
|
51
|
-
end
|
51
|
+
@headers.each do |header_key, value|
|
52
|
+
req.headers[header_key] = value
|
53
|
+
end unless @headers.nil?
|
52
54
|
|
53
55
|
req.body = body.to_json
|
54
56
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module NubankSdk
|
4
|
+
class User
|
5
|
+
def initialize(cpf:, connection_adapter: nil)
|
6
|
+
@cpf = cpf
|
7
|
+
@connection_adapter = connection_adapter
|
8
|
+
end
|
9
|
+
|
10
|
+
def auth
|
11
|
+
@auth ||= NubankSdk::Auth.new(
|
12
|
+
cpf: @cpf,
|
13
|
+
api_routes: api_routes,
|
14
|
+
connection_adapter: @connection_adapter
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
def account
|
19
|
+
@account ||= NubankSdk::Account.new(connection: connection, api_routes: api_routes)
|
20
|
+
end
|
21
|
+
|
22
|
+
def api_routes
|
23
|
+
@api_routes ||= NubankSdk::ApiRoutes.new
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def connection
|
29
|
+
@connection ||= setup_connection
|
30
|
+
end
|
31
|
+
|
32
|
+
def setup_connection
|
33
|
+
connection = Client::HTTPS.new(
|
34
|
+
auth.certificate.encoded,
|
35
|
+
@connection_adapter
|
36
|
+
)
|
37
|
+
connection.headers = { 'Authorization': "Bearer #{auth.access_token}" }
|
38
|
+
connection
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/nubank_sdk/version.rb
CHANGED
data/usage_example.rb
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
require 'nubank_sdk'
|
2
2
|
|
3
3
|
# instance a nubank account object
|
4
|
-
|
4
|
+
user = NubankSdk::User.new(cpf: '12345678909')
|
5
5
|
password = 'dracarys'
|
6
6
|
# authenticate the account
|
7
7
|
|
8
8
|
# request an email code
|
9
|
-
account_email =
|
9
|
+
account_email = user.auth.request_email_code(password)
|
10
10
|
|
11
11
|
# get the email code from the user
|
12
12
|
puts "Enter the code sent to #{account_email}: "
|
13
13
|
email_code = gets.chomp
|
14
|
-
|
14
|
+
user.auth.exchange_certs(email_code, password)
|
15
15
|
|
16
|
-
|
16
|
+
user.auth.authenticate_with_certificate(password)
|
17
17
|
|
18
18
|
# get the account balance
|
19
|
-
account.
|
19
|
+
user.account.balance
|
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.
|
4
|
+
version: 0.5.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-
|
11
|
+
date: 2022-10-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -105,6 +105,7 @@ files:
|
|
105
105
|
- lib/nubank_sdk/auth.rb
|
106
106
|
- lib/nubank_sdk/certificate.rb
|
107
107
|
- lib/nubank_sdk/client.rb
|
108
|
+
- lib/nubank_sdk/user.rb
|
108
109
|
- lib/nubank_sdk/version.rb
|
109
110
|
- nubank_sdk.gemspec
|
110
111
|
- usage_example.rb
|