1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-19 01:33:33 +00:00

[bug] Add several state value defaults (#593)

* [bug] Add several state value defaults

* [refactor] Implement StateVersion as an enum

* [refactor] Implement StateVersion enum over magic number
This commit is contained in:
Addison Beck
2021-12-31 09:14:43 -05:00
committed by GitHub
parent d68c1dafaf
commit e4cd0af2f9
5 changed files with 20 additions and 15 deletions

View File

@@ -2,6 +2,7 @@ import { StorageService } from "../abstractions/storage.service";
import { Account } from "../models/domain/account";
import { GeneratedPasswordHistory } from "../models/domain/generatedPasswordHistory";
import { GlobalState } from "../models/domain/globalState";
import { State } from "../models/domain/state";
import { StorageOptions } from "../models/domain/storageOptions";
@@ -16,6 +17,7 @@ import { SendData } from "../models/data/sendData";
import { HtmlStorageLocation } from "../enums/htmlStorageLocation";
import { KdfType } from "../enums/kdfType";
import { StateVersion } from "../enums/stateVersion";
// Originally (before January 2022) storage was handled as a flat key/value pair store.
// With the move to a typed object for state storage these keys should no longer be in use anywhere outside of this migration.
@@ -111,8 +113,6 @@ const v1KeyPrefixes = {
};
export class StateMigrationService {
readonly latestVersion: number = 2;
constructor(
protected storageService: StorageService,
protected secureStorageService: StorageService
@@ -124,15 +124,16 @@ export class StateMigrationService {
htmlStorageLocation: HtmlStorageLocation.Local,
})
)?.globals?.stateVersion;
return currentStateVersion == null || currentStateVersion < this.latestVersion;
return currentStateVersion == null || currentStateVersion < StateVersion.Latest;
}
async migrate(): Promise<void> {
let currentStateVersion =
(await this.storageService.get<State<Account>>("state"))?.globals?.stateVersion ?? 1;
while (currentStateVersion < this.latestVersion) {
(await this.storageService.get<State<Account>>("state"))?.globals?.stateVersion ??
StateVersion.One;
while (currentStateVersion < StateVersion.Latest) {
switch (currentStateVersion) {
case 1:
case StateVersion.One:
await this.migrateStateFrom1To2();
break;
}
@@ -147,9 +148,7 @@ export class StateMigrationService {
const initialState: State<Account> =
userId == null
? {
globals: {
stateVersion: 2,
},
globals: new GlobalState(),
accounts: {},
activeUserId: null,
}
@@ -209,7 +208,7 @@ export class StateMigrationService {
v1Keys.rememberedEmail,
options
),
stateVersion: 2,
stateVersion: StateVersion.Two,
theme: await this.storageService.get<string>(v1Keys.theme, options),
twoFactorToken: await this.storageService.get<string>(
v1KeyPrefixes.twoFactorToken + userId,