1 import { CryptoProxy } from '@proton/crypto';
2 import isTruthy from '@proton/utils/isTruthy';
4 import type { API_KEY_SOURCE } from '../../constants';
8 KeyTransparencyVerificationResult,
10 VerifyOutboundPublicKeys,
11 } from '../../interfaces';
12 import { getAllPublicKeys } from '../keys';
14 export interface ApiKeysWithKTStatus {
16 Keys: ApiAddressKey[];
17 SignedKeyList: FetchedSignedKeyList | null;
19 addressKeys: ProcessedApiKey[];
20 addressKTResult?: KeyTransparencyVerificationResult;
21 catchAllKeys?: ProcessedApiKey[];
22 catchAllKTResult?: KeyTransparencyVerificationResult;
23 unverifiedKeys?: ProcessedApiKey[];
24 hasValidProtonMX?: boolean;
29 interface ApiAddressKey {
32 Source: API_KEY_SOURCE;
35 const importKeys = async (keys: ApiAddressKey[], checkCompatibility?: boolean): Promise<ProcessedApiKey[]> => {
36 const promises = await Promise.all(
37 keys.map(async ({ PublicKey: armoredKey, Flags, Source }) => {
38 const publicKey = await CryptoProxy.importPublicKey({ armoredKey, checkCompatibility }).catch(() => null);
53 return promises.filter(isTruthy);
56 export const getAndVerifyApiKeys = async ({
60 verifyOutboundPublicKeys,
61 skipVerificationOfExternalDomains = false,
67 internalKeysOnly: boolean;
68 /** KT verification function, or `null` for legacy use-case where KT is disabled */
69 verifyOutboundPublicKeys: VerifyOutboundPublicKeys | null;
70 /** Optimisations _only_ for apps where users with external domains do not have valid keys (e.g. Mail) */
71 skipVerificationOfExternalDomains?: boolean;
74 }): Promise<ApiKeysWithKTStatus> => {
75 const config: any = { ...getAllPublicKeys({ Email: email, InternalOnly: internalKeysOnly ? 1 : 0 }), silence };
77 config.cache = 'no-cache';
79 const { Address, CatchAll, Unverified, ProtonMX, ...rest } = await api<{
81 Keys: ApiAddressKey[];
82 SignedKeyList: FetchedSignedKeyList | null;
86 Keys: ApiAddressKey[];
87 SignedKeyList: FetchedSignedKeyList | null;
91 Keys: ApiAddressKey[];
96 const addressKeys = await importKeys(Address.Keys);
97 const unverifiedKeys = Unverified ? await importKeys(Unverified.Keys, true) : undefined;
98 const catchAllKeys = CatchAll ? await importKeys(CatchAll.Keys) : undefined;
99 const ktResult = verifyOutboundPublicKeys
100 ? await verifyOutboundPublicKeys(
102 skipVerificationOfExternalDomains,
103 { keyList: addressKeys, signedKeyList: Address.SignedKeyList },
104 CatchAll ? { keyList: catchAllKeys!, signedKeyList: CatchAll.SignedKeyList } : undefined
112 hasValidProtonMX: ProtonMX,