1 import type { PrivateKeyReference, PublicKeyReference } from '@proton/crypto';
2 import { CryptoProxy, VERIFICATION_STATUS } from '@proton/crypto';
4 import { uint8ArrayToBase64String } from '../helpers/encoding';
7 * Decrypts a member token with the organization private key
9 export const decryptMemberToken = async (
11 privateKeys: PrivateKeyReference[],
12 publicKeys: PublicKeyReference[]
14 const { data: decryptedToken, verified } = await CryptoProxy.decryptMessage({
15 armoredMessage: token,
16 decryptionKeys: privateKeys,
17 verificationKeys: publicKeys,
20 if (verified !== VERIFICATION_STATUS.SIGNED_AND_VALID) {
21 const error = new Error('Signature verification failed');
22 error.name = 'SignatureError';
26 return `${decryptedToken}`;
30 * Generates the member token to decrypt its member key
32 export const generateMemberToken = () => {
33 const token = crypto.getRandomValues(new Uint8Array(128));
34 return uint8ArrayToBase64String(token);
38 * Encrypt the member key password with a key.
39 * @param token - The member key token in base64
40 * @param privateKey - The key to encrypt the token with
42 export const encryptMemberToken = async (token: string, privateKey: PrivateKeyReference) => {
43 const { message: encryptedToken } = await CryptoProxy.encryptMessage({
45 stripTrailingSpaces: true,
46 encryptionKeys: [privateKey],
47 signingKeys: [privateKey],
49 return encryptedToken;