-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathestateguru.lua
192 lines (148 loc) · 5.22 KB
/
estateguru.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
WebBanking {
version = 1.3,
url = "https://estateguru.co",
description = "Estateguru",
services = {"Estateguru"}
}
local currency = "EUR" -- fixme: Don't hardcode
local currencyName = "EUR" -- fixme: Don't hardcode
local connection
local credentialCache = {}
function SupportsBank(protocol, bankCode)
return protocol == ProtocolWebBanking and bankCode == "Estateguru"
end
local function requestJSON(method, url, data)
headers = {
accept = "application/json"
}
content, charset, mimeType = connection:request(method, url, JSON():set(data):json(), "application/json", headers)
return JSON(content):dictionary()
end
local function request2FA(username, password, sms)
if sms then
data = {
username = username,
password = password
}
response = requestJSON("POST", "https://account.estateguru.co/api/login/2fa/resend", data)
if response['status'] ~= 200 then
return LoginFailed
end
end
credentialCache = {
username = username,
password = password
}
if sms then
return {
title = "Two-step authentication",
challenge = "We sent a verification code to the phone number attached to your account. Please enter it here to log in.",
label = "SMS-Code"
}
end
return {
title = "Two-step authentication",
challenge = "Please enter the two-step authentication code provided by the authenticator app.",
label = "Authenticator-Code"
}
end
local function authenticatePass(username, password)
data = {
username = username,
password = password,
rememberMe = false
}
response = requestJSON("POST", "https://account.estateguru.co/api/login", data)
if response['status'] ~= 200 then
return LoginFailed
end
if response['twoFactorAuthenticationMethods'][1] == "SMS" then
return request2FA(username, password, true)
elseif response['twoFactorAuthenticationMethods'][1] == "GOOGLE" then
return request2FA(username, password, false)
end
return LoginFailed
end
local function hasToken(response)
return response['token'] ~= nil and response['token'] ~= ''
end
local function switchAccount()
headers = {
accept = "application/json"
}
content, charset, mimeType = connection:request("GET", "https://account.estateguru.co/api/user/info?username=" ..
MM.urlencode(credentialCache.username), "", headers)
response = JSON(content):dictionary()
content, charset, mimeType = connection:request("POST", "https://account.estateguru.co/api/account/" ..
response['defaultAccountId'] .. "/switch", "application/x-www-form-urlencoded; charset=UTF-8", headers)
response = JSON(content):dictionary()
if not hasToken(response) then
return "No token token received. Is this a bug?"
end
return nil
end
local function authenticate2FA(username, password, code)
data = {
username = username,
password = password,
twoFactorOtp = code,
rememberMe = false
}
response = requestJSON("POST", "https://account.estateguru.co/api/login", data)
-- flush credentials password
credentialCache.password = nil
if not hasToken(response) then
return "No token token received. Is this a bug?"
end
return switchAccount()
end
function InitializeSession2(protocol, bankCode, step, credentials, interactive)
if step == 1 then
connection = Connection()
-- Enforce english language
connection:setCookie("lng=en-US; path=/")
connection:setCookie("portal.lang=en; path=/")
connection.language = "en-gb"
return authenticatePass(credentials[1], credentials[2])
elseif step == 2 then
return authenticate2FA(credentialCache.username, credentialCache.password, credentials[1])
end
end
function ListAccounts(knownAccounts)
local account = {
name = "Estateguru",
accountNumber = "Estateguru",
currency = currency,
portfolio = true,
type = "AccountTypePortfolio"
}
return {account}
end
function RefreshAccount(account, since)
local s = {}
content = HTML(connection:get("https://app.estateguru.co/portfolio/ajaxFillAccountAndLoanData"))
account_value = content:xpath(
'/html/body/div/div[3]/div/div[2]/ul/li[1]/div[1]/span[2]'):text()
account_value = string.gsub(string.gsub(account_value, "€", ""), ",", "")
invested = content:xpath(
'/html/body/div/div[3]/div/div[2]/ul/li[2]/div[1]/span[2]'):text()
invested = string.gsub(string.gsub(string.gsub(string.gsub(invested, "€", ""), ",", ""), "-", ""), "%s+", "")
print("account value: " .. account_value)
print("invested: " .. invested)
local security = {
name = "Account Summary",
price = account_value,
purchasePrice = invested,
quantity = 1,
curreny = nil
}
table.insert(s, security)
return {
securities = s
}
end
function EndSession()
content, charset, mimeType = connection:request("POST", "https://account.estateguru.co/api/logout",
"application/x-www-form-urlencoded; charset=UTF-8", headers)
return nil
end