1 import { c } from 'ttag';
3 import type { AuthTypes } from '@proton/components/containers/login/interface';
4 import { CryptoProxy } from '@proton/crypto';
5 import type { AuthResponse } from '@proton/shared/lib/authentication/interface';
6 import type { APP_NAMES } from '@proton/shared/lib/constants';
7 import { PASSWORD_MODE } from '@proton/shared/lib/constants';
8 import type { KeySalt as tsKeySalt } from '@proton/shared/lib/interfaces/KeySalt';
9 import type { User as tsUser } from '@proton/shared/lib/interfaces/User';
10 import { getPrimaryKeyWithSalt } from '@proton/shared/lib/keys/keys';
11 import { getHasFIDO2Enabled, getHasTOTPEnabled } from '@proton/shared/lib/settings/twoFactor';
12 import { getHasFIDO2Support } from '@proton/shared/lib/webauthn/helper';
13 import { computeKeyPassword } from '@proton/srp';
15 export const getAuthTypes = (info: AuthResponse, app: APP_NAMES): AuthTypes => {
16 const Enabled = info?.['2FA']?.Enabled || 0;
18 totp: getHasTOTPEnabled(Enabled),
19 fido2: getHasFIDO2Enabled(Enabled) && getHasFIDO2Support(app, location.hostname),
20 unlock: info?.PasswordMode === PASSWORD_MODE.TWO_PASSWORD,
24 export const handleUnlockKey = async (User: tsUser, KeySalts: tsKeySalt[], rawKeyPassword: string) => {
25 const { KeySalt, PrivateKey } = getPrimaryKeyWithSalt(User.Keys, KeySalts);
28 throw new Error('Missing private key');
31 // Support for versions without a key salt.
32 const keyPassword = KeySalt ? ((await computeKeyPassword(rawKeyPassword, KeySalt)) as string) : rawKeyPassword;
33 const primaryKey = await CryptoProxy.importPrivateKey({ armoredKey: PrivateKey, passphrase: keyPassword });
41 export const getUnlockError = () => {
42 const error: any = new Error(c('Error').t`Incorrect second password. Please try again.`);
43 error.name = 'PasswordError';