mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 08:13:42 +00:00
[PM-5275] Migrate state in Fido2ClientService to State Providers (#7745)
* added state definition and key definition * created vault settings service * created enable passkeys migrations * created enable passkeys migrations * renamed the state definition * created vault settings service * updated enable passkey key definition * updated references with vault settings service * renamed files to avoid conflict * removed set and get enable passkeys from state service * removed comment * fixed comments * added readonly keyword * removed service registartion from service module * removed readonly keyword from abstract class * swicted to used optional chaining * renamed files * added disk-local argument for web
This commit is contained in:
@@ -12,6 +12,7 @@ import { ProviderKeyMigrator } from "./migrations/13-move-provider-keys-to-state
|
||||
import { MoveBiometricClientKeyHalfToStateProviders } from "./migrations/14-move-biometric-client-key-half-state-to-providers";
|
||||
import { FolderMigrator } from "./migrations/15-move-folder-state-to-state-provider";
|
||||
import { LastSyncMigrator } from "./migrations/16-move-last-sync-to-state-provider";
|
||||
import { EnablePasskeysMigrator } from "./migrations/17-move-enable-passkeys-to-state-providers";
|
||||
import { FixPremiumMigrator } from "./migrations/3-fix-premium";
|
||||
import { RemoveEverBeenUnlockedMigrator } from "./migrations/4-remove-ever-been-unlocked";
|
||||
import { AddKeyTypeToOrgKeysMigrator } from "./migrations/5-add-key-type-to-org-keys";
|
||||
@@ -22,7 +23,7 @@ import { MoveBrowserSettingsToGlobal } from "./migrations/9-move-browser-setting
|
||||
import { MinVersionMigrator } from "./migrations/min-version";
|
||||
|
||||
export const MIN_VERSION = 2;
|
||||
export const CURRENT_VERSION = 16;
|
||||
export const CURRENT_VERSION = 17;
|
||||
export type MinVersion = typeof MIN_VERSION;
|
||||
|
||||
export async function migrate(
|
||||
@@ -54,7 +55,8 @@ export async function migrate(
|
||||
.with(ProviderKeyMigrator, 12, 13)
|
||||
.with(MoveBiometricClientKeyHalfToStateProviders, 13, 14)
|
||||
.with(FolderMigrator, 14, 15)
|
||||
.with(LastSyncMigrator, 15, CURRENT_VERSION)
|
||||
.with(LastSyncMigrator, 15, 16)
|
||||
.with(EnablePasskeysMigrator, 16, CURRENT_VERSION)
|
||||
|
||||
.migrate(migrationHelper);
|
||||
}
|
||||
|
||||
@@ -120,7 +120,7 @@ describe("FolderMigrator", () => {
|
||||
|
||||
describe("rollback", () => {
|
||||
beforeEach(() => {
|
||||
helper = mockMigrationHelper(rollbackJSON(), 14);
|
||||
helper = mockMigrationHelper(rollbackJSON(), 15);
|
||||
sut = new FolderMigrator(14, 15);
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
import { MockProxy } from "jest-mock-extended";
|
||||
|
||||
import { MigrationHelper } from "../migration-helper";
|
||||
import { mockMigrationHelper } from "../migration-helper.spec";
|
||||
|
||||
import { EnablePasskeysMigrator } from "./17-move-enable-passkeys-to-state-providers";
|
||||
|
||||
function exampleJSON() {
|
||||
return {
|
||||
global: {
|
||||
enablePasskeys: true,
|
||||
otherStuff: "otherStuff1",
|
||||
},
|
||||
authenticatedAccounts: ["user-1", "user-2"],
|
||||
"user-1": {
|
||||
settings: {
|
||||
otherStuff: "otherStuff2",
|
||||
},
|
||||
otherStuff: "otherStuff3",
|
||||
},
|
||||
"user-2": {
|
||||
settings: {
|
||||
otherStuff: "otherStuff4",
|
||||
},
|
||||
otherStuff: "otherStuff5",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function rollbackJSON() {
|
||||
return {
|
||||
global_vaultSettings_enablePasskeys: true,
|
||||
global: {
|
||||
otherStuff: "otherStuff1",
|
||||
},
|
||||
authenticatedAccounts: ["user-1", "user-2"],
|
||||
"user-1": {
|
||||
settings: {
|
||||
otherStuff: "otherStuff2",
|
||||
},
|
||||
otherStuff: "otherStuff3",
|
||||
},
|
||||
"user-2": {
|
||||
settings: {
|
||||
otherStuff: "otherStuff4",
|
||||
},
|
||||
otherStuff: "otherStuff5",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe("EnablePasskeysMigrator", () => {
|
||||
let helper: MockProxy<MigrationHelper>;
|
||||
let sut: EnablePasskeysMigrator;
|
||||
|
||||
describe("migrate", () => {
|
||||
beforeEach(() => {
|
||||
helper = mockMigrationHelper(exampleJSON(), 16);
|
||||
sut = new EnablePasskeysMigrator(16, 17);
|
||||
});
|
||||
|
||||
it("should remove enablePasskeys from global", async () => {
|
||||
await sut.migrate(helper);
|
||||
expect(helper.set).toHaveBeenCalledWith("global", {
|
||||
otherStuff: "otherStuff1",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("rollback", () => {
|
||||
beforeEach(() => {
|
||||
helper = mockMigrationHelper(rollbackJSON(), 17);
|
||||
sut = new EnablePasskeysMigrator(16, 17);
|
||||
});
|
||||
|
||||
it("should move enablePasskeys to global", async () => {
|
||||
await sut.rollback(helper);
|
||||
expect(helper.set).toHaveBeenCalledWith("global", {
|
||||
enablePasskeys: true,
|
||||
otherStuff: "otherStuff1",
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,36 @@
|
||||
import { KeyDefinitionLike, MigrationHelper } from "../migration-helper";
|
||||
import { Migrator } from "../migrator";
|
||||
|
||||
type ExpectedGlobalType = {
|
||||
enablePasskeys?: boolean;
|
||||
};
|
||||
|
||||
const USER_ENABLE_PASSKEYS: KeyDefinitionLike = {
|
||||
key: "enablePasskeys",
|
||||
stateDefinition: {
|
||||
name: "vaultSettings",
|
||||
},
|
||||
};
|
||||
|
||||
export class EnablePasskeysMigrator extends Migrator<16, 17> {
|
||||
async migrate(helper: MigrationHelper): Promise<void> {
|
||||
const global = await helper.get<ExpectedGlobalType>("global");
|
||||
|
||||
if (global?.enablePasskeys != null) {
|
||||
await helper.setToGlobal(USER_ENABLE_PASSKEYS, global.enablePasskeys);
|
||||
delete global?.enablePasskeys;
|
||||
await helper.set("global", global);
|
||||
}
|
||||
}
|
||||
|
||||
async rollback(helper: MigrationHelper): Promise<void> {
|
||||
let global = await helper.get<ExpectedGlobalType>("global");
|
||||
const globalEnablePasskeys = await helper.getFromGlobal<boolean>(USER_ENABLE_PASSKEYS);
|
||||
|
||||
if (globalEnablePasskeys != null) {
|
||||
global = Object.assign(global ?? {}, { enablePasskeys: globalEnablePasskeys });
|
||||
await helper.set("global", global);
|
||||
await helper.setToGlobal(USER_ENABLE_PASSKEYS, undefined);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user