Skip to content
This repository has been archived by the owner on Aug 14, 2023. It is now read-only.

Commit

Permalink
fix login interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandawg93 committed Oct 24, 2020
1 parent cc03f61 commit 937171c
Showing 1 changed file with 51 additions and 54 deletions.
105 changes: 51 additions & 54 deletions src/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,46 +227,6 @@ export async function login(email?: string, password?: string, uix?: HomebridgeU
Object.setPrototypeOf(navigator, newProto);
});
await page.goto(homeUrl, { waitUntil: 'networkidle2' });
if (headless) {
try {
await page.waitForSelector('button[data-test="google-button-login"]');
await page.click('button[data-test="google-button-login"]');
} catch (error) {
console.error(`Unable to find login button.`);
if (uix) {
uix.loginFailed(`Unable to find login button.`);
}
return;
}

await inputData('#identifierId', 'username', 'Email or phone: ', email, page);
await inputData('input[type="password"]', 'password', 'Password: ', password, page);

console.log('Finishing up...');
try {
await page.waitForSelector(
'figure[data-illustration="authzenGmailApp"],figure[data-illustration="authzenHiddenPin"]',
{ timeout: 1000 },
);
console.log('Open the Gmail app and tap Yes on the prompt to sign in.');
} catch (error) {
// Gmail 2FA is not enabled
try {
await page.waitForSelector('#assistiveActionOutOfQuota', { timeout: 1000 });
console.error('Unavailable because of too many failed attempts. Try again in a few hours.');
if (uix) {
uix.loginFailed('Unavailable because of too many failed attempts. Try again in a few hours.');
}
try {
browser.close();
} catch (e) {}
return;
} catch (error) {
// Login did not fail because of too many attempts
}
}
}

await page.setRequestInterception(true);
page.on('request', async (request: Browser.Request) => {
const url = request.url();
Expand Down Expand Up @@ -313,21 +273,58 @@ export async function login(email?: string, password?: string, uix?: HomebridgeU

request.continue();
});
if (headless) {
try {
await page.waitForSelector('button[data-test="google-button-login"]');
await page.click('button[data-test="google-button-login"]');
} catch (error) {
console.error(`Unable to find login button.`);
if (uix) {
uix.loginFailed(`Unable to find login button.`);
}
return;
}

// the two factor catch is after the page interceptors intentionally
try {
await page.waitForSelector('input[name="totpPin"],input[name="idvPin"]', { timeout: 5000 });
console.log('2-step Verification Required');

await inputData(
'input[name="totpPin"],input[name="idvPin"]',
'totp',
'Please enter the verification code from the Google Authenticator app or SMS: ',
undefined,
page,
);
} catch (e) {
// totp is not enabled
await inputData('#identifierId', 'username', 'Email or phone: ', email, page);
await inputData('input[type="password"]', 'password', 'Password: ', password, page);
console.log('Finishing up...');

try {
await page.waitForSelector('input[name="totpPin"],input[name="idvPin"]', { timeout: 5000 });
console.log('2-step Verification Required');

await inputData(
'input[name="totpPin"],input[name="idvPin"]',
'totp',
'Please enter the verification code from the Google Authenticator app or SMS: ',
undefined,
page,
);
} catch (e) {
// totp is not enabled
try {
await page.waitForSelector(
'figure[data-illustration="authzenGmailApp"],figure[data-illustration="authzenHiddenPin"]',
{ timeout: 1000 },
);
console.log('Open the Gmail app and tap Yes on the prompt to sign in.');
} catch (error) {
// Gmail 2FA is not enabled
try {
await page.waitForSelector('#assistiveActionOutOfQuota', { timeout: 1000 });
console.error('Unavailable because of too many failed attempts. Try again in a few hours.');
if (uix) {
uix.loginFailed('Unavailable because of too many failed attempts. Try again in a few hours.');
}
try {
browser.close();
} catch (e) {}
return;
} catch (error) {
// Login did not fail because of too many attempts
}
}
}
}
} catch (err) {
console.error('Unable to retrieve credentials.');
Expand Down

0 comments on commit 937171c

Please sign in to comment.