-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathais-decoupled.js
More file actions
177 lines (143 loc) · 6.57 KB
/
ais-decoupled.js
File metadata and controls
177 lines (143 loc) · 6.57 KB
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
const { FintectureClient } = require('fintecture-client');
const app = require("express")();
const path = require('path');
const dotenv = require('dotenv');
const qrcode = require('qrcode');
dotenv.config({ path: path.join(__dirname, '.env') });
require('dotenv').config();
// Create the fintecture client instance
let client = new FintectureClient({ app_id: process.env.APP_ID, app_secret: process.env.APP_SECRET, private_key: process.env.APP_PRIV_KEY, env: process.env.FINTECTURE_ENV });
// Define accessToken and CustomerID global variables
let accessToken;
let customerId;
// Construct a provider selector pane
app.get("/", async (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.write('<html><body>');
try {
// Get list of available banks
let options = { 'filter[ais]': 'accounts', 'filter[psu_type]': 'retail', 'filter[auth_model]': 'decoupled', 'sort[full_name]': 'asc' }
let providers = await client.getProviders(options);
res.write(_prettyDisplayProviders(providers));
}
catch (err) {
res.write(err.response ? JSON.stringify(err.response.data) : 'error getting providers');
}
res.end('</html></body>');
});
app.get("/provider/:provider/psu", async (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.end(_prettyDisplayPSUform(req.params.provider));
});
app.get("/provider/:provider/auth", async (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.write('<html><body>');
let psuId = req.query.psu_id;
let psuIpAddress = req.headers['x-forwarded-for'] || '127.0.0.1';
try {
let providerAuth = await client.getDecoupledAuthUrl(null, req.params.provider, psuId, psuIpAddress, process.env.APP_REDIRECT_URI);
res.write('Mobile authentication started, please open your bank app to authenticate. <br><br>');
if (!!providerAuth.triggers) {
const b64img = await qrcode.toDataURL(providerAuth.triggers.qr);
res.write('<img src="' + b64img + '" alt="QR" /><br><br>');
res.write('A2A <a href="' + providerAuth.triggers.a2a +'">App2App</a> <br><br>');
}
res.write('loading . .')
// Initiate a decopled authentication and get the polling URL
while (true) {
await delay(2100);
try {
// poll the decoupled authentication status
let auth = await client.getDecoupledAuthStatus(null, req.params.provider, providerAuth.polling_id);
if (auth.status == 'COMPLETED') {
res.write(_redirect('/callback?code=' + auth.code + '&customer_id=' + auth.customer_id));
break;
} else if (auth.status == 'FAILED') {
res.write('<br><br>Decoupled auth FAILED');
break;
} else {
res.write(' .')
}
} catch (err) {
res.write('error');
}
}
}
catch (err) {
res.write(err.response ? JSON.stringify(err.response.data) : 'error');
}
res.end('</html></body>');
});
// Get 'code' querystring parameter and hit data api
app.get("/callback", async (req, res) => {
const code = req.query.code || 'unknown';
customerId = req.query.customer_id;
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.write('</html></body>');
try {
// get the Fintecture access token to request the AIS APIs
const tokens = await client.getAccessToken(code);
accessToken = tokens.access_token;
// get the Account details from the PSU
const accounts = await client.getAccounts(accessToken, customerId);
res.write(_prettyDisplayAccounts(accounts));
}
catch (err) {
res.write(err.response ? JSON.stringify(err.response.data) : 'error')
}
res.write('</html></body>');
res.end();
});
app.get("/transactions/:account", async (req, res) => {
const account = req.params.account;
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.write('</html></body>');
try {
// get the Transaction details from the PSU
const transactions = await client.getTransactions(accessToken, customerId, account);
res.write(_prettyDisplayTransactions(transactions));
}
catch (err) {
res.write(err.response ? JSON.stringify(err.response.data) : 'error')
}
res.end('</html></body>');
});
var _prettyDisplayProviders = function (providers) {
let list = '';
providers.data.forEach(provider => {
list = list + '<a href="/provider/' + provider.id + '/psu">' + provider.attributes.full_name + '</a><br>';
});
return list;
}
var _prettyDisplayAccounts = function (accounts) {
let headers = '<tr><th>account_id</th><th>IBAN</th><th>Name</th><th>balance</th><th>currency</th></tr>'
let rows = '';
accounts.data.forEach(account => {
rows = rows + '<tr><td><a href="../transactions/' + account.id + '">' + account.attributes.account_id + '</a></td><td>' + account.attributes.iban + '</td><td>' + account.attributes.account_name + '</td><td>' + account.attributes.balance + '</td><td>' + account.attributes.currency + '</td><tr>';
});
return '<table style="border:1px black;padding: 10px;">' + headers + rows + '</table>';
};
var _prettyDisplayTransactions = function (transactions) {
let headers = '<tr><th>date</th><th>communication</th><th>amount</th><th>currency</th></tr>'
let rows = '';
transactions.data.forEach(txn => {
rows = rows + '<tr><td>' + txn.attributes.booking_date + '</td><td>' + txn.attributes.communication + '</td><td>' + txn.attributes.amount + '</td><td>' + txn.attributes.currency + '</td><tr>';
});
return '<table style="border:1px black;padding: 10px;">' + headers + rows + '</table>';
}
var _prettyDisplayPSUform = function (provider) {
return `
<html><body><br>
PSU ID: <input type="text" id="psu_id" value="193805010844"><br>
<br>
<button onclick="next()">Next</button>
<script>function next() { window.location.href = "/provider/${provider}/auth?psu_id=" + document.getElementById("psu_id").value }</script>
</html></body>`
}
var _redirect = function (url) {
return '<script type="text/javascript"> window.location.href = "' + url + '"; </script>'
}
var delay = async function (ms) {
return await new Promise(resolve => setTimeout(resolve, ms));
}
app.listen(1235, () => console.log("Fintecture App listening on port 1235..."))