1
0
mirror of https://github.com/bitwarden/jslib synced 2025-12-06 00:03:29 +00:00

Finish migration and add tests

This commit is contained in:
Thomas Rittson
2022-05-12 12:15:20 +10:00
parent 85a31b2da1
commit f6e8cffd94
3 changed files with 64 additions and 5 deletions

View File

@@ -28,7 +28,7 @@ describe("State Migration Service", () => {
);
});
describe("StateVersion 3 to 4 migration", async () => {
describe("StateVersion 3 to 4 migration", () => {
beforeEach(() => {
const globalVersion3: Partial<GlobalState> = {
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()
);
});
});
});

View File

@@ -3,6 +3,6 @@ export enum StateVersion {
Two = 2, // Move to a typed State object
Three = 3, // Fix migration of users' premium status
Four = 4, // Fix 'Never Lock' option by removing stale data
Five = 5,
Five = 5, // Migrate to new storage of encrypted organization keys
Latest = Five,
}

View File

@@ -1,3 +1,5 @@
import { EncryptedOrganizationKeyData } from "jslib-common/models/data/encryptedOrganizationKeyData";
import { StorageService } from "../abstractions/storage.service";
import { HtmlStorageLocation } from "../enums/htmlStorageLocation";
import { KdfType } from "../enums/kdfType";
@@ -493,12 +495,18 @@ export class StateMigrationService<
protected async migrateStateFrom4To5(): Promise<void> {
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 encryptedOrgKeys = account.keys.organizationKeys?.encrypted;
if (encryptedOrgKeys != null) {
// TODO, iterate through KVPs and update value
if (encryptedOrgKeys == null) {
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);