Skip to content

Commit

Permalink
protonmail: use custom 2fa token submitting detection
Browse files Browse the repository at this point in the history
* the need caused by recent built-in protonmail's web client update: url remains the same for all 3 auth forms
* related commit 2cf88ea
  • Loading branch information
vladimiry committed Apr 6, 2019
1 parent a245520 commit 9b2334c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
18 changes: 15 additions & 3 deletions src/electron-preload/webview/protonmail/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,29 @@ const endpoints: ProtonmailApi = {
login2fa: ({secret, zoneName}) => from((async (logger = curryFunctionMembers(_logger, "login2fa()", zoneName)) => {
logger.info();

const elements = await resolveDomElements({
const resolveElementsConfig = {
input: () => document.getElementById("twoFactorCode") as HTMLInputElement,
button: () => document.getElementById("login_btn_2fa") as HTMLElement,
});
logger.verbose(`elements resolved`);
};
const elements = await resolveDomElements(resolveElementsConfig);

logger.verbose("elements resolved");

return await submitTotpToken(
elements.input,
elements.button,
() => authenticator.generate(secret),
logger,
{
submittingDetection: async () => {
try {
await resolveDomElements(resolveElementsConfig, {iterationsLimit: 1});
} catch {
return true;
}
return false;
},
},
);
})()),

Expand Down
18 changes: 16 additions & 2 deletions src/electron-preload/webview/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,15 @@ export async function submitTotpToken(
{
submitTimeoutMs = ONE_SECOND_MS * 4,
newTokenDelayMs = ONE_SECOND_MS * 2,
submittingDetection,
}: {
submitTimeoutMs?: number;
newTokenDelayMs?: number;
submittingDetection?: () => Promise<boolean>;
} = {},
): Promise<null> {
const logger = curryFunctionMembers(_logger, "submitTotpToken()");

logger.info();

if (input.value) {
Expand All @@ -148,7 +151,14 @@ export async function submitTotpToken(

async function submit() {
logger.verbose("submit - start");
const urlBeforeSubmit = getLocationHref();

const submitted: () => Promise<boolean> = (
submittingDetection
||
((urlBeforeSubmit = getLocationHref()) => {
return async () => getLocationHref() !== urlBeforeSubmit;
})()
);

await fillInputValue(input, tokenResolver());
logger.verbose("input filled");
Expand All @@ -158,7 +168,11 @@ export async function submitTotpToken(

await asyncDelay(submitTimeoutMs);

if (getLocationHref() === urlBeforeSubmit) {
// TODO consider using unified submitting detection
// like for example testing that input/button elements no longer attached to DOM or visible
if (
!(await submitted())
) {
throw new Error(errorMessage);
}

Expand Down

0 comments on commit 9b2334c

Please sign in to comment.