Files
upstream-rocketchat/apps/meteor/client/lib/2fa/overrideLoginMethod.ts
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

111 lines
3.5 KiB
TypeScript

import { Accounts } from 'meteor/accounts-base';
import type { MeteorErrorLike } from './types';
import { isTotpInvalidError, isTotpMaxAttemptsError, isTotpRequiredError } from './utils';
export type LoginCallback = (error: MeteorErrorLike | undefined, result?: unknown) => void;
export const overrideLoginMethod = <TArgs extends any[]>(
loginMethod: (...args: [...args: TArgs, cb: LoginCallback]) => void,
loginArgs: TArgs,
callback: LoginCallback | undefined,
loginMethodTOTP: (...args: [...args: TArgs, code: string, cb: LoginCallback]) => void,
) => {
loginMethod(...loginArgs, async (error: MeteorErrorLike | undefined, result?: unknown) => {
if (!isTotpRequiredError(error)) {
callback?.(error);
return error;
}
const { process2faReturn } = await import('./process2faReturn');
try {
await process2faReturn({
error,
result,
emailOrUsername: typeof loginArgs[0] === 'string' ? loginArgs[0] : undefined,
originalCallback: callback,
onCode: (code: string) => {
return new Promise<void>((resolve, reject) => {
loginMethodTOTP(...loginArgs, code, (error: MeteorErrorLike | undefined, result?: unknown) => {
if (!error) {
callback?.(undefined, result);
resolve();
return;
}
if (isTotpInvalidError(error)) {
reject(error);
return;
}
Promise.all([import('../../../app/utils/lib/i18n'), import('../toast')]).then(([{ t }, { dispatchToastMessage }]) => {
if (isTotpMaxAttemptsError(error)) {
dispatchToastMessage({ type: 'error', message: t('totp-max-attempts') });
reject(error);
return;
}
dispatchToastMessage({ type: 'error', message: t('Invalid_two_factor_code') });
reject(error);
});
});
});
},
});
} catch (error) {
callback?.(error as MeteorErrorLike);
}
});
};
export const handleLogin = <TLoginFunction extends (...args: any[]) => Promise<any>>(
login: TLoginFunction,
loginWithTOTP: (...args: [...args: Parameters<TLoginFunction>, code: string]) => ReturnType<TLoginFunction>,
) => {
return (...args: [...loginArgs: Parameters<TLoginFunction>, callback?: LoginCallback]) => {
const loginArgs = args.slice(0, -1) as Parameters<TLoginFunction>;
const callback = args.slice(-1)[0] as LoginCallback | undefined;
return login(...loginArgs)
.catch(async (error: MeteorErrorLike | undefined) => {
if (!isTotpRequiredError(error)) {
return Promise.reject(error);
}
const { process2faAsyncReturn } = await import('./process2faReturn');
return process2faAsyncReturn({
emailOrUsername: typeof loginArgs[0] === 'string' ? loginArgs[0] : undefined,
error,
onCode: (code: string) => loginWithTOTP(...loginArgs, code),
});
})
.then((result: unknown) => callback?.(undefined, result))
.catch((error: MeteorErrorLike | undefined) => {
if (!isTotpInvalidError(error)) {
callback?.(error);
return;
}
Promise.all([import('../../../app/utils/lib/i18n'), import('../toast')]).then(([{ t }, { dispatchToastMessage }]) => {
dispatchToastMessage({ type: 'error', message: t('Invalid_two_factor_code') });
callback?.(undefined);
});
});
};
};
export const callLoginMethod = (options: Omit<Accounts.LoginMethodOptions, 'userCallback'>) =>
new Promise<void>((resolve, reject) => {
Accounts.callLoginMethod({
...options,
userCallback: (error) => {
if (error) {
reject(error);
} else {
resolve();
}
},
});
});