This repository has been archived by the owner on Feb 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathsession.js
187 lines (160 loc) · 5.96 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
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;
//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() {
// Check that we're looking at the logged in homepage and throw an
// error if we aren't.
await u.wait(this.page, '.accounts-body');
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() {
let accData = await this.page.$$eval('.o-account-list__item', accounts => {
return accounts.map(acc => {
return [
acc.querySelector('.my-account-link').getAttribute('href'),
acc.querySelector('.o-account').getAttribute('id').replace(/[^0-9]/g, ''),
acc.querySelector('.my-account-link').textContent.trim(),
acc.querySelector('.o-account__balance-head') !== null ? acc.querySelector('.o-account__balance-head').textContent.trim().replace(/[£$€]/g, '') : ''
]
});
});
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.click(this.page, '[aria-label="Home"]');
await u.wait(this.page, '.accounts-body');
}
}
exports.launch = async (options) => {
const sess = new Session();
await sess.init(options);
return sess;
};