mirror of
https://github.com/bitwarden/browser.git
synced 2025-03-02 03:41:09 +01:00
Use KeyDerivations in DerivedState
This commit is contained in:
parent
dfdde4daba
commit
6c88677d70
libs/common/src
platform/state
vault/services/folder
@ -1,7 +1,7 @@
|
||||
import { EncryptService } from "../abstractions/encrypt.service";
|
||||
import { UserKey } from "../models/domain/symmetric-crypto-key";
|
||||
|
||||
import { StorageLocation } from "./state-definition";
|
||||
import { KeyDefinition } from "./key-definition";
|
||||
|
||||
// TODO: Move type
|
||||
export class DeriveContext {
|
||||
@ -10,7 +10,7 @@ export class DeriveContext {
|
||||
|
||||
export class DerivedStateDefinition<TFrom, TTo> {
|
||||
constructor(
|
||||
readonly location: StorageLocation,
|
||||
readonly KeyDefinition: KeyDefinition<TTo>,
|
||||
readonly converter: (data: TFrom, context: DeriveContext) => Promise<TTo>
|
||||
) {}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { Jsonify } from "type-fest";
|
||||
|
||||
import { DeriveContext, DerivedStateDefinition } from "./derived-state-definition";
|
||||
import { StateDefinition, StorageLocation } from "./state-definition";
|
||||
import { StateDefinition } from "./state-definition";
|
||||
|
||||
/**
|
||||
*
|
||||
@ -47,14 +47,14 @@ export class KeyDefinition<T> {
|
||||
|
||||
/**
|
||||
* Helper for defining a derived definition that will often be used alongside a given key
|
||||
* @param storageLocation
|
||||
* @param decrypt
|
||||
* @param keyDefinition The key definition detailing storage details for the derived state
|
||||
* @param deriveCallback The callback used to convert from the parent state to the derived state
|
||||
* @returns
|
||||
*/
|
||||
createDerivedDefinition<TTo>(
|
||||
storageLocation: StorageLocation,
|
||||
decrypt: (data: T, context: DeriveContext) => Promise<TTo>
|
||||
keyDefinition: KeyDefinition<TTo>,
|
||||
deriveCallback: (data: T, context: DeriveContext) => Promise<TTo>
|
||||
) {
|
||||
return new DerivedStateDefinition(storageLocation, decrypt);
|
||||
return new DerivedStateDefinition(keyDefinition, deriveCallback);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,8 @@
|
||||
import { FolderData } from "../../vault/models/data/folder.data";
|
||||
import { FolderView } from "../../vault/models/view/folder.view";
|
||||
|
||||
import { KeyDefinition } from "./key-definition";
|
||||
import { FOLDER_SERVICE_DISK } from "./state-definitions";
|
||||
import { FOLDER_SERVICE_DISK, FOLDER_SERVICE_MEMORY } from "./state-definitions";
|
||||
|
||||
// FolderService Keys
|
||||
export const FOLDERS = KeyDefinition.record<FolderData>(
|
||||
@ -9,3 +10,8 @@ export const FOLDERS = KeyDefinition.record<FolderData>(
|
||||
"folders",
|
||||
FolderData.fromJSON
|
||||
);
|
||||
export const FOLDER_VIEWS = KeyDefinition.array<FolderView>(
|
||||
FOLDER_SERVICE_MEMORY,
|
||||
"folderViews",
|
||||
FolderView.fromJSON
|
||||
);
|
||||
|
@ -1,3 +1,4 @@
|
||||
import { StateDefinition } from "./state-definition";
|
||||
|
||||
export const FOLDER_SERVICE_DISK = new StateDefinition("FolderService", "disk");
|
||||
export const FOLDER_SERVICE_MEMORY = new StateDefinition("FolderService", "memory");
|
||||
|
@ -8,7 +8,7 @@ import { UserState } from "../../../platform/interfaces/user-state";
|
||||
import { Utils } from "../../../platform/misc/utils";
|
||||
import { SymmetricCryptoKey } from "../../../platform/models/domain/symmetric-crypto-key";
|
||||
import { DerivedUserState } from "../../../platform/state";
|
||||
import { FOLDERS } from "../../../platform/state/key-definitions";
|
||||
import { FOLDERS, FOLDER_VIEWS } from "../../../platform/state/key-definitions";
|
||||
import { UserId } from "../../../types/guid";
|
||||
import { CipherService } from "../../../vault/abstractions/cipher.service";
|
||||
import { InternalFolderService as InternalFolderServiceAbstraction } from "../../../vault/abstractions/folder/folder.service.abstraction";
|
||||
@ -34,7 +34,7 @@ export class FolderService implements InternalFolderServiceAbstraction {
|
||||
(window as any).services ||= {};
|
||||
(window as any).services.folderService = this;
|
||||
const derivedFoldersDefinition = FOLDERS.createDerivedDefinition(
|
||||
"memory",
|
||||
FOLDER_VIEWS,
|
||||
async (foldersMap) => {
|
||||
const folders = this.flattenMap(foldersMap);
|
||||
const decryptedFolders = await this.decryptFolders(folders);
|
||||
|
Loading…
Reference in New Issue
Block a user