forked from OpenBankProject/Hello-OBP-OAuth1.0a-Node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth.js
469 lines (333 loc) · 14.4 KB
/
oauth.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// Open Bank Project
// Copyright 2011-2016 TESOBE Ltd.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Open Bank Project (http://www.openbankproject.com)
// Copyright 2011-2016 TESOBE Ltd.
// This product includes software developed at
// TESOBE (http://www.tesobe.com/) contact AT tesobe DOT com
// by:
// Nina Gänsdorfer
// Everett Sochowski
// Stefan Bethge
// Simon Redfern
var express = require('express', template = require('pug'));
var session = require('express-session')
var util = require('util');
var oauth = require('oauth');
var app = express();
/////////////////////////////////////
// OAuth Config:
// To make authenticated calls to the API, you need OAuth keys
// To get them, please register your App here:
// https://YOUR-OBP-API-HOST/consumer-registration
// See README.md for an example config.json
// This loads your consumer key and secret from a file you create.
var config = require('./config.json');
//////////////////////////////////////
// Template engine (previously known as Jade)
var pug = require('pug');
// Used to validate forms
var bodyParser = require('body-parser')
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
var _openbankConsumerKey = config.consumerKey;
var _openbankConsumerSecret = config.consumerSecret;
var _openbankRedirectUrl = config.redirectUrl;
var port = 8085
// The location, on the interweb, of the OBP API server we want to use.
var apiHost = config.apiHost;
console.log ("Your apiHost is: " + apiHost)
console.log ("This application is running on http://127.0.0.1:" + port)
console.log ("The redirect URL is: " + _openbankRedirectUrl)
function onException (res, exception, moreData) {
template = "./template/oops.pug";
title = "Oops, something went wrong."
subTitle = "Maybe you are not logged in?"
console.log('we got an exception:' + exception);
var options = { title: title, subTitle: subTitle, exception: exception, moreData: moreData};
var html = pug.renderFile(template, options);
res.status(500).send(html)
}
var consumer = new oauth.OAuth(
apiHost + '/oauth/initiate',
apiHost + '/oauth/token',
_openbankConsumerKey,
_openbankConsumerSecret,
'1.0', //rfc oauth 1.0, includes 1.0a
_openbankRedirectUrl,
'HMAC-SHA1');
var cookieParser = require('cookie-parser');
app.use(session({
secret: "very secret",
resave: false,
saveUninitialized: true
}));
app.get('/connect', function(req, res){
consumer.getOAuthRequestToken(function(error, oauthToken, oauthTokenSecret, results){
if (error) {
res.status(500).send("Error getting OAuth request token : " + util.inspect(error));
} else {
req.session.oauthRequestToken = oauthToken;
req.session.oauthRequestTokenSecret = oauthTokenSecret;
res.redirect(apiHost + "/oauth/authorize?oauth_token="+req.session.oauthRequestToken);
}
});
});
app.get('/callback', function(req, res){
consumer.getOAuthAccessToken(
req.session.oauthRequestToken,
req.session.oauthRequestTokenSecret,
req.query.oauth_verifier,
function(error, oauthAccessToken, oauthAccessTokenSecret, result) {
if (error) {
//oauthAccessToken, -Secret and result are now undefined
res.status(500).send("Error getting OAuth access token : " + util.inspect(error));
} else {
//error is now undefined
req.session.oauthAccessToken = oauthAccessToken;
req.session.oauthAccessTokenSecret = oauthAccessTokenSecret;
res.redirect('/signed_in');
}
}
);
});
app.get('/signed_in', function(req, res){
var template = "./template/signedIn.pug"
var options = {}
var html = pug.renderFile(template, options)
res.status(200).send(html)
});
app.get('/getCurrentUser', function(req, res){
consumer.get(apiHost + "/obp/v3.1.0/users/current",
req.session.oauthAccessToken,
req.session.oauthAccessTokenSecret,
function (error, data, response) {
try {
var parsedData = JSON.parse(data);
res.status(200).send(parsedData)
} catch (exception){
onException(res, exception, data);
}
});
});
app.get('/getMyAccountsJson', function(req, res){
consumer.get(apiHost + "/obp/v3.0.0/my/accounts",
req.session.oauthAccessToken,
req.session.oauthAccessTokenSecret,
function (error, data, response) {
try {
var parsedData = JSON.parse(data);
res.status(200).send(parsedData)
} catch (exception){
onException(res, exception, data);
}
});
});
app.get('/getMyAccounts', function(req, res){
const util = require('util');
var template = "./template/accounts.pug";
var title = "Accounts"
consumer.get(apiHost + "/obp/v3.0.0/my/accounts",
req.session.oauthAccessToken,
req.session.oauthAccessTokenSecret,
// When the GET request completes, we call the following function with the data we got back:
function (error, data, response) {
//console.log("error is: " + error);
//console.log("data is: " + data);
//console.log("response is: " + response);
try {
var json = JSON.parse(data);
//console.log("json is: " + util.inspect(json, false, null))
var options = { title: title, error: error, json : json, response: response};
var html = pug.renderFile(template, options);
res.status(200).send(html)
} catch (exception) {
onException(res, exception, data);
}
});
});
app.get('/createTransactionRequest', function(req, res){
var template = "./template/createTransactionRequest.pug";
var options = { transactionRequestType :"SANDBOX_TAN"};
var html = pug.renderFile(template, options);
consumer.get(apiHost + "/obp/v3.0.0/my/accounts",
req.session.oauthAccessToken,
req.session.oauthAccessTokenSecret,
function (error, data, response) {
var parsedData = JSON.parse(data);
res.status(200).send(html)
});
});
app.post('/createTransactionRequest', urlencodedParser, function(req, res){
var template = "./template/createTransactionRequest.pug";
if (!req.body) return res.sendStatus(400)
var fromBankId = req.body.from_bank_id;
var fromAccountId = req.body.from_account_id;
var toBankId = req.body.to_bank_id;
var toAccountId = req.body.to_account_id;
var currency = req.body.currency;
var amount = req.body.amount;
var description = req.body.description;
var transactionRequestType = req.body.transaction_request_type;
console.log("transactionRequestType is: " + transactionRequestType);
if (transactionRequestType.length == 0){
transactionRequestType = "SANDBOX_TAN";
}
// Build the body that we will post
var toObj = {"bank_id": toBankId, "account_id": toAccountId};
var valueObj = {"currency":currency, "amount":amount};
var detailsObj = {"to": toObj, "value": valueObj, "description": description}
var details = JSON.stringify(detailsObj)
console.log("details is: " + details);
var viewId = "owner"
var apiHost = config.apiHost
var postUrl = apiHost + "/obp/v3.1.0/banks/" + fromBankId + "/accounts/" + fromAccountId + "/" + viewId + "/transaction-request-types/" + transactionRequestType + "/transaction-requests";
console.log("postUrl is " + postUrl);
consumer.post(postUrl,
req.session.oauthAccessToken,
req.session.oauthAccessTokenSecret,
details, // This is the body of the request
"application/json", // Must specify this else will get 404
function (error, data, response) {
var error = JSON.stringify(error)
console.log("error is: " + error)
console.log("data is: " + data)
console.log("response is: " + response)
try {
var parsedData = JSON.parse(data);
console.log("parsedData is: " + parsedData)
message = ""
} catch (err) {
// handle the error safely
console.log(err)
message = "Something went wrong creating a transaction request - did you supply the correct values?"
}
var options = {"error": error,
"postUrl" : postUrl,
"fromBankId": fromBankId,
"fromAccountId": fromAccountId,
"toBankId": toBankId,
"toAccountId" : toAccountId,
"currency" : currency,
"transactionRequestType" : transactionRequestType,
"details": details,
"data": data};
var html = pug.renderFile(template, options)
res.status(200).send(html)
});
});
// Loop through a Customers file, find the User matching email, Post the customer (which links to the User)
app.get('/loadCustomers', function(req, res) {
var template = "./template/loadCustomers.pug";
// Location of customer file is stored in filesConfig.json like this:
//
// {
// "customerFile": "/path-to/OBP_sandbox_customers_pretty.json",
// "sandboxFile": "/path-to/OBP_sandbox_pretty.json"
// }
var filesConfig = require('./filesConfig.json');
var customers = require(filesConfig.customerFile);
console.log('before customer loop. There are ' + customers.length + ' customers.')
customers.forEach(function processCustomer(customer) {
var usersByEmailUrl = apiHost + '/obp/v3.1.0/users/' + customer.email;
console.log('url to call: ' + usersByEmailUrl)
// get user by email
consumer.get(usersByEmailUrl,
req.session.oauthAccessToken,
req.session.oauthAccessTokenSecret,
function getUserForCustomer(error, data) {
if (error) return console.log(error);
var usersData = JSON.parse(data);
console.log('usersData is: ' + JSON.stringify(usersData))
var userId = usersData.users[0].user_id
console.log('I got userId: ' + userId)
console.log('I got customer with email , number : ' + customer.email + ' , ' + customer.customer_number)
customerToPost = {
"user_id": userId,
"customer_number": customer.customer_number,
"legal_name": customer.legal_name,
"mobile_phone_number": customer.mobile_phone_number,
"email": customer.email,
"face_image": customer.face_image,
"date_of_birth": customer.date_of_birth,
"relationship_status": customer.relationship_status,
"dependants": customer.dependants,
"dob_of_dependants": customer.dob_of_dependants,
"highest_education_attained": customer.highest_education_attained,
"employment_status": customer.employment_status,
"kyc_status": customer.kyc_status,
"last_ok_date": customer.last_ok_date
}
console.log('customerToPost: ' + JSON.stringify(customerToPost))
var postCustomerUrl = apiHost + '/obp/v3.1.0/banks/' + customer.bank_id + '/customers';
console.log('postCustomerUrl: ' + postCustomerUrl)
consumer.post(postCustomerUrl,
req.session.oauthAccessToken,
req.session.oauthAccessTokenSecret,
JSON.stringify(customerToPost), // This is the body of the request
"application/json", // Must specify this else will get 404
function (error, data) {
if (error) return console.log(error);
var parsedData = JSON.parse(data);
console.log('response from postCustomerUrl: ' + JSON.stringify(parsedData))
}); // End post customer
}); // End get user by email
}); // End Customer loop
var options = {
"countCustomers": customers.length
};
var html = pug.renderFile(template, options);
res.status(200).send(html)
});
// Create Entitlements for user (e.g. loop through banks)
app.get('/createEntitlements', function(req, res) {
var template = "./template/simple.pug"
// Location of sandbox file is stored in filesConfig.json like this:
//
// {
// "customerFile": "/path-to/OBP_sandbox_customers_pretty.json",
// "sandboxFile": "/path-to/OBP_sandbox_pretty.json"
// }
var dataConfig = require('./filesConfig.json')
var sandbox = require(dataConfig.sandboxFile)
var banks = sandbox.banks
console.log('before loop. There are ' + banks.length + ' banks.')
// {
// "userId": "asiodiuiof35234"
// }
var miscConfig = require('./miscConfig.json')
var userId = miscConfig.userId
banks.forEach(function processCustomer(bank) {
var postUrl = apiHost + '/obp/v3.1.0/users/' + userId + '/entitlements';
console.log('url to call: ' + postUrl)
//var postBody = {"bank_id":bank.id, "role_name":"CanCreateCustomer"}
var postBody = {"bank_id":bank.id, "role_name":"CanCreateUserCustomerLink"}
consumer.post(postUrl,
req.session.oauthAccessToken,
req.session.oauthAccessTokenSecret,
JSON.stringify(postBody), // This is the body of the request
"application/json", // Must specify this else will get 404
function getUserForCustomer(error, data) {
if (error) return console.log(error);
var data = JSON.parse(data);
console.log('data is: ' + JSON.stringify(data))
}); // End POST
}); // End Loop
var options = {
"count": banks.length
};
var html = pug.renderFile(template, options);
res.status(200).send(html)
});
app.get('*', function(req, res){
res.redirect('/connect');
});
app.listen(port);