Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression: Add support for 2FA errors to Meteor.callAsync #27767

Merged
merged 5 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions apps/meteor/app/2fa/client/overrideMeteorCall.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Meteor } from 'meteor/meteor';

import { t } from '../../utils/client';
import { process2faReturn } from '../../../client/lib/2fa/process2faReturn';
import { process2faReturn, process2faAsyncReturn } from '../../../client/lib/2fa/process2faReturn';
import { isTotpInvalidError } from '../../../client/lib/2fa/utils';

const { call } = Meteor;
const { call, callAsync } = Meteor;

type Callback = {
(error: unknown): void;
Expand Down Expand Up @@ -34,8 +34,34 @@ const callWithoutTotp = (methodName: string, args: unknown[], callback: Callback
});
});

const callAsyncWithTotp =
(methodName: string, args: unknown[]) =>
async (twoFactorCode: string, twoFactorMethod: string): Promise<unknown> => {
try {
const result = await callAsync(methodName, ...args, { twoFactorCode, twoFactorMethod });

return result;
} catch (error: unknown) {
if (isTotpInvalidError(error)) {
throw new Error(twoFactorMethod === 'password' ? t('Invalid_password') : t('Invalid_two_factor_code'));
}

throw error;
}
};

Meteor.call = function (methodName: string, ...args: unknown[]): unknown {
const callback = args.length > 0 && typeof args[args.length - 1] === 'function' ? (args.pop() as Callback) : (): void => undefined;

return callWithoutTotp(methodName, args, callback)();
};

Meteor.callAsync = async function _callAsyncWithTotp(methodName: string, ...args: unknown[]): Promise<unknown> {
const promise = callAsync(methodName, ...args);

return process2faAsyncReturn({
promise,
onCode: callAsyncWithTotp(methodName, args),
emailOrUsername: undefined,
});
};
50 changes: 50 additions & 0 deletions apps/meteor/client/lib/2fa/process2faReturn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,53 @@ export function process2faReturn({
},
});
}

export async function process2faAsyncReturn({
promise,
onCode,
emailOrUsername,
}: {
promise: Promise<unknown>;
onCode: (code: string, method: string) => void;
emailOrUsername: string | null | undefined;
}): Promise<unknown> {
return new Promise<unknown>((resolve, reject) => {
promise
// if the promise is resolved, we don't need to do anything
.then((result) => {
resolve(result);
})
sampaiodiego marked this conversation as resolved.
Show resolved Hide resolved
// if the promise is rejected, we need to check if it's a 2fa error
.catch((error) => {
// if it's not a 2fa error, we reject the promise
if (!isTotpRequiredError(error) || !hasRequiredTwoFactorMethod(error)) {
reject(error);
return;
}

const props = {
method: error.details.method,
emailOrUsername: emailOrUsername || error.details.emailOrUsername || Meteor.user()?.username,
};

assertModalProps(props);

imperativeModal.open({
component: TwoFactorModal,
props: {
...props,
onConfirm: (code: string, method: string): void => {
imperativeModal.close();

// once we have the code, we resolve the promise with the result of the `onCode` callback
resolve(onCode(method === 'password' ? SHA256(code) : code, method));
},
onClose: (): void => {
imperativeModal.close();
reject(new Meteor.Error('totp-canceled'));
},
},
});
});
});
}