mirror of
https://github.com/bitwarden/browser
synced 2025-12-20 18:23:31 +00:00
* Use typescript-strict-plugin to iteratively turn on strict * Add strict testing to pipeline Can be executed locally through either `npm run test:types` for full type checking including spec files, or `npx tsc-strict` for only tsconfig.json included files. * turn on strict for scripts directory * Use plugin for all tsconfigs in monorepo vscode is capable of executing tsc with plugins, but uses the most relevant tsconfig to do so. If the plugin is not a part of that config, it is skipped and developers get no feedback of strict compile time issues. These updates remedy that at the cost of slightly more complex removal of the plugin when the time comes. * remove plugin from configs that extend one that already has it * Update workspace settings to honor strict plugin * Apply strict-plugin to native message test runner * Update vscode workspace to use root tsc version * `./node_modules/.bin/update-strict-comments` 🤖 This is a one-time operation. All future files should adhere to strict type checking. * Add fixme to `ts-strict-ignore` comments * `update-strict-comments` 🤖 repeated for new merge files
114 lines
3.7 KiB
TypeScript
114 lines
3.7 KiB
TypeScript
// FIXME: Update this file to be type safe and remove this and next line
|
|
// @ts-strict-ignore
|
|
import { KeyDefinitionLike, MigrationHelper } from "../migration-helper";
|
|
import { Migrator } from "../migrator";
|
|
|
|
type ExpectedAccountType = {
|
|
keys?: {
|
|
masterKeyEncryptedUserKey?: string;
|
|
};
|
|
profile?: {
|
|
forceSetPasswordReason?: number;
|
|
keyHash?: string;
|
|
};
|
|
};
|
|
|
|
export const FORCE_SET_PASSWORD_REASON_DEFINITION: KeyDefinitionLike = {
|
|
key: "forceSetPasswordReason",
|
|
stateDefinition: {
|
|
name: "masterPassword",
|
|
},
|
|
};
|
|
|
|
export const MASTER_KEY_HASH_DEFINITION: KeyDefinitionLike = {
|
|
key: "masterKeyHash",
|
|
stateDefinition: {
|
|
name: "masterPassword",
|
|
},
|
|
};
|
|
|
|
export const MASTER_KEY_ENCRYPTED_USER_KEY_DEFINITION: KeyDefinitionLike = {
|
|
key: "masterKeyEncryptedUserKey",
|
|
stateDefinition: {
|
|
name: "masterPassword",
|
|
},
|
|
};
|
|
|
|
export class MoveMasterKeyStateToProviderMigrator extends Migrator<54, 55> {
|
|
async migrate(helper: MigrationHelper): Promise<void> {
|
|
const accounts = await helper.getAccounts<ExpectedAccountType>();
|
|
async function migrateAccount(userId: string, account: ExpectedAccountType): Promise<void> {
|
|
const forceSetPasswordReason = account?.profile?.forceSetPasswordReason;
|
|
if (forceSetPasswordReason != null) {
|
|
await helper.setToUser(
|
|
userId,
|
|
FORCE_SET_PASSWORD_REASON_DEFINITION,
|
|
forceSetPasswordReason,
|
|
);
|
|
|
|
delete account.profile.forceSetPasswordReason;
|
|
await helper.set(userId, account);
|
|
}
|
|
|
|
const masterKeyHash = account?.profile?.keyHash;
|
|
if (masterKeyHash != null) {
|
|
await helper.setToUser(userId, MASTER_KEY_HASH_DEFINITION, masterKeyHash);
|
|
|
|
delete account.profile.keyHash;
|
|
await helper.set(userId, account);
|
|
}
|
|
|
|
const masterKeyEncryptedUserKey = account?.keys?.masterKeyEncryptedUserKey;
|
|
if (masterKeyEncryptedUserKey != null) {
|
|
await helper.setToUser(
|
|
userId,
|
|
MASTER_KEY_ENCRYPTED_USER_KEY_DEFINITION,
|
|
masterKeyEncryptedUserKey,
|
|
);
|
|
|
|
delete account.keys.masterKeyEncryptedUserKey;
|
|
await helper.set(userId, account);
|
|
}
|
|
}
|
|
|
|
await Promise.all([...accounts.map(({ userId, account }) => migrateAccount(userId, account))]);
|
|
}
|
|
async rollback(helper: MigrationHelper): Promise<void> {
|
|
const accounts = await helper.getAccounts<ExpectedAccountType>();
|
|
async function rollbackAccount(userId: string, account: ExpectedAccountType): Promise<void> {
|
|
const forceSetPasswordReason = await helper.getFromUser(
|
|
userId,
|
|
FORCE_SET_PASSWORD_REASON_DEFINITION,
|
|
);
|
|
const masterKeyHash = await helper.getFromUser(userId, MASTER_KEY_HASH_DEFINITION);
|
|
const masterKeyEncryptedUserKey = await helper.getFromUser(
|
|
userId,
|
|
MASTER_KEY_ENCRYPTED_USER_KEY_DEFINITION,
|
|
);
|
|
if (account != null) {
|
|
if (forceSetPasswordReason != null) {
|
|
account.profile = Object.assign(account.profile ?? {}, {
|
|
forceSetPasswordReason,
|
|
});
|
|
}
|
|
if (masterKeyHash != null) {
|
|
account.profile = Object.assign(account.profile ?? {}, {
|
|
keyHash: masterKeyHash,
|
|
});
|
|
}
|
|
if (masterKeyEncryptedUserKey != null) {
|
|
account.keys = Object.assign(account.keys ?? {}, {
|
|
masterKeyEncryptedUserKey,
|
|
});
|
|
}
|
|
await helper.set(userId, account);
|
|
}
|
|
|
|
await helper.setToUser(userId, FORCE_SET_PASSWORD_REASON_DEFINITION, null);
|
|
await helper.setToUser(userId, MASTER_KEY_HASH_DEFINITION, null);
|
|
}
|
|
|
|
await Promise.all([...accounts.map(({ userId, account }) => rollbackAccount(userId, account))]);
|
|
}
|
|
}
|