mirror of
https://github.com/bitwarden/jslib
synced 2025-12-10 21:33:17 +00:00
Finish migration and add tests
This commit is contained in:
@@ -28,7 +28,7 @@ describe("State Migration Service", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("StateVersion 3 to 4 migration", async () => {
|
describe("StateVersion 3 to 4 migration", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
const globalVersion3: Partial<GlobalState> = {
|
const globalVersion3: Partial<GlobalState> = {
|
||||||
stateVersion: StateVersion.Three,
|
stateVersion: StateVersion.Three,
|
||||||
@@ -81,4 +81,55 @@ describe("State Migration Service", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("StateVersion 4 to 5 migration", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
const globalVersion4: Partial<GlobalState> = {
|
||||||
|
stateVersion: StateVersion.Four,
|
||||||
|
};
|
||||||
|
|
||||||
|
storageService.get("global", Arg.any()).resolves(globalVersion4);
|
||||||
|
storageService.get("authenticatedAccounts", Arg.any()).resolves([userId]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("migrates organization keys to new format", async () => {
|
||||||
|
const accountVersion4 = new Account({
|
||||||
|
keys: {
|
||||||
|
organizationKeys: {
|
||||||
|
encrypted: {
|
||||||
|
orgOneId: "orgOneEncKey",
|
||||||
|
orgTwoId: "orgTwoEncKey",
|
||||||
|
orgThreeId: "orgThreeEncKey",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as any);
|
||||||
|
|
||||||
|
storageService.get(userId, Arg.any()).resolves(accountVersion4);
|
||||||
|
|
||||||
|
await stateMigrationService.migrate();
|
||||||
|
|
||||||
|
storageService.received(1).save(
|
||||||
|
userId,
|
||||||
|
Arg.is((account: Account) => {
|
||||||
|
return (
|
||||||
|
account.keys.organizationKeys.encrypted["orgOneId"].key === "orgOneEncKey" &&
|
||||||
|
account.keys.organizationKeys.encrypted["orgTwoId"].key === "orgTwoEncKey" &&
|
||||||
|
account.keys.organizationKeys.encrypted["orgThreeId"].key === "orgThreeEncKey"
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
Arg.any()
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("updates StateVersion number", async () => {
|
||||||
|
await stateMigrationService.migrate();
|
||||||
|
|
||||||
|
storageService.received(1).save(
|
||||||
|
"global",
|
||||||
|
Arg.is((globals: GlobalState) => globals.stateVersion === StateVersion.Five),
|
||||||
|
Arg.any()
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -3,6 +3,6 @@ export enum StateVersion {
|
|||||||
Two = 2, // Move to a typed State object
|
Two = 2, // Move to a typed State object
|
||||||
Three = 3, // Fix migration of users' premium status
|
Three = 3, // Fix migration of users' premium status
|
||||||
Four = 4, // Fix 'Never Lock' option by removing stale data
|
Four = 4, // Fix 'Never Lock' option by removing stale data
|
||||||
Five = 5,
|
Five = 5, // Migrate to new storage of encrypted organization keys
|
||||||
Latest = Five,
|
Latest = Five,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import { EncryptedOrganizationKeyData } from "jslib-common/models/data/encryptedOrganizationKeyData";
|
||||||
|
|
||||||
import { StorageService } from "../abstractions/storage.service";
|
import { StorageService } from "../abstractions/storage.service";
|
||||||
import { HtmlStorageLocation } from "../enums/htmlStorageLocation";
|
import { HtmlStorageLocation } from "../enums/htmlStorageLocation";
|
||||||
import { KdfType } from "../enums/kdfType";
|
import { KdfType } from "../enums/kdfType";
|
||||||
@@ -493,12 +495,18 @@ export class StateMigrationService<
|
|||||||
|
|
||||||
protected async migrateStateFrom4To5(): Promise<void> {
|
protected async migrateStateFrom4To5(): Promise<void> {
|
||||||
const authenticatedUserIds = await this.get<string[]>(keys.authenticatedAccounts);
|
const authenticatedUserIds = await this.get<string[]>(keys.authenticatedAccounts);
|
||||||
for (const userId in authenticatedUserIds) {
|
for (const userId of authenticatedUserIds) {
|
||||||
const account = await this.get<TAccount>(userId);
|
const account = await this.get<TAccount>(userId);
|
||||||
const encryptedOrgKeys = account.keys.organizationKeys?.encrypted;
|
const encryptedOrgKeys = account.keys.organizationKeys?.encrypted;
|
||||||
if (encryptedOrgKeys != null) {
|
if (encryptedOrgKeys == null) {
|
||||||
// TODO, iterate through KVPs and update value
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const [orgId, encKey] of Object.entries(encryptedOrgKeys)) {
|
||||||
|
encryptedOrgKeys[orgId] = new EncryptedOrganizationKeyData(encKey as unknown as string);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.set(userId, account);
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.setCurrentStateVersion(StateVersion.Five);
|
await this.setCurrentStateVersion(StateVersion.Five);
|
||||||
|
|||||||
Reference in New Issue
Block a user