This repository was archived by the owner on Mar 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
172 lines (155 loc) · 5.66 KB
/
index.js
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
'use strict';
const enablebanking = require('enablebanking');
const readline = require('readline');
const REDIRECT_URL = "https://enablebanking.com/auth_redirect" // PUT YOUR REDIRECT URI HERE
const CONNECTOR_NAME = "Nordea"
const CONNECTOR_COUNTRY = "FI"
/**
* Bank connector specific settings
*/
const CONNECTOR_SETTINGS = {
sandbox: true,
consentId: null,
accessToken: null,
redirectUri: REDIRECT_URL,
country: CONNECTOR_COUNTRY,
clientId: "client_id", // API Client ID
clientSecret: "client_secret", // API Client Secret
signKeyPath: "/path/to/private.key", // Path or URI to QSeal private key in PEM format
language: null
}
const readRedirectUrl = async (url, redirectUrl) => {
console.log(`Please, open this page in browser: ${url}`)
console.log("Log in, authenticate and copy/paste back the URL where you got redirected.")
return input(`URL (starts with ${redirectUrl}): `)
}
const input = async (query) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise(resolve => rl.question(query, ans => {
rl.close();
resolve(ans);
}))
}
function sleep(seconds) {
return new Promise(resolve => setTimeout(resolve, seconds * 1000));
}
const getConnectorMeta = async (connectorName, connectorCountry) => {
const metaApi = new enablebanking.MetaApi(enablebanking.ApiClient())
for (const conn of (await metaApi.getConnectors({ country: connectorCountry })).connectors) {
if (conn.name == connectorName) {
return conn
}
}
throw new Error("Meta not found")
};
async function main() {
const apiMeta = await getConnectorMeta(CONNECTOR_NAME, CONNECTOR_COUNTRY);
const apiClient = new enablebanking.ApiClient(CONNECTOR_NAME, CONNECTOR_SETTINGS);
const authApi = new enablebanking.AuthApi(apiClient);
const clientInfo = new enablebanking.ClientInfo();
const connectorPsuHeaders = apiMeta.requiredPsuHeaders;
if (connectorPsuHeaders.includes('psuIpAddress')) {
clientInfo.psuIpAddress = '10.10.10.10'
}
if (connectorPsuHeaders.includes('psuUserAgent')) {
clientInfo.psuUserAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1.1 Safari/605.1.15'
}
await authApi.setClientInfo({ clientInfo: clientInfo })
const validUntil = new Date()
const days = 89
validUntil.setDate(validUntil.getDate() + days)
const access = new enablebanking.Access({
validUntil: validUntil
});
const getAuthParams = { state: "test" }
if (apiMeta.authInfo[0].info.access) {
getAuthParams.access = access
}
if (apiMeta.authInfo[0].info.credentials) {
const credentials = [];
for (const cred of apiMeta.authInfo[0].info.credentials) {
credentials.push(await input(`Please enter ${cred.title}:`)) // obtain data in `cred` and push appropriate credential
}
getAuthParams.credentials = credentials;
}
const authResponse = await authApi.getAuth(getAuthParams)
let makeTokenResponse
if (authResponse.url) {
// if url is returned, then we are doing redirect flow
const redirectedUrl = await readRedirectUrl(authResponse.url, REDIRECT_URL)
const queryParams = await authApi.parseRedirectUrl(redirectedUrl)
console.log(`Parsed query: ${queryParams}`)
makeTokenResponse = await authApi.makeToken(
"authorization_code",
queryParams.code,
{ authEnv: authResponse.env }
)
} else {
// decoupled flow
// Doing a retry to check periodically whether user has already authorized a consent
const sleepTime = 3
for (let i = 0; i < 10; i++) {
try {
makeTokenResponse = await authApi.makeToken(
"authorization_code",
"",
{ authEnv: authResponse.env }
)
break
} catch (err) {
if (err instanceof enablebanking.errors.MakeTokenError) {
if (err.retry) {
console.log(`Not ready, retrying in ${sleepTime} seconds`)
await sleep(sleepTime)
continue
}
}
console.error(err)
throw err
}
}
}
console.log(`Token is: ${makeTokenResponse}`)
// apiClient has already accessToken and refreshTOken applied after call to makeToken()
const aispApi = new enablebanking.AISPApi(apiClient)
if (apiMeta.modifyConsentsInfo[0].info.beforeAccounts) {
const consent = await aispApi.modifyConsents({ access: access })
console.log(`Consent: ${consent}`);
try {
const consentUrl = consent._links.redirect.href;
const redirectUrl = await readRedirectUrl(consentUrl, REDIRECT_URL)
console.log(`Redirect url: ${redirectUrl}`);
} catch (err) {
console.error(err);
throw err;
}
}
const accounts = await aispApi.getAccounts();
console.log(accounts);
if (apiMeta.modifyConsentsInfo[0].info.accountsRequired) {
const accountIds = getAccountsResult.accounts.map((el) => new enablebanking.AccountIdentification({ iban: el.accountId.iban }));
access.accounts = accountIds;
const consent = await aispApi.modifyConsents({ access: access })
console.log(`Consent: ${consent}`);
try {
const consentUrl = consent._links.redirect.href;
const redirectUrl = await readRedirectUrl(consentUrl, REDIRECT_URL)
console.log(`Redirect url: ${redirectUrl}`);
} catch (err) {
console.error(err);
throw err;
}
}
for (const account of accounts.accounts) {
let transactions = await aispApi.getAccountTransactions(account.resourceId)
console.log(`Transactions info: ${transactions}`)
let balances = await aispApi.getAccountTransactions(account.resourceId)
console.log(`Balances info: ${balances}`)
}
};
main().then(function () {
console.log("All done!")
});