Skip to content
Snippets Groups Projects
Commit 07a8835a authored by Valere's avatar Valere
Browse files

sonar : reduce Cognitive Complexity

parent 84644485
No related tags found
No related merge requests found
......@@ -350,31 +350,11 @@ export async function accessSecretStorage(func = async (): Promise<void> => {},
async function doAccessSecretStorage(func: () => Promise<void>, forceReset: boolean): Promise<void> {
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<boolean> => {
// 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,6 +380,55 @@ async function doAccessSecretStorage(func: () => Promise<void>, forceReset: bool
getKeyBackupPassphrase: promptForBackupPassphrase,
});
await handleDeviceDehydration(cli);
}
// `return await` needed here to ensure `finally` block runs after the
// inner operation completes.
return await func();
} catch (e) {
SecurityCustomisations.catchAccessSecretStorageError?.(e);
logger.error(e);
// Re-throw so that higher level logic can abort as needed
throw e;
}
}
/**
* 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<boolean | undefined> {
// 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<boolean> => {
// 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<void> {
const keyId = Object.keys(secretStorageKeys)[0];
if (keyId && SettingsStore.getValue("feature_dehydration")) {
let dehydrationKeyInfo = {};
......@@ -415,17 +444,6 @@ async function doAccessSecretStorage(func: () => Promise<void>, forceReset: bool
}
}
// `return await` needed here to ensure `finally` block runs after the
// inner operation completes.
return await func();
} catch (e) {
SecurityCustomisations.catchAccessSecretStorageError?.(e);
logger.error(e);
// Re-throw so that higher level logic can abort as needed
throw e;
}
}
// FIXME: this function name is a bit of a mouthful
export async function tryToUnlockSecretStorageWithDehydrationKey(client: MatrixClient): Promise<void> {
const key = dehydrationCache.key;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment