Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
sonar : reduce Cognitive Complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
BillCarsonFr committed Jan 8, 2024
1 parent 8464448 commit 6d48469
Showing 1 changed file with 55 additions and 38 deletions.
93 changes: 55 additions & 38 deletions src/SecurityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
if (!created) {
throw new Error("Secret storage creation canceled");
}
} else {
Expand All @@ -400,19 +380,7 @@ async function doAccessSecretStorage(func: () => Promise<void>, 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
Expand All @@ -426,6 +394,55 @@ async function doAccessSecretStorage(func: () => Promise<void>, 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.
*/
async function performSecretStorageCreationFlow(cli: MatrixClient): 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,

Check failure on line 411 in src/SecurityManager.ts

View workflow job for this annotation

GitHub Actions / Typescript Syntax Check

No value exists in scope for the shorthand property 'forceReset'. Either declare one or provide an initializer.
},
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 = {};
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<void> {
const key = dehydrationCache.key;
Expand Down

0 comments on commit 6d48469

Please sign in to comment.