12 lines
4.3 KiB
JavaScript
12 lines
4.3 KiB
JavaScript
// Dedicated synthetic account only. Restore its initially-disabled MFA in finally.
|
|
const {chromium}=require('playwright'),fs=require('node:fs'),crypto=require('node:crypto'),assert=require('node:assert/strict');const file=process.argv[2];assert(file?.startsWith('/tmp/pd-dev-browser-'));const i=JSON.parse(fs.readFileSync(file));assert.equal(i.Database,'PlotDirector_Development');assert(i.Email.startsWith('pwa-browser-'));const base='https://dev.plotdirector.com',out='/tmp/plotdirector-app-domain-mfa';fs.mkdirSync(out,{recursive:true});
|
|
function totp(secret,stepOffset=0){let bits='';for(const c of secret.replace(/[=\s]/g,'').toUpperCase()){const n='ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.indexOf(c);assert(n>=0);bits+=n.toString(2).padStart(5,'0');}const bytes=[];for(let n=0;n+8<=bits.length;n+=8)bytes.push(parseInt(bits.slice(n,n+8),2));const counter=Buffer.alloc(8);counter.writeBigUInt64BE(BigInt(Math.floor(Date.now()/30000)+stepOffset));const hash=crypto.createHmac('sha1',Buffer.from(bytes)).update(counter).digest(),offset=hash.at(-1)&15;return ((hash.readUInt32BE(offset)&0x7fffffff)%1000000).toString().padStart(6,'0');}
|
|
(async()=>{const b=await chromium.launch();let admin,enabled=false,step='initial login';const checks=[];const login=async p=>{await p.locator('#Email').fill(i.Email);await p.locator('#Password').fill(i.Password);await Promise.all([p.waitForNavigation(),p.locator('form button[type=submit]').click()]);};try{
|
|
admin=await (await b.newContext()).newPage();await admin.goto(base+'/Account/Login');await login(admin);assert.equal(new URL(admin.url()).pathname,'/Projects','fixture MFA must initially be disabled');
|
|
const deep=await admin.locator('a[href^="/Projects/Details/"]').first().getAttribute('href');assert(deep);step='enable synthetic MFA';await admin.goto(base+'/ManageAccount/EnableTwoFactor');const secret=await admin.locator('#SecretKey').inputValue();await admin.locator('#Code').fill(totp(secret));enabled=true;await Promise.all([admin.waitForNavigation(),admin.locator('form[action="/ManageAccount/EnableTwoFactor"] button[type=submit]').click()]);assert(['/ManageAccount/RecoveryCodes','/Account/Login'].includes(new URL(admin.url()).pathname));checks.push('enabled MFA on dedicated synthetic account');
|
|
const p=await (await b.newContext()).newPage();await p.goto(base+'/Account/Login?returnUrl='+encodeURIComponent(deep));step='password then MFA challenge';await login(p);assert.equal(new URL(p.url()).pathname,'/Account/TwoFactor');assert.equal(await p.locator('html').getAttribute('data-site-theme'),'public');checks.push('real pending MFA challenge uses public brand');
|
|
await p.goto(base+'/Account/TwoFactorRecovery?returnUrl='+encodeURIComponent(deep));assert.equal(new URL(p.url()).pathname,'/Account/TwoFactorRecovery');assert.equal(await p.locator('html').getAttribute('data-site-theme'),'public');checks.push('real recovery challenge uses public brand');
|
|
await p.goto(base+'/Account/TwoFactor?returnUrl='+encodeURIComponent(deep));step='verify authenticator and deep link';const code=totp(secret,1);const returned=p.waitForURL('**'+deep);for(let n=0;n<6;n++)await p.locator('[data-two-factor-digit]').nth(n).fill(code[n]);await returned;assert.equal(new URL(p.url()).pathname,deep);checks.push('real TOTP verification returns requested protected deep link');admin=p;
|
|
}catch(e){console.log(JSON.stringify({errorType:e.name,step,path:admin?new URL(admin.url()).pathname:null}));process.exitCode=1;}
|
|
finally{if(enabled&&admin){try{await admin.goto(base+'/ManageAccount/DisableTwoFactor');await admin.locator('#CurrentPassword').fill(i.Password);await Promise.all([admin.waitForNavigation(),admin.locator('form[action="/ManageAccount/DisableTwoFactor"] button[type=submit]').click()]);assert.notEqual(new URL(admin.url()).pathname,'/ManageAccount/DisableTwoFactor');enabled=false;checks.push('original disabled MFA state restored');}catch(e){console.log(JSON.stringify({errorType:e.name,step,path:admin?new URL(admin.url()).pathname:null}));process.exitCode=1;}}fs.writeFileSync(out+'/results.json',JSON.stringify({passed:!process.exitCode,checks,fixtureMfaRestored:!enabled,failedStep:process.exitCode?step:null,noSecretsOrRecoveryCodesRecorded:true},null,2));await b.close();console.log(JSON.stringify({passed:!process.exitCode,checks:checks.length,fixtureMfaRestored:!enabled}));}})().catch(()=>{console.error('MFA acceptance failed; no credentials recorded.');process.exitCode=1;});
|