From 07a8835a4b85d57be6815513e59bd567bc32d913 Mon Sep 17 00:00:00 2001 From: Valere Date: Mon, 8 Jan 2024 10:28:31 +0100 Subject: [PATCH] sonar : reduce Cognitive Complexity --- src/SecurityManager.ts | 94 +++++++++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 38 deletions(-) diff --git a/src/SecurityManager.ts b/src/SecurityManager.ts index 485807494f3..8f3a9a51822 100644 --- a/src/SecurityManager.ts +++ b/src/SecurityManager.ts @@ -350,31 +350,11 @@ export async function accessSecretStorage(func = async (): Promise => {}, async function doAccessSecretStorage(func: () => Promise, forceReset: boolean): Promise { try { const cli = MatrixClientPeg.safeGet(); - if (!(await cli.secretStorage.hasKey()) || forceReset) { - // This dialog calls bootstrap itself after guiding the user through - // passphrase creation. - const { finished } = Modal.createDialogAsync( - import("./async-components/views/dialogs/security/CreateSecretStorageDialog") as unknown as Promise< - typeof CreateSecretStorageDialog - >, - { - forceReset, - }, - undefined, - /* priority = */ false, - /* static = */ true, - /* options = */ { - onBeforeClose: async (reason): Promise => { - // If Secure Backup is required, you cannot leave the modal. - if (reason === "backgroundClick") { - return !isSecureBackupRequired(cli); - } - return true; - }, - }, - ); - const [confirmed] = await finished; - if (!confirmed) { + const isSecretStorageConfigured = await cli.secretStorage.hasKey(); + const shouldCreateSecretStorage = !isSecretStorageConfigured || forceReset; + if (shouldCreateSecretStorage) { + const created = await performSecretStorageCreationFlow(cli, forceReset); + if (!created) { throw new Error("Secret storage creation canceled"); } } else { @@ -400,19 +380,7 @@ async function doAccessSecretStorage(func: () => Promise, forceReset: bool getKeyBackupPassphrase: promptForBackupPassphrase, }); - const keyId = Object.keys(secretStorageKeys)[0]; - if (keyId && SettingsStore.getValue("feature_dehydration")) { - let dehydrationKeyInfo = {}; - if (secretStorageKeyInfo[keyId] && secretStorageKeyInfo[keyId].passphrase) { - dehydrationKeyInfo = { passphrase: secretStorageKeyInfo[keyId].passphrase }; - } - logger.log("Setting dehydration key"); - await cli.setDehydrationKey(secretStorageKeys[keyId], dehydrationKeyInfo, "Backup device"); - } else if (!keyId) { - logger.warn("Not setting dehydration key: no SSSS key found"); - } else { - logger.log("Not setting dehydration key: feature disabled"); - } + await handleDeviceDehydration(cli); } // `return await` needed here to ensure `finally` block runs after the @@ -426,6 +394,56 @@ async function doAccessSecretStorage(func: () => Promise, forceReset: bool } } +/** + * Opens the CreateSecretStorageDialog and returns whether the user completed the flow. + * This will create the secret storage then bootstrap cross-signing and backup if needed. + * + * @param {MatrixClient} cli The client to use for the operation. + * @param {bool} forceReset Reset secret storage even if it's already set up + */ +async function performSecretStorageCreationFlow(cli: MatrixClient, forceReset: boolean): Promise { + // This dialog calls bootstrap itself after guiding the user through + // passphrase creation. + const { finished } = Modal.createDialogAsync( + import("./async-components/views/dialogs/security/CreateSecretStorageDialog") as unknown as Promise< + typeof CreateSecretStorageDialog + >, + { + forceReset, + }, + undefined, + /* priority = */ false, + /* static = */ true, + /* options = */ { + onBeforeClose: async (reason): Promise => { + // If Secure Backup is required, you cannot leave the modal. + if (reason === "backgroundClick") { + return !isSecureBackupRequired(cli); + } + return true; + }, + }, + ); + const [confirmed] = await finished; + return confirmed; +} + +async function handleDeviceDehydration(cli: MatrixClient): Promise { + const keyId = Object.keys(secretStorageKeys)[0]; + if (keyId && SettingsStore.getValue("feature_dehydration")) { + let dehydrationKeyInfo = {}; + if (secretStorageKeyInfo[keyId] && secretStorageKeyInfo[keyId].passphrase) { + dehydrationKeyInfo = { passphrase: secretStorageKeyInfo[keyId].passphrase }; + } + logger.log("Setting dehydration key"); + await cli.setDehydrationKey(secretStorageKeys[keyId], dehydrationKeyInfo, "Backup device"); + } else if (!keyId) { + logger.warn("Not setting dehydration key: no SSSS key found"); + } else { + logger.log("Not setting dehydration key: feature disabled"); + } +} + // FIXME: this function name is a bit of a mouthful export async function tryToUnlockSecretStorageWithDehydrationKey(client: MatrixClient): Promise { const key = dehydrationCache.key;