Use same lock values as mobile clients
[ProtonMail-WebClient.git] / packages / shared / lib / api / helpers / getPublicKeysEmailHelper.ts
blob3e6f238b2915748a6f7efe5b03496b405c021ec2
1 import { c } from 'ttag';
3 import { ktKeyVerificationFailureTelemetryAndMetrics, ktSentryReport } from '@proton/key-transparency/lib';
5 import type { Api, ApiKeysConfig, VerifyOutboundPublicKeys } from '../../interfaces';
6 import { KT_VERIFICATION_STATUS, KeyTransparencyActivation } from '../../interfaces';
7 import getPublicKeysEmailHelperWithKT from './getPublicKeysEmailHelperWithKT';
9 export const KEY_VERIFICATION_ERROR_MESSAGE = c('loc_nightly: Key verification error')
10     .t`Unable to verify this address at this time`;
12 /**
13  * Ask the API for public keys for a given email address. The response will contain keys both
14  * for internal users and for external users with e.g. WKD keys
15  */
16 const getPublicKeysEmailHelper = async ({
17     email,
18     internalKeysOnly = false,
19     includeInternalKeysWithE2EEDisabledForMail = false,
20     api,
21     ktActivation,
22     verifyOutboundPublicKeys,
23     silence,
24     noCache,
25 }: {
26     email: string;
27     internalKeysOnly?: boolean;
28     api: Api;
29     ktActivation: KeyTransparencyActivation;
30     verifyOutboundPublicKeys: VerifyOutboundPublicKeys;
31     /**
32      * Whether to return internal keys which cannot be used for email encryption, as the owner has disabled E2EE.
33      * These keys may still be used for e.g. calendar sharing or message verification.
34      **/
35     includeInternalKeysWithE2EEDisabledForMail?: boolean;
36     silence?: boolean;
37     noCache?: boolean;
38 }): Promise<ApiKeysConfig> => {
39     if (ktActivation === KeyTransparencyActivation.DISABLED) {
40         const { ktVerificationResult, ...resultWithoutKT } = await getPublicKeysEmailHelperWithKT({
41             email,
42             internalKeysOnly,
43             includeInternalKeysWithE2EEDisabledForMail,
44             api,
45             verifyOutboundPublicKeys: null, // skip KT verification
46             silence,
47             noCache,
48         });
50         return resultWithoutKT;
51     }
52     const result = await getPublicKeysEmailHelperWithKT({
53         email,
54         internalKeysOnly,
55         includeInternalKeysWithE2EEDisabledForMail,
56         api,
57         verifyOutboundPublicKeys,
58         silence,
59         noCache,
60     });
61     if (result.ktVerificationResult?.status === KT_VERIFICATION_STATUS.VERIFICATION_FAILED) {
62         const visible = ktActivation === KeyTransparencyActivation.SHOW_UI;
63         ktSentryReport('Key verification error', { email });
64         await ktKeyVerificationFailureTelemetryAndMetrics(api, visible);
65         if (visible) {
66             return {
67                 publicKeys: [],
68                 ktVerificationResult: result.ktVerificationResult,
69                 Errors: [KEY_VERIFICATION_ERROR_MESSAGE],
70             };
71         }
72     }
73     if (ktActivation === KeyTransparencyActivation.LOG_ONLY) {
74         return {
75             ...result,
76             ktVerificationResult: undefined,
77         };
78     }
79     return result;
82 export default getPublicKeysEmailHelper;