1 import { c } from 'ttag';
3 import { activateAuthDeviceConfig } from '@proton/shared/lib/api/authDevice';
4 import { getApiError } from '@proton/shared/lib/api/helpers/apiErrorHelper';
5 import type { Address, Api, DecryptedKey } from '@proton/shared/lib/interfaces';
10 decryptAuthDeviceActivationToken,
11 encryptAuthDeviceSecret,
15 export class ConfirmAuthDeviceError extends Error {}
17 export const getAuthDeviceActivation = ({
22 pendingAuthDevice: AuthDeviceOutput;
24 const activation = getValidActivation({ addresses, pendingAuthDevice });
26 throw new ConfirmAuthDeviceError('Unable to find address for device');
31 export const decryptAuthDeviceActivation = async ({
37 addressKeys: DecryptedKey[];
41 const deviceSecretData = await decryptAuthDeviceActivationToken({
43 decryptionKeys: addressKeys.map(({ privateKey }) => privateKey),
44 armoredMessage: token,
46 return deviceSecretData;
48 throw new ConfirmAuthDeviceError('Unable to decrypt activation token');
52 export const validateAuthDevice = ({
56 deviceSecretData: DeviceSecretData;
57 confirmationCode: string;
59 if (deviceSecretData.confirmationCode !== confirmationCode) {
60 throw new ConfirmAuthDeviceError(c('sso').t`Invalid confirmation code`);
64 export const activateAuthDevice = async ({
72 deviceSecretData: DeviceSecretData;
73 pendingAuthDevice: AuthDeviceOutput;
76 const encryptedSecret = await encryptAuthDeviceSecret({
81 activateAuthDeviceConfig({
82 DeviceID: pendingAuthDevice.ID,
83 EncryptedSecret: encryptedSecret,
87 const { message } = getApiError(error);
88 throw new ConfirmAuthDeviceError(message || 'Failed to activate device');