forked from russss/barclayscrape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.js
205 lines (171 loc) · 7.28 KB
/
session.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
const puppeteer = require('puppeteer');
const u = require('./utils.js');
const Account = require('./account.js');
class Session {
async init(options) {
this.browser = await puppeteer.launch(options);
this.page = await this.browser.newPage();
this.logged_in = false;
// .accounts_body - used for business banking and pre-2023 personal banking
// div.c-section.c-section--primary - used for post-2023 personal banking
this.selector_IsLoggedIn = '.accounts-body, div.c-section.c-section--primary';
//this.page.on('console', msg => console.log('PAGE LOG:', msg.text()));
await this.page.setViewport({width: 1000, height: 1500});
await this.page.goto('https://bank.barclays.co.uk');
}
async close() {
this.browser.close();
}
async loginStage1(credentials) {
// Stage 1 of login - enter surname and membership number.
await u.wait(this.page, '#membership0');
await u.fillFields(this.page, {
'#surnameMem': credentials['surname'],
'#membership0': credentials['membershipno'],
});
await u.click(this.page, 'button#continue');
}
async loginSelectMethod(method) {
// There's now a tab bar along the top of the page which needs clicking to switch method.
let selector = 'button';
switch (method) {
case 'motp':
selector += '#athenticationType_tab_button_0';
break;
case 'otp':
selector += '#athenticationType_tab_button_1';
break;
case 'plogin':
selector += '#athenticationType_tab_button_2';
break;
default:
return;
}
await u.wait(this.page, selector);
await this.page.$eval(selector, el => { el.click() });
}
async ensureLoggedIn() {
await u.wait(this.page, this.selector_IsLoggedIn);
this.logged_in = true;
}
async loginOTP(credentials) {
// Log in using a one time password (PinSentry).
await this.loginStage1(credentials);
await this.loginSelectMethod('otp');
await u.wait(this.page, '#mobilePinsentryCode-input-1');
await u.fillFields(this.page, {
'input[name="lastDigits"]': credentials['card_digits'],
'#mobilePinsentryCode-input-1': credentials['otp'].slice(0, 4),
'#mobilePinsentryCode-input-2': credentials['otp'].slice(4, 8),
});
// Press tab and wait 500ms so annoying JS validation can run
await this.page.keyboard.press('Tab');
await new Promise(resolve => setTimeout(resolve, 500));
await u.click(this.page, 'button#submitAuthentication');
await this.ensureLoggedIn();
}
async loginMOTP(credentials) {
// Log in using Mobile PinSentry.
await this.loginStage1(credentials);
await this.loginSelectMethod('motp');
await u.wait(this.page, '#mobilePinsentry-input-1');
await u.fillFields(this.page, {
'#mobilePinsentry-input-1': credentials['motp'].slice(0, 4),
'#mobilePinsentry-input-2': credentials['motp'].slice(4, 8),
});
// Press tab and wait 500ms so annoying JS validation can run
await this.page.keyboard.press('Tab');
await new Promise(resolve => setTimeout(resolve, 500));
await u.click(this.page, 'button#submitAuthentication');
await this.ensureLoggedIn();
}
async loginPasscode(credentials) {
// Log in using memorable passcode and password
await this.loginStage1(credentials);
await this.loginSelectMethod('plogin');
await u.wait(this.page, '#passcode');
await u.fillFields(this.page, {
'input[name="passcode"]': credentials["passcode"]
})
let digits = /[0-9]{1,2}/g;
let char_selectors = [
'div.memorableWordInputSpaceFirst #memorableCharacters-1',
'div.memorableWordInputSpace #memorableCharacters-2'
];
for (const [idx, selector] of char_selectors.entries()) {
await u.wait(this.page, selector);
let input = await this.page.$(selector)
let index_label = await this.page.evaluate(el => el.textContent, input)
let charindex = index_label.match(digits);
const passcode_char = credentials['password'].substr(charindex-1, 1);
let field_selector = "input[type='text']#memorableCharacters-input-" + (idx+1).toString();
await u.fillField(this.page, field_selector, passcode_char)
}
// blur the memorable char input (by re-focusing passcode input). This is necessary to allow onblur validation to take place
await this.page.focus("input#passcode");
let button_selector = 'button#submitAuthentication';
await u.wait(this.page, button_selector);
await u.click(this.page, button_selector);
// bypass occasional security page, if presented
await this.loginPasscode_interim_page(credentials);
await this.ensureLoggedIn();
}
async loginPasscode_interim_page(credentials) {
// check for interim security page
try {
await this.page.waitForSelector("span#label-scaCardLastDigits")
} catch (error) {
return;
}
await u.fillField(this.page, "input#scaCardLastDigits", credentials['card_digits'])
await u.fillField(this.page, "input#scaSecurityCode", credentials['card_cvv'])
await u.click(this.page, "button#saveScaAuthentication")
}
async accounts() {
// by default only the first few accounts will be shown
// if a 'Show All' button exists, click that before proceeding, to reveal all accounts
const btn_exists = await this.page.$eval("#showAllButton button", () => true).catch(() => false)
if (btn_exists) {
await u.wait(this.page, "#showAllButton button")
await u.click_nonav(this.page, "#showAllButton button")
}
await u.wait(this.page, ".c-account__content")
// once the full list of accounts is shown, it potentially includes some accounts with different markup
// for example, mortgage and insurance accounts are shown differently
// because all of this runs within the context of the page, it has to be coded really defensively to avoid an in-browser exception
let accData = await this.page.$$eval('.c-account__content', accounts => {
return accounts.map(acc => {
const account_link = acc.querySelector('.c-account__body a')
const account_link_href = account_link != null ? account_link.getAttribute('href') : ''
const account_link_txt = account_link !== null ? account_link.textContent.trim() : ''
const account_balance = acc.querySelector('.c-account__balance [description="Available balance"]')
const account_balance_txt = account_balance != null ? account_balance.textContent.trim().replace(/[^-0-9\.]/g, '') : ''
const account_detail = Array.from(acc.querySelectorAll('.c-account__detail--multi span')).map((span) => span.textContent.replace(/[^0-9]/g, '')).join('')
return [
account_link_href,
account_detail,
account_link_txt,
account_balance_txt,
]
});
});
let res = [];
accData.forEach(a => {
if ((a[1] == '') || (a[3] == '')) {
return;
}
res.push(new Account(this, a[0], a[1], a[2], a[3]));
});
return res;
}
async home() {
await u.wait(this.page, "a[href$='/olb/balances/digital/btr/home']");
await u.click(this.page, "a[href$='/olb/balances/digital/btr/home']");
await u.wait(this.page, this.selector_IsLoggedIn);
}
}
exports.launch = async (options) => {
const sess = new Session();
await sess.init(options);
return sess;
};