From a10c61379ba38c080c9705dc510e17a53f02d900 Mon Sep 17 00:00:00 2001 From: Ralf Vogler Date: Thu, 29 Dec 2022 14:54:13 +0100 Subject: [PATCH] pg: prompts for login & MFA --- prime-gaming.js | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/prime-gaming.js b/prime-gaming.js index f31d3f4..34a8236 100644 --- a/prime-gaming.js +++ b/prime-gaming.js @@ -2,6 +2,11 @@ import { firefox } from 'playwright'; // stealth plugin needs no outdated playwr import path from 'path'; import { dirs, jsonDb, datetime, stealth, filenamify } from './util.js'; +import prompts from 'prompts'; // alternatives: enquirer, inquirer +// import enquirer from 'enquirer'; const { prompt } = enquirer; +// single prompt that just returns the non-empty value instead of an object - why name things if there's just one? +const prompt = async o => (await prompts({name: 'name', type: 'text', message: 'Enter value', validate: s => s.length, ...o})).name; + const debug = process.env.PWDEBUG == '1'; // runs non-headless and opens https://playwright.dev/docs/inspector const show = process.argv.includes('show', 2); const headless = !debug && !show; @@ -36,16 +41,35 @@ try { await page.goto(URL_CLAIM, { waitUntil: 'domcontentloaded' }); // default 'load' takes forever // need to wait for some elements to exist before checking if signed in or accepting cookies: await Promise.any(['button:has-text("Sign in")', '[data-a-target="user-dropdown-first-name-text"]'].map(s => page.waitForSelector(s))); - await page.click('[aria-label="Cookies usage disclaimer banner"] button:has-text("Accept Cookies")').catch(_ => { }); // to not waste screen space in --debug + page.click('[aria-label="Cookies usage disclaimer banner"] button:has-text("Accept Cookies")').catch(_ => { }); // to not waste screen space when non-headless, TODO does not work reliably, need to wait for something else first? while (await page.locator('button:has-text("Sign in")').count() > 0) { console.error('Not signed in anymore.'); - if (headless) { - console.log('Please run `node prime-gaming show` to login in the opened browser.'); - await context.close(); // not needed? - process.exit(1); - } await page.click('button:has-text("Sign in")'); if (!debug) context.setDefaultTimeout(0); // give user time to log in without timeout + console.info('Press ESC to skip if you want to login in the browser (not possible in default headless mode).'); + const email = process.env.EMAIL || await prompt({message: 'Enter email'}); + const password = process.env.PASSWORD || await prompt({type: 'password', message: 'Enter password'}); + if (email && password) { + await page.fill('[name=email]', email); + await page.fill('[name=password]', password); + await page.check('[name=rememberMe]'); + await page.click('input[type="submit"]'); + // handle MFA, but don't await it + page.waitForNavigation({ url: '**/ap/mfa**'}).then(async () => { + console.log('Two-Step Verification - enter the One Time Password (OTP), e.g. generated by your Authenticator App'); + await page.check('[name=rememberDevice]'); + const otp = await prompt({type: 'text', message: 'Enter two-factor sign in code', validate: n => n.toString().length == 6 || 'The code must be 6 digits!'}); // can't use type: 'number' since it strips away leading zeros and codes sometimes have them + await page.type('input[name=otpCode]', otp.toString()); + await page.click('input[type="submit"]'); + }); + } else { + if (headless) { + console.log('Please run `node prime-gaming show` to login in the opened browser.'); + await context.close(); // not needed? + process.exit(1); + } + console.log('Waiting for you to login in the browser.'); + } await page.waitForNavigation({ url: 'https://gaming.amazon.com/home?signedIn=true' }); if (!debug) context.setDefaultTimeout(TIMEOUT); }