1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-11 05:43:41 +00:00

[PM-12049] Remove usage of ActiveUserState from folder service (#11880)

* Migrated folder service from using active user state to single user state

Added extra test cases for encrypted folder and decrypted folders

Updated derived state to use decrypt with key

* Update callers in the web

* Update callers in the browser

* Update callers in libs

* Update callers in cli

* Fixed test

* Fixed folder state test

* Fixed test

* removed duplicate activeUserId

* Added takewhile operator to only make calls when userId is present

* Simplified to accept a single user id instead of an observable

* Required userid to be passed from notification service

* [PM-15635] Folders not working on desktop (#12333)

* Added folders memory state definition

* added decrypted folders state

* Refactored service to remove derived state

* removed combinedstate and added clear decrypted folders to methods

* Fixed test

* Fixed issue with editing folder on the desktop app

* Fixed test

* Changed state name

* fixed ts strict issue

* fixed ts strict issue

* fixed ts strict issue

* removed unnecessasry null encrypteed folder check

* Handle null folderdata

* [PM-16197] "Items with No Folder" shows as a folder to edit name and delete (#12470)

* Force redcryption anytime encryption state changes

* Fixed text file

* revert changes

* create new object with nofolder instead of modifying exisiting object

* Fixed failing test

* switched to use memory-large-object

* Fixed ts sctrict issue

---------

Co-authored-by: Matt Bishop <mbishop@bitwarden.com>
Co-authored-by: bnagawiecki <107435978+bnagawiecki@users.noreply.github.com>
This commit is contained in:
SmithThe4th
2025-01-02 17:16:33 -05:00
committed by GitHub
parent b9660194be
commit 10c8a2101a
49 changed files with 600 additions and 395 deletions

View File

@@ -60,10 +60,18 @@ describe("NotificationBackground", () => {
const configService = mock<ConfigService>(); const configService = mock<ConfigService>();
const accountService = mock<AccountService>(); const accountService = mock<AccountService>();
const activeAccountSubject = new BehaviorSubject<{ id: UserId } & AccountInfo>({
id: "testId" as UserId,
email: "test@example.com",
emailVerified: true,
name: "Test User",
});
beforeEach(() => { beforeEach(() => {
activeAccountStatusMock$ = new BehaviorSubject(AuthenticationStatus.Locked); activeAccountStatusMock$ = new BehaviorSubject(AuthenticationStatus.Locked);
authService = mock<AuthService>(); authService = mock<AuthService>();
authService.activeAccountStatus$ = activeAccountStatusMock$; authService.activeAccountStatus$ = activeAccountStatusMock$;
accountService.activeAccount$ = activeAccountSubject;
notificationBackground = new NotificationBackground( notificationBackground = new NotificationBackground(
autofillService, autofillService,
cipherService, cipherService,
@@ -683,13 +691,6 @@ describe("NotificationBackground", () => {
}); });
describe("saveOrUpdateCredentials", () => { describe("saveOrUpdateCredentials", () => {
const activeAccountSubject = new BehaviorSubject<{ id: UserId } & AccountInfo>({
id: "testId" as UserId,
email: "test@example.com",
emailVerified: true,
name: "Test User",
});
let getDecryptedCipherByIdSpy: jest.SpyInstance; let getDecryptedCipherByIdSpy: jest.SpyInstance;
let getAllDecryptedForUrlSpy: jest.SpyInstance; let getAllDecryptedForUrlSpy: jest.SpyInstance;
let updatePasswordSpy: jest.SpyInstance; let updatePasswordSpy: jest.SpyInstance;

View File

@@ -83,6 +83,8 @@ export default class NotificationBackground {
getWebVaultUrlForNotification: () => this.getWebVaultUrl(), getWebVaultUrlForNotification: () => this.getWebVaultUrl(),
}; };
private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id));
constructor( constructor(
private autofillService: AutofillService, private autofillService: AutofillService,
private cipherService: CipherService, private cipherService: CipherService,
@@ -569,9 +571,7 @@ export default class NotificationBackground {
return; return;
} }
const activeUserId = await firstValueFrom( const activeUserId = await firstValueFrom(this.activeUserId$);
this.accountService.activeAccount$.pipe(map((a) => a?.id)),
);
const cipher = await this.cipherService.encrypt(newCipher, activeUserId); const cipher = await this.cipherService.encrypt(newCipher, activeUserId);
try { try {
@@ -611,10 +611,7 @@ export default class NotificationBackground {
return; return;
} }
const activeUserId = await firstValueFrom( const activeUserId = await firstValueFrom(this.activeUserId$);
this.accountService.activeAccount$.pipe(map((a) => a?.id)),
);
const cipher = await this.cipherService.encrypt(cipherView, activeUserId); const cipher = await this.cipherService.encrypt(cipherView, activeUserId);
try { try {
// We've only updated the password, no need to broadcast editedCipher message // We've only updated the password, no need to broadcast editedCipher message
@@ -647,17 +644,15 @@ export default class NotificationBackground {
if (Utils.isNullOrWhitespace(folderId) || folderId === "null") { if (Utils.isNullOrWhitespace(folderId) || folderId === "null") {
return false; return false;
} }
const activeUserId = await firstValueFrom(this.activeUserId$);
const folders = await firstValueFrom(this.folderService.folderViews$); const folders = await firstValueFrom(this.folderService.folderViews$(activeUserId));
return folders.some((x) => x.id === folderId); return folders.some((x) => x.id === folderId);
} }
private async getDecryptedCipherById(cipherId: string) { private async getDecryptedCipherById(cipherId: string) {
const cipher = await this.cipherService.get(cipherId); const cipher = await this.cipherService.get(cipherId);
if (cipher != null && cipher.type === CipherType.Login) { if (cipher != null && cipher.type === CipherType.Login) {
const activeUserId = await firstValueFrom( const activeUserId = await firstValueFrom(this.activeUserId$);
this.accountService.activeAccount$.pipe(map((a) => a?.id)),
);
return await cipher.decrypt( return await cipher.decrypt(
await this.cipherService.getKeyForCipherKeyDecryption(cipher, activeUserId), await this.cipherService.getKeyForCipherKeyDecryption(cipher, activeUserId),
@@ -697,7 +692,8 @@ export default class NotificationBackground {
* Returns the first value found from the folder service's folderViews$ observable. * Returns the first value found from the folder service's folderViews$ observable.
*/ */
private async getFolderData() { private async getFolderData() {
return await firstValueFrom(this.folderService.folderViews$); const activeUserId = await firstValueFrom(this.activeUserId$);
return await firstValueFrom(this.folderService.folderViews$(activeUserId));
} }
private async getWebVaultUrl(): Promise<string> { private async getWebVaultUrl(): Promise<string> {

View File

@@ -3,7 +3,6 @@ import { Subject } from "rxjs";
import { CollectionService } from "@bitwarden/admin-console/common"; import { CollectionService } from "@bitwarden/admin-console/common";
import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service"; import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { StateService } from "@bitwarden/common/platform/abstractions/state.service"; import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
@@ -30,12 +29,12 @@ describe("ForegroundSyncService", () => {
const cipherService = mock<CipherService>(); const cipherService = mock<CipherService>();
const collectionService = mock<CollectionService>(); const collectionService = mock<CollectionService>();
const apiService = mock<ApiService>(); const apiService = mock<ApiService>();
const accountService = mock<AccountService>(); const accountService = mockAccountServiceWith(userId);
const authService = mock<AuthService>(); const authService = mock<AuthService>();
const sendService = mock<InternalSendService>(); const sendService = mock<InternalSendService>();
const sendApiService = mock<SendApiService>(); const sendApiService = mock<SendApiService>();
const messageListener = mock<MessageListener>(); const messageListener = mock<MessageListener>();
const stateProvider = new FakeStateProvider(mockAccountServiceWith(userId)); const stateProvider = new FakeStateProvider(accountService);
const sut = new ForegroundSyncService( const sut = new ForegroundSyncService(
stateService, stateService,

View File

@@ -171,7 +171,7 @@ describe("AddEditFolderDialogComponent", () => {
it("deletes the folder", async () => { it("deletes the folder", async () => {
await component.deleteFolder(); await component.deleteFolder();
expect(deleteFolder).toHaveBeenCalledWith(folderView.id); expect(deleteFolder).toHaveBeenCalledWith(folderView.id, "");
expect(showToast).toHaveBeenCalledWith({ expect(showToast).toHaveBeenCalledWith({
variant: "success", variant: "success",
title: null, title: null,

View File

@@ -13,7 +13,7 @@ import {
} from "@angular/core"; } from "@angular/core";
import { takeUntilDestroyed } from "@angular/core/rxjs-interop"; import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
import { FormBuilder, ReactiveFormsModule, Validators } from "@angular/forms"; import { FormBuilder, ReactiveFormsModule, Validators } from "@angular/forms";
import { firstValueFrom } from "rxjs"; import { firstValueFrom, map } from "rxjs";
import { JslibModule } from "@bitwarden/angular/jslib.module"; import { JslibModule } from "@bitwarden/angular/jslib.module";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
@@ -67,6 +67,7 @@ export class AddEditFolderDialogComponent implements AfterViewInit, OnInit {
name: ["", Validators.required], name: ["", Validators.required],
}); });
private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id));
private destroyRef = inject(DestroyRef); private destroyRef = inject(DestroyRef);
constructor( constructor(
@@ -114,10 +115,10 @@ export class AddEditFolderDialogComponent implements AfterViewInit, OnInit {
this.folder.name = this.folderForm.controls.name.value; this.folder.name = this.folderForm.controls.name.value;
try { try {
const activeUserId = await firstValueFrom(this.accountService.activeAccount$); const activeUserId = await firstValueFrom(this.activeUserId$);
const userKey = await this.keyService.getUserKeyWithLegacySupport(activeUserId.id); const userKey = await this.keyService.getUserKeyWithLegacySupport(activeUserId);
const folder = await this.folderService.encrypt(this.folder, userKey); const folder = await this.folderService.encrypt(this.folder, userKey);
await this.folderApiService.save(folder); await this.folderApiService.save(folder, activeUserId);
this.toastService.showToast({ this.toastService.showToast({
variant: "success", variant: "success",
@@ -144,7 +145,8 @@ export class AddEditFolderDialogComponent implements AfterViewInit, OnInit {
} }
try { try {
await this.folderApiService.delete(this.folder.id); const activeUserId = await firstValueFrom(this.activeUserId$);
await this.folderApiService.delete(this.folder.id, activeUserId);
this.toastService.showToast({ this.toastService.showToast({
variant: "success", variant: "success",
title: null, title: null,

View File

@@ -7,9 +7,12 @@ import { OrganizationService } from "@bitwarden/common/admin-console/abstraction
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction"; import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
import { PolicyType } from "@bitwarden/common/admin-console/enums"; import { PolicyType } from "@bitwarden/common/admin-console/enums";
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { ProductTierType } from "@bitwarden/common/billing/enums"; import { ProductTierType } from "@bitwarden/common/billing/enums";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { StateProvider } from "@bitwarden/common/platform/state"; import { StateProvider } from "@bitwarden/common/platform/state";
import { mockAccountServiceWith } from "@bitwarden/common/spec";
import { UserId } from "@bitwarden/common/types/guid";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction"; import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
import { CipherType } from "@bitwarden/common/vault/enums"; import { CipherType } from "@bitwarden/common/vault/enums";
@@ -32,7 +35,7 @@ describe("VaultPopupListFiltersService", () => {
} as unknown as CollectionService; } as unknown as CollectionService;
const folderService = { const folderService = {
folderViews$, folderViews$: () => folderViews$,
} as unknown as FolderService; } as unknown as FolderService;
const cipherService = { const cipherService = {
@@ -60,6 +63,8 @@ describe("VaultPopupListFiltersService", () => {
policyAppliesToActiveUser$.next(false); policyAppliesToActiveUser$.next(false);
policyService.policyAppliesToActiveUser$.mockClear(); policyService.policyAppliesToActiveUser$.mockClear();
const accountService = mockAccountServiceWith("userId" as UserId);
collectionService.getAllNested = () => Promise.resolve([]); collectionService.getAllNested = () => Promise.resolve([]);
TestBed.configureTestingModule({ TestBed.configureTestingModule({
providers: [ providers: [
@@ -92,6 +97,10 @@ describe("VaultPopupListFiltersService", () => {
useValue: { getGlobal: () => ({ state$, update }) }, useValue: { getGlobal: () => ({ state$, update }) },
}, },
{ provide: FormBuilder, useClass: FormBuilder }, { provide: FormBuilder, useClass: FormBuilder },
{
provide: AccountService,
useValue: accountService,
},
], ],
}); });

View File

@@ -20,6 +20,7 @@ import { OrganizationService } from "@bitwarden/common/admin-console/abstraction
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction"; import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
import { PolicyType } from "@bitwarden/common/admin-console/enums"; import { PolicyType } from "@bitwarden/common/admin-console/enums";
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { ProductTierType } from "@bitwarden/common/billing/enums"; import { ProductTierType } from "@bitwarden/common/billing/enums";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { Utils } from "@bitwarden/common/platform/misc/utils"; import { Utils } from "@bitwarden/common/platform/misc/utils";
@@ -102,6 +103,8 @@ export class VaultPopupListFiltersService {
map((ciphers) => Object.values(ciphers)), map((ciphers) => Object.values(ciphers)),
); );
private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id));
constructor( constructor(
private folderService: FolderService, private folderService: FolderService,
private cipherService: CipherService, private cipherService: CipherService,
@@ -111,6 +114,7 @@ export class VaultPopupListFiltersService {
private formBuilder: FormBuilder, private formBuilder: FormBuilder,
private policyService: PolicyService, private policyService: PolicyService,
private stateProvider: StateProvider, private stateProvider: StateProvider,
private accountService: AccountService,
) { ) {
this.filterForm.controls.organization.valueChanges this.filterForm.controls.organization.valueChanges
.pipe(takeUntilDestroyed()) .pipe(takeUntilDestroyed())
@@ -264,61 +268,68 @@ export class VaultPopupListFiltersService {
/** /**
* Folder array structured to be directly passed to `ChipSelectComponent` * Folder array structured to be directly passed to `ChipSelectComponent`
*/ */
folders$: Observable<ChipSelectOption<FolderView>[]> = combineLatest([ folders$: Observable<ChipSelectOption<FolderView>[]> = this.activeUserId$.pipe(
this.filters$.pipe( switchMap((userId) =>
distinctUntilChanged( combineLatest([
(previousFilter, currentFilter) => this.filters$.pipe(
// Only update the collections when the organizationId filter changes distinctUntilChanged(
previousFilter.organization?.id === currentFilter.organization?.id, (previousFilter, currentFilter) =>
// Only update the collections when the organizationId filter changes
previousFilter.organization?.id === currentFilter.organization?.id,
),
),
this.folderService.folderViews$(userId),
this.cipherViews$,
]).pipe(
map(([filters, folders, cipherViews]): [PopupListFilter, FolderView[], CipherView[]] => {
if (folders.length === 1 && folders[0].id === null) {
// Do not display folder selections when only the "no folder" option is available.
return [filters, [], cipherViews];
}
// Sort folders by alphabetic name
folders.sort(Utils.getSortFunction(this.i18nService, "name"));
let arrangedFolders = folders;
const noFolder = folders.find((f) => f.id === null);
if (noFolder) {
// Update `name` of the "no folder" option to "Items with no folder"
const updatedNoFolder = {
...noFolder,
name: this.i18nService.t("itemsWithNoFolder"),
};
// Move the "no folder" option to the end of the list
arrangedFolders = [...folders.filter((f) => f.id !== null), updatedNoFolder];
}
return [filters, arrangedFolders, cipherViews];
}),
map(([filters, folders, cipherViews]) => {
const organizationId = filters.organization?.id ?? null;
// When no org or "My vault" is selected, return all folders
if (organizationId === null || organizationId === MY_VAULT_ID) {
return folders;
}
const orgCiphers = cipherViews.filter((c) => c.organizationId === organizationId);
// Return only the folders that have ciphers within the filtered organization
return folders.filter((f) => orgCiphers.some((oc) => oc.folderId === f.id));
}),
map((folders) => {
const nestedFolders = this.getAllFoldersNested(folders);
return new DynamicTreeNode<FolderView>({
fullList: folders,
nestedList: nestedFolders,
});
}),
map((folders) =>
folders.nestedList.map((f) => this.convertToChipSelectOption(f, "bwi-folder")),
),
), ),
), ),
this.folderService.folderViews$,
this.cipherViews$,
]).pipe(
map(([filters, folders, cipherViews]): [PopupListFilter, FolderView[], CipherView[]] => {
if (folders.length === 1 && folders[0].id === null) {
// Do not display folder selections when only the "no folder" option is available.
return [filters, [], cipherViews];
}
// Sort folders by alphabetic name
folders.sort(Utils.getSortFunction(this.i18nService, "name"));
let arrangedFolders = folders;
const noFolder = folders.find((f) => f.id === null);
if (noFolder) {
// Update `name` of the "no folder" option to "Items with no folder"
noFolder.name = this.i18nService.t("itemsWithNoFolder");
// Move the "no folder" option to the end of the list
arrangedFolders = [...folders.filter((f) => f.id !== null), noFolder];
}
return [filters, arrangedFolders, cipherViews];
}),
map(([filters, folders, cipherViews]) => {
const organizationId = filters.organization?.id ?? null;
// When no org or "My vault" is selected, return all folders
if (organizationId === null || organizationId === MY_VAULT_ID) {
return folders;
}
const orgCiphers = cipherViews.filter((c) => c.organizationId === organizationId);
// Return only the folders that have ciphers within the filtered organization
return folders.filter((f) => orgCiphers.some((oc) => oc.folderId === f.id));
}),
map((folders) => {
const nestedFolders = this.getAllFoldersNested(folders);
return new DynamicTreeNode<FolderView>({
fullList: folders,
nestedList: nestedFolders,
});
}),
map((folders) =>
folders.nestedList.map((f) => this.convertToChipSelectOption(f, "bwi-folder")),
),
); );
/** /**

View File

@@ -4,10 +4,13 @@ import { By } from "@angular/platform-browser";
import { mock } from "jest-mock-extended"; import { mock } from "jest-mock-extended";
import { BehaviorSubject } from "rxjs"; import { BehaviorSubject } from "rxjs";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service"; import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { mockAccountServiceWith } from "@bitwarden/common/spec";
import { UserId } from "@bitwarden/common/types/guid";
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction"; import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
import { FolderView } from "@bitwarden/common/vault/models/view/folder.view"; import { FolderView } from "@bitwarden/common/vault/models/view/folder.view";
import { DialogService } from "@bitwarden/components"; import { DialogService } from "@bitwarden/components";
@@ -52,8 +55,9 @@ describe("FoldersV2Component", () => {
{ provide: PlatformUtilsService, useValue: mock<PlatformUtilsService>() }, { provide: PlatformUtilsService, useValue: mock<PlatformUtilsService>() },
{ provide: ConfigService, useValue: mock<ConfigService>() }, { provide: ConfigService, useValue: mock<ConfigService>() },
{ provide: LogService, useValue: mock<LogService>() }, { provide: LogService, useValue: mock<LogService>() },
{ provide: FolderService, useValue: { folderViews$ } }, { provide: FolderService, useValue: { folderViews$: () => folderViews$ } },
{ provide: I18nService, useValue: { t: (key: string) => key } }, { provide: I18nService, useValue: { t: (key: string) => key } },
{ provide: AccountService, useValue: mockAccountServiceWith("UserId" as UserId) },
], ],
}) })
.overrideComponent(FoldersV2Component, { .overrideComponent(FoldersV2Component, {

View File

@@ -1,8 +1,10 @@
import { CommonModule } from "@angular/common"; import { CommonModule } from "@angular/common";
import { Component } from "@angular/core"; import { Component } from "@angular/core";
import { map, Observable } from "rxjs"; import { filter, map, Observable, switchMap } from "rxjs";
import { JslibModule } from "@bitwarden/angular/jslib.module"; import { JslibModule } from "@bitwarden/angular/jslib.module";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { UserId } from "@bitwarden/common/types/guid";
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction"; import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
import { FolderView } from "@bitwarden/common/vault/models/view/folder.view"; import { FolderView } from "@bitwarden/common/vault/models/view/folder.view";
import { import {
@@ -45,18 +47,21 @@ export class FoldersV2Component {
folders$: Observable<FolderView[]>; folders$: Observable<FolderView[]>;
NoFoldersIcon = VaultIcons.NoFolders; NoFoldersIcon = VaultIcons.NoFolders;
private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id));
constructor( constructor(
private folderService: FolderService, private folderService: FolderService,
private dialogService: DialogService, private dialogService: DialogService,
private accountService: AccountService,
) { ) {
this.folders$ = this.folderService.folderViews$.pipe( this.folders$ = this.activeUserId$.pipe(
filter((userId): userId is UserId => userId !== null),
switchMap((userId) => this.folderService.folderViews$(userId)),
map((folders) => { map((folders) => {
// Remove the last folder, which is the "no folder" option folder // Remove the last folder, which is the "no folder" option folder
if (folders.length > 0) { if (folders.length > 0) {
return folders.slice(0, folders.length - 1); return folders.slice(0, folders.length - 1);
} }
return folders; return folders;
}), }),
); );

View File

@@ -1,7 +1,9 @@
import { Component } from "@angular/core"; import { Component } from "@angular/core";
import { Router } from "@angular/router"; import { Router } from "@angular/router";
import { map, Observable } from "rxjs"; import { filter, map, Observable, switchMap } from "rxjs";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { UserId } from "@bitwarden/common/types/guid";
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction"; import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
import { FolderView } from "@bitwarden/common/vault/models/view/folder.view"; import { FolderView } from "@bitwarden/common/vault/models/view/folder.view";
@@ -12,16 +14,21 @@ import { FolderView } from "@bitwarden/common/vault/models/view/folder.view";
export class FoldersComponent { export class FoldersComponent {
folders$: Observable<FolderView[]>; folders$: Observable<FolderView[]>;
private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id));
constructor( constructor(
private folderService: FolderService, private folderService: FolderService,
private router: Router, private router: Router,
private accountService: AccountService,
) { ) {
this.folders$ = this.folderService.folderViews$.pipe( this.folders$ = this.activeUserId$.pipe(
filter((userId): userId is UserId => userId != null),
switchMap((userId) => this.folderService.folderViews$(userId)),
map((folders) => { map((folders) => {
// Remove the last folder, which is the "no folder" option folder
if (folders.length > 0) { if (folders.length > 0) {
folders = folders.slice(0, folders.length - 1); return folders.slice(0, folders.length - 1);
} }
return folders; return folders;
}), }),
); );

View File

@@ -24,7 +24,7 @@ export class VaultFilterService extends BaseVaultFilterService {
collectionService: CollectionService, collectionService: CollectionService,
policyService: PolicyService, policyService: PolicyService,
stateProvider: StateProvider, stateProvider: StateProvider,
private accountService: AccountService, accountService: AccountService,
) { ) {
super( super(
organizationService, organizationService,
@@ -33,6 +33,7 @@ export class VaultFilterService extends BaseVaultFilterService {
collectionService, collectionService,
policyService, policyService,
stateProvider, stateProvider,
accountService,
); );
this.vaultFilter.myVaultOnly = false; this.vaultFilter.myVaultOnly = false;
this.vaultFilter.selectedOrganizationId = null; this.vaultFilter.selectedOrganizationId = null;

View File

@@ -24,6 +24,8 @@ import { CipherResponse } from "../vault/models/cipher.response";
import { FolderResponse } from "../vault/models/folder.response"; import { FolderResponse } from "../vault/models/folder.response";
export class EditCommand { export class EditCommand {
private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id));
constructor( constructor(
private cipherService: CipherService, private cipherService: CipherService,
private folderService: FolderService, private folderService: FolderService,
@@ -121,12 +123,12 @@ export class EditCommand {
cipher.collectionIds = req; cipher.collectionIds = req;
try { try {
const activeUserId = await firstValueFrom(
this.accountService.activeAccount$.pipe(map((a) => a?.id)),
);
const updatedCipher = await this.cipherService.saveCollectionsWithServer(cipher); const updatedCipher = await this.cipherService.saveCollectionsWithServer(cipher);
const decCipher = await updatedCipher.decrypt( const decCipher = await updatedCipher.decrypt(
await this.cipherService.getKeyForCipherKeyDecryption(updatedCipher, activeUserId), await this.cipherService.getKeyForCipherKeyDecryption(
updatedCipher,
await firstValueFrom(this.activeUserId$),
),
); );
const res = new CipherResponse(decCipher); const res = new CipherResponse(decCipher);
return Response.success(res); return Response.success(res);
@@ -136,7 +138,8 @@ export class EditCommand {
} }
private async editFolder(id: string, req: FolderExport) { private async editFolder(id: string, req: FolderExport) {
const folder = await this.folderService.getFromState(id); const activeUserId = await firstValueFrom(this.activeUserId$);
const folder = await this.folderService.getFromState(id, activeUserId);
if (folder == null) { if (folder == null) {
return Response.notFound(); return Response.notFound();
} }
@@ -144,12 +147,11 @@ export class EditCommand {
let folderView = await folder.decrypt(); let folderView = await folder.decrypt();
folderView = FolderExport.toView(req, folderView); folderView = FolderExport.toView(req, folderView);
const activeUserId = await firstValueFrom(this.accountService.activeAccount$); const userKey = await this.keyService.getUserKeyWithLegacySupport(activeUserId);
const userKey = await this.keyService.getUserKeyWithLegacySupport(activeUserId.id);
const encFolder = await this.folderService.encrypt(folderView, userKey); const encFolder = await this.folderService.encrypt(folderView, userKey);
try { try {
await this.folderApiService.save(encFolder); await this.folderApiService.save(encFolder, activeUserId);
const updatedFolder = await this.folderService.get(folder.id); const updatedFolder = await this.folderService.get(folder.id, activeUserId);
const decFolder = await updatedFolder.decrypt(); const decFolder = await updatedFolder.decrypt();
const res = new FolderResponse(decFolder); const res = new FolderResponse(decFolder);
return Response.success(res); return Response.success(res);

View File

@@ -51,6 +51,8 @@ import { FolderResponse } from "../vault/models/folder.response";
import { DownloadCommand } from "./download.command"; import { DownloadCommand } from "./download.command";
export class GetCommand extends DownloadCommand { export class GetCommand extends DownloadCommand {
private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id));
constructor( constructor(
private cipherService: CipherService, private cipherService: CipherService,
private folderService: FolderService, private folderService: FolderService,
@@ -113,10 +115,8 @@ export class GetCommand extends DownloadCommand {
let decCipher: CipherView = null; let decCipher: CipherView = null;
if (Utils.isGuid(id)) { if (Utils.isGuid(id)) {
const cipher = await this.cipherService.get(id); const cipher = await this.cipherService.get(id);
const activeUserId = await firstValueFrom(
this.accountService.activeAccount$.pipe(map((a) => a?.id)),
);
if (cipher != null) { if (cipher != null) {
const activeUserId = await firstValueFrom(this.activeUserId$);
decCipher = await cipher.decrypt( decCipher = await cipher.decrypt(
await this.cipherService.getKeyForCipherKeyDecryption(cipher, activeUserId), await this.cipherService.getKeyForCipherKeyDecryption(cipher, activeUserId),
); );
@@ -383,13 +383,14 @@ export class GetCommand extends DownloadCommand {
private async getFolder(id: string) { private async getFolder(id: string) {
let decFolder: FolderView = null; let decFolder: FolderView = null;
const activeUserId = await firstValueFrom(this.activeUserId$);
if (Utils.isGuid(id)) { if (Utils.isGuid(id)) {
const folder = await this.folderService.getFromState(id); const folder = await this.folderService.getFromState(id, activeUserId);
if (folder != null) { if (folder != null) {
decFolder = await folder.decrypt(); decFolder = await folder.decrypt();
} }
} else if (id.trim() !== "") { } else if (id.trim() !== "") {
let folders = await this.folderService.getAllDecryptedFromState(); let folders = await this.folderService.getAllDecryptedFromState(activeUserId);
folders = CliUtils.searchFolders(folders, id); folders = CliUtils.searchFolders(folders, id);
if (folders.length > 1) { if (folders.length > 1) {
return Response.multipleResults(folders.map((f) => f.id)); return Response.multipleResults(folders.map((f) => f.id));
@@ -551,9 +552,7 @@ export class GetCommand extends DownloadCommand {
private async getFingerprint(id: string) { private async getFingerprint(id: string) {
let fingerprint: string[] = null; let fingerprint: string[] = null;
if (id === "me") { if (id === "me") {
const activeUserId = await firstValueFrom( const activeUserId = await firstValueFrom(this.activeUserId$);
this.accountService.activeAccount$.pipe(map((a) => a?.id)),
);
const publicKey = await firstValueFrom(this.keyService.userPublicKey$(activeUserId)); const publicKey = await firstValueFrom(this.keyService.userPublicKey$(activeUserId));
fingerprint = await this.keyService.getFingerprint(activeUserId, publicKey); fingerprint = await this.keyService.getFingerprint(activeUserId, publicKey);
} else if (Utils.isGuid(id)) { } else if (Utils.isGuid(id)) {

View File

@@ -1,4 +1,4 @@
import { firstValueFrom } from "rxjs"; import { firstValueFrom, map } from "rxjs";
import { import {
OrganizationUserApiService, OrganizationUserApiService,
@@ -12,6 +12,7 @@ import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { EventCollectionService } from "@bitwarden/common/abstractions/event/event-collection.service"; import { EventCollectionService } from "@bitwarden/common/abstractions/event/event-collection.service";
import { SearchService } from "@bitwarden/common/abstractions/search.service"; import { SearchService } from "@bitwarden/common/abstractions/search.service";
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { EventType } from "@bitwarden/common/enums"; import { EventType } from "@bitwarden/common/enums";
import { ListResponse as ApiListResponse } from "@bitwarden/common/models/response/list.response"; import { ListResponse as ApiListResponse } from "@bitwarden/common/models/response/list.response";
import { Utils } from "@bitwarden/common/platform/misc/utils"; import { Utils } from "@bitwarden/common/platform/misc/utils";
@@ -38,6 +39,7 @@ export class ListCommand {
private organizationUserApiService: OrganizationUserApiService, private organizationUserApiService: OrganizationUserApiService,
private apiService: ApiService, private apiService: ApiService,
private eventCollectionService: EventCollectionService, private eventCollectionService: EventCollectionService,
private accountService: AccountService,
) {} ) {}
async run(object: string, cmdOptions: Record<string, any>): Promise<Response> { async run(object: string, cmdOptions: Record<string, any>): Promise<Response> {
@@ -135,7 +137,10 @@ export class ListCommand {
} }
private async listFolders(options: Options) { private async listFolders(options: Options) {
let folders = await this.folderService.getAllDecryptedFromState(); const activeUserId = await firstValueFrom(
this.accountService.activeAccount$.pipe(map((a) => a?.id)),
);
let folders = await this.folderService.getAllDecryptedFromState(activeUserId);
if (options.search != null && options.search.trim() !== "") { if (options.search != null && options.search.trim() !== "") {
folders = CliUtils.searchFolders(folders, options.search); folders = CliUtils.searchFolders(folders, options.search);

View File

@@ -76,6 +76,7 @@ export class OssServeConfigurator {
this.serviceContainer.organizationUserApiService, this.serviceContainer.organizationUserApiService,
this.serviceContainer.apiService, this.serviceContainer.apiService,
this.serviceContainer.eventCollectionService, this.serviceContainer.eventCollectionService,
this.serviceContainer.accountService,
); );
this.createCommand = new CreateCommand( this.createCommand = new CreateCommand(
this.serviceContainer.cipherService, this.serviceContainer.cipherService,
@@ -115,6 +116,7 @@ export class OssServeConfigurator {
this.serviceContainer.folderApiService, this.serviceContainer.folderApiService,
this.serviceContainer.billingAccountProfileStateService, this.serviceContainer.billingAccountProfileStateService,
this.serviceContainer.cipherAuthorizationService, this.serviceContainer.cipherAuthorizationService,
this.serviceContainer.accountService,
); );
this.confirmCommand = new ConfirmCommand( this.confirmCommand = new ConfirmCommand(
this.serviceContainer.apiService, this.serviceContainer.apiService,

View File

@@ -113,6 +113,7 @@ export class VaultProgram extends BaseProgram {
this.serviceContainer.organizationUserApiService, this.serviceContainer.organizationUserApiService,
this.serviceContainer.apiService, this.serviceContainer.apiService,
this.serviceContainer.eventCollectionService, this.serviceContainer.eventCollectionService,
this.serviceContainer.accountService,
); );
const response = await command.run(object, cmd); const response = await command.run(object, cmd);
@@ -321,6 +322,7 @@ export class VaultProgram extends BaseProgram {
this.serviceContainer.folderApiService, this.serviceContainer.folderApiService,
this.serviceContainer.billingAccountProfileStateService, this.serviceContainer.billingAccountProfileStateService,
this.serviceContainer.cipherAuthorizationService, this.serviceContainer.cipherAuthorizationService,
this.serviceContainer.accountService,
); );
const response = await command.run(object, id, cmd); const response = await command.run(object, id, cmd);
this.processResponse(response); this.processResponse(response);

View File

@@ -30,6 +30,8 @@ import { CipherResponse } from "./models/cipher.response";
import { FolderResponse } from "./models/folder.response"; import { FolderResponse } from "./models/folder.response";
export class CreateCommand { export class CreateCommand {
private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id));
constructor( constructor(
private cipherService: CipherService, private cipherService: CipherService,
private folderService: FolderService, private folderService: FolderService,
@@ -86,9 +88,7 @@ export class CreateCommand {
} }
private async createCipher(req: CipherExport) { private async createCipher(req: CipherExport) {
const activeUserId = await firstValueFrom( const activeUserId = await firstValueFrom(this.activeUserId$);
this.accountService.activeAccount$.pipe(map((a) => a?.id)),
);
const cipher = await this.cipherService.encrypt(CipherExport.toView(req), activeUserId); const cipher = await this.cipherService.encrypt(CipherExport.toView(req), activeUserId);
try { try {
const newCipher = await this.cipherService.createWithServer(cipher); const newCipher = await this.cipherService.createWithServer(cipher);
@@ -152,9 +152,7 @@ export class CreateCommand {
} }
try { try {
const activeUserId = await firstValueFrom( const activeUserId = await firstValueFrom(this.activeUserId$);
this.accountService.activeAccount$.pipe(map((a) => a?.id)),
);
const updatedCipher = await this.cipherService.saveAttachmentRawWithServer( const updatedCipher = await this.cipherService.saveAttachmentRawWithServer(
cipher, cipher,
fileName, fileName,
@@ -171,12 +169,12 @@ export class CreateCommand {
} }
private async createFolder(req: FolderExport) { private async createFolder(req: FolderExport) {
const activeAccountId = await firstValueFrom(this.accountService.activeAccount$); const activeUserId = await firstValueFrom(this.activeUserId$);
const userKey = await this.keyService.getUserKeyWithLegacySupport(activeAccountId.id); const userKey = await this.keyService.getUserKeyWithLegacySupport(activeUserId);
const folder = await this.folderService.encrypt(FolderExport.toView(req), userKey); const folder = await this.folderService.encrypt(FolderExport.toView(req), userKey);
try { try {
await this.folderApiService.save(folder); await this.folderApiService.save(folder, activeUserId);
const newFolder = await this.folderService.get(folder.id); const newFolder = await this.folderService.get(folder.id, activeUserId);
const decFolder = await newFolder.decrypt(); const decFolder = await newFolder.decrypt();
const res = new FolderResponse(decFolder); const res = new FolderResponse(decFolder);
return Response.success(res); return Response.success(res);

View File

@@ -1,6 +1,7 @@
import { firstValueFrom } from "rxjs"; import { firstValueFrom, map } from "rxjs";
import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service";
import { Utils } from "@bitwarden/common/platform/misc/utils"; import { Utils } from "@bitwarden/common/platform/misc/utils";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
@@ -19,6 +20,7 @@ export class DeleteCommand {
private folderApiService: FolderApiServiceAbstraction, private folderApiService: FolderApiServiceAbstraction,
private accountProfileService: BillingAccountProfileStateService, private accountProfileService: BillingAccountProfileStateService,
private cipherAuthorizationService: CipherAuthorizationService, private cipherAuthorizationService: CipherAuthorizationService,
private accountService: AccountService,
) {} ) {}
async run(object: string, id: string, cmdOptions: Record<string, any>): Promise<Response> { async run(object: string, id: string, cmdOptions: Record<string, any>): Promise<Response> {
@@ -103,13 +105,16 @@ export class DeleteCommand {
} }
private async deleteFolder(id: string) { private async deleteFolder(id: string) {
const folder = await this.folderService.getFromState(id); const activeUserId = await firstValueFrom(
this.accountService.activeAccount$.pipe(map((a) => a?.id)),
);
const folder = await this.folderService.getFromState(id, activeUserId);
if (folder == null) { if (folder == null) {
return Response.notFound(); return Response.notFound();
} }
try { try {
await this.folderApiService.delete(id); await this.folderApiService.delete(id, activeUserId);
return Response.success(); return Response.success();
} catch (e) { } catch (e) {
return Response.error(e); return Response.error(e);

View File

@@ -6,7 +6,11 @@ import { mock } from "jest-mock-extended";
import { CollectionService } from "@bitwarden/admin-console/common"; import { CollectionService } from "@bitwarden/admin-console/common";
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { Utils } from "@bitwarden/common/platform/misc/utils";
import { FakeAccountService, mockAccountServiceWith } from "@bitwarden/common/spec";
import { UserId } from "@bitwarden/common/types/guid";
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction"; import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
import { CipherType } from "@bitwarden/common/vault/enums"; import { CipherType } from "@bitwarden/common/vault/enums";
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view"; import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
@@ -29,6 +33,8 @@ describe("EmergencyViewDialogComponent", () => {
card: {}, card: {},
} as CipherView; } as CipherView;
const accountService: FakeAccountService = mockAccountServiceWith(Utils.newGuid() as UserId);
beforeEach(async () => { beforeEach(async () => {
open.mockClear(); open.mockClear();
close.mockClear(); close.mockClear();
@@ -43,6 +49,7 @@ describe("EmergencyViewDialogComponent", () => {
{ provide: DialogService, useValue: { open } }, { provide: DialogService, useValue: { open } },
{ provide: DialogRef, useValue: { close } }, { provide: DialogRef, useValue: { close } },
{ provide: DIALOG_DATA, useValue: { cipher: mockCipher } }, { provide: DIALOG_DATA, useValue: { cipher: mockCipher } },
{ provide: AccountService, useValue: accountService },
], ],
}).compileComponents(); }).compileComponents();

View File

@@ -83,7 +83,7 @@ export class MigrateFromLegacyEncryptionComponent {
}); });
if (deleteFolders) { if (deleteFolders) {
await this.folderApiService.deleteAll(); await this.folderApiService.deleteAll(activeUser.id);
await this.syncService.fullSync(true, true); await this.syncService.fullSync(true, true);
await this.submit(); await this.submit();
return; return;

View File

@@ -3,8 +3,9 @@
import { DialogConfig, DialogRef, DIALOG_DATA } from "@angular/cdk/dialog"; import { DialogConfig, DialogRef, DIALOG_DATA } from "@angular/cdk/dialog";
import { Component, Inject, OnInit } from "@angular/core"; import { Component, Inject, OnInit } from "@angular/core";
import { FormBuilder, Validators } from "@angular/forms"; import { FormBuilder, Validators } from "@angular/forms";
import { firstValueFrom, Observable } from "rxjs"; import { firstValueFrom, map, Observable } from "rxjs";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
@@ -47,6 +48,8 @@ export class BulkMoveDialogComponent implements OnInit {
}); });
folders$: Observable<FolderView[]>; folders$: Observable<FolderView[]>;
private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id));
constructor( constructor(
@Inject(DIALOG_DATA) params: BulkMoveDialogParams, @Inject(DIALOG_DATA) params: BulkMoveDialogParams,
private dialogRef: DialogRef<BulkMoveDialogResult>, private dialogRef: DialogRef<BulkMoveDialogResult>,
@@ -55,12 +58,14 @@ export class BulkMoveDialogComponent implements OnInit {
private i18nService: I18nService, private i18nService: I18nService,
private folderService: FolderService, private folderService: FolderService,
private formBuilder: FormBuilder, private formBuilder: FormBuilder,
private accountService: AccountService,
) { ) {
this.cipherIds = params.cipherIds ?? []; this.cipherIds = params.cipherIds ?? [];
} }
async ngOnInit() { async ngOnInit() {
this.folders$ = this.folderService.folderViews$; const activeUserId = await firstValueFrom(this.activeUserId$);
this.folders$ = this.folderService.folderViews$(activeUserId);
this.formGroup.patchValue({ this.formGroup.patchValue({
folderId: (await firstValueFrom(this.folders$))[0].id, folderId: (await firstValueFrom(this.folders$))[0].id,
}); });

View File

@@ -61,7 +61,7 @@ export class FolderAddEditComponent extends BaseFolderAddEditComponent {
} }
try { try {
await this.folderApiService.delete(this.folder.id); await this.folderApiService.delete(this.folder.id, await firstValueFrom(this.activeUserId$));
this.toastService.showToast({ this.toastService.showToast({
variant: "success", variant: "success",
title: null, title: null,
@@ -82,10 +82,10 @@ export class FolderAddEditComponent extends BaseFolderAddEditComponent {
} }
try { try {
const activeAccountId = (await firstValueFrom(this.accountSerivce.activeAccount$)).id; const activeAccountId = await firstValueFrom(this.activeUserId$);
const userKey = await this.keyService.getUserKeyWithLegacySupport(activeAccountId); const userKey = await this.keyService.getUserKeyWithLegacySupport(activeAccountId);
const folder = await this.folderService.encrypt(this.folder, userKey); const folder = await this.folderService.encrypt(this.folder, userKey);
this.formPromise = this.folderApiService.save(folder); this.formPromise = this.folderApiService.save(folder, activeAccountId);
await this.formPromise; await this.formPromise;
this.platformUtilsService.showToast( this.platformUtilsService.showToast(
"success", "success",

View File

@@ -63,7 +63,7 @@ describe("vault filter service", () => {
singleOrgPolicy = new ReplaySubject<boolean>(1); singleOrgPolicy = new ReplaySubject<boolean>(1);
organizationService.memberOrganizations$ = organizations; organizationService.memberOrganizations$ = organizations;
folderService.folderViews$ = folderViews; folderService.folderViews$.mockReturnValue(folderViews);
collectionService.decryptedCollections$ = collectionViews; collectionService.decryptedCollections$ = collectionViews;
policyService.policyAppliesToActiveUser$ policyService.policyAppliesToActiveUser$
.calledWith(PolicyType.PersonalOwnership) .calledWith(PolicyType.PersonalOwnership)
@@ -81,6 +81,7 @@ describe("vault filter service", () => {
i18nService, i18nService,
stateProvider, stateProvider,
collectionService, collectionService,
accountService,
); );
collapsedGroupingsState = stateProvider.activeUser.getFake(COLLAPSED_GROUPINGS); collapsedGroupingsState = stateProvider.activeUser.getFake(COLLAPSED_GROUPINGS);
}); });

View File

@@ -21,6 +21,7 @@ import { OrganizationService } from "@bitwarden/common/admin-console/abstraction
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction"; import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
import { PolicyType } from "@bitwarden/common/admin-console/enums"; import { PolicyType } from "@bitwarden/common/admin-console/enums";
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { ActiveUserState, StateProvider } from "@bitwarden/common/platform/state"; import { ActiveUserState, StateProvider } from "@bitwarden/common/platform/state";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
@@ -45,6 +46,8 @@ const NestingDelimiter = "/";
@Injectable() @Injectable()
export class VaultFilterService implements VaultFilterServiceAbstraction { export class VaultFilterService implements VaultFilterServiceAbstraction {
private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id));
organizationTree$: Observable<TreeNode<OrganizationFilter>> = combineLatest([ organizationTree$: Observable<TreeNode<OrganizationFilter>> = combineLatest([
this.organizationService.memberOrganizations$, this.organizationService.memberOrganizations$,
this.policyService.policyAppliesToActiveUser$(PolicyType.SingleOrg), this.policyService.policyAppliesToActiveUser$(PolicyType.SingleOrg),
@@ -57,8 +60,14 @@ export class VaultFilterService implements VaultFilterServiceAbstraction {
protected _organizationFilter = new BehaviorSubject<Organization>(null); protected _organizationFilter = new BehaviorSubject<Organization>(null);
filteredFolders$: Observable<FolderView[]> = this.folderService.folderViews$.pipe( filteredFolders$: Observable<FolderView[]> = this.activeUserId$.pipe(
combineLatestWith(this.cipherService.cipherViews$, this._organizationFilter), switchMap((userId) =>
combineLatest([
this.folderService.folderViews$(userId),
this.cipherService.cipherViews$,
this._organizationFilter,
]),
),
switchMap(([folders, ciphers, org]) => { switchMap(([folders, ciphers, org]) => {
return this.filterFolders(folders, ciphers, org); return this.filterFolders(folders, ciphers, org);
}), }),
@@ -95,6 +104,7 @@ export class VaultFilterService implements VaultFilterServiceAbstraction {
protected i18nService: I18nService, protected i18nService: I18nService,
protected stateProvider: StateProvider, protected stateProvider: StateProvider,
protected collectionService: CollectionService, protected collectionService: CollectionService,
protected accountService: AccountService,
) {} ) {}
async getCollectionNodeFromTree(id: string) { async getCollectionNodeFromTree(id: string) {

View File

@@ -5,11 +5,14 @@ import { mock } from "jest-mock-extended";
import { CollectionService } from "@bitwarden/admin-console/common"; import { CollectionService } from "@bitwarden/admin-console/common";
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service";
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service"; import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service"; import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
import { mockAccountServiceWith } from "@bitwarden/common/spec";
import { UserId } from "@bitwarden/common/types/guid";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction"; import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view"; import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
@@ -63,6 +66,7 @@ describe("ViewComponent", () => {
useValue: mock<BillingAccountProfileStateService>(), useValue: mock<BillingAccountProfileStateService>(),
}, },
{ provide: ConfigService, useValue: mock<ConfigService>() }, { provide: ConfigService, useValue: mock<ConfigService>() },
{ provide: AccountService, useValue: mockAccountServiceWith("UserId" as UserId) },
{ {
provide: CipherAuthorizationService, provide: CipherAuthorizationService,
useValue: { useValue: {

View File

@@ -4,6 +4,7 @@ import { map, Observable, ReplaySubject, Subject } from "rxjs";
import { CollectionAdminView, CollectionService } from "@bitwarden/admin-console/common"; import { CollectionAdminView, CollectionService } from "@bitwarden/admin-console/common";
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction"; import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { StateProvider } from "@bitwarden/common/platform/state"; import { StateProvider } from "@bitwarden/common/platform/state";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
@@ -32,6 +33,7 @@ export class VaultFilterService extends BaseVaultFilterService implements OnDest
i18nService: I18nService, i18nService: I18nService,
stateProvider: StateProvider, stateProvider: StateProvider,
collectionService: CollectionService, collectionService: CollectionService,
accountService: AccountService,
) { ) {
super( super(
organizationService, organizationService,
@@ -41,6 +43,7 @@ export class VaultFilterService extends BaseVaultFilterService implements OnDest
i18nService, i18nService,
stateProvider, stateProvider,
collectionService, collectionService,
accountService,
); );
} }

View File

@@ -102,6 +102,8 @@ export class AddEditComponent implements OnInit, OnDestroy {
private personalOwnershipPolicyAppliesToActiveUser: boolean; private personalOwnershipPolicyAppliesToActiveUser: boolean;
private previousCipherId: string; private previousCipherId: string;
private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id));
get fido2CredentialCreationDateValue(): string { get fido2CredentialCreationDateValue(): string {
const dateCreated = this.i18nService.t("dateCreated"); const dateCreated = this.i18nService.t("dateCreated");
const creationDate = this.datePipe.transform( const creationDate = this.datePipe.transform(
@@ -259,12 +261,10 @@ export class AddEditComponent implements OnInit, OnDestroy {
const loadedAddEditCipherInfo = await this.loadAddEditCipherInfo(); const loadedAddEditCipherInfo = await this.loadAddEditCipherInfo();
const activeUserId = await firstValueFrom(this.activeUserId$);
if (this.cipher == null) { if (this.cipher == null) {
if (this.editMode) { if (this.editMode) {
const cipher = await this.loadCipher(); const cipher = await this.loadCipher();
const activeUserId = await firstValueFrom(
this.accountService.activeAccount$.pipe(map((a) => a?.id)),
);
this.cipher = await cipher.decrypt( this.cipher = await cipher.decrypt(
await this.cipherService.getKeyForCipherKeyDecryption(cipher, activeUserId), await this.cipherService.getKeyForCipherKeyDecryption(cipher, activeUserId),
); );
@@ -323,7 +323,7 @@ export class AddEditComponent implements OnInit, OnDestroy {
this.cipher.login.fido2Credentials = null; this.cipher.login.fido2Credentials = null;
} }
this.folders$ = this.folderService.folderViews$; this.folders$ = this.folderService.folderViews$(activeUserId);
if (this.editMode && this.previousCipherId !== this.cipherId) { if (this.editMode && this.previousCipherId !== this.cipherId) {
void this.eventCollectionService.collectMany(EventType.Cipher_ClientViewed, [this.cipher]); void this.eventCollectionService.collectMany(EventType.Cipher_ClientViewed, [this.cipher]);

View File

@@ -2,7 +2,7 @@
// @ts-strict-ignore // @ts-strict-ignore
import { Directive, EventEmitter, Input, OnInit, Output } from "@angular/core"; import { Directive, EventEmitter, Input, OnInit, Output } from "@angular/core";
import { Validators, FormBuilder } from "@angular/forms"; import { Validators, FormBuilder } from "@angular/forms";
import { firstValueFrom } from "rxjs"; import { firstValueFrom, map } from "rxjs";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
@@ -27,6 +27,8 @@ export class FolderAddEditComponent implements OnInit {
deletePromise: Promise<any>; deletePromise: Promise<any>;
protected componentName = ""; protected componentName = "";
protected activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id));
formGroup = this.formBuilder.group({ formGroup = this.formBuilder.group({
name: ["", [Validators.required]], name: ["", [Validators.required]],
}); });
@@ -59,10 +61,10 @@ export class FolderAddEditComponent implements OnInit {
} }
try { try {
const activeAccountId = await firstValueFrom(this.accountService.activeAccount$); const activeUserId = await firstValueFrom(this.activeUserId$);
const userKey = await this.keyService.getUserKeyWithLegacySupport(activeAccountId.id); const userKey = await this.keyService.getUserKeyWithLegacySupport(activeUserId);
const folder = await this.folderService.encrypt(this.folder, userKey); const folder = await this.folderService.encrypt(this.folder, userKey);
this.formPromise = this.folderApiService.save(folder); this.formPromise = this.folderApiService.save(folder, activeUserId);
await this.formPromise; await this.formPromise;
this.platformUtilsService.showToast( this.platformUtilsService.showToast(
"success", "success",
@@ -90,7 +92,8 @@ export class FolderAddEditComponent implements OnInit {
} }
try { try {
this.deletePromise = this.folderApiService.delete(this.folder.id); const activeUserId = await firstValueFrom(this.activeUserId$);
this.deletePromise = this.folderApiService.delete(this.folder.id, activeUserId);
await this.deletePromise; await this.deletePromise;
this.platformUtilsService.showToast("success", null, this.i18nService.t("deletedFolder")); this.platformUtilsService.showToast("success", null, this.i18nService.t("deletedFolder"));
this.onDeletedFolder.emit(this.folder); this.onDeletedFolder.emit(this.folder);
@@ -107,8 +110,10 @@ export class FolderAddEditComponent implements OnInit {
if (this.editMode) { if (this.editMode) {
this.editMode = true; this.editMode = true;
this.title = this.i18nService.t("editFolder"); this.title = this.i18nService.t("editFolder");
const folder = await this.folderService.get(this.folderId); const activeUserId = await firstValueFrom(this.activeUserId$);
this.folder = await folder.decrypt(); this.folder = await firstValueFrom(
this.folderService.getDecrypted$(this.folderId, activeUserId),
);
} else { } else {
this.title = this.i18nService.t("addFolder"); this.title = this.i18nService.t("addFolder");
} }

View File

@@ -79,6 +79,8 @@ export class ViewComponent implements OnDestroy, OnInit {
private previousCipherId: string; private previousCipherId: string;
private passwordReprompted = false; private passwordReprompted = false;
private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id));
get fido2CredentialCreationDateValue(): string { get fido2CredentialCreationDateValue(): string {
const dateCreated = this.i18nService.t("dateCreated"); const dateCreated = this.i18nService.t("dateCreated");
const creationDate = this.datePipe.transform( const creationDate = this.datePipe.transform(
@@ -141,9 +143,7 @@ export class ViewComponent implements OnDestroy, OnInit {
this.cleanUp(); this.cleanUp();
const cipher = await this.cipherService.get(this.cipherId); const cipher = await this.cipherService.get(this.cipherId);
const activeUserId = await firstValueFrom( const activeUserId = await firstValueFrom(this.activeUserId$);
this.accountService.activeAccount$.pipe(map((a) => a?.id)),
);
this.cipher = await cipher.decrypt( this.cipher = await cipher.decrypt(
await this.cipherService.getKeyForCipherKeyDecryption(cipher, activeUserId), await this.cipherService.getKeyForCipherKeyDecryption(cipher, activeUserId),
); );
@@ -158,7 +158,7 @@ export class ViewComponent implements OnDestroy, OnInit {
if (this.cipher.folderId) { if (this.cipher.folderId) {
this.folder = await ( this.folder = await (
await firstValueFrom(this.folderService.folderViews$) await firstValueFrom(this.folderService.folderViews$(activeUserId))
).find((f) => f.id == this.cipher.folderId); ).find((f) => f.id == this.cipher.folderId);
} }

View File

@@ -1,7 +1,7 @@
// FIXME: Update this file to be type safe and remove this and next line // FIXME: Update this file to be type safe and remove this and next line
// @ts-strict-ignore // @ts-strict-ignore
import { Injectable } from "@angular/core"; import { Injectable } from "@angular/core";
import { firstValueFrom, from, map, mergeMap, Observable } from "rxjs"; import { firstValueFrom, from, map, mergeMap, Observable, switchMap } from "rxjs";
import { CollectionService, CollectionView } from "@bitwarden/admin-console/common"; import { CollectionService, CollectionView } from "@bitwarden/admin-console/common";
import { import {
@@ -11,6 +11,7 @@ import {
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction"; import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
import { PolicyType } from "@bitwarden/common/admin-console/enums"; import { PolicyType } from "@bitwarden/common/admin-console/enums";
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { ActiveUserState, StateProvider } from "@bitwarden/common/platform/state"; import { ActiveUserState, StateProvider } from "@bitwarden/common/platform/state";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction"; import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
@@ -32,6 +33,8 @@ export class VaultFilterService implements DeprecatedVaultFilterServiceAbstracti
private readonly collapsedGroupings$: Observable<Set<string>> = private readonly collapsedGroupings$: Observable<Set<string>> =
this.collapsedGroupingsState.state$.pipe(map((c) => new Set(c))); this.collapsedGroupingsState.state$.pipe(map((c) => new Set(c)));
private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id));
constructor( constructor(
protected organizationService: OrganizationService, protected organizationService: OrganizationService,
protected folderService: FolderService, protected folderService: FolderService,
@@ -39,6 +42,7 @@ export class VaultFilterService implements DeprecatedVaultFilterServiceAbstracti
protected collectionService: CollectionService, protected collectionService: CollectionService,
protected policyService: PolicyService, protected policyService: PolicyService,
protected stateProvider: StateProvider, protected stateProvider: StateProvider,
protected accountService: AccountService,
) {} ) {}
async storeCollapsedFilterNodes(collapsedFilterNodes: Set<string>): Promise<void> { async storeCollapsedFilterNodes(collapsedFilterNodes: Set<string>): Promise<void> {
@@ -81,7 +85,8 @@ export class VaultFilterService implements DeprecatedVaultFilterServiceAbstracti
}); });
}; };
return this.folderService.folderViews$.pipe( return this.activeUserId$.pipe(
switchMap((userId) => this.folderService.folderViews$(userId)),
mergeMap((folders) => from(transformation(folders))), mergeMap((folders) => from(transformation(folders))),
); );
} }
@@ -126,8 +131,9 @@ export class VaultFilterService implements DeprecatedVaultFilterServiceAbstracti
} }
async getFolderNested(id: string): Promise<TreeNode<FolderView>> { async getFolderNested(id: string): Promise<TreeNode<FolderView>> {
const activeUserId = await firstValueFrom(this.activeUserId$);
const folders = await this.getAllFoldersNested( const folders = await this.getAllFoldersNested(
await firstValueFrom(this.folderService.folderViews$), await firstValueFrom(this.folderService.folderViews$(activeUserId)),
); );
return ServiceUtils.getTreeNodeObjectFromList(folders, id) as TreeNode<FolderView>; return ServiceUtils.getTreeNodeObjectFromList(folders, id) as TreeNode<FolderView>;
} }

View File

@@ -150,6 +150,9 @@ export const COLLECTION_DATA = new StateDefinition("collection", "disk", {
web: "memory", web: "memory",
}); });
export const FOLDER_DISK = new StateDefinition("folder", "disk", { web: "memory" }); export const FOLDER_DISK = new StateDefinition("folder", "disk", { web: "memory" });
export const FOLDER_MEMORY = new StateDefinition("decryptedFolders", "memory", {
browser: "memory-large-object",
});
export const VAULT_FILTER_DISK = new StateDefinition("vaultFilter", "disk", { export const VAULT_FILTER_DISK = new StateDefinition("vaultFilter", "disk", {
web: "disk-local", web: "disk-local",
}); });

View File

@@ -85,18 +85,25 @@ export abstract class CoreSyncService implements SyncService {
await this.stateProvider.getUser(userId, LAST_SYNC_DATE).update(() => date); await this.stateProvider.getUser(userId, LAST_SYNC_DATE).update(() => date);
} }
async syncUpsertFolder(notification: SyncFolderNotification, isEdit: boolean): Promise<boolean> { async syncUpsertFolder(
notification: SyncFolderNotification,
isEdit: boolean,
userId: UserId,
): Promise<boolean> {
this.syncStarted(); this.syncStarted();
if (await this.stateService.getIsAuthenticated()) {
const authStatus = await firstValueFrom(this.authService.authStatusFor$(userId));
if (authStatus >= AuthenticationStatus.Locked) {
try { try {
const localFolder = await this.folderService.get(notification.id); const localFolder = await this.folderService.get(notification.id, userId);
if ( if (
(!isEdit && localFolder == null) || (!isEdit && localFolder == null) ||
(isEdit && localFolder != null && localFolder.revisionDate < notification.revisionDate) (isEdit && localFolder != null && localFolder.revisionDate < notification.revisionDate)
) { ) {
const remoteFolder = await this.folderApiService.get(notification.id); const remoteFolder = await this.folderApiService.get(notification.id);
if (remoteFolder != null) { if (remoteFolder != null) {
await this.folderService.upsert(new FolderData(remoteFolder)); await this.folderService.upsert(new FolderData(remoteFolder), userId);
this.messageSender.send("syncedUpsertedFolder", { folderId: notification.id }); this.messageSender.send("syncedUpsertedFolder", { folderId: notification.id });
return this.syncCompleted(true); return this.syncCompleted(true);
} }
@@ -108,10 +115,13 @@ export abstract class CoreSyncService implements SyncService {
return this.syncCompleted(false); return this.syncCompleted(false);
} }
async syncDeleteFolder(notification: SyncFolderNotification): Promise<boolean> { async syncDeleteFolder(notification: SyncFolderNotification, userId: UserId): Promise<boolean> {
this.syncStarted(); this.syncStarted();
if (await this.stateService.getIsAuthenticated()) {
await this.folderService.delete(notification.id); const authStatus = await firstValueFrom(this.authService.authStatusFor$(userId));
if (authStatus >= AuthenticationStatus.Locked) {
await this.folderService.delete(notification.id, userId);
this.messageSender.send("syncedDeletedFolder", { folderId: notification.id }); this.messageSender.send("syncedDeletedFolder", { folderId: notification.id });
this.syncCompleted(true); this.syncCompleted(true);
return true; return true;

View File

@@ -56,8 +56,9 @@ export abstract class SyncService {
abstract syncUpsertFolder( abstract syncUpsertFolder(
notification: SyncFolderNotification, notification: SyncFolderNotification,
isEdit: boolean, isEdit: boolean,
userId: UserId,
): Promise<boolean>; ): Promise<boolean>;
abstract syncDeleteFolder(notification: SyncFolderNotification): Promise<boolean>; abstract syncDeleteFolder(notification: SyncFolderNotification, userId: UserId): Promise<boolean>;
abstract syncUpsertCipher( abstract syncUpsertCipher(
notification: SyncCipherNotification, notification: SyncCipherNotification,
isEdit: boolean, isEdit: boolean,

View File

@@ -168,10 +168,14 @@ export class NotificationsService implements NotificationsServiceAbstraction {
await this.syncService.syncUpsertFolder( await this.syncService.syncUpsertFolder(
notification.payload as SyncFolderNotification, notification.payload as SyncFolderNotification,
notification.type === NotificationType.SyncFolderUpdate, notification.type === NotificationType.SyncFolderUpdate,
payloadUserId,
); );
break; break;
case NotificationType.SyncFolderDelete: case NotificationType.SyncFolderDelete:
await this.syncService.syncDeleteFolder(notification.payload as SyncFolderNotification); await this.syncService.syncDeleteFolder(
notification.payload as SyncFolderNotification,
payloadUserId,
);
break; break;
case NotificationType.SyncVault: case NotificationType.SyncVault:
case NotificationType.SyncCiphers: case NotificationType.SyncCiphers:

View File

@@ -334,7 +334,7 @@ describe("VaultTimeoutService", () => {
// Active users should have additional steps ran // Active users should have additional steps ran
expect(searchService.clearIndex).toHaveBeenCalled(); expect(searchService.clearIndex).toHaveBeenCalled();
expect(folderService.clearCache).toHaveBeenCalled(); expect(folderService.clearDecryptedFolderState).toHaveBeenCalled();
expectUserToHaveLoggedOut("3"); // They have chosen logout as their action and it's available, log them out expectUserToHaveLoggedOut("3"); // They have chosen logout as their action and it's available, log them out
expectUserToHaveLoggedOut("4"); // They may have had lock as their chosen action but it's not available to them so logout expectUserToHaveLoggedOut("4"); // They may have had lock as their chosen action but it's not available to them so logout

View File

@@ -135,10 +135,10 @@ export class VaultTimeoutService implements VaultTimeoutServiceAbstraction {
if (userId == null || userId === currentUserId) { if (userId == null || userId === currentUserId) {
await this.searchService.clearIndex(); await this.searchService.clearIndex();
await this.folderService.clearCache();
await this.collectionService.clearActiveUserCache(); await this.collectionService.clearActiveUserCache();
} }
await this.folderService.clearDecryptedFolderState(userId);
await this.masterPasswordService.clearMasterKey(lockingUserId); await this.masterPasswordService.clearMasterKey(lockingUserId);
await this.stateService.setUserKeyAutoUnlock(null, { userId: lockingUserId }); await this.stateService.setUserKeyAutoUnlock(null, { userId: lockingUserId });

View File

@@ -1,11 +1,13 @@
// FIXME: Update this file to be type safe and remove this and next line // FIXME: Update this file to be type safe and remove this and next line
// @ts-strict-ignore // @ts-strict-ignore
import { UserId } from "@bitwarden/common/types/guid";
import { Folder } from "../../models/domain/folder"; import { Folder } from "../../models/domain/folder";
import { FolderResponse } from "../../models/response/folder.response"; import { FolderResponse } from "../../models/response/folder.response";
export class FolderApiServiceAbstraction { export class FolderApiServiceAbstraction {
save: (folder: Folder) => Promise<any>; save: (folder: Folder, userId: UserId) => Promise<any>;
delete: (id: string) => Promise<any>; delete: (id: string, userId: UserId) => Promise<any>;
get: (id: string) => Promise<FolderResponse>; get: (id: string) => Promise<FolderResponse>;
deleteAll: () => Promise<void>; deleteAll: (userId: UserId) => Promise<void>;
} }

View File

@@ -13,23 +13,27 @@ import { FolderWithIdRequest } from "../../models/request/folder-with-id.request
import { FolderView } from "../../models/view/folder.view"; import { FolderView } from "../../models/view/folder.view";
export abstract class FolderService implements UserKeyRotationDataProvider<FolderWithIdRequest> { export abstract class FolderService implements UserKeyRotationDataProvider<FolderWithIdRequest> {
folders$: Observable<Folder[]>; folders$: (userId: UserId) => Observable<Folder[]>;
folderViews$: Observable<FolderView[]>; folderViews$: (userId: UserId) => Observable<FolderView[]>;
clearCache: () => Promise<void>; clearDecryptedFolderState: (userId: UserId) => Promise<void>;
encrypt: (model: FolderView, key: SymmetricCryptoKey) => Promise<Folder>; encrypt: (model: FolderView, key: SymmetricCryptoKey) => Promise<Folder>;
get: (id: string) => Promise<Folder>; get: (id: string, userId: UserId) => Promise<Folder>;
getDecrypted$: (id: string) => Observable<FolderView | undefined>; getDecrypted$: (id: string, userId: UserId) => Observable<FolderView | undefined>;
getAllFromState: () => Promise<Folder[]>; /**
* @deprecated Use firstValueFrom(folders$) directly instead
* @param userId The user id
* @returns Promise of folders array
*/
getAllFromState: (userId: UserId) => Promise<Folder[]>;
/** /**
* @deprecated Only use in CLI! * @deprecated Only use in CLI!
*/ */
getFromState: (id: string) => Promise<Folder>; getFromState: (id: string, userId: UserId) => Promise<Folder>;
/** /**
* @deprecated Only use in CLI! * @deprecated Only use in CLI!
*/ */
getAllDecryptedFromState: () => Promise<FolderView[]>; getAllDecryptedFromState: (userId: UserId) => Promise<FolderView[]>;
decryptFolders: (folders: Folder[]) => Promise<FolderView[]>;
/** /**
* Returns user folders re-encrypted with the new user key. * Returns user folders re-encrypted with the new user key.
* @param originalUserKey the original user key * @param originalUserKey the original user key
@@ -46,8 +50,8 @@ export abstract class FolderService implements UserKeyRotationDataProvider<Folde
} }
export abstract class InternalFolderService extends FolderService { export abstract class InternalFolderService extends FolderService {
upsert: (folder: FolderData | FolderData[]) => Promise<void>; upsert: (folder: FolderData | FolderData[], userId: UserId) => Promise<void>;
replace: (folders: { [id: string]: FolderData }, userId: UserId) => Promise<void>; replace: (folders: { [id: string]: FolderData }, userId: UserId) => Promise<void>;
clear: (userId?: string) => Promise<void>; clear: (userId: UserId) => Promise<void>;
delete: (id: string | string[]) => Promise<any>; delete: (id: string | string[], userId: UserId) => Promise<any>;
} }

View File

@@ -1,3 +1,5 @@
import { UserId } from "@bitwarden/common/types/guid";
import { ApiService } from "../../../abstractions/api.service"; import { ApiService } from "../../../abstractions/api.service";
import { FolderApiServiceAbstraction } from "../../../vault/abstractions/folder/folder-api.service.abstraction"; import { FolderApiServiceAbstraction } from "../../../vault/abstractions/folder/folder-api.service.abstraction";
import { InternalFolderService } from "../../../vault/abstractions/folder/folder.service.abstraction"; import { InternalFolderService } from "../../../vault/abstractions/folder/folder.service.abstraction";
@@ -12,7 +14,7 @@ export class FolderApiService implements FolderApiServiceAbstraction {
private apiService: ApiService, private apiService: ApiService,
) {} ) {}
async save(folder: Folder): Promise<any> { async save(folder: Folder, userId: UserId): Promise<any> {
const request = new FolderRequest(folder); const request = new FolderRequest(folder);
let response: FolderResponse; let response: FolderResponse;
@@ -24,17 +26,17 @@ export class FolderApiService implements FolderApiServiceAbstraction {
} }
const data = new FolderData(response); const data = new FolderData(response);
await this.folderService.upsert(data); await this.folderService.upsert(data, userId);
} }
async delete(id: string): Promise<any> { async delete(id: string, userId: UserId): Promise<any> {
await this.deleteFolder(id); await this.deleteFolder(id);
await this.folderService.delete(id); await this.folderService.delete(id, userId);
} }
async deleteAll(): Promise<void> { async deleteAll(userId: UserId): Promise<void> {
await this.apiService.send("DELETE", "/folders/all", null, true, false); await this.apiService.send("DELETE", "/folders/all", null, true, false);
await this.folderService.clear(); await this.folderService.clear(userId);
} }
async get(id: string): Promise<FolderResponse> { async get(id: string): Promise<FolderResponse> {

View File

@@ -1,10 +1,10 @@
import { mock, MockProxy } from "jest-mock-extended"; import { mock, MockProxy } from "jest-mock-extended";
import { firstValueFrom } from "rxjs"; import { BehaviorSubject, firstValueFrom } from "rxjs";
import { KeyService } from "../../../../../key-management/src/abstractions/key.service"; import { KeyService } from "../../../../../key-management/src/abstractions/key.service";
import { makeStaticByteArray } from "../../../../spec"; import { makeEncString } from "../../../../spec";
import { FakeAccountService, mockAccountServiceWith } from "../../../../spec/fake-account-service"; import { FakeAccountService, mockAccountServiceWith } from "../../../../spec/fake-account-service";
import { FakeActiveUserState } from "../../../../spec/fake-state"; import { FakeSingleUserState } from "../../../../spec/fake-state";
import { FakeStateProvider } from "../../../../spec/fake-state-provider"; import { FakeStateProvider } from "../../../../spec/fake-state-provider";
import { EncryptService } from "../../../platform/abstractions/encrypt.service"; import { EncryptService } from "../../../platform/abstractions/encrypt.service";
import { I18nService } from "../../../platform/abstractions/i18n.service"; import { I18nService } from "../../../platform/abstractions/i18n.service";
@@ -17,7 +17,7 @@ import { CipherService } from "../../abstractions/cipher.service";
import { FolderData } from "../../models/data/folder.data"; import { FolderData } from "../../models/data/folder.data";
import { FolderView } from "../../models/view/folder.view"; import { FolderView } from "../../models/view/folder.view";
import { FolderService } from "../../services/folder/folder.service"; import { FolderService } from "../../services/folder/folder.service";
import { FOLDER_ENCRYPTED_FOLDERS } from "../key-state/folder.state"; import { FOLDER_DECRYPTED_FOLDERS, FOLDER_ENCRYPTED_FOLDERS } from "../key-state/folder.state";
describe("Folder Service", () => { describe("Folder Service", () => {
let folderService: FolderService; let folderService: FolderService;
@@ -30,7 +30,7 @@ describe("Folder Service", () => {
const mockUserId = Utils.newGuid() as UserId; const mockUserId = Utils.newGuid() as UserId;
let accountService: FakeAccountService; let accountService: FakeAccountService;
let folderState: FakeActiveUserState<Record<string, FolderData>>; let folderState: FakeSingleUserState<Record<string, FolderData>>;
beforeEach(() => { beforeEach(() => {
keyService = mock<KeyService>(); keyService = mock<KeyService>();
@@ -42,11 +42,9 @@ describe("Folder Service", () => {
stateProvider = new FakeStateProvider(accountService); stateProvider = new FakeStateProvider(accountService);
i18nService.collator = new Intl.Collator("en"); i18nService.collator = new Intl.Collator("en");
i18nService.t.mockReturnValue("No Folder");
keyService.hasUserKey.mockResolvedValue(true); keyService.userKey$.mockReturnValue(new BehaviorSubject("mockOriginalUserKey" as any));
keyService.getUserKeyWithLegacySupport.mockResolvedValue(
new SymmetricCryptoKey(makeStaticByteArray(32)) as UserKey,
);
encryptService.decryptToUtf8.mockResolvedValue("DEC"); encryptService.decryptToUtf8.mockResolvedValue("DEC");
folderService = new FolderService( folderService = new FolderService(
@@ -57,10 +55,53 @@ describe("Folder Service", () => {
stateProvider, stateProvider,
); );
folderState = stateProvider.activeUser.getFake(FOLDER_ENCRYPTED_FOLDERS); folderState = stateProvider.singleUser.getFake(mockUserId, FOLDER_ENCRYPTED_FOLDERS);
// Initial state // Initial state
folderState.nextState({ "1": folderData("1", "test") }); folderState.nextState({ "1": folderData("1") });
});
describe("folders$", () => {
it("emits encrypted folders from state", async () => {
const folder1 = folderData("1");
const folder2 = folderData("2");
await stateProvider.setUserState(
FOLDER_ENCRYPTED_FOLDERS,
Object.fromEntries([folder1, folder2].map((f) => [f.id, f])),
mockUserId,
);
const result = await firstValueFrom(folderService.folders$(mockUserId));
expect(result.length).toBe(2);
expect(result).toIncludeAllPartialMembers([
{ id: "1", name: makeEncString("ENC_STRING_1") },
{ id: "2", name: makeEncString("ENC_STRING_2") },
]);
});
});
describe("folderView$", () => {
it("emits decrypted folders from state", async () => {
const folder1 = folderData("1");
const folder2 = folderData("2");
await stateProvider.setUserState(
FOLDER_ENCRYPTED_FOLDERS,
Object.fromEntries([folder1, folder2].map((f) => [f.id, f])),
mockUserId,
);
const result = await firstValueFrom(folderService.folderViews$(mockUserId));
expect(result.length).toBe(3);
expect(result).toIncludeAllPartialMembers([
{ id: "1", name: "DEC" },
{ id: "2", name: "DEC" },
{ name: "No Folder" },
]);
});
}); });
it("encrypt", async () => { it("encrypt", async () => {
@@ -83,105 +124,83 @@ describe("Folder Service", () => {
describe("get", () => { describe("get", () => {
it("exists", async () => { it("exists", async () => {
const result = await folderService.get("1"); const result = await folderService.get("1", mockUserId);
expect(result).toEqual({ expect(result).toEqual({
id: "1", id: "1",
name: { name: makeEncString("ENC_STRING_" + 1),
encryptedString: "test",
encryptionType: 0,
},
revisionDate: null, revisionDate: null,
}); });
}); });
it("not exists", async () => { it("not exists", async () => {
const result = await folderService.get("2"); const result = await folderService.get("2", mockUserId);
expect(result).toBe(undefined); expect(result).toBe(undefined);
}); });
}); });
it("upsert", async () => { it("upsert", async () => {
await folderService.upsert(folderData("2", "test 2")); await folderService.upsert(folderData("2"), mockUserId);
expect(await firstValueFrom(folderService.folders$)).toEqual([ expect(await firstValueFrom(folderService.folders$(mockUserId))).toEqual([
{ {
id: "1", id: "1",
name: { name: makeEncString("ENC_STRING_" + 1),
encryptedString: "test",
encryptionType: 0,
},
revisionDate: null, revisionDate: null,
}, },
{ {
id: "2", id: "2",
name: { name: makeEncString("ENC_STRING_" + 2),
encryptedString: "test 2",
encryptionType: 0,
},
revisionDate: null, revisionDate: null,
}, },
]); ]);
}); });
it("replace", async () => { it("replace", async () => {
await folderService.replace({ "2": folderData("2", "test 2") }, mockUserId); await folderService.replace({ "4": folderData("4") }, mockUserId);
expect(await firstValueFrom(folderService.folders$)).toEqual([ expect(await firstValueFrom(folderService.folders$(mockUserId))).toEqual([
{ {
id: "2", id: "4",
name: { name: makeEncString("ENC_STRING_" + 4),
encryptedString: "test 2",
encryptionType: 0,
},
revisionDate: null, revisionDate: null,
}, },
]); ]);
}); });
it("delete", async () => { it("delete", async () => {
await folderService.delete("1"); await folderService.delete("1", mockUserId);
expect((await firstValueFrom(folderService.folders$)).length).toBe(0); expect((await firstValueFrom(folderService.folders$(mockUserId))).length).toBe(0);
}); });
it("clearCache", async () => { describe("clearDecryptedFolderState", () => {
await folderService.clearCache();
expect((await firstValueFrom(folderService.folders$)).length).toBe(1);
expect((await firstValueFrom(folderService.folderViews$)).length).toBe(0);
});
describe("clear", () => {
it("null userId", async () => { it("null userId", async () => {
await folderService.clear(); await expect(folderService.clearDecryptedFolderState(null)).rejects.toThrow(
"User ID is required.",
expect((await firstValueFrom(folderService.folders$)).length).toBe(0); );
expect((await firstValueFrom(folderService.folderViews$)).length).toBe(0);
}); });
/** it("userId provided", async () => {
* TODO: Fix this test to address the problem where the fakes for the active user state is not await folderService.clearDecryptedFolderState(mockUserId);
* updated as expected
*/
// it("matching userId", async () => {
// stateService.getUserId.mockResolvedValue("1");
// await folderService.clear("1" as UserId);
// expect((await firstValueFrom(folderService.folders$)).length).toBe(0); expect((await firstValueFrom(folderService.folders$(mockUserId))).length).toBe(1);
// }); expect(
(await firstValueFrom(stateProvider.getUserState$(FOLDER_DECRYPTED_FOLDERS, mockUserId)))
.length,
).toBe(0);
});
});
/** it("clear", async () => {
* TODO: Fix this test to address the problem where the fakes for the active user state is not await folderService.clear(mockUserId);
* updated as expected
*/
// it("mismatching userId", async () => {
// await folderService.clear("12" as UserId);
// expect((await firstValueFrom(folderService.folders$)).length).toBe(1); expect((await firstValueFrom(folderService.folders$(mockUserId))).length).toBe(0);
// expect((await firstValueFrom(folderService.folderViews$)).length).toBe(2);
// }); const folderViews = await firstValueFrom(folderService.folderViews$(mockUserId));
expect(folderViews.length).toBe(1);
expect(folderViews[0].id).toBeNull(); // Should be the "No Folder" folder
}); });
describe("getRotatedData", () => { describe("getRotatedData", () => {
@@ -207,10 +226,10 @@ describe("Folder Service", () => {
}); });
}); });
function folderData(id: string, name: string) { function folderData(id: string) {
const data = new FolderData({} as any); const data = new FolderData({} as any);
data.id = id; data.id = id;
data.name = name; data.name = makeEncString("ENC_STRING_" + data.id).encryptedString;
return data; return data;
} }

View File

@@ -1,14 +1,14 @@
// FIXME: Update this file to be type safe and remove this and next line // FIXME: Update this file to be type safe and remove this and next line
// @ts-strict-ignore // @ts-strict-ignore
import { Observable, firstValueFrom, map, shareReplay } from "rxjs"; import { Observable, Subject, firstValueFrom, map, shareReplay, switchMap, merge } from "rxjs";
import { EncryptService } from "@bitwarden/common/platform/abstractions/encrypt.service"; import { EncryptService } from "@bitwarden/common/platform/abstractions/encrypt.service";
import { Utils } from "@bitwarden/common/platform/misc/utils";
import { KeyService } from "../../../../../key-management/src/abstractions/key.service"; import { KeyService } from "../../../../../key-management/src/abstractions/key.service";
import { I18nService } from "../../../platform/abstractions/i18n.service"; import { I18nService } from "../../../platform/abstractions/i18n.service";
import { Utils } from "../../../platform/misc/utils";
import { SymmetricCryptoKey } from "../../../platform/models/domain/symmetric-crypto-key"; import { SymmetricCryptoKey } from "../../../platform/models/domain/symmetric-crypto-key";
import { ActiveUserState, DerivedState, StateProvider } from "../../../platform/state"; import { StateProvider } from "../../../platform/state";
import { UserId } from "../../../types/guid"; import { UserId } from "../../../types/guid";
import { UserKey } from "../../../types/key"; import { UserKey } from "../../../types/key";
import { CipherService } from "../../../vault/abstractions/cipher.service"; import { CipherService } from "../../../vault/abstractions/cipher.service";
@@ -21,11 +21,18 @@ import { FolderWithIdRequest } from "../../models/request/folder-with-id.request
import { FOLDER_DECRYPTED_FOLDERS, FOLDER_ENCRYPTED_FOLDERS } from "../key-state/folder.state"; import { FOLDER_DECRYPTED_FOLDERS, FOLDER_ENCRYPTED_FOLDERS } from "../key-state/folder.state";
export class FolderService implements InternalFolderServiceAbstraction { export class FolderService implements InternalFolderServiceAbstraction {
folders$: Observable<Folder[]>; /**
folderViews$: Observable<FolderView[]>; * Ensures we reuse the same observable stream for each userId rather than
* creating a new one on each folderViews$ call.
*/
private folderViewCache = new Map<UserId, Observable<FolderView[]>>();
private encryptedFoldersState: ActiveUserState<Record<string, FolderData>>; /**
private decryptedFoldersState: DerivedState<FolderView[]>; * Used to force the folderviews$ Observable to re-emit with a provided value.
* Required because shareReplay with refCount: false maintains last emission.
* Used during cleanup to force emit empty arrays, ensuring stale data isn't retained.
*/
private forceFolderViews: Record<UserId, Subject<FolderView[]>> = {};
constructor( constructor(
private keyService: KeyService, private keyService: KeyService,
@@ -33,23 +40,44 @@ export class FolderService implements InternalFolderServiceAbstraction {
private i18nService: I18nService, private i18nService: I18nService,
private cipherService: CipherService, private cipherService: CipherService,
private stateProvider: StateProvider, private stateProvider: StateProvider,
) { ) {}
this.encryptedFoldersState = this.stateProvider.getActive(FOLDER_ENCRYPTED_FOLDERS);
this.decryptedFoldersState = this.stateProvider.getDerived(
this.encryptedFoldersState.state$,
FOLDER_DECRYPTED_FOLDERS,
{ folderService: this, keyService: this.keyService },
);
this.folders$ = this.encryptedFoldersState.state$.pipe( folders$(userId: UserId): Observable<Folder[]> {
map((folderData) => Object.values(folderData).map((f) => new Folder(f))), return this.encryptedFoldersState(userId).state$.pipe(
); map((folders) => {
if (folders == null) {
return [];
}
this.folderViews$ = this.decryptedFoldersState.state$; return Object.values(folders).map((f) => new Folder(f));
}),
);
} }
async clearCache(): Promise<void> { /**
await this.decryptedFoldersState.forceValue([]); * Returns an Observable of decrypted folder views for the given userId.
* Uses folderViewCache to maintain a single Observable instance per user,
* combining normal folder state updates with forced updates.
*/
folderViews$(userId: UserId): Observable<FolderView[]> {
if (!this.folderViewCache.has(userId)) {
if (!this.forceFolderViews[userId]) {
this.forceFolderViews[userId] = new Subject<FolderView[]>();
}
const observable = merge(
this.forceFolderViews[userId],
this.encryptedFoldersState(userId).state$.pipe(
switchMap((folderData) => {
return this.decryptFolders(userId, folderData);
}),
),
).pipe(shareReplay({ refCount: false, bufferSize: 1 }));
this.folderViewCache.set(userId, observable);
}
return this.folderViewCache.get(userId);
} }
// TODO: This should be moved to EncryptService or something // TODO: This should be moved to EncryptService or something
@@ -60,29 +88,29 @@ export class FolderService implements InternalFolderServiceAbstraction {
return folder; return folder;
} }
async get(id: string): Promise<Folder> { async get(id: string, userId: UserId): Promise<Folder> {
const folders = await firstValueFrom(this.folders$); const folders = await firstValueFrom(this.folders$(userId));
return folders.find((folder) => folder.id === id); return folders.find((folder) => folder.id === id);
} }
getDecrypted$(id: string): Observable<FolderView | undefined> { getDecrypted$(id: string, userId: UserId): Observable<FolderView | undefined> {
return this.folderViews$.pipe( return this.folderViews$(userId).pipe(
map((folders) => folders.find((folder) => folder.id === id)), map((folders) => folders.find((folder) => folder.id === id)),
shareReplay({ refCount: true, bufferSize: 1 }), shareReplay({ refCount: true, bufferSize: 1 }),
); );
} }
async getAllFromState(): Promise<Folder[]> { async getAllFromState(userId: UserId): Promise<Folder[]> {
return await firstValueFrom(this.folders$); return await firstValueFrom(this.folders$(userId));
} }
/** /**
* @deprecated For the CLI only * @deprecated For the CLI only
* @param id id of the folder * @param id id of the folder
*/ */
async getFromState(id: string): Promise<Folder> { async getFromState(id: string, userId: UserId): Promise<Folder> {
const folder = await this.get(id); const folder = await this.get(id, userId);
if (!folder) { if (!folder) {
return null; return null;
} }
@@ -93,12 +121,13 @@ export class FolderService implements InternalFolderServiceAbstraction {
/** /**
* @deprecated Only use in CLI! * @deprecated Only use in CLI!
*/ */
async getAllDecryptedFromState(): Promise<FolderView[]> { async getAllDecryptedFromState(userId: UserId): Promise<FolderView[]> {
return await firstValueFrom(this.folderViews$); return await firstValueFrom(this.folderViews$(userId));
} }
async upsert(folderData: FolderData | FolderData[]): Promise<void> { async upsert(folderData: FolderData | FolderData[], userId: UserId): Promise<void> {
await this.encryptedFoldersState.update((folders) => { await this.clearDecryptedFolderState(userId);
await this.encryptedFoldersState(userId).update((folders) => {
if (folders == null) { if (folders == null) {
folders = {}; folders = {};
} }
@@ -120,24 +149,31 @@ export class FolderService implements InternalFolderServiceAbstraction {
if (!folders) { if (!folders) {
return; return;
} }
await this.clearDecryptedFolderState(userId);
await this.stateProvider.getUser(userId, FOLDER_ENCRYPTED_FOLDERS).update(() => { await this.stateProvider.getUser(userId, FOLDER_ENCRYPTED_FOLDERS).update(() => {
const newFolders: Record<string, FolderData> = { ...folders }; const newFolders: Record<string, FolderData> = { ...folders };
return newFolders; return newFolders;
}); });
} }
async clear(userId?: UserId): Promise<void> { async clearDecryptedFolderState(userId: UserId): Promise<void> {
if (userId == null) { if (userId == null) {
await this.encryptedFoldersState.update(() => ({})); throw new Error("User ID is required.");
await this.decryptedFoldersState.forceValue([]);
} else {
await this.stateProvider.getUser(userId, FOLDER_ENCRYPTED_FOLDERS).update(() => ({}));
} }
await this.setDecryptedFolders([], userId);
} }
async delete(id: string | string[]): Promise<any> { async clear(userId: UserId): Promise<void> {
await this.encryptedFoldersState.update((folders) => { this.forceFolderViews[userId]?.next([]);
await this.encryptedFoldersState(userId).update(() => ({}));
await this.clearDecryptedFolderState(userId);
}
async delete(id: string | string[], userId: UserId): Promise<any> {
await this.clearDecryptedFolderState(userId);
await this.encryptedFoldersState(userId).update((folders) => {
if (folders == null) { if (folders == null) {
return; return;
} }
@@ -164,25 +200,11 @@ export class FolderService implements InternalFolderServiceAbstraction {
} }
} }
if (updates.length > 0) { if (updates.length > 0) {
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling. await this.cipherService.upsert(updates.map((c) => c.toCipherData()));
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.cipherService.upsert(updates.map((c) => c.toCipherData()));
} }
} }
} }
async decryptFolders(folders: Folder[]) {
const decryptFolderPromises = folders.map((f) => f.decrypt());
const decryptedFolders = await Promise.all(decryptFolderPromises);
decryptedFolders.sort(Utils.getSortFunction(this.i18nService, "name"));
const noneFolder = new FolderView();
noneFolder.name = this.i18nService.t("noneFolder");
decryptedFolders.push(noneFolder);
return decryptedFolders;
}
async getRotatedData( async getRotatedData(
originalUserKey: UserKey, originalUserKey: UserKey,
newUserKey: UserKey, newUserKey: UserKey,
@@ -193,7 +215,7 @@ export class FolderService implements InternalFolderServiceAbstraction {
} }
let encryptedFolders: FolderWithIdRequest[] = []; let encryptedFolders: FolderWithIdRequest[] = [];
const folders = await firstValueFrom(this.folderViews$); const folders = await firstValueFrom(this.folderViews$(userId));
if (!folders) { if (!folders) {
return encryptedFolders; return encryptedFolders;
} }
@@ -205,4 +227,63 @@ export class FolderService implements InternalFolderServiceAbstraction {
); );
return encryptedFolders; return encryptedFolders;
} }
/**
* Decrypts the folders for a user.
* @param userId the user id
* @param folderData encrypted folders
* @returns a list of decrypted folders
*/
private async decryptFolders(
userId: UserId,
folderData: Record<string, FolderData>,
): Promise<FolderView[]> {
// Check if the decrypted folders are already cached
const decrypted = await firstValueFrom(
this.stateProvider.getUser(userId, FOLDER_DECRYPTED_FOLDERS).state$,
);
if (decrypted?.length) {
return decrypted;
}
if (folderData == null) {
return [];
}
const folders = Object.values(folderData).map((f) => new Folder(f));
const userKey = await firstValueFrom(this.keyService.userKey$(userId));
if (!userKey) {
return [];
}
const decryptFolderPromises = folders.map((f) =>
f.decryptWithKey(userKey, this.encryptService),
);
const decryptedFolders = await Promise.all(decryptFolderPromises);
decryptedFolders.sort(Utils.getSortFunction(this.i18nService, "name"));
const noneFolder = new FolderView();
noneFolder.name = this.i18nService.t("noneFolder");
decryptedFolders.push(noneFolder);
// Cache the decrypted folders
await this.setDecryptedFolders(decryptedFolders, userId);
return decryptedFolders;
}
/**
* @returns a SingleUserState for the encrypted folders.
*/
private encryptedFoldersState(userId: UserId) {
return this.stateProvider.getUser(userId, FOLDER_ENCRYPTED_FOLDERS);
}
/**
* Sets the decrypted folders state for a user.
* @param folders the decrypted folders
* @param userId the user id
*/
private async setDecryptedFolders(folders: FolderView[], userId: UserId): Promise<void> {
await this.stateProvider.setUserState(FOLDER_DECRYPTED_FOLDERS, folders, userId);
}
} }

View File

@@ -1,11 +1,3 @@
import { mock } from "jest-mock-extended";
import { KeyService } from "../../../../../key-management/src/abstractions/key.service";
import { FolderService } from "../../abstractions/folder/folder.service.abstraction";
import { FolderData } from "../../models/data/folder.data";
import { Folder } from "../../models/domain/folder";
import { FolderView } from "../../models/view/folder.view";
import { FOLDER_DECRYPTED_FOLDERS, FOLDER_ENCRYPTED_FOLDERS } from "./folder.state"; import { FOLDER_DECRYPTED_FOLDERS, FOLDER_ENCRYPTED_FOLDERS } from "./folder.state";
describe("encrypted folders", () => { describe("encrypted folders", () => {
@@ -31,48 +23,32 @@ describe("encrypted folders", () => {
}); });
describe("derived decrypted folders", () => { describe("derived decrypted folders", () => {
const keyService = mock<KeyService>();
const folderService = mock<FolderService>();
const sut = FOLDER_DECRYPTED_FOLDERS; const sut = FOLDER_DECRYPTED_FOLDERS;
let data: FolderData;
beforeEach(() => { it("should deserialize decrypted folders", async () => {
data = { const inputObj = [
id: "id", {
name: "encName", id: "id",
revisionDate: "2024-01-31T12:00:00.000Z", name: "encName",
}; revisionDate: "2024-01-31T12:00:00.000Z",
},
];
const expectedFolderView = [
{
id: "id",
name: "encName",
revisionDate: new Date("2024-01-31T12:00:00.000Z"),
},
];
const result = sut.deserializer(JSON.parse(JSON.stringify(inputObj)));
expect(result).toEqual(expectedFolderView);
}); });
afterEach(() => { it("should handle null input", async () => {
jest.resetAllMocks(); const result = sut.deserializer(null);
}); expect(result).toEqual([]);
it("should deserialize encrypted folders", async () => {
const inputObj = [data];
const expectedFolderView = {
id: "id",
name: "encName",
revisionDate: new Date("2024-01-31T12:00:00.000Z"),
};
const result = sut.deserialize(JSON.parse(JSON.stringify(inputObj)));
expect(result).toEqual([expectedFolderView]);
});
it("should derive encrypted folders", async () => {
const folderViewMock = new FolderView(new Folder(data));
keyService.hasUserKey.mockResolvedValue(true);
folderService.decryptFolders.mockResolvedValue([folderViewMock]);
const encryptedFoldersState = { id: data };
const derivedStateResult = await sut.derive(encryptedFoldersState, {
folderService,
keyService,
});
expect(derivedStateResult).toEqual([folderViewMock]);
}); });
}); });

View File

@@ -1,34 +1,23 @@
import { Jsonify } from "type-fest"; import { Jsonify } from "type-fest";
import { KeyService } from "../../../../../key-management/src/abstractions/key.service"; import { FOLDER_DISK, FOLDER_MEMORY, UserKeyDefinition } from "../../../platform/state";
import { DeriveDefinition, FOLDER_DISK, UserKeyDefinition } from "../../../platform/state";
import { FolderService } from "../../abstractions/folder/folder.service.abstraction";
import { FolderData } from "../../models/data/folder.data"; import { FolderData } from "../../models/data/folder.data";
import { Folder } from "../../models/domain/folder";
import { FolderView } from "../../models/view/folder.view"; import { FolderView } from "../../models/view/folder.view";
export const FOLDER_ENCRYPTED_FOLDERS = UserKeyDefinition.record<FolderData>( export const FOLDER_ENCRYPTED_FOLDERS = UserKeyDefinition.record<FolderData>(
FOLDER_DISK, FOLDER_DISK,
"folders", "folder",
{ {
deserializer: (obj: Jsonify<FolderData>) => FolderData.fromJSON(obj), deserializer: (obj: Jsonify<FolderData>) => FolderData.fromJSON(obj),
clearOn: ["logout"], clearOn: ["logout"],
}, },
); );
export const FOLDER_DECRYPTED_FOLDERS = DeriveDefinition.from< export const FOLDER_DECRYPTED_FOLDERS = new UserKeyDefinition<FolderView[]>(
Record<string, FolderData>, FOLDER_MEMORY,
FolderView[], "decryptedFolders",
{ folderService: FolderService; keyService: KeyService } {
>(FOLDER_ENCRYPTED_FOLDERS, { deserializer: (obj: Jsonify<FolderView[]>) => obj?.map((f) => FolderView.fromJSON(f)) ?? [],
deserializer: (obj) => obj.map((f) => FolderView.fromJSON(f)), clearOn: ["logout", "lock"],
derive: async (from, { folderService, keyService }) => {
const folders = Object.values(from || {}).map((f) => new Folder(f));
if (await keyService.hasUserKey()) {
return await folderService.decryptFolders(folders);
} else {
return [];
}
}, },
}); );

View File

@@ -16,7 +16,7 @@ import {
import { FormBuilder, ReactiveFormsModule, Validators } from "@angular/forms"; import { FormBuilder, ReactiveFormsModule, Validators } from "@angular/forms";
import * as JSZip from "jszip"; import * as JSZip from "jszip";
import { concat, Observable, Subject, lastValueFrom, combineLatest, firstValueFrom } from "rxjs"; import { concat, Observable, Subject, lastValueFrom, combineLatest, firstValueFrom } from "rxjs";
import { filter, map, takeUntil } from "rxjs/operators"; import { filter, map, switchMap, takeUntil } from "rxjs/operators";
import { CollectionService, CollectionView } from "@bitwarden/admin-console/common"; import { CollectionService, CollectionView } from "@bitwarden/admin-console/common";
import { JslibModule } from "@bitwarden/angular/jslib.module"; import { JslibModule } from "@bitwarden/angular/jslib.module";
@@ -153,6 +153,8 @@ export class ImportComponent implements OnInit, OnDestroy, AfterViewInit {
private _importBlockedByPolicy = false; private _importBlockedByPolicy = false;
protected isFromAC = false; protected isFromAC = false;
private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id));
formGroup = this.formBuilder.group({ formGroup = this.formBuilder.group({
vaultSelector: [ vaultSelector: [
"myVault", "myVault",
@@ -206,6 +208,7 @@ export class ImportComponent implements OnInit, OnDestroy, AfterViewInit {
@Optional() @Optional()
protected importCollectionService: ImportCollectionServiceAbstraction, protected importCollectionService: ImportCollectionServiceAbstraction,
protected toastService: ToastService, protected toastService: ToastService,
protected accountService: AccountService,
) {} ) {}
protected get importBlockedByPolicy(): boolean { protected get importBlockedByPolicy(): boolean {
@@ -257,7 +260,10 @@ export class ImportComponent implements OnInit, OnDestroy, AfterViewInit {
private handleImportInit() { private handleImportInit() {
// Filter out the no folder-item from folderViews$ // Filter out the no folder-item from folderViews$
this.folders$ = this.folderService.folderViews$.pipe( this.folders$ = this.activeUserId$.pipe(
switchMap((userId) => {
return this.folderService.folderViews$(userId);
}),
map((folders) => folders.filter((f) => f.id != null)), map((folders) => folders.filter((f) => f.id != null)),
); );

View File

@@ -1,5 +1,5 @@
import { mock, MockProxy } from "jest-mock-extended"; import { mock, MockProxy } from "jest-mock-extended";
import { BehaviorSubject } from "rxjs"; import { BehaviorSubject, of } from "rxjs";
import { PinServiceAbstraction } from "@bitwarden/auth/common"; import { PinServiceAbstraction } from "@bitwarden/auth/common";
import { AccountInfo, AccountService } from "@bitwarden/common/auth/abstractions/account.service"; import { AccountInfo, AccountService } from "@bitwarden/common/auth/abstractions/account.service";
@@ -178,8 +178,8 @@ describe("VaultExportService", () => {
const activeAccount = { id: userId, ...accountInfo }; const activeAccount = { id: userId, ...accountInfo };
accountService.activeAccount$ = new BehaviorSubject(activeAccount); accountService.activeAccount$ = new BehaviorSubject(activeAccount);
folderService.getAllDecryptedFromState.mockResolvedValue(UserFolderViews); folderService.folderViews$.mockReturnValue(of(UserFolderViews));
folderService.getAllFromState.mockResolvedValue(UserFolders); folderService.folders$.mockReturnValue(of(UserFolders));
kdfConfigService.getKdfConfig.mockResolvedValue(DEFAULT_KDF_CONFIG); kdfConfigService.getKdfConfig.mockResolvedValue(DEFAULT_KDF_CONFIG);
encryptService.encrypt.mockResolvedValue(new EncString("encrypted")); encryptService.encrypt.mockResolvedValue(new EncString("encrypted"));
@@ -295,7 +295,7 @@ describe("VaultExportService", () => {
it("exported unencrypted object contains folders", async () => { it("exported unencrypted object contains folders", async () => {
cipherService.getAllDecrypted.mockResolvedValue(UserCipherViews.slice(0, 1)); cipherService.getAllDecrypted.mockResolvedValue(UserCipherViews.slice(0, 1));
await folderService.getAllDecryptedFromState(); folderService.folderViews$.mockReturnValue(of(UserFolderViews));
const actual = await exportService.getExport("json"); const actual = await exportService.getExport("json");
expectEqualFolderViews(UserFolderViews, actual); expectEqualFolderViews(UserFolderViews, actual);
@@ -303,7 +303,7 @@ describe("VaultExportService", () => {
it("exported encrypted json contains folders", async () => { it("exported encrypted json contains folders", async () => {
cipherService.getAll.mockResolvedValue(UserCipherDomains.slice(0, 1)); cipherService.getAll.mockResolvedValue(UserCipherDomains.slice(0, 1));
await folderService.getAllFromState(); folderService.folders$.mockReturnValue(of(UserFolders));
const actual = await exportService.getExport("encrypted_json"); const actual = await exportService.getExport("encrypted_json");
expectEqualFolders(UserFolders, actual); expectEqualFolders(UserFolders, actual);

View File

@@ -32,6 +32,8 @@ export class IndividualVaultExportService
extends BaseVaultExportService extends BaseVaultExportService
implements IndividualVaultExportServiceAbstraction implements IndividualVaultExportServiceAbstraction
{ {
private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id));
constructor( constructor(
private folderService: FolderService, private folderService: FolderService,
private cipherService: CipherService, private cipherService: CipherService,
@@ -61,9 +63,10 @@ export class IndividualVaultExportService
let decFolders: FolderView[] = []; let decFolders: FolderView[] = [];
let decCiphers: CipherView[] = []; let decCiphers: CipherView[] = [];
const promises = []; const promises = [];
const activeUserId = await firstValueFrom(this.activeUserId$);
promises.push( promises.push(
this.folderService.getAllDecryptedFromState().then((folders) => { firstValueFrom(this.folderService.folderViews$(activeUserId)).then((folders) => {
decFolders = folders; decFolders = folders;
}), }),
); );
@@ -87,9 +90,10 @@ export class IndividualVaultExportService
let folders: Folder[] = []; let folders: Folder[] = [];
let ciphers: Cipher[] = []; let ciphers: Cipher[] = [];
const promises = []; const promises = [];
const activeUserId = await firstValueFrom(this.activeUserId$);
promises.push( promises.push(
this.folderService.getAllFromState().then((f) => { firstValueFrom(this.folderService.folders$(activeUserId)).then((f) => {
folders = f; folders = f;
}), }),
); );
@@ -102,10 +106,9 @@ export class IndividualVaultExportService
await Promise.all(promises); await Promise.all(promises);
const activeUserId = await firstValueFrom( const userKey = await this.keyService.getUserKeyWithLegacySupport(
this.accountService.activeAccount$.pipe(map((a) => a?.id)), await firstValueFrom(this.activeUserId$),
); );
const userKey = await this.keyService.getUserKeyWithLegacySupport(activeUserId);
const encKeyValidation = await this.encryptService.encrypt(Utils.newGuid(), userKey); const encKeyValidation = await this.encryptService.encrypt(Utils.newGuid(), userKey);
const jsonDoc: BitwardenEncryptedIndividualJsonExport = { const jsonDoc: BitwardenEncryptedIndividualJsonExport = {

View File

@@ -1,5 +1,5 @@
import { mock, MockProxy } from "jest-mock-extended"; import { mock, MockProxy } from "jest-mock-extended";
import { BehaviorSubject } from "rxjs"; import { BehaviorSubject, of } from "rxjs";
import { PinServiceAbstraction } from "@bitwarden/auth/common"; import { PinServiceAbstraction } from "@bitwarden/auth/common";
import { AccountInfo, AccountService } from "@bitwarden/common/auth/abstractions/account.service"; import { AccountInfo, AccountService } from "@bitwarden/common/auth/abstractions/account.service";
@@ -168,8 +168,8 @@ describe("VaultExportService", () => {
kdfConfigService = mock<KdfConfigService>(); kdfConfigService = mock<KdfConfigService>();
folderService.getAllDecryptedFromState.mockResolvedValue(UserFolderViews); folderService.folderViews$.mockReturnValue(of(UserFolderViews));
folderService.getAllFromState.mockResolvedValue(UserFolders); folderService.folders$.mockReturnValue(of(UserFolders));
kdfConfigService.getKdfConfig.mockResolvedValue(DEFAULT_KDF_CONFIG); kdfConfigService.getKdfConfig.mockResolvedValue(DEFAULT_KDF_CONFIG);
encryptService.encrypt.mockResolvedValue(new EncString("encrypted")); encryptService.encrypt.mockResolvedValue(new EncString("encrypted"));
keyService.userKey$.mockReturnValue(new BehaviorSubject("mockOriginalUserKey" as any)); keyService.userKey$.mockReturnValue(new BehaviorSubject("mockOriginalUserKey" as any));
@@ -294,7 +294,7 @@ describe("VaultExportService", () => {
it("exported unencrypted object contains folders", async () => { it("exported unencrypted object contains folders", async () => {
cipherService.getAllDecrypted.mockResolvedValue(UserCipherViews.slice(0, 1)); cipherService.getAllDecrypted.mockResolvedValue(UserCipherViews.slice(0, 1));
await folderService.getAllDecryptedFromState();
const actual = await exportService.getExport("json"); const actual = await exportService.getExport("json");
expectEqualFolderViews(UserFolderViews, actual); expectEqualFolderViews(UserFolderViews, actual);
@@ -302,7 +302,7 @@ describe("VaultExportService", () => {
it("exported encrypted json contains folders", async () => { it("exported encrypted json contains folders", async () => {
cipherService.getAll.mockResolvedValue(UserCipherDomains.slice(0, 1)); cipherService.getAll.mockResolvedValue(UserCipherDomains.slice(0, 1));
await folderService.getAllFromState();
const actual = await exportService.getExport("encrypted_json"); const actual = await exportService.getExport("encrypted_json");
expectEqualFolders(UserFolders, actual); expectEqualFolders(UserFolders, actual);

View File

@@ -7,6 +7,7 @@ import { CollectionService } from "@bitwarden/admin-console/common";
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction"; import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
import { OrganizationUserStatusType, PolicyType } from "@bitwarden/common/admin-console/enums"; import { OrganizationUserStatusType, PolicyType } from "@bitwarden/common/admin-console/enums";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { CipherId } from "@bitwarden/common/types/guid"; import { CipherId } from "@bitwarden/common/types/guid";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction"; import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
@@ -31,12 +32,17 @@ export class DefaultCipherFormConfigService implements CipherFormConfigService {
private cipherService: CipherService = inject(CipherService); private cipherService: CipherService = inject(CipherService);
private folderService: FolderService = inject(FolderService); private folderService: FolderService = inject(FolderService);
private collectionService: CollectionService = inject(CollectionService); private collectionService: CollectionService = inject(CollectionService);
private accountService = inject(AccountService);
private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id));
async buildConfig( async buildConfig(
mode: CipherFormMode, mode: CipherFormMode,
cipherId?: CipherId, cipherId?: CipherId,
cipherType?: CipherType, cipherType?: CipherType,
): Promise<CipherFormConfig> { ): Promise<CipherFormConfig> {
const activeUserId = await firstValueFrom(this.activeUserId$);
const [organizations, collections, allowPersonalOwnership, folders, cipher] = const [organizations, collections, allowPersonalOwnership, folders, cipher] =
await firstValueFrom( await firstValueFrom(
combineLatest([ combineLatest([
@@ -49,9 +55,9 @@ export class DefaultCipherFormConfigService implements CipherFormConfigService {
), ),
), ),
this.allowPersonalOwnership$, this.allowPersonalOwnership$,
this.folderService.folders$.pipe( this.folderService.folders$(activeUserId).pipe(
switchMap((f) => switchMap((f) =>
this.folderService.folderViews$.pipe( this.folderService.folderViews$(activeUserId).pipe(
filter((d) => d.length - 1 === f.length), // -1 for "No Folder" in folderViews$ filter((d) => d.length - 1 === f.length), // -1 for "No Folder" in folderViews$
), ),
), ),

View File

@@ -1,11 +1,12 @@
import { CommonModule } from "@angular/common"; import { CommonModule } from "@angular/common";
import { Component, Input, OnChanges, OnDestroy } from "@angular/core"; import { Component, Input, OnChanges, OnDestroy } from "@angular/core";
import { firstValueFrom, Observable, Subject, takeUntil } from "rxjs"; import { firstValueFrom, map, Observable, Subject, takeUntil } from "rxjs";
import { CollectionService, CollectionView } from "@bitwarden/admin-console/common"; import { CollectionService, CollectionView } from "@bitwarden/admin-console/common";
import { JslibModule } from "@bitwarden/angular/jslib.module"; import { JslibModule } from "@bitwarden/angular/jslib.module";
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { isCardExpired } from "@bitwarden/common/autofill/utils"; import { isCardExpired } from "@bitwarden/common/autofill/utils";
import { CollectionId } from "@bitwarden/common/types/guid"; import { CollectionId } from "@bitwarden/common/types/guid";
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction"; import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
@@ -48,6 +49,8 @@ import { ViewIdentitySectionsComponent } from "./view-identity-sections/view-ide
export class CipherViewComponent implements OnChanges, OnDestroy { export class CipherViewComponent implements OnChanges, OnDestroy {
@Input({ required: true }) cipher: CipherView | null = null; @Input({ required: true }) cipher: CipherView | null = null;
private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id));
/** /**
* Optional list of collections the cipher is assigned to. If none are provided, they will be fetched using the * Optional list of collections the cipher is assigned to. If none are provided, they will be fetched using the
* `CipherService` and the `collectionIds` property of the cipher. * `CipherService` and the `collectionIds` property of the cipher.
@@ -66,6 +69,7 @@ export class CipherViewComponent implements OnChanges, OnDestroy {
private organizationService: OrganizationService, private organizationService: OrganizationService,
private collectionService: CollectionService, private collectionService: CollectionService,
private folderService: FolderService, private folderService: FolderService,
private accountService: AccountService,
) {} ) {}
async ngOnChanges() { async ngOnChanges() {
@@ -136,8 +140,14 @@ export class CipherViewComponent implements OnChanges, OnDestroy {
} }
if (this.cipher.folderId) { if (this.cipher.folderId) {
const activeUserId = await firstValueFrom(this.activeUserId$);
if (!activeUserId) {
return;
}
this.folder$ = this.folderService this.folder$ = this.folderService
.getDecrypted$(this.cipher.folderId) .getDecrypted$(this.cipher.folderId, activeUserId)
.pipe(takeUntil(this.destroyed$)); .pipe(takeUntil(this.destroyed$));
} }
} }