[PM-13779] Add vNext CollectionService without ActiveUserState (#11705)
- add tests - install jest-extended for additional matchers - allow for generation of different crypto keys in tests
This commit is contained in:
parent
e83dca529b
commit
d0ed9aaa5d
|
@ -0,0 +1,41 @@
|
||||||
|
import { Observable } from "rxjs";
|
||||||
|
|
||||||
|
import { OrganizationId, UserId } from "@bitwarden/common/types/guid";
|
||||||
|
import { OrgKey } from "@bitwarden/common/types/key";
|
||||||
|
import { TreeNode } from "@bitwarden/common/vault/models/domain/tree-node";
|
||||||
|
|
||||||
|
import { CollectionData, Collection, CollectionView } from "../models";
|
||||||
|
|
||||||
|
export abstract class vNextCollectionService {
|
||||||
|
encryptedCollections$: (userId$: Observable<UserId>) => Observable<Collection[]>;
|
||||||
|
decryptedCollections$: (userId$: Observable<UserId>) => Observable<CollectionView[]>;
|
||||||
|
upsert: (collection: CollectionData | CollectionData[], userId: UserId) => Promise<any>;
|
||||||
|
replace: (collections: { [id: string]: CollectionData }, userId: UserId) => Promise<any>;
|
||||||
|
/**
|
||||||
|
* Clear decrypted state without affecting encrypted state.
|
||||||
|
* Used for locking the vault.
|
||||||
|
*/
|
||||||
|
clearDecryptedState: (userId: UserId) => Promise<void>;
|
||||||
|
/**
|
||||||
|
* Clear decrypted and encrypted state.
|
||||||
|
* Used for logging out.
|
||||||
|
*/
|
||||||
|
clear: (userId: string) => Promise<void>;
|
||||||
|
delete: (id: string | string[], userId: UserId) => Promise<any>;
|
||||||
|
encrypt: (model: CollectionView) => Promise<Collection>;
|
||||||
|
/**
|
||||||
|
* @deprecated This method will soon be made private, use `decryptedCollections$` instead.
|
||||||
|
*/
|
||||||
|
decryptMany: (
|
||||||
|
collections: Collection[],
|
||||||
|
orgKeys?: Record<OrganizationId, OrgKey>,
|
||||||
|
) => Promise<CollectionView[]>;
|
||||||
|
/**
|
||||||
|
* Transforms the input CollectionViews into TreeNodes
|
||||||
|
*/
|
||||||
|
getAllNested: (collections: CollectionView[]) => TreeNode<CollectionView>[];
|
||||||
|
/**
|
||||||
|
* Transforms the input CollectionViews into TreeNodes and then returns the Treenode with the specified id
|
||||||
|
*/
|
||||||
|
getNested: (collections: CollectionView[], id: string) => TreeNode<CollectionView>;
|
||||||
|
}
|
|
@ -0,0 +1,325 @@
|
||||||
|
import { mock, MockProxy } from "jest-mock-extended";
|
||||||
|
import { firstValueFrom, of, ReplaySubject } from "rxjs";
|
||||||
|
|
||||||
|
import { EncryptService } from "@bitwarden/common/platform/abstractions/encrypt.service";
|
||||||
|
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||||
|
import { Utils } from "@bitwarden/common/platform/misc/utils";
|
||||||
|
import { EncString } from "@bitwarden/common/platform/models/domain/enc-string";
|
||||||
|
import { SymmetricCryptoKey } from "@bitwarden/common/platform/models/domain/symmetric-crypto-key";
|
||||||
|
import { ContainerService } from "@bitwarden/common/platform/services/container.service";
|
||||||
|
import {
|
||||||
|
FakeStateProvider,
|
||||||
|
makeEncString,
|
||||||
|
makeSymmetricCryptoKey,
|
||||||
|
mockAccountServiceWith,
|
||||||
|
} from "@bitwarden/common/spec";
|
||||||
|
import { CollectionId, OrganizationId, UserId } from "@bitwarden/common/types/guid";
|
||||||
|
import { OrgKey } from "@bitwarden/common/types/key";
|
||||||
|
import { KeyService } from "@bitwarden/key-management";
|
||||||
|
|
||||||
|
import { CollectionData } from "../models";
|
||||||
|
|
||||||
|
import { DefaultvNextCollectionService } from "./default-vnext-collection.service";
|
||||||
|
import { ENCRYPTED_COLLECTION_DATA_KEY } from "./vnext-collection.state";
|
||||||
|
|
||||||
|
describe("DefaultvNextCollectionService", () => {
|
||||||
|
let keyService: MockProxy<KeyService>;
|
||||||
|
let encryptService: MockProxy<EncryptService>;
|
||||||
|
let i18nService: MockProxy<I18nService>;
|
||||||
|
let stateProvider: FakeStateProvider;
|
||||||
|
|
||||||
|
let userId: UserId;
|
||||||
|
|
||||||
|
let cryptoKeys: ReplaySubject<Record<OrganizationId, OrgKey> | null>;
|
||||||
|
|
||||||
|
let collectionService: DefaultvNextCollectionService;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
userId = Utils.newGuid() as UserId;
|
||||||
|
|
||||||
|
keyService = mock();
|
||||||
|
encryptService = mock();
|
||||||
|
i18nService = mock();
|
||||||
|
stateProvider = new FakeStateProvider(mockAccountServiceWith(userId));
|
||||||
|
|
||||||
|
cryptoKeys = new ReplaySubject(1);
|
||||||
|
keyService.orgKeys$.mockReturnValue(cryptoKeys);
|
||||||
|
|
||||||
|
// Set up mock decryption
|
||||||
|
encryptService.decryptToUtf8
|
||||||
|
.calledWith(expect.any(EncString), expect.any(SymmetricCryptoKey))
|
||||||
|
.mockImplementation((encString, key) =>
|
||||||
|
Promise.resolve(encString.data.replace("ENC_", "DEC_")),
|
||||||
|
);
|
||||||
|
|
||||||
|
(window as any).bitwardenContainerService = new ContainerService(keyService, encryptService);
|
||||||
|
|
||||||
|
// Arrange i18nService so that sorting algorithm doesn't throw
|
||||||
|
i18nService.collator = null;
|
||||||
|
|
||||||
|
collectionService = new DefaultvNextCollectionService(
|
||||||
|
keyService,
|
||||||
|
encryptService,
|
||||||
|
i18nService,
|
||||||
|
stateProvider,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
delete (window as any).bitwardenContainerService;
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("decryptedCollections$", () => {
|
||||||
|
it("emits decrypted collections from state", async () => {
|
||||||
|
// Arrange test data
|
||||||
|
const org1 = Utils.newGuid() as OrganizationId;
|
||||||
|
const orgKey1 = makeSymmetricCryptoKey<OrgKey>(64, 1);
|
||||||
|
const collection1 = collectionDataFactory(org1);
|
||||||
|
|
||||||
|
const org2 = Utils.newGuid() as OrganizationId;
|
||||||
|
const orgKey2 = makeSymmetricCryptoKey<OrgKey>(64, 2);
|
||||||
|
const collection2 = collectionDataFactory(org2);
|
||||||
|
|
||||||
|
// Arrange dependencies
|
||||||
|
await setEncryptedState([collection1, collection2]);
|
||||||
|
cryptoKeys.next({
|
||||||
|
[org1]: orgKey1,
|
||||||
|
[org2]: orgKey2,
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await firstValueFrom(collectionService.decryptedCollections$(of(userId)));
|
||||||
|
|
||||||
|
// Assert emitted values
|
||||||
|
expect(result.length).toBe(2);
|
||||||
|
expect(result).toIncludeAllPartialMembers([
|
||||||
|
{
|
||||||
|
id: collection1.id,
|
||||||
|
name: "DEC_NAME_" + collection1.id,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: collection2.id,
|
||||||
|
name: "DEC_NAME_" + collection2.id,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Assert that the correct org keys were used for each encrypted string
|
||||||
|
expect(encryptService.decryptToUtf8).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining(new EncString(collection1.name)),
|
||||||
|
orgKey1,
|
||||||
|
);
|
||||||
|
expect(encryptService.decryptToUtf8).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining(new EncString(collection2.name)),
|
||||||
|
orgKey2,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("handles null collection state", async () => {
|
||||||
|
// Arrange dependencies
|
||||||
|
await setEncryptedState(null);
|
||||||
|
cryptoKeys.next({});
|
||||||
|
|
||||||
|
const encryptedCollections = await firstValueFrom(
|
||||||
|
collectionService.encryptedCollections$(of(userId)),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(encryptedCollections.length).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("encryptedCollections$", () => {
|
||||||
|
it("emits encrypted collections from state", async () => {
|
||||||
|
// Arrange test data
|
||||||
|
const collection1 = collectionDataFactory();
|
||||||
|
const collection2 = collectionDataFactory();
|
||||||
|
|
||||||
|
// Arrange dependencies
|
||||||
|
await setEncryptedState([collection1, collection2]);
|
||||||
|
|
||||||
|
const result = await firstValueFrom(collectionService.encryptedCollections$(of(userId)));
|
||||||
|
|
||||||
|
expect(result.length).toBe(2);
|
||||||
|
expect(result).toIncludeAllPartialMembers([
|
||||||
|
{
|
||||||
|
id: collection1.id,
|
||||||
|
name: makeEncString("ENC_NAME_" + collection1.id),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: collection2.id,
|
||||||
|
name: makeEncString("ENC_NAME_" + collection2.id),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("handles null collection state", async () => {
|
||||||
|
await setEncryptedState(null);
|
||||||
|
|
||||||
|
const decryptedCollections = await firstValueFrom(
|
||||||
|
collectionService.encryptedCollections$(of(userId)),
|
||||||
|
);
|
||||||
|
expect(decryptedCollections.length).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("upsert", () => {
|
||||||
|
it("upserts to existing collections", async () => {
|
||||||
|
const collection1 = collectionDataFactory();
|
||||||
|
const collection2 = collectionDataFactory();
|
||||||
|
|
||||||
|
await setEncryptedState([collection1, collection2]);
|
||||||
|
|
||||||
|
const updatedCollection1 = Object.assign(new CollectionData({} as any), collection1, {
|
||||||
|
name: makeEncString("UPDATED_ENC_NAME_" + collection1.id).encryptedString,
|
||||||
|
});
|
||||||
|
const newCollection3 = collectionDataFactory();
|
||||||
|
|
||||||
|
await collectionService.upsert([updatedCollection1, newCollection3], userId);
|
||||||
|
|
||||||
|
const result = await firstValueFrom(collectionService.encryptedCollections$(of(userId)));
|
||||||
|
expect(result.length).toBe(3);
|
||||||
|
expect(result).toIncludeAllPartialMembers([
|
||||||
|
{
|
||||||
|
id: collection1.id,
|
||||||
|
name: makeEncString("UPDATED_ENC_NAME_" + collection1.id),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: collection2.id,
|
||||||
|
name: makeEncString("ENC_NAME_" + collection2.id),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: newCollection3.id,
|
||||||
|
name: makeEncString("ENC_NAME_" + newCollection3.id),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("upserts to a null state", async () => {
|
||||||
|
const collection1 = collectionDataFactory();
|
||||||
|
|
||||||
|
await setEncryptedState(null);
|
||||||
|
|
||||||
|
await collectionService.upsert(collection1, userId);
|
||||||
|
|
||||||
|
const result = await firstValueFrom(collectionService.encryptedCollections$(of(userId)));
|
||||||
|
expect(result.length).toBe(1);
|
||||||
|
expect(result).toIncludeAllPartialMembers([
|
||||||
|
{
|
||||||
|
id: collection1.id,
|
||||||
|
name: makeEncString("ENC_NAME_" + collection1.id),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("replace", () => {
|
||||||
|
it("replaces all collections", async () => {
|
||||||
|
await setEncryptedState([collectionDataFactory(), collectionDataFactory()]);
|
||||||
|
|
||||||
|
const newCollection3 = collectionDataFactory();
|
||||||
|
await collectionService.replace(
|
||||||
|
{
|
||||||
|
[newCollection3.id]: newCollection3,
|
||||||
|
},
|
||||||
|
userId,
|
||||||
|
);
|
||||||
|
|
||||||
|
const result = await firstValueFrom(collectionService.encryptedCollections$(of(userId)));
|
||||||
|
expect(result.length).toBe(1);
|
||||||
|
expect(result).toIncludeAllPartialMembers([
|
||||||
|
{
|
||||||
|
id: newCollection3.id,
|
||||||
|
name: makeEncString("ENC_NAME_" + newCollection3.id),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("clearDecryptedState", async () => {
|
||||||
|
await setEncryptedState([collectionDataFactory(), collectionDataFactory()]);
|
||||||
|
|
||||||
|
await collectionService.clearDecryptedState(userId);
|
||||||
|
|
||||||
|
// Encrypted state remains
|
||||||
|
const encryptedState = await firstValueFrom(
|
||||||
|
collectionService.encryptedCollections$(of(userId)),
|
||||||
|
);
|
||||||
|
expect(encryptedState.length).toEqual(2);
|
||||||
|
|
||||||
|
// Decrypted state is cleared
|
||||||
|
const decryptedState = await firstValueFrom(
|
||||||
|
collectionService.decryptedCollections$(of(userId)),
|
||||||
|
);
|
||||||
|
expect(decryptedState.length).toEqual(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("clear", async () => {
|
||||||
|
await setEncryptedState([collectionDataFactory(), collectionDataFactory()]);
|
||||||
|
cryptoKeys.next({});
|
||||||
|
|
||||||
|
await collectionService.clear(userId);
|
||||||
|
|
||||||
|
// Encrypted state is cleared
|
||||||
|
const encryptedState = await firstValueFrom(
|
||||||
|
collectionService.encryptedCollections$(of(userId)),
|
||||||
|
);
|
||||||
|
expect(encryptedState.length).toEqual(0);
|
||||||
|
|
||||||
|
// Decrypted state is cleared
|
||||||
|
const decryptedState = await firstValueFrom(
|
||||||
|
collectionService.decryptedCollections$(of(userId)),
|
||||||
|
);
|
||||||
|
expect(decryptedState.length).toEqual(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("delete", () => {
|
||||||
|
it("deletes a collection", async () => {
|
||||||
|
const collection1 = collectionDataFactory();
|
||||||
|
const collection2 = collectionDataFactory();
|
||||||
|
await setEncryptedState([collection1, collection2]);
|
||||||
|
|
||||||
|
await collectionService.delete(collection1.id, userId);
|
||||||
|
|
||||||
|
const result = await firstValueFrom(collectionService.encryptedCollections$(of(userId)));
|
||||||
|
expect(result.length).toEqual(1);
|
||||||
|
expect(result[0]).toMatchObject({ id: collection2.id });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("deletes several collections", async () => {
|
||||||
|
const collection1 = collectionDataFactory();
|
||||||
|
const collection2 = collectionDataFactory();
|
||||||
|
const collection3 = collectionDataFactory();
|
||||||
|
await setEncryptedState([collection1, collection2, collection3]);
|
||||||
|
|
||||||
|
await collectionService.delete([collection1.id, collection3.id], userId);
|
||||||
|
|
||||||
|
const result = await firstValueFrom(collectionService.encryptedCollections$(of(userId)));
|
||||||
|
expect(result.length).toEqual(1);
|
||||||
|
expect(result[0]).toMatchObject({ id: collection2.id });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("handles null collections", async () => {
|
||||||
|
const collection1 = collectionDataFactory();
|
||||||
|
await setEncryptedState(null);
|
||||||
|
|
||||||
|
await collectionService.delete(collection1.id, userId);
|
||||||
|
|
||||||
|
const result = await firstValueFrom(collectionService.encryptedCollections$(of(userId)));
|
||||||
|
expect(result.length).toEqual(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const setEncryptedState = (collectionData: CollectionData[] | null) =>
|
||||||
|
stateProvider.setUserState(
|
||||||
|
ENCRYPTED_COLLECTION_DATA_KEY,
|
||||||
|
collectionData == null ? null : Object.fromEntries(collectionData.map((c) => [c.id, c])),
|
||||||
|
userId,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
const collectionDataFactory = (orgId?: OrganizationId) => {
|
||||||
|
const collection = new CollectionData({} as any);
|
||||||
|
collection.id = Utils.newGuid() as CollectionId;
|
||||||
|
collection.organizationId = orgId ?? (Utils.newGuid() as OrganizationId);
|
||||||
|
collection.name = makeEncString("ENC_NAME_" + collection.id).encryptedString;
|
||||||
|
|
||||||
|
return collection;
|
||||||
|
};
|
|
@ -0,0 +1,196 @@
|
||||||
|
import { combineLatest, firstValueFrom, map, Observable, of, switchMap } from "rxjs";
|
||||||
|
|
||||||
|
import { EncryptService } from "@bitwarden/common/platform/abstractions/encrypt.service";
|
||||||
|
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||||
|
import { Utils } from "@bitwarden/common/platform/misc/utils";
|
||||||
|
import { StateProvider, DerivedState } from "@bitwarden/common/platform/state";
|
||||||
|
import { CollectionId, OrganizationId, UserId } from "@bitwarden/common/types/guid";
|
||||||
|
import { OrgKey } from "@bitwarden/common/types/key";
|
||||||
|
import { TreeNode } from "@bitwarden/common/vault/models/domain/tree-node";
|
||||||
|
import { ServiceUtils } from "@bitwarden/common/vault/service-utils";
|
||||||
|
import { KeyService } from "@bitwarden/key-management";
|
||||||
|
|
||||||
|
import { vNextCollectionService } from "../abstractions/vnext-collection.service";
|
||||||
|
import { Collection, CollectionData, CollectionView } from "../models";
|
||||||
|
|
||||||
|
import {
|
||||||
|
DECRYPTED_COLLECTION_DATA_KEY,
|
||||||
|
ENCRYPTED_COLLECTION_DATA_KEY,
|
||||||
|
} from "./vnext-collection.state";
|
||||||
|
|
||||||
|
const NestingDelimiter = "/";
|
||||||
|
|
||||||
|
export class DefaultvNextCollectionService implements vNextCollectionService {
|
||||||
|
constructor(
|
||||||
|
private keyService: KeyService,
|
||||||
|
private encryptService: EncryptService,
|
||||||
|
private i18nService: I18nService,
|
||||||
|
protected stateProvider: StateProvider,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
encryptedCollections$(userId$: Observable<UserId>) {
|
||||||
|
return userId$.pipe(
|
||||||
|
switchMap((userId) => this.encryptedState(userId).state$),
|
||||||
|
map((collections) => {
|
||||||
|
if (collections == null) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return Object.values(collections).map((c) => new Collection(c));
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
decryptedCollections$(userId$: Observable<UserId>) {
|
||||||
|
return userId$.pipe(
|
||||||
|
switchMap((userId) => this.decryptedState(userId).state$),
|
||||||
|
map((collections) => collections ?? []),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async upsert(toUpdate: CollectionData | CollectionData[], userId: UserId): Promise<void> {
|
||||||
|
if (toUpdate == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await this.encryptedState(userId).update((collections) => {
|
||||||
|
if (collections == null) {
|
||||||
|
collections = {};
|
||||||
|
}
|
||||||
|
if (Array.isArray(toUpdate)) {
|
||||||
|
toUpdate.forEach((c) => {
|
||||||
|
collections[c.id] = c;
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
collections[toUpdate.id] = toUpdate;
|
||||||
|
}
|
||||||
|
return collections;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async replace(collections: Record<CollectionId, CollectionData>, userId: UserId): Promise<void> {
|
||||||
|
await this.encryptedState(userId).update(() => collections);
|
||||||
|
}
|
||||||
|
|
||||||
|
async clearDecryptedState(userId: UserId): Promise<void> {
|
||||||
|
if (userId == null) {
|
||||||
|
throw new Error("User ID is required.");
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.decryptedState(userId).forceValue(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
async clear(userId: UserId): Promise<void> {
|
||||||
|
await this.encryptedState(userId).update(() => null);
|
||||||
|
// This will propagate from the encrypted state update, but by doing it explicitly
|
||||||
|
// the promise doesn't resolve until the update is complete.
|
||||||
|
await this.decryptedState(userId).forceValue(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
async delete(id: CollectionId | CollectionId[], userId: UserId): Promise<any> {
|
||||||
|
await this.encryptedState(userId).update((collections) => {
|
||||||
|
if (collections == null) {
|
||||||
|
collections = {};
|
||||||
|
}
|
||||||
|
if (typeof id === "string") {
|
||||||
|
delete collections[id];
|
||||||
|
} else {
|
||||||
|
(id as CollectionId[]).forEach((i) => {
|
||||||
|
delete collections[i];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return collections;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async encrypt(model: CollectionView): Promise<Collection> {
|
||||||
|
if (model.organizationId == null) {
|
||||||
|
throw new Error("Collection has no organization id.");
|
||||||
|
}
|
||||||
|
const key = await this.keyService.getOrgKey(model.organizationId);
|
||||||
|
if (key == null) {
|
||||||
|
throw new Error("No key for this collection's organization.");
|
||||||
|
}
|
||||||
|
const collection = new Collection();
|
||||||
|
collection.id = model.id;
|
||||||
|
collection.organizationId = model.organizationId;
|
||||||
|
collection.readOnly = model.readOnly;
|
||||||
|
collection.externalId = model.externalId;
|
||||||
|
collection.name = await this.encryptService.encrypt(model.name, key);
|
||||||
|
return collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: this should be private and orgKeys should be required.
|
||||||
|
// See https://bitwarden.atlassian.net/browse/PM-12375
|
||||||
|
async decryptMany(
|
||||||
|
collections: Collection[],
|
||||||
|
orgKeys?: Record<OrganizationId, OrgKey>,
|
||||||
|
): Promise<CollectionView[]> {
|
||||||
|
if (collections == null || collections.length === 0) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
const decCollections: CollectionView[] = [];
|
||||||
|
|
||||||
|
orgKeys ??= await firstValueFrom(this.keyService.activeUserOrgKeys$);
|
||||||
|
|
||||||
|
const promises: Promise<any>[] = [];
|
||||||
|
collections.forEach((collection) => {
|
||||||
|
promises.push(
|
||||||
|
collection
|
||||||
|
.decrypt(orgKeys[collection.organizationId as OrganizationId])
|
||||||
|
.then((c) => decCollections.push(c)),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
await Promise.all(promises);
|
||||||
|
return decCollections.sort(Utils.getSortFunction(this.i18nService, "name"));
|
||||||
|
}
|
||||||
|
|
||||||
|
getAllNested(collections: CollectionView[]): TreeNode<CollectionView>[] {
|
||||||
|
const nodes: TreeNode<CollectionView>[] = [];
|
||||||
|
collections.forEach((c) => {
|
||||||
|
const collectionCopy = new CollectionView();
|
||||||
|
collectionCopy.id = c.id;
|
||||||
|
collectionCopy.organizationId = c.organizationId;
|
||||||
|
const parts = c.name != null ? c.name.replace(/^\/+|\/+$/g, "").split(NestingDelimiter) : [];
|
||||||
|
ServiceUtils.nestedTraverse(nodes, 0, parts, collectionCopy, null, NestingDelimiter);
|
||||||
|
});
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated August 30 2022: Moved to new Vault Filter Service
|
||||||
|
* Remove when Desktop and Browser are updated
|
||||||
|
*/
|
||||||
|
getNested(collections: CollectionView[], id: string): TreeNode<CollectionView> {
|
||||||
|
const nestedCollections = this.getAllNested(collections);
|
||||||
|
return ServiceUtils.getTreeNodeObjectFromList(
|
||||||
|
nestedCollections,
|
||||||
|
id,
|
||||||
|
) as TreeNode<CollectionView>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns a SingleUserState for encrypted collection data.
|
||||||
|
*/
|
||||||
|
private encryptedState(userId: UserId) {
|
||||||
|
return this.stateProvider.getUser(userId, ENCRYPTED_COLLECTION_DATA_KEY);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns a SingleUserState for decrypted collection data.
|
||||||
|
*/
|
||||||
|
private decryptedState(userId: UserId): DerivedState<CollectionView[]> {
|
||||||
|
const encryptedCollectionsWithKeys = this.encryptedState(userId).combinedState$.pipe(
|
||||||
|
switchMap(([userId, collectionData]) =>
|
||||||
|
combineLatest([of(collectionData), this.keyService.orgKeys$(userId)]),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
return this.stateProvider.getDerived(
|
||||||
|
encryptedCollectionsWithKeys,
|
||||||
|
DECRYPTED_COLLECTION_DATA_KEY,
|
||||||
|
{
|
||||||
|
collectionService: this,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
import { Jsonify } from "type-fest";
|
||||||
|
|
||||||
|
import {
|
||||||
|
COLLECTION_DATA,
|
||||||
|
DeriveDefinition,
|
||||||
|
UserKeyDefinition,
|
||||||
|
} from "@bitwarden/common/platform/state";
|
||||||
|
import { CollectionId, OrganizationId } from "@bitwarden/common/types/guid";
|
||||||
|
import { OrgKey } from "@bitwarden/common/types/key";
|
||||||
|
|
||||||
|
import { vNextCollectionService } from "../abstractions/vnext-collection.service";
|
||||||
|
import { Collection, CollectionData, CollectionView } from "../models";
|
||||||
|
|
||||||
|
export const ENCRYPTED_COLLECTION_DATA_KEY = UserKeyDefinition.record<CollectionData, CollectionId>(
|
||||||
|
COLLECTION_DATA,
|
||||||
|
"collections",
|
||||||
|
{
|
||||||
|
deserializer: (jsonData: Jsonify<CollectionData>) => CollectionData.fromJSON(jsonData),
|
||||||
|
clearOn: ["logout"],
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
export const DECRYPTED_COLLECTION_DATA_KEY = new DeriveDefinition<
|
||||||
|
[Record<CollectionId, CollectionData>, Record<OrganizationId, OrgKey>],
|
||||||
|
CollectionView[],
|
||||||
|
{ collectionService: vNextCollectionService }
|
||||||
|
>(COLLECTION_DATA, "decryptedCollections", {
|
||||||
|
deserializer: (obj) => obj.map((collection) => CollectionView.fromJSON(collection)),
|
||||||
|
derive: async ([collections, orgKeys], { collectionService }) => {
|
||||||
|
if (collections == null) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = Object.values(collections).map((c) => new Collection(c));
|
||||||
|
return await collectionService.decryptMany(data, orgKeys);
|
||||||
|
},
|
||||||
|
});
|
|
@ -1,3 +1,5 @@
|
||||||
|
import * as matchers from "jest-extended";
|
||||||
|
|
||||||
import { toBeFulfilled, toBeResolved, toBeRejected } from "./promise-fulfilled";
|
import { toBeFulfilled, toBeResolved, toBeRejected } from "./promise-fulfilled";
|
||||||
import { toAlmostEqual } from "./to-almost-equal";
|
import { toAlmostEqual } from "./to-almost-equal";
|
||||||
import { toEqualBuffer } from "./to-equal-buffer";
|
import { toEqualBuffer } from "./to-equal-buffer";
|
||||||
|
@ -6,6 +8,9 @@ export * from "./to-equal-buffer";
|
||||||
export * from "./to-almost-equal";
|
export * from "./to-almost-equal";
|
||||||
export * from "./promise-fulfilled";
|
export * from "./promise-fulfilled";
|
||||||
|
|
||||||
|
// add all jest-extended matchers
|
||||||
|
expect.extend(matchers);
|
||||||
|
|
||||||
export function addCustomMatchers() {
|
export function addCustomMatchers() {
|
||||||
expect.extend({
|
expect.extend({
|
||||||
toEqualBuffer: toEqualBuffer,
|
toEqualBuffer: toEqualBuffer,
|
||||||
|
|
|
@ -46,8 +46,15 @@ export function makeStaticByteArray(length: number, start = 0) {
|
||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function makeSymmetricCryptoKey<T extends SymmetricCryptoKey>(length: 32 | 64 = 64) {
|
/**
|
||||||
return new SymmetricCryptoKey(makeStaticByteArray(length)) as T;
|
* Creates a symmetric crypto key for use in tests. This is deterministic, i.e. it will produce identical keys
|
||||||
|
* for identical argument values. Provide a unique value to the `seed` parameter to create different keys.
|
||||||
|
*/
|
||||||
|
export function makeSymmetricCryptoKey<T extends SymmetricCryptoKey>(
|
||||||
|
length: 32 | 64 = 64,
|
||||||
|
seed = 0,
|
||||||
|
) {
|
||||||
|
return new SymmetricCryptoKey(makeStaticByteArray(length, seed)) as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -154,6 +154,7 @@
|
||||||
"html-webpack-injector": "1.1.4",
|
"html-webpack-injector": "1.1.4",
|
||||||
"html-webpack-plugin": "5.6.0",
|
"html-webpack-plugin": "5.6.0",
|
||||||
"husky": "9.1.4",
|
"husky": "9.1.4",
|
||||||
|
"jest-extended": "^4.0.2",
|
||||||
"jest-junit": "16.0.0",
|
"jest-junit": "16.0.0",
|
||||||
"jest-mock-extended": "3.0.7",
|
"jest-mock-extended": "3.0.7",
|
||||||
"jest-preset-angular": "14.1.1",
|
"jest-preset-angular": "14.1.1",
|
||||||
|
@ -23930,6 +23931,27 @@
|
||||||
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
|
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/jest-extended": {
|
||||||
|
"version": "4.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/jest-extended/-/jest-extended-4.0.2.tgz",
|
||||||
|
"integrity": "sha512-FH7aaPgtGYHc9mRjriS0ZEHYM5/W69tLrFTIdzm+yJgeoCmmrSB/luSfMSqWP9O29QWHPEmJ4qmU6EwsZideog==",
|
||||||
|
"dev": true,
|
||||||
|
"dependencies": {
|
||||||
|
"jest-diff": "^29.0.0",
|
||||||
|
"jest-get-type": "^29.0.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"jest": ">=27.2.5"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"jest": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/jest-get-type": {
|
"node_modules/jest-get-type": {
|
||||||
"version": "29.6.3",
|
"version": "29.6.3",
|
||||||
"resolved": "https://registry.npmjs.org/jest-get-type/-/jest-get-type-29.6.3.tgz",
|
"resolved": "https://registry.npmjs.org/jest-get-type/-/jest-get-type-29.6.3.tgz",
|
||||||
|
|
|
@ -115,6 +115,7 @@
|
||||||
"html-webpack-injector": "1.1.4",
|
"html-webpack-injector": "1.1.4",
|
||||||
"html-webpack-plugin": "5.6.0",
|
"html-webpack-plugin": "5.6.0",
|
||||||
"husky": "9.1.4",
|
"husky": "9.1.4",
|
||||||
|
"jest-extended": "^4.0.2",
|
||||||
"jest-junit": "16.0.0",
|
"jest-junit": "16.0.0",
|
||||||
"jest-mock-extended": "3.0.7",
|
"jest-mock-extended": "3.0.7",
|
||||||
"jest-preset-angular": "14.1.1",
|
"jest-preset-angular": "14.1.1",
|
||||||
|
|
Loading…
Reference in New Issue