From c6431768ed5f3066e296a61bd81d5da2a875f430 Mon Sep 17 00:00:00 2001 From: gbubemismith Date: Fri, 24 Jan 2025 13:38:47 -0500 Subject: [PATCH] Used getUserId utility function --- .../background/notification.background.ts | 19 +++++++-------- .../autofill/background/overlay.background.ts | 13 ++++------ .../background/web-request.background.ts | 7 +++--- .../browser/cipher-context-menu-handler.ts | 12 +++------- .../browser/context-menu-clicked-handler.ts | 7 +++--- .../autofill/popup/fido2/fido2.component.ts | 3 ++- .../src/autofill/services/autofill.service.ts | 11 ++++----- .../src/platform/listeners/update-badge.ts | 7 +++--- .../vault-list-items-container.component.ts | 5 ++-- .../components/vault-v2/vault-v2.component.ts | 10 +++----- .../vault-v2/view-v2/view-v2.component.ts | 11 ++++----- .../trash-list-items-container.component.ts | 11 ++++----- .../admin-console/commands/share.command.ts | 7 +++--- apps/cli/src/commands/edit.command.ts | 17 +++++-------- apps/cli/src/commands/get.command.ts | 12 ++++------ apps/cli/src/commands/list.command.ts | 14 ++--------- apps/cli/src/commands/restore.command.ts | 10 +++----- apps/cli/src/vault/create.command.ts | 9 ++++--- apps/cli/src/vault/delete.command.ts | 24 +++++-------------- .../services/desktop-autofill.service.ts | 10 ++------ .../encrypted-message-handler.service.ts | 23 ++++++------------ .../exposed-passwords-report.component.ts | 7 +++--- .../reused-passwords-report.component.ts | 7 +++--- .../tools/weak-passwords-report.component.ts | 8 +++---- .../settings/change-password.component.ts | 5 ++-- .../reports/pages/cipher-report.component.ts | 16 ++++--------- .../vault-item-dialog.component.ts | 10 +++----- .../bulk-delete-dialog.component.ts | 8 +++---- .../bulk-move-dialog.component.ts | 9 ++++--- .../vault/individual-vault/vault.component.ts | 13 +++++----- .../app/vault/org-vault/add-edit.component.ts | 5 ++-- .../vault/org-vault/attachments.component.ts | 3 ++- ...dmin-console-cipher-form-config.service.ts | 5 ++-- .../app/vault/org-vault/vault.component.ts | 7 +++--- .../components/collections.component.ts | 7 +++--- .../angular/src/components/share.component.ts | 7 +++--- .../vault/components/add-edit.component.ts | 11 ++++----- .../vault/components/attachments.component.ts | 13 +++++----- .../components/password-history.component.ts | 7 +++--- .../vault/components/vault-items.component.ts | 7 +++--- .../src/vault/components/view.component.ts | 13 +++++----- .../services/vault-filter.service.ts | 8 +++---- .../fido2/fido2-authenticator.service.ts | 15 ++++++------ .../event/event-collection.service.ts | 1 - .../individual-vault-export.service.ts | 11 ++++----- .../src/services/org-vault-export.service.ts | 11 ++++----- .../cipher-attachments.component.ts | 7 +++--- .../delete-attachment.component.ts | 7 +++--- .../default-cipher-form-config.service.ts | 5 ++-- .../services/default-cipher-form.service.ts | 9 ++++--- .../autofill-options-view.component.ts | 7 +++--- .../assign-collections.component.ts | 3 +-- 52 files changed, 191 insertions(+), 303 deletions(-) diff --git a/apps/browser/src/autofill/background/notification.background.ts b/apps/browser/src/autofill/background/notification.background.ts index b4adeed41b9..5c7cac2445a 100644 --- a/apps/browser/src/autofill/background/notification.background.ts +++ b/apps/browser/src/autofill/background/notification.background.ts @@ -1,12 +1,13 @@ // FIXME: Update this file to be type safe and remove this and next line // @ts-strict-ignore -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction"; import { PolicyType } from "@bitwarden/common/admin-console/enums"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service"; import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { ExtensionCommand, ExtensionCommandType, @@ -83,8 +84,6 @@ export default class NotificationBackground { getWebVaultUrlForNotification: () => this.getWebVaultUrl(), }; - private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id)); - constructor( private autofillService: AutofillService, private cipherService: CipherService, @@ -257,7 +256,7 @@ export default class NotificationBackground { return; } - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const ciphers = await this.cipherService.getAllDecryptedForUrl(loginInfo.url, activeUserId); const usernameMatches = ciphers.filter( (c) => c.login.username != null && c.login.username.toLowerCase() === normalizedUsername, @@ -336,7 +335,7 @@ export default class NotificationBackground { } let id: string = null; - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const ciphers = await this.cipherService.getAllDecryptedForUrl(changeData.url, activeUserId); if (changeData.currentPassword != null) { const passwordMatches = ciphers.filter( @@ -490,7 +489,7 @@ export default class NotificationBackground { this.notificationQueue.splice(i, 1); - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); if (queueMessage.type === NotificationQueueMessageType.ChangePassword) { const cipherView = await this.getDecryptedCipherById(queueMessage.cipherId, activeUserId); @@ -555,7 +554,7 @@ export default class NotificationBackground { ) { cipherView.login.password = newPassword; - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); if (edit) { await this.editItem(cipherView, activeUserId, tab); @@ -600,7 +599,7 @@ export default class NotificationBackground { if (Utils.isNullOrWhitespace(folderId) || folderId === "null") { return false; } - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const folders = await firstValueFrom(this.folderService.folderViews$(activeUserId)); return folders.some((x) => x.id === folderId); } @@ -608,7 +607,7 @@ export default class NotificationBackground { private async getDecryptedCipherById(cipherId: string, userId: UserId) { const cipher = await this.cipherService.get(cipherId, userId); if (cipher != null && cipher.type === CipherType.Login) { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); return await cipher.decrypt( await this.cipherService.getKeyForCipherKeyDecryption(cipher, activeUserId), @@ -648,7 +647,7 @@ export default class NotificationBackground { * Returns the first value found from the folder service's folderViews$ observable. */ private async getFolderData() { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); return await firstValueFrom(this.folderService.folderViews$(activeUserId)); } diff --git a/apps/browser/src/autofill/background/overlay.background.ts b/apps/browser/src/autofill/background/overlay.background.ts index cbfba8d4858..2a4bac197ed 100644 --- a/apps/browser/src/autofill/background/overlay.background.ts +++ b/apps/browser/src/autofill/background/overlay.background.ts @@ -16,6 +16,7 @@ import { parse } from "tldts"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service"; import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { AutofillOverlayVisibility, SHOW_AUTOFILL_BUTTON, @@ -411,9 +412,7 @@ export class OverlayBackground implements OverlayBackgroundInterface { return this.getAllCipherTypeViews(currentTab); } - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const cipherViews = ( await this.cipherService.getAllDecryptedForUrl(currentTab.url || "", activeUserId) ).sort((a, b) => this.cipherService.sortCiphersByLastUsedThenName(a, b)); @@ -434,9 +433,7 @@ export class OverlayBackground implements OverlayBackgroundInterface { } this.cardAndIdentityCiphers.clear(); - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const cipherViews = ( await this.cipherService.getAllDecryptedForUrl(currentTab.url || "", activeUserId, [ CipherType.Card, @@ -2407,9 +2404,7 @@ export class OverlayBackground implements OverlayBackgroundInterface { try { this.closeInlineMenu(sender); - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); await this.cipherService.setAddEditCipherInfo( { cipher: cipherView, diff --git a/apps/browser/src/autofill/background/web-request.background.ts b/apps/browser/src/autofill/background/web-request.background.ts index cacc5615654..5609d056381 100644 --- a/apps/browser/src/autofill/background/web-request.background.ts +++ b/apps/browser/src/autofill/background/web-request.background.ts @@ -1,10 +1,11 @@ // FIXME: Update this file to be type safe and remove this and next line // @ts-strict-ignore -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service"; import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { UriMatchStrategy } from "@bitwarden/common/models/domain/domain-service"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; @@ -59,9 +60,7 @@ export default class WebRequestBackground { // eslint-disable-next-line private async resolveAuthCredentials(domain: string, success: Function, error: Function) { - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const authStatus = await firstValueFrom(this.authService.authStatusFor$(activeUserId)); if (authStatus < AuthenticationStatus.Unlocked) { diff --git a/apps/browser/src/autofill/browser/cipher-context-menu-handler.ts b/apps/browser/src/autofill/browser/cipher-context-menu-handler.ts index b2bb22943d6..240a5ad3955 100644 --- a/apps/browser/src/autofill/browser/cipher-context-menu-handler.ts +++ b/apps/browser/src/autofill/browser/cipher-context-menu-handler.ts @@ -1,8 +1,9 @@ -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service"; import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { Utils } from "@bitwarden/common/platform/misc/utils"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; import { CipherType } from "@bitwarden/common/vault/enums"; @@ -39,14 +40,7 @@ export class CipherContextMenuHandler { return; } - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); - - if (!activeUserId) { - // Show error be thrown here or is it okay to just return? - return; - } + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const ciphers = await this.cipherService.getAllDecryptedForUrl(url, activeUserId, [ CipherType.Card, diff --git a/apps/browser/src/autofill/browser/context-menu-clicked-handler.ts b/apps/browser/src/autofill/browser/context-menu-clicked-handler.ts index 18ae9fa01ef..5ea15eac4f4 100644 --- a/apps/browser/src/autofill/browser/context-menu-clicked-handler.ts +++ b/apps/browser/src/autofill/browser/context-menu-clicked-handler.ts @@ -1,12 +1,13 @@ // FIXME: Update this file to be type safe and remove this and next line // @ts-strict-ignore -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { EventCollectionService } from "@bitwarden/common/abstractions/event/event-collection.service"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service"; import { UserVerificationService } from "@bitwarden/common/auth/abstractions/user-verification/user-verification.service.abstraction"; import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { AUTOFILL_CARD_ID, AUTOFILL_ID, @@ -105,9 +106,7 @@ export class ContextMenuClickedHandler { menuItemId as string, ); - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); if (isCreateCipherAction) { // pass; defer to logic below diff --git a/apps/browser/src/autofill/popup/fido2/fido2.component.ts b/apps/browser/src/autofill/popup/fido2/fido2.component.ts index 427c598bbf4..2a423bc52fb 100644 --- a/apps/browser/src/autofill/popup/fido2/fido2.component.ts +++ b/apps/browser/src/autofill/popup/fido2/fido2.component.ts @@ -20,6 +20,7 @@ import { import { JslibModule } from "@bitwarden/angular/jslib.module"; import { SearchService } from "@bitwarden/common/abstractions/search.service"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { DomainSettingsService } from "@bitwarden/common/autofill/services/domain-settings.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; @@ -187,7 +188,7 @@ export class Fido2Component implements OnInit, OnDestroy { ); const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), + getUserId(this.accountService.activeAccount$), ); this.ciphers = (await this.cipherService.getAllDecrypted(activeUserId)).filter( (cipher) => cipher.type === CipherType.Login && !cipher.isDeleted, diff --git a/apps/browser/src/autofill/services/autofill.service.ts b/apps/browser/src/autofill/services/autofill.service.ts index 669cfb5c3c3..4c1255ebd03 100644 --- a/apps/browser/src/autofill/services/autofill.service.ts +++ b/apps/browser/src/autofill/services/autofill.service.ts @@ -1,13 +1,14 @@ // FIXME: Update this file to be type safe and remove this and next line // @ts-strict-ignore import { filter, firstValueFrom, merge, Observable, ReplaySubject, scan, startWith } from "rxjs"; -import { map, pairwise } from "rxjs/operators"; +import { pairwise } from "rxjs/operators"; import { EventCollectionService } from "@bitwarden/common/abstractions/event/event-collection.service"; import { AccountInfo, AccountService } from "@bitwarden/common/auth/abstractions/account.service"; import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service"; import { UserVerificationService } from "@bitwarden/common/auth/abstractions/user-verification/user-verification.service.abstraction"; import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { AutofillOverlayVisibility, CardExpiryDateDelimiters, @@ -67,8 +68,6 @@ export default class AutofillService implements AutofillServiceInterface { private currentlyOpeningPasswordRepromptPopout = false; private autofillScriptPortsSet = new Set(); - private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id)); - static searchFieldNamesSet = new Set(AutoFillConstants.SearchFieldNames); constructor( @@ -429,7 +428,7 @@ export default class AutofillService implements AutofillServiceInterface { options.cipher.login.totp = null; } - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); let didAutofill = false; await Promise.all( @@ -533,7 +532,7 @@ export default class AutofillService implements AutofillServiceInterface { ): Promise { let cipher: CipherView; - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); if (fromCommand) { cipher = await this.cipherService.getNextCipherForUrl(tab.url, activeUserId); @@ -638,7 +637,7 @@ export default class AutofillService implements AutofillServiceInterface { let cipher: CipherView; let cacheKey = ""; - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); if (cipherType === CipherType.Card) { cacheKey = "cardCiphers"; diff --git a/apps/browser/src/platform/listeners/update-badge.ts b/apps/browser/src/platform/listeners/update-badge.ts index e001dd4f971..73fad4b3d10 100644 --- a/apps/browser/src/platform/listeners/update-badge.ts +++ b/apps/browser/src/platform/listeners/update-badge.ts @@ -1,10 +1,11 @@ // FIXME: Update this file to be type safe and remove this and next line // @ts-strict-ignore -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service"; import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { BadgeSettingsServiceAbstraction } from "@bitwarden/common/autofill/services/badge-settings.service"; import { Utils } from "@bitwarden/common/platform/misc/utils"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; @@ -90,9 +91,7 @@ export class UpdateBadge { return; } - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const ciphers = await this.cipherService.getAllDecryptedForUrl(opts?.tab?.url, activeUserId); let countText = ciphers.length == 0 ? "" : ciphers.length.toString(); if (ciphers.length > 9) { diff --git a/apps/browser/src/vault/popup/components/vault-v2/vault-list-items-container/vault-list-items-container.component.ts b/apps/browser/src/vault/popup/components/vault-v2/vault-list-items-container/vault-list-items-container.component.ts index c3795ea8f3b..51fdaaea862 100644 --- a/apps/browser/src/vault/popup/components/vault-v2/vault-list-items-container/vault-list-items-container.component.ts +++ b/apps/browser/src/vault/popup/components/vault-v2/vault-list-items-container/vault-list-items-container.component.ts @@ -20,6 +20,7 @@ import { firstValueFrom, Observable, map } from "rxjs"; import { JslibModule } from "@bitwarden/angular/jslib.module"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; import { CipherId } from "@bitwarden/common/types/guid"; @@ -268,9 +269,7 @@ export class VaultListItemsContainerComponent implements OnInit, AfterViewInit { this.viewCipherTimeout = null; } - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); await this.cipherService.updateLastLaunchedDate(cipher.id, activeUserId); await BrowserApi.createNewTab(cipher.login.launchUri); diff --git a/apps/browser/src/vault/popup/components/vault-v2/vault-v2.component.ts b/apps/browser/src/vault/popup/components/vault-v2/vault-v2.component.ts index dfa919b9d12..9871cd338c1 100644 --- a/apps/browser/src/vault/popup/components/vault-v2/vault-v2.component.ts +++ b/apps/browser/src/vault/popup/components/vault-v2/vault-v2.component.ts @@ -8,7 +8,8 @@ import { filter, map, take } from "rxjs/operators"; import { JslibModule } from "@bitwarden/angular/jslib.module"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; -import { CipherId, CollectionId, OrganizationId, UserId } from "@bitwarden/common/types/guid"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; +import { CipherId, CollectionId, OrganizationId } from "@bitwarden/common/types/guid"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; import { CipherType } from "@bitwarden/common/vault/enums"; import { ButtonModule, DialogService, Icons, NoItemsModule } from "@bitwarden/components"; @@ -122,12 +123,7 @@ export class VaultV2Component implements OnInit, OnDestroy { } async ngOnInit() { - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe( - map((a) => a?.id), - filter((userId): userId is UserId => userId != null), - ), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); this.cipherService .failedToDecryptCiphers$(activeUserId) diff --git a/apps/browser/src/vault/popup/components/vault-v2/view-v2/view-v2.component.ts b/apps/browser/src/vault/popup/components/vault-v2/view-v2/view-v2.component.ts index 7c8c739869b..72847c49fb5 100644 --- a/apps/browser/src/vault/popup/components/vault-v2/view-v2/view-v2.component.ts +++ b/apps/browser/src/vault/popup/components/vault-v2/view-v2/view-v2.component.ts @@ -5,13 +5,14 @@ import { Component } from "@angular/core"; import { takeUntilDestroyed } from "@angular/core/rxjs-interop"; import { FormsModule } from "@angular/forms"; import { ActivatedRoute, Router } from "@angular/router"; -import { firstValueFrom, map, Observable, switchMap } from "rxjs"; +import { firstValueFrom, Observable, switchMap } from "rxjs"; import { CollectionView } from "@bitwarden/admin-console/common"; import { JslibModule } from "@bitwarden/angular/jslib.module"; import { EventCollectionService } from "@bitwarden/common/abstractions/event/event-collection.service"; import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { AUTOFILL_ID, COPY_PASSWORD_ID, @@ -100,8 +101,6 @@ export class ViewV2Component { loadAction: LoadAction; senderTabId?: number; - private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id)); - constructor( private route: ActivatedRoute, private router: Router, @@ -166,7 +165,7 @@ export class ViewV2Component { } async getCipherData(id: string) { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const cipher = await this.cipherService.get(id, activeUserId); return await cipher.decrypt( await this.cipherService.getKeyForCipherKeyDecryption(cipher, activeUserId), @@ -197,7 +196,7 @@ export class ViewV2Component { } try { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); await this.deleteCipher(activeUserId); } catch (e) { this.logService.error(e); @@ -217,7 +216,7 @@ export class ViewV2Component { restore = async (): Promise => { try { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); await this.cipherService.restoreWithServer(this.cipher.id, activeUserId); } catch (e) { this.logService.error(e); diff --git a/apps/browser/src/vault/popup/settings/trash-list-items-container/trash-list-items-container.component.ts b/apps/browser/src/vault/popup/settings/trash-list-items-container/trash-list-items-container.component.ts index 780c0cc322c..19fa567c34d 100644 --- a/apps/browser/src/vault/popup/settings/trash-list-items-container/trash-list-items-container.component.ts +++ b/apps/browser/src/vault/popup/settings/trash-list-items-container/trash-list-items-container.component.ts @@ -3,10 +3,11 @@ import { CommonModule } from "@angular/common"; import { ChangeDetectionStrategy, Component, Input } from "@angular/core"; import { Router } from "@angular/router"; -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { JslibModule } from "@bitwarden/angular/jslib.module"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { CipherId } from "@bitwarden/common/types/guid"; @@ -84,9 +85,7 @@ export class TrashListItemsContainerComponent { async restore(cipher: CipherView) { try { - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); await this.cipherService.restoreWithServer(cipher.id, activeUserId); await this.router.navigate(["/trash"]); @@ -118,9 +117,7 @@ export class TrashListItemsContainerComponent { } try { - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); await this.cipherService.deleteWithServer(cipher.id, activeUserId); await this.router.navigate(["/trash"]); diff --git a/apps/cli/src/admin-console/commands/share.command.ts b/apps/cli/src/admin-console/commands/share.command.ts index d3e3f1b8f84..9159f2c63af 100644 --- a/apps/cli/src/admin-console/commands/share.command.ts +++ b/apps/cli/src/admin-console/commands/share.command.ts @@ -1,8 +1,9 @@ // FIXME: Update this file to be type safe and remove this and next line // @ts-strict-ignore -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; import { Response } from "../../models/response"; @@ -48,9 +49,7 @@ export class ShareCommand { organizationId = organizationId.toLowerCase(); } - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const cipher = await this.cipherService.get(id, activeUserId); if (cipher == null) { diff --git a/apps/cli/src/commands/edit.command.ts b/apps/cli/src/commands/edit.command.ts index d1325a7e994..2d1adaefa87 100644 --- a/apps/cli/src/commands/edit.command.ts +++ b/apps/cli/src/commands/edit.command.ts @@ -1,11 +1,12 @@ // FIXME: Update this file to be type safe and remove this and next line // @ts-strict-ignore -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { CollectionRequest } from "@bitwarden/admin-console/common"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { SelectionReadOnlyRequest } from "@bitwarden/common/admin-console/models/request/selection-read-only.request"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { CipherExport } from "@bitwarden/common/models/export/cipher.export"; import { CollectionExport } from "@bitwarden/common/models/export/collection.export"; import { FolderExport } from "@bitwarden/common/models/export/folder.export"; @@ -25,8 +26,6 @@ import { CipherResponse } from "../vault/models/cipher.response"; import { FolderResponse } from "../vault/models/folder.response"; export class EditCommand { - private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id)); - constructor( private cipherService: CipherService, private folderService: FolderService, @@ -85,9 +84,7 @@ export class EditCommand { } private async editCipher(id: string, req: CipherExport) { - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const cipher = await this.cipherService.get(id, activeUserId); if (cipher == null) { return Response.notFound(); @@ -114,9 +111,7 @@ export class EditCommand { } private async editCipherCollections(id: string, req: string[]) { - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const cipher = await this.cipherService.get(id, activeUserId); if (cipher == null) { @@ -137,7 +132,7 @@ export class EditCommand { const decCipher = await updatedCipher.decrypt( await this.cipherService.getKeyForCipherKeyDecryption( updatedCipher, - await firstValueFrom(this.activeUserId$), + await firstValueFrom(getUserId(this.accountService.activeAccount$)), ), ); const res = new CipherResponse(decCipher); @@ -148,7 +143,7 @@ export class EditCommand { } private async editFolder(id: string, req: FolderExport) { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const folder = await this.folderService.getFromState(id, activeUserId); if (folder == null) { return Response.notFound(); diff --git a/apps/cli/src/commands/get.command.ts b/apps/cli/src/commands/get.command.ts index 338f176be45..a2b2854e77c 100644 --- a/apps/cli/src/commands/get.command.ts +++ b/apps/cli/src/commands/get.command.ts @@ -52,8 +52,6 @@ import { FolderResponse } from "../vault/models/folder.response"; import { DownloadCommand } from "./download.command"; export class GetCommand extends DownloadCommand { - private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id)); - constructor( private cipherService: CipherService, private folderService: FolderService, @@ -114,7 +112,7 @@ export class GetCommand extends DownloadCommand { private async getCipherView(id: string): Promise { let decCipher: CipherView = null; - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); if (Utils.isGuid(id)) { const cipher = await this.cipherService.get(id, activeUserId); if (cipher != null) { @@ -267,7 +265,7 @@ export class GetCommand extends DownloadCommand { ); if (!canAccessPremium) { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const originalCipher = await this.cipherService.get(cipher.id, activeUserId); if ( originalCipher == null || @@ -354,7 +352,7 @@ export class GetCommand extends DownloadCommand { this.accountProfileService.hasPremiumFromAnySource$(account.id), ); if (!canAccessPremium) { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const originalCipher = await this.cipherService.get(cipher.id, activeUserId); if (originalCipher == null || originalCipher.organizationId == null) { return Response.error("Premium status is required to use this feature."); @@ -387,7 +385,7 @@ export class GetCommand extends DownloadCommand { private async getFolder(id: string) { let decFolder: FolderView = null; - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); if (Utils.isGuid(id)) { const folder = await this.folderService.getFromState(id, activeUserId); if (folder != null) { @@ -564,7 +562,7 @@ export class GetCommand extends DownloadCommand { private async getFingerprint(id: string) { let fingerprint: string[] = null; if (id === "me") { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const publicKey = await firstValueFrom(this.keyService.userPublicKey$(activeUserId)); fingerprint = await this.keyService.getFingerprint(activeUserId, publicKey); } else if (Utils.isGuid(id)) { diff --git a/apps/cli/src/commands/list.command.ts b/apps/cli/src/commands/list.command.ts index 4a371eda91d..b85a8f8dcfd 100644 --- a/apps/cli/src/commands/list.command.ts +++ b/apps/cli/src/commands/list.command.ts @@ -66,12 +66,7 @@ export class ListCommand { private async listCiphers(options: Options) { let ciphers: CipherView[]; - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); - if (activeUserId == null) { - return Response.error("No active user id found."); - } + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); options.trash = options.trash || false; if (options.url != null && options.url.trim() !== "") { @@ -146,12 +141,7 @@ export class ListCommand { } private async listFolders(options: Options) { - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); - if (activeUserId == null) { - return Response.error("No active user id found."); - } + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); let folders = await this.folderService.getAllDecryptedFromState(activeUserId); diff --git a/apps/cli/src/commands/restore.command.ts b/apps/cli/src/commands/restore.command.ts index 64c0969680f..691969099df 100644 --- a/apps/cli/src/commands/restore.command.ts +++ b/apps/cli/src/commands/restore.command.ts @@ -1,6 +1,7 @@ -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; import { Response } from "../models/response"; @@ -25,12 +26,7 @@ export class RestoreCommand { } private async restoreCipher(id: string) { - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); - if (activeUserId == null) { - return Response.error("No active user id found."); - } + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const cipher = await this.cipherService.get(id, activeUserId); if (cipher == null) { diff --git a/apps/cli/src/vault/create.command.ts b/apps/cli/src/vault/create.command.ts index b64d37af9a5..19ecad3a204 100644 --- a/apps/cli/src/vault/create.command.ts +++ b/apps/cli/src/vault/create.command.ts @@ -10,6 +10,7 @@ import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; import { SelectionReadOnlyRequest } from "@bitwarden/common/admin-console/models/request/selection-read-only.request"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { CipherExport } from "@bitwarden/common/models/export/cipher.export"; import { CollectionExport } from "@bitwarden/common/models/export/collection.export"; @@ -30,8 +31,6 @@ import { CipherResponse } from "./models/cipher.response"; import { FolderResponse } from "./models/folder.response"; export class CreateCommand { - private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id)); - constructor( private cipherService: CipherService, private folderService: FolderService, @@ -90,7 +89,7 @@ export class CreateCommand { } private async createCipher(req: CipherExport) { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const cipher = await this.cipherService.encrypt(CipherExport.toView(req), activeUserId); try { const newCipher = await this.cipherService.createWithServer(cipher); @@ -132,7 +131,7 @@ export class CreateCommand { return Response.badRequest("File name not provided."); } - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const itemId = options.itemId.toLowerCase(); const cipher = await this.cipherService.get(itemId, activeUserId); @@ -173,7 +172,7 @@ export class CreateCommand { } private async createFolder(req: FolderExport) { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const userKey = await this.keyService.getUserKeyWithLegacySupport(activeUserId); const folder = await this.folderService.encrypt(FolderExport.toView(req), userKey); try { diff --git a/apps/cli/src/vault/delete.command.ts b/apps/cli/src/vault/delete.command.ts index 1ffc7eae6d1..304c7e18105 100644 --- a/apps/cli/src/vault/delete.command.ts +++ b/apps/cli/src/vault/delete.command.ts @@ -1,7 +1,8 @@ -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { Utils } from "@bitwarden/common/platform/misc/utils"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; @@ -44,12 +45,7 @@ export class DeleteCommand { } private async deleteCipher(id: string, options: Options) { - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); - if (activeUserId == null) { - return Response.error("No active user id found."); - } + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const cipher = await this.cipherService.get(id, activeUserId); if (cipher == null) { @@ -81,12 +77,7 @@ export class DeleteCommand { return Response.badRequest("`itemid` option is required."); } - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); - if (activeUserId == null) { - return Response.error("No active user id found."); - } + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const itemId = options.itemId.toLowerCase(); const cipher = await this.cipherService.get(itemId, activeUserId); @@ -103,9 +94,8 @@ export class DeleteCommand { return Response.error("Attachment `" + id + "` was not found."); } - const account = await firstValueFrom(this.accountService.activeAccount$); const canAccessPremium = await firstValueFrom( - this.accountProfileService.hasPremiumFromAnySource$(account.id), + this.accountProfileService.hasPremiumFromAnySource$(activeUserId), ); if (cipher.organizationId == null && !canAccessPremium) { return Response.error("Premium status is required to use this feature."); @@ -124,9 +114,7 @@ export class DeleteCommand { } private async deleteFolder(id: string) { - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const folder = await this.folderService.getFromState(id, activeUserId); if (folder == null) { return Response.notFound(); diff --git a/apps/desktop/src/autofill/services/desktop-autofill.service.ts b/apps/desktop/src/autofill/services/desktop-autofill.service.ts index 3087249a934..e5c1ac2984e 100644 --- a/apps/desktop/src/autofill/services/desktop-autofill.service.ts +++ b/apps/desktop/src/autofill/services/desktop-autofill.service.ts @@ -13,6 +13,7 @@ import { } from "rxjs"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum"; import { UriMatchStrategy } from "@bitwarden/common/models/domain/domain-service"; import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service"; @@ -170,14 +171,7 @@ export class DesktopAutofillService implements OnDestroy { // TODO: For some reason the credentialId is passed as an empty array in the request, so we need to // get it from the cipher. For that we use the recordIdentifier, which is the cipherId. if (request.recordIdentifier && request.credentialId.length === 0) { - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); - if (!activeUserId) { - this.logService.error("listenPasskeyAssertion error", "Active user not found"); - callback(new Error("Active user not found"), null); - return; - } + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const cipher = await this.cipherService.get(request.recordIdentifier, activeUserId); if (!cipher) { diff --git a/apps/desktop/src/services/encrypted-message-handler.service.ts b/apps/desktop/src/services/encrypted-message-handler.service.ts index 0c470a52d5a..a8a1e738644 100644 --- a/apps/desktop/src/services/encrypted-message-handler.service.ts +++ b/apps/desktop/src/services/encrypted-message-handler.service.ts @@ -1,12 +1,13 @@ // FIXME: Update this file to be type safe and remove this and next line // @ts-strict-ignore -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction"; import { PolicyType } from "@bitwarden/common/admin-console/enums"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service"; import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service"; import { UserId } from "@bitwarden/common/types/guid"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; @@ -65,9 +66,7 @@ export class EncryptedMessageHandlerService { } private async checkUserStatus(userId: string): Promise { - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); if (userId !== activeUserId) { return "not-active-user"; @@ -83,9 +82,7 @@ export class EncryptedMessageHandlerService { private async statusCommandHandler(): Promise { const accounts = await firstValueFrom(this.accountService.accounts$); - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); if (!accounts || !Object.keys(accounts)) { return []; @@ -114,9 +111,7 @@ export class EncryptedMessageHandlerService { } const ciphersResponse: CipherResponse[] = []; - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const authStatus = await this.authService.getAuthStatus(activeUserId); if (authStatus !== AuthenticationStatus.Unlocked) { @@ -166,9 +161,7 @@ export class EncryptedMessageHandlerService { cipherView.login.uris[0].uri = credentialCreatePayload.uri; try { - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const encrypted = await this.cipherService.encrypt(cipherView, activeUserId); await this.cipherService.createWithServer(encrypted); @@ -200,9 +193,7 @@ export class EncryptedMessageHandlerService { } try { - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const cipher = await this.cipherService.get( credentialUpdatePayload.credentialId, diff --git a/apps/web/src/app/admin-console/organizations/tools/exposed-passwords-report.component.ts b/apps/web/src/app/admin-console/organizations/tools/exposed-passwords-report.component.ts index c233b1c7602..2bac020853e 100644 --- a/apps/web/src/app/admin-console/organizations/tools/exposed-passwords-report.component.ts +++ b/apps/web/src/app/admin-console/organizations/tools/exposed-passwords-report.component.ts @@ -2,7 +2,7 @@ // @ts-strict-ignore import { Component, OnInit } from "@angular/core"; import { ActivatedRoute } from "@angular/router"; -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { ModalService } from "@bitwarden/angular/services/modal.service"; import { AuditService } from "@bitwarden/common/abstractions/audit.service"; @@ -11,6 +11,7 @@ import { OrganizationService, } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; import { SyncService } from "@bitwarden/common/vault/abstractions/sync/sync.service.abstraction"; @@ -59,9 +60,7 @@ export class ExposedPasswordsReportComponent this.isAdminConsoleActive = true; // eslint-disable-next-line rxjs-angular/prefer-takeuntil, rxjs/no-async-subscribe this.route.parent.parent.params.subscribe(async (params) => { - const userId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const userId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); this.organization = await firstValueFrom( this.organizationService .organizations$(userId) diff --git a/apps/web/src/app/admin-console/organizations/tools/reused-passwords-report.component.ts b/apps/web/src/app/admin-console/organizations/tools/reused-passwords-report.component.ts index e6b6637478d..81b097a67a0 100644 --- a/apps/web/src/app/admin-console/organizations/tools/reused-passwords-report.component.ts +++ b/apps/web/src/app/admin-console/organizations/tools/reused-passwords-report.component.ts @@ -2,7 +2,7 @@ // @ts-strict-ignore import { Component, OnInit } from "@angular/core"; import { ActivatedRoute } from "@angular/router"; -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { ModalService } from "@bitwarden/angular/services/modal.service"; import { @@ -10,6 +10,7 @@ import { OrganizationService, } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; import { SyncService } from "@bitwarden/common/vault/abstractions/sync/sync.service.abstraction"; @@ -56,9 +57,7 @@ export class ReusedPasswordsReportComponent this.isAdminConsoleActive = true; // eslint-disable-next-line rxjs-angular/prefer-takeuntil, rxjs/no-async-subscribe this.route.parent.parent.params.subscribe(async (params) => { - const userId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const userId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); this.organization = await firstValueFrom( this.organizationService .organizations$(userId) diff --git a/apps/web/src/app/admin-console/organizations/tools/weak-passwords-report.component.ts b/apps/web/src/app/admin-console/organizations/tools/weak-passwords-report.component.ts index eab1e131936..e676aecf76b 100644 --- a/apps/web/src/app/admin-console/organizations/tools/weak-passwords-report.component.ts +++ b/apps/web/src/app/admin-console/organizations/tools/weak-passwords-report.component.ts @@ -2,7 +2,7 @@ // @ts-strict-ignore import { Component, OnInit } from "@angular/core"; import { ActivatedRoute } from "@angular/router"; -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { ModalService } from "@bitwarden/angular/services/modal.service"; import { @@ -10,6 +10,7 @@ import { OrganizationService, } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { PasswordStrengthServiceAbstraction } from "@bitwarden/common/tools/password-strength"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; @@ -59,9 +60,8 @@ export class WeakPasswordsReportComponent this.isAdminConsoleActive = true; // eslint-disable-next-line rxjs-angular/prefer-takeuntil, rxjs/no-async-subscribe this.route.parent.parent.params.subscribe(async (params) => { - const userId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const userId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); + this.organization = await firstValueFrom( this.organizationService .organizations$(userId) diff --git a/apps/web/src/app/auth/settings/change-password.component.ts b/apps/web/src/app/auth/settings/change-password.component.ts index a9e919e88dc..b63e1a2ed74 100644 --- a/apps/web/src/app/auth/settings/change-password.component.ts +++ b/apps/web/src/app/auth/settings/change-password.component.ts @@ -94,9 +94,8 @@ export class ChangePasswordComponent async rotateUserKeyClicked() { if (this.rotateUserKey) { - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); + const ciphers = await this.cipherService.getAllDecrypted(activeUserId); let hasOldAttachments = false; if (ciphers != null) { diff --git a/apps/web/src/app/tools/reports/pages/cipher-report.component.ts b/apps/web/src/app/tools/reports/pages/cipher-report.component.ts index c7408e99632..69689c41b56 100644 --- a/apps/web/src/app/tools/reports/pages/cipher-report.component.ts +++ b/apps/web/src/app/tools/reports/pages/cipher-report.component.ts @@ -1,20 +1,13 @@ // FIXME: Update this file to be type safe and remove this and next line // @ts-strict-ignore import { Directive, ViewChild, ViewContainerRef, OnDestroy } from "@angular/core"; -import { - BehaviorSubject, - Observable, - Subject, - firstValueFrom, - map, - switchMap, - takeUntil, -} from "rxjs"; +import { BehaviorSubject, Observable, Subject, firstValueFrom, switchMap, takeUntil } from "rxjs"; import { ModalService } from "@bitwarden/angular/services/modal.service"; import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { OrganizationId } from "@bitwarden/common/types/guid"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; @@ -47,7 +40,6 @@ export class CipherReportComponent implements OnDestroy { vaultMsg: string = "vault"; currentFilterStatus: number | string; protected filterOrgStatus$ = new BehaviorSubject(0); - private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id)); private destroyed$: Subject = new Subject(); constructor( @@ -59,7 +51,7 @@ export class CipherReportComponent implements OnDestroy { protected i18nService: I18nService, private syncService: SyncService, ) { - this.organizations$ = this.activeUserId$.pipe( + this.organizations$ = getUserId(this.accountService.activeAccount$).pipe( switchMap((userId) => this.organizationService.organizations$(userId)), ); this.organizations$.pipe(takeUntil(this.destroyed$)).subscribe((orgs) => { @@ -191,7 +183,7 @@ export class CipherReportComponent implements OnDestroy { } protected async getAllCiphers(): Promise { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); return await this.cipherService.getAllDecrypted(activeUserId); } diff --git a/apps/web/src/app/vault/components/vault-item-dialog/vault-item-dialog.component.ts b/apps/web/src/app/vault/components/vault-item-dialog/vault-item-dialog.component.ts index e70b95502d6..7197bf077dd 100644 --- a/apps/web/src/app/vault/components/vault-item-dialog/vault-item-dialog.component.ts +++ b/apps/web/src/app/vault/components/vault-item-dialog/vault-item-dialog.component.ts @@ -12,6 +12,7 @@ import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { EventCollectionService } from "@bitwarden/common/abstractions/event/event-collection.service"; import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions"; import { EventType } from "@bitwarden/common/enums"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; @@ -342,10 +343,7 @@ export class VaultItemDialogComponent implements OnInit, OnDestroy { this.formConfig.mode = "edit"; this.formConfig.initialValues = null; } - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); - + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); let cipher = await this.cipherService.get(cipherView.id, activeUserId); // When the form config is used within the Admin Console, retrieve the cipher from the admin endpoint (if not found in local state) @@ -461,9 +459,7 @@ export class VaultItemDialogComponent implements OnInit, OnDestroy { if (config.originalCipher == null) { return; } - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); return await config.originalCipher.decrypt( await this.cipherService.getKeyForCipherKeyDecryption(config.originalCipher, activeUserId), ); diff --git a/apps/web/src/app/vault/individual-vault/bulk-action-dialogs/bulk-delete-dialog/bulk-delete-dialog.component.ts b/apps/web/src/app/vault/individual-vault/bulk-action-dialogs/bulk-delete-dialog/bulk-delete-dialog.component.ts index 74e06600d75..cc09cacd72f 100644 --- a/apps/web/src/app/vault/individual-vault/bulk-action-dialogs/bulk-delete-dialog/bulk-delete-dialog.component.ts +++ b/apps/web/src/app/vault/individual-vault/bulk-action-dialogs/bulk-delete-dialog/bulk-delete-dialog.component.ts @@ -2,12 +2,13 @@ // @ts-strict-ignore import { DIALOG_DATA, DialogConfig, DialogRef } from "@angular/cdk/dialog"; import { Component, Inject } from "@angular/core"; -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { CollectionService, CollectionView } from "@bitwarden/admin-console/common"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; @@ -119,10 +120,7 @@ export class BulkDeleteDialogComponent { private async deleteCiphers(): Promise { const asAdmin = this.organization?.canEditAllCiphers; - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); - + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); if (this.permanent) { await this.cipherService.deleteManyWithServer(this.cipherIds, activeUserId, asAdmin); } else { diff --git a/apps/web/src/app/vault/individual-vault/bulk-action-dialogs/bulk-move-dialog/bulk-move-dialog.component.ts b/apps/web/src/app/vault/individual-vault/bulk-action-dialogs/bulk-move-dialog/bulk-move-dialog.component.ts index 31160ba969c..c6e04c67576 100644 --- a/apps/web/src/app/vault/individual-vault/bulk-action-dialogs/bulk-move-dialog/bulk-move-dialog.component.ts +++ b/apps/web/src/app/vault/individual-vault/bulk-action-dialogs/bulk-move-dialog/bulk-move-dialog.component.ts @@ -3,9 +3,10 @@ import { DialogConfig, DialogRef, DIALOG_DATA } from "@angular/cdk/dialog"; import { Component, Inject, OnInit } from "@angular/core"; import { FormBuilder, Validators } from "@angular/forms"; -import { firstValueFrom, map, Observable } from "rxjs"; +import { firstValueFrom, Observable } from "rxjs"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; @@ -48,8 +49,6 @@ export class BulkMoveDialogComponent implements OnInit { }); folders$: Observable; - private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id)); - constructor( @Inject(DIALOG_DATA) params: BulkMoveDialogParams, private dialogRef: DialogRef, @@ -65,7 +64,7 @@ export class BulkMoveDialogComponent implements OnInit { } async ngOnInit() { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); this.folders$ = this.folderService.folderViews$(activeUserId); this.formGroup.patchValue({ folderId: (await firstValueFrom(this.folders$))[0].id, @@ -81,7 +80,7 @@ export class BulkMoveDialogComponent implements OnInit { return; } - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); await this.cipherService.moveManyWithServer( this.cipherIds, this.formGroup.value.folderId, diff --git a/apps/web/src/app/vault/individual-vault/vault.component.ts b/apps/web/src/app/vault/individual-vault/vault.component.ts index 4e6a6032bb2..4201db38ffe 100644 --- a/apps/web/src/app/vault/individual-vault/vault.component.ts +++ b/apps/web/src/app/vault/individual-vault/vault.component.ts @@ -53,6 +53,7 @@ import { } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { OrganizationBillingServiceAbstraction } from "@bitwarden/common/billing/abstractions"; import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { BillingApiServiceAbstraction } from "@bitwarden/common/billing/abstractions/billing-api.service.abstraction"; @@ -194,8 +195,6 @@ export class VaultComponent implements OnInit, OnDestroy { private extensionRefreshEnabled: boolean; private hasSubscription$ = new BehaviorSubject(false); - private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id)); - private vaultItemDialogRef?: DialogRef | undefined; private organizations$ = this.accountService.activeAccount$ .pipe(map((a) => a?.id)) @@ -297,7 +296,7 @@ export class VaultComponent implements OnInit, OnDestroy { : "trashCleanupWarning", ); - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const firstSetup$ = this.route.queryParams.pipe( first(), @@ -820,7 +819,7 @@ export class VaultComponent implements OnInit, OnDestroy { } async editCipherId(id: string, cloneMode?: boolean) { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const cipher = await this.cipherService.get(id, activeUserId); if ( @@ -900,7 +899,7 @@ export class VaultComponent implements OnInit, OnDestroy { * @returns Promise */ async viewCipherById(id: string) { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const cipher = await this.cipherService.get(id, activeUserId); // If cipher exists (cipher is null when new) and MP reprompt // is on for this cipher, then show password reprompt. @@ -1096,7 +1095,7 @@ export class VaultComponent implements OnInit, OnDestroy { } try { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); await this.cipherService.restoreWithServer(c.id, activeUserId); this.toastService.showToast({ variant: "success", @@ -1179,7 +1178,7 @@ export class VaultComponent implements OnInit, OnDestroy { } try { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); await this.deleteCipherWithServer(c.id, activeUserId, permanent); this.toastService.showToast({ diff --git a/apps/web/src/app/vault/org-vault/add-edit.component.ts b/apps/web/src/app/vault/org-vault/add-edit.component.ts index 9eea8a82bc9..941a2a593ec 100644 --- a/apps/web/src/app/vault/org-vault/add-edit.component.ts +++ b/apps/web/src/app/vault/org-vault/add-edit.component.ts @@ -11,6 +11,7 @@ import { EventCollectionService } from "@bitwarden/common/abstractions/event/eve 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 { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.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 { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; @@ -99,7 +100,7 @@ export class AddEditComponent extends BaseAddEditComponent { protected async loadCipher() { this.isAdminConsoleAction = true; - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); // Calling loadCipher first to assess if the cipher is unassigned. If null use apiService getCipherAdmin const firstCipherCheck = await super.loadCipher(activeUserId); @@ -125,7 +126,7 @@ export class AddEditComponent extends BaseAddEditComponent { protected async deleteCipher() { if (!this.organization.canEditAllCiphers) { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); return super.deleteCipher(activeUserId); } return this.cipher.isDeleted diff --git a/apps/web/src/app/vault/org-vault/attachments.component.ts b/apps/web/src/app/vault/org-vault/attachments.component.ts index 81f36449368..50d3b3d4500 100644 --- a/apps/web/src/app/vault/org-vault/attachments.component.ts +++ b/apps/web/src/app/vault/org-vault/attachments.component.ts @@ -6,6 +6,7 @@ import { firstValueFrom } from "rxjs"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { EncryptService } from "@bitwarden/common/platform/abstractions/encrypt.service"; import { FileDownloadService } from "@bitwarden/common/platform/abstractions/file-download/file-download.service"; @@ -75,7 +76,7 @@ export class AttachmentsComponent extends BaseAttachmentsComponent implements On protected async loadCipher() { if (!this.organization.canEditAllCiphers) { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); return await super.loadCipher(activeUserId); } const response = await this.apiService.getCipherAdmin(this.cipherId); diff --git a/apps/web/src/app/vault/org-vault/services/admin-console-cipher-form-config.service.ts b/apps/web/src/app/vault/org-vault/services/admin-console-cipher-form-config.service.ts index 7e2629477d0..45069e1543a 100644 --- a/apps/web/src/app/vault/org-vault/services/admin-console-cipher-form-config.service.ts +++ b/apps/web/src/app/vault/org-vault/services/admin-console-cipher-form-config.service.ts @@ -10,6 +10,7 @@ import { PolicyService } from "@bitwarden/common/admin-console/abstractions/poli import { OrganizationUserStatusType, PolicyType } from "@bitwarden/common/admin-console/enums"; import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { CipherId } from "@bitwarden/common/types/guid"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; import { CipherType } from "@bitwarden/common/vault/enums"; @@ -104,9 +105,7 @@ export class AdminConsoleCipherFormConfigService implements CipherFormConfigServ return null; } - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const localCipher = await this.cipherService.get(id, activeUserId); diff --git a/apps/web/src/app/vault/org-vault/vault.component.ts b/apps/web/src/app/vault/org-vault/vault.component.ts index 213331b7bb9..022817785ac 100644 --- a/apps/web/src/app/vault/org-vault/vault.component.ts +++ b/apps/web/src/app/vault/org-vault/vault.component.ts @@ -50,6 +50,7 @@ import { OrganizationApiServiceAbstraction } from "@bitwarden/common/admin-conso import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { OrganizationBillingServiceAbstraction } from "@bitwarden/common/billing/abstractions"; import { BillingApiServiceAbstraction } from "@bitwarden/common/billing/abstractions/billing-api.service.abstraction"; import { EventType } from "@bitwarden/common/enums"; @@ -239,8 +240,6 @@ export class VaultComponent implements OnInit, OnDestroy { ), ); - private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id)); - constructor( private route: ActivatedRoute, private organizationService: OrganizationService, @@ -1072,7 +1071,7 @@ export class VaultComponent implements OnInit, OnDestroy { // Allow restore of an Unassigned Item try { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const asAdmin = this.organization?.canEditAnyCollection || c.isUnassigned; await this.cipherService.restoreWithServer(c.id, activeUserId, asAdmin); this.toastService.showToast({ @@ -1165,7 +1164,7 @@ export class VaultComponent implements OnInit, OnDestroy { } try { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); await this.deleteCipherWithServer(c.id, activeUserId, permanent, c.isUnassigned); this.toastService.showToast({ variant: "success", diff --git a/libs/angular/src/admin-console/components/collections.component.ts b/libs/angular/src/admin-console/components/collections.component.ts index ef38d4e3d6d..32aee4087be 100644 --- a/libs/angular/src/admin-console/components/collections.component.ts +++ b/libs/angular/src/admin-console/components/collections.component.ts @@ -7,6 +7,7 @@ import { CollectionService, CollectionView } from "@bitwarden/admin-console/comm import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction"; import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; @@ -30,8 +31,6 @@ export class CollectionsComponent implements OnInit { protected cipherDomain: Cipher; - private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id)); - constructor( protected collectionService: CollectionService, protected platformUtilsService: PlatformUtilsService, @@ -48,7 +47,7 @@ export class CollectionsComponent implements OnInit { } async load() { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); this.cipherDomain = await this.loadCipher(activeUserId); this.collectionIds = this.loadCipherCollections(); this.cipher = await this.cipherDomain.decrypt( @@ -96,7 +95,7 @@ export class CollectionsComponent implements OnInit { } this.cipherDomain.collectionIds = selectedCollectionIds; try { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); this.formPromise = this.saveCollections(activeUserId); await this.formPromise; this.onSavedCollections.emit(); diff --git a/libs/angular/src/components/share.component.ts b/libs/angular/src/components/share.component.ts index 62acd0a2ecc..31aabe17d53 100644 --- a/libs/angular/src/components/share.component.ts +++ b/libs/angular/src/components/share.component.ts @@ -8,6 +8,7 @@ import { OrganizationService } from "@bitwarden/common/admin-console/abstraction import { OrganizationUserStatusType } from "@bitwarden/common/admin-console/enums"; import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; @@ -29,8 +30,6 @@ export class ShareComponent implements OnInit, OnDestroy { protected writeableCollections: Checkable[] = []; - private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id)); - private _destroy = new Subject(); constructor( @@ -75,7 +74,7 @@ export class ShareComponent implements OnInit, OnDestroy { } }); - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const cipherDomain = await this.cipherService.get(this.cipherId, activeUserId); this.cipher = await cipherDomain.decrypt( await this.cipherService.getKeyForCipherKeyDecryption(cipherDomain, activeUserId), @@ -104,7 +103,7 @@ export class ShareComponent implements OnInit, OnDestroy { return; } - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const cipherDomain = await this.cipherService.get(this.cipherId, activeUserId); const cipherView = await cipherDomain.decrypt( await this.cipherService.getKeyForCipherKeyDecryption(cipherDomain, activeUserId), diff --git a/libs/angular/src/vault/components/add-edit.component.ts b/libs/angular/src/vault/components/add-edit.component.ts index bae6a63b125..37c30e735f1 100644 --- a/libs/angular/src/vault/components/add-edit.component.ts +++ b/libs/angular/src/vault/components/add-edit.component.ts @@ -12,6 +12,7 @@ import { PolicyService } from "@bitwarden/common/admin-console/abstractions/poli import { OrganizationUserStatusType, PolicyType } from "@bitwarden/common/admin-console/enums"; import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { normalizeExpiryYearFormat } from "@bitwarden/common/autofill/utils"; import { EventType } from "@bitwarden/common/enums"; import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum"; @@ -101,8 +102,6 @@ export class AddEditComponent implements OnInit, OnDestroy { private personalOwnershipPolicyAppliesToActiveUser: boolean; private previousCipherId: string; - protected activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id)); - get fido2CredentialCreationDateValue(): string { const dateCreated = this.i18nService.t("dateCreated"); const creationDate = this.datePipe.transform( @@ -263,7 +262,7 @@ export class AddEditComponent implements OnInit, OnDestroy { this.title = this.i18nService.t("addItem"); } - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const loadedAddEditCipherInfo = await this.loadAddEditCipherInfo(activeUserId); @@ -421,7 +420,7 @@ export class AddEditComponent implements OnInit, OnDestroy { this.cipher.id = null; } - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const cipher = await this.encryptCipher(activeUserId); try { this.formPromise = this.saveCipher(cipher); @@ -515,7 +514,7 @@ export class AddEditComponent implements OnInit, OnDestroy { } try { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); this.deletePromise = this.deleteCipher(activeUserId); await this.deletePromise; this.toastService.showToast({ @@ -542,7 +541,7 @@ export class AddEditComponent implements OnInit, OnDestroy { } try { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); this.restorePromise = this.restoreCipher(activeUserId); await this.restorePromise; this.toastService.showToast({ diff --git a/libs/angular/src/vault/components/attachments.component.ts b/libs/angular/src/vault/components/attachments.component.ts index 3765184811d..6b4b4190d4e 100644 --- a/libs/angular/src/vault/components/attachments.component.ts +++ b/libs/angular/src/vault/components/attachments.component.ts @@ -1,10 +1,11 @@ // FIXME: Update this file to be type safe and remove this and next line // @ts-strict-ignore import { Directive, EventEmitter, Input, OnInit, Output } from "@angular/core"; -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { ErrorResponse } from "@bitwarden/common/models/response/error.response"; import { EncryptService } from "@bitwarden/common/platform/abstractions/encrypt.service"; @@ -39,8 +40,6 @@ export class AttachmentsComponent implements OnInit { emergencyAccessId?: string = null; protected componentName = ""; - protected activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id)); - constructor( protected cipherService: CipherService, protected i18nService: I18nService, @@ -85,7 +84,7 @@ export class AttachmentsComponent implements OnInit { } try { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); this.formPromise = this.saveCipherAttachment(files[0], activeUserId); this.cipherDomain = await this.formPromise; this.cipher = await this.cipherDomain.decrypt( @@ -124,7 +123,7 @@ export class AttachmentsComponent implements OnInit { } try { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); this.deletePromises[attachment.id] = this.deleteCipherAttachment(attachment.id, activeUserId); await this.deletePromises[attachment.id]; this.toastService.showToast({ @@ -219,7 +218,7 @@ export class AttachmentsComponent implements OnInit { } protected async init() { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); this.cipherDomain = await this.loadCipher(activeUserId); this.cipher = await this.cipherDomain.decrypt( await this.cipherService.getKeyForCipherKeyDecryption(this.cipherDomain, activeUserId), @@ -275,7 +274,7 @@ export class AttachmentsComponent implements OnInit { ? attachment.key : await this.keyService.getOrgKey(this.cipher.organizationId); const decBuf = await this.encryptService.decryptToBytes(encBuf, key); - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); this.cipherDomain = await this.cipherService.saveAttachmentRawWithServer( this.cipherDomain, attachment.fileName, diff --git a/libs/angular/src/vault/components/password-history.component.ts b/libs/angular/src/vault/components/password-history.component.ts index b1a45e06bcd..4d17a5f7380 100644 --- a/libs/angular/src/vault/components/password-history.component.ts +++ b/libs/angular/src/vault/components/password-history.component.ts @@ -1,9 +1,10 @@ // FIXME: Update this file to be type safe and remove this and next line // @ts-strict-ignore import { Directive, OnInit } from "@angular/core"; -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; @@ -39,9 +40,7 @@ export class PasswordHistoryComponent implements OnInit { } protected async init() { - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const cipher = await this.cipherService.get(this.cipherId, activeUserId); const decCipher = await cipher.decrypt( await this.cipherService.getKeyForCipherKeyDecryption(cipher, activeUserId), diff --git a/libs/angular/src/vault/components/vault-items.component.ts b/libs/angular/src/vault/components/vault-items.component.ts index 688393c0e23..6746b5c77bf 100644 --- a/libs/angular/src/vault/components/vault-items.component.ts +++ b/libs/angular/src/vault/components/vault-items.component.ts @@ -1,11 +1,12 @@ // FIXME: Update this file to be type safe and remove this and next line // @ts-strict-ignore import { Directive, EventEmitter, Input, OnDestroy, OnInit, Output } from "@angular/core"; -import { BehaviorSubject, firstValueFrom, from, Subject, map, switchMap, takeUntil } from "rxjs"; +import { BehaviorSubject, firstValueFrom, from, Subject, switchMap, takeUntil } from "rxjs"; import { SearchService } from "@bitwarden/common/abstractions/search.service"; import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view"; @@ -119,9 +120,7 @@ export class VaultItemsComponent implements OnInit, OnDestroy { protected deletedFilter: (cipher: CipherView) => boolean = (c) => c.isDeleted === this.deleted; protected async doSearch(indexedCiphers?: CipherView[]) { - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); indexedCiphers = indexedCiphers ?? (await this.cipherService.getAllDecrypted(activeUserId)); const failedCiphers = await firstValueFrom( diff --git a/libs/angular/src/vault/components/view.component.ts b/libs/angular/src/vault/components/view.component.ts index 17c470f99be..e447979543a 100644 --- a/libs/angular/src/vault/components/view.component.ts +++ b/libs/angular/src/vault/components/view.component.ts @@ -11,13 +11,14 @@ import { OnInit, Output, } from "@angular/core"; -import { firstValueFrom, map, Observable } from "rxjs"; +import { firstValueFrom, Observable } from "rxjs"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { AuditService } from "@bitwarden/common/abstractions/audit.service"; import { EventCollectionService } from "@bitwarden/common/abstractions/event/event-collection.service"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; import { TokenService } from "@bitwarden/common/auth/abstractions/token.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service"; import { EventType } from "@bitwarden/common/enums"; import { ErrorResponse } from "@bitwarden/common/models/response/error.response"; @@ -80,8 +81,6 @@ export class ViewComponent implements OnDestroy, OnInit { private previousCipherId: string; private passwordReprompted = false; - private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id)); - get fido2CredentialCreationDateValue(): string { const dateCreated = this.i18nService.t("dateCreated"); const creationDate = this.datePipe.transform( @@ -144,7 +143,7 @@ export class ViewComponent implements OnDestroy, OnInit { async load() { this.cleanUp(); - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const cipher = await this.cipherService.get(this.cipherId, activeUserId); this.cipher = await cipher.decrypt( await this.cipherService.getKeyForCipherKeyDecryption(cipher, activeUserId), @@ -246,7 +245,7 @@ export class ViewComponent implements OnDestroy, OnInit { } try { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); await this.deleteCipher(activeUserId); this.toastService.showToast({ variant: "success", @@ -269,7 +268,7 @@ export class ViewComponent implements OnDestroy, OnInit { } try { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); await this.restoreCipher(activeUserId); this.toastService.showToast({ variant: "success", @@ -378,7 +377,7 @@ export class ViewComponent implements OnDestroy, OnInit { } if (cipherId) { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); await this.cipherService.updateLastLaunchedDate(cipherId, activeUserId); } diff --git a/libs/angular/src/vault/vault-filter/services/vault-filter.service.ts b/libs/angular/src/vault/vault-filter/services/vault-filter.service.ts index 1009c131810..93967fcf59c 100644 --- a/libs/angular/src/vault/vault-filter/services/vault-filter.service.ts +++ b/libs/angular/src/vault/vault-filter/services/vault-filter.service.ts @@ -33,8 +33,6 @@ export class VaultFilterService implements DeprecatedVaultFilterServiceAbstracti private readonly collapsedGroupings$: Observable> = this.collapsedGroupingsState.state$.pipe(map((c) => new Set(c))); - private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id)); - constructor( protected organizationService: OrganizationService, protected folderService: FolderService, @@ -73,7 +71,7 @@ export class VaultFilterService implements DeprecatedVaultFilterServiceAbstracti if (organizationId == null || organizationId == "MyVault") { folders = storedFolders; } else { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); // Otherwise, show only folders that have ciphers from the selected org and the "no folder" folder const ciphers = await this.cipherService.getAllDecrypted(activeUserId); const orgCiphers = ciphers.filter((c) => c.organizationId == organizationId); @@ -89,7 +87,7 @@ export class VaultFilterService implements DeprecatedVaultFilterServiceAbstracti }); }; - return this.activeUserId$.pipe( + return getUserId(this.accountService.activeAccount$).pipe( switchMap((userId) => this.folderService.folderViews$(userId)), mergeMap((folders) => from(transformation(folders))), ); @@ -135,7 +133,7 @@ export class VaultFilterService implements DeprecatedVaultFilterServiceAbstracti } async getFolderNested(id: string): Promise> { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const folders = await this.getAllFoldersNested( await firstValueFrom(this.folderService.folderViews$(activeUserId)), ); diff --git a/libs/common/src/platform/services/fido2/fido2-authenticator.service.ts b/libs/common/src/platform/services/fido2/fido2-authenticator.service.ts index b1910828210..ecc7f36c8fe 100644 --- a/libs/common/src/platform/services/fido2/fido2-authenticator.service.ts +++ b/libs/common/src/platform/services/fido2/fido2-authenticator.service.ts @@ -1,8 +1,9 @@ // FIXME: Update this file to be type safe and remove this and next line // @ts-strict-ignore -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { AccountService } from "../../../auth/abstractions/account.service"; +import { getUserId } from "../../../auth/services/account.service"; import { CipherService } from "../../../vault/abstractions/cipher.service"; import { SyncService } from "../../../vault/abstractions/sync/sync.service.abstraction"; import { CipherRepromptType } from "../../../vault/enums/cipher-reprompt-type"; @@ -46,8 +47,6 @@ const KeyUsages: KeyUsage[] = ["sign"]; export class Fido2AuthenticatorService implements Fido2AuthenticatorServiceAbstraction { - private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id)); - constructor( private cipherService: CipherService, private userInterface: Fido2UserInterfaceService, @@ -147,7 +146,7 @@ export class Fido2AuthenticatorService try { keyPair = await createKeyPair(); pubKeyDer = await crypto.subtle.exportKey("spki", keyPair.publicKey); - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const encrypted = await this.cipherService.get(cipherId, activeUserId); cipher = await encrypted.decrypt( @@ -308,7 +307,7 @@ export class Fido2AuthenticatorService }; if (selectedFido2Credential.counter > 0) { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const encrypted = await this.cipherService.encrypt(selectedCipher, activeUserId); await this.cipherService.updateWithServer(encrypted); await this.cipherService.clearCache(activeUserId); @@ -398,7 +397,7 @@ export class Fido2AuthenticatorService return []; } - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const ciphers = await this.cipherService.getAllDecrypted(activeUserId); return ciphers .filter( @@ -420,7 +419,7 @@ export class Fido2AuthenticatorService return []; } - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const ciphers = await this.cipherService.getAllDecrypted(activeUserId); return ciphers.filter( (cipher) => @@ -438,7 +437,7 @@ export class Fido2AuthenticatorService } private async findCredentialsByRp(rpId: string): Promise { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const ciphers = await this.cipherService.getAllDecrypted(activeUserId); return ciphers.filter( (cipher) => diff --git a/libs/common/src/services/event/event-collection.service.ts b/libs/common/src/services/event/event-collection.service.ts index e52d06973d1..b37ec0de271 100644 --- a/libs/common/src/services/event/event-collection.service.ts +++ b/libs/common/src/services/event/event-collection.service.ts @@ -6,7 +6,6 @@ import { OrganizationService } from "@bitwarden/common/admin-console/abstraction import { Organization } from "@bitwarden/common/admin-console/models/domain/organization"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; import { getUserId } from "@bitwarden/common/auth/services/account.service"; - import { UserId } from "@bitwarden/common/types/guid"; import { EventCollectionService as EventCollectionServiceAbstraction } from "../../abstractions/event/event-collection.service"; diff --git a/libs/tools/export/vault-export/vault-export-core/src/services/individual-vault-export.service.ts b/libs/tools/export/vault-export/vault-export-core/src/services/individual-vault-export.service.ts index 31c8aaa3bc7..f6d12784b01 100644 --- a/libs/tools/export/vault-export/vault-export-core/src/services/individual-vault-export.service.ts +++ b/libs/tools/export/vault-export/vault-export-core/src/services/individual-vault-export.service.ts @@ -1,10 +1,11 @@ // FIXME: Update this file to be type safe and remove this and next line // @ts-strict-ignore import * as papa from "papaparse"; -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { PinServiceAbstraction } from "@bitwarden/auth/common"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { CipherWithIdExport, FolderWithIdExport } from "@bitwarden/common/models/export"; import { CryptoFunctionService } from "@bitwarden/common/platform/abstractions/crypto-function.service"; import { EncryptService } from "@bitwarden/common/platform/abstractions/encrypt.service"; @@ -32,8 +33,6 @@ export class IndividualVaultExportService extends BaseVaultExportService implements IndividualVaultExportServiceAbstraction { - private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id)); - constructor( private folderService: FolderService, private cipherService: CipherService, @@ -63,7 +62,7 @@ export class IndividualVaultExportService let decFolders: FolderView[] = []; let decCiphers: CipherView[] = []; const promises = []; - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); promises.push( firstValueFrom(this.folderService.folderViews$(activeUserId)).then((folders) => { @@ -90,7 +89,7 @@ export class IndividualVaultExportService let folders: Folder[] = []; let ciphers: Cipher[] = []; const promises = []; - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); promises.push( firstValueFrom(this.folderService.folders$(activeUserId)).then((f) => { @@ -107,7 +106,7 @@ export class IndividualVaultExportService await Promise.all(promises); const userKey = await this.keyService.getUserKeyWithLegacySupport( - await firstValueFrom(this.activeUserId$), + await firstValueFrom(getUserId(this.accountService.activeAccount$)), ); const encKeyValidation = await this.encryptService.encrypt(Utils.newGuid(), userKey); diff --git a/libs/tools/export/vault-export/vault-export-core/src/services/org-vault-export.service.ts b/libs/tools/export/vault-export/vault-export-core/src/services/org-vault-export.service.ts index 6807d7d458e..2f1d5e33e7e 100644 --- a/libs/tools/export/vault-export/vault-export-core/src/services/org-vault-export.service.ts +++ b/libs/tools/export/vault-export/vault-export-core/src/services/org-vault-export.service.ts @@ -1,7 +1,7 @@ // FIXME: Update this file to be type safe and remove this and next line // @ts-strict-ignore import * as papa from "papaparse"; -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { CollectionService, @@ -13,6 +13,7 @@ import { import { PinServiceAbstraction } from "@bitwarden/auth/common"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { CipherWithIdExport, CollectionWithIdExport } from "@bitwarden/common/models/export"; import { CryptoFunctionService } from "@bitwarden/common/platform/abstractions/crypto-function.service"; import { EncryptService } from "@bitwarden/common/platform/abstractions/encrypt.service"; @@ -39,8 +40,6 @@ export class OrganizationVaultExportService extends BaseVaultExportService implements OrganizationVaultExportServiceAbstraction { - private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id)); - constructor( private cipherService: CipherService, private apiService: ApiService, @@ -96,7 +95,7 @@ export class OrganizationVaultExportService const decCollections: CollectionView[] = []; const decCiphers: CipherView[] = []; const promises = []; - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); promises.push( this.apiService.getOrganizationExport(organizationId).then((exportData) => { @@ -184,7 +183,7 @@ export class OrganizationVaultExportService let allDecCiphers: CipherView[] = []; let decCollections: CollectionView[] = []; const promises = []; - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); promises.push( this.collectionService.getAllDecrypted().then(async (collections) => { @@ -217,7 +216,7 @@ export class OrganizationVaultExportService let allCiphers: Cipher[] = []; let encCollections: Collection[] = []; const promises = []; - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); promises.push( this.collectionService.getAll().then((collections) => { diff --git a/libs/vault/src/cipher-form/components/attachments/cipher-attachments.component.ts b/libs/vault/src/cipher-form/components/attachments/cipher-attachments.component.ts index ba43a187a3f..e49a05aba97 100644 --- a/libs/vault/src/cipher-form/components/attachments/cipher-attachments.component.ts +++ b/libs/vault/src/cipher-form/components/attachments/cipher-attachments.component.ts @@ -21,10 +21,11 @@ import { ReactiveFormsModule, Validators, } from "@angular/forms"; -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { JslibModule } from "@bitwarden/angular/jslib.module"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { CipherId, UserId } from "@bitwarden/common/types/guid"; @@ -118,9 +119,7 @@ export class CipherAttachmentsComponent implements OnInit, AfterViewInit { } async ngOnInit(): Promise { - this.activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + this.activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); this.cipherDomain = await this.cipherService.get(this.cipherId, this.activeUserId); this.cipher = await this.cipherDomain.decrypt( await this.cipherService.getKeyForCipherKeyDecryption(this.cipherDomain, this.activeUserId), diff --git a/libs/vault/src/cipher-form/components/attachments/delete-attachment/delete-attachment.component.ts b/libs/vault/src/cipher-form/components/attachments/delete-attachment/delete-attachment.component.ts index f7c75d96c08..a7406e1c5cb 100644 --- a/libs/vault/src/cipher-form/components/attachments/delete-attachment/delete-attachment.component.ts +++ b/libs/vault/src/cipher-form/components/attachments/delete-attachment/delete-attachment.component.ts @@ -1,9 +1,10 @@ import { CommonModule } from "@angular/common"; import { Component, EventEmitter, Input, Output } from "@angular/core"; -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { JslibModule } from "@bitwarden/angular/jslib.module"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; @@ -53,9 +54,7 @@ export class DeleteAttachmentComponent { } try { - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); if (activeUserId == null) { throw new Error("An active user is expected while deleting an attachment."); diff --git a/libs/vault/src/cipher-form/services/default-cipher-form-config.service.ts b/libs/vault/src/cipher-form/services/default-cipher-form-config.service.ts index 69b38f54fb8..cdd714e1250 100644 --- a/libs/vault/src/cipher-form/services/default-cipher-form-config.service.ts +++ b/libs/vault/src/cipher-form/services/default-cipher-form-config.service.ts @@ -8,6 +8,7 @@ import { OrganizationService } from "@bitwarden/common/admin-console/abstraction import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction"; import { OrganizationUserStatusType, PolicyType } from "@bitwarden/common/admin-console/enums"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { CipherId, UserId } from "@bitwarden/common/types/guid"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction"; @@ -34,14 +35,12 @@ export class DefaultCipherFormConfigService implements CipherFormConfigService { private collectionService: CollectionService = inject(CollectionService); private accountService = inject(AccountService); - private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id)); - async buildConfig( mode: CipherFormMode, cipherId?: CipherId, cipherType?: CipherType, ): Promise { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const [organizations, collections, allowPersonalOwnership, folders, cipher] = await firstValueFrom( diff --git a/libs/vault/src/cipher-form/services/default-cipher-form.service.ts b/libs/vault/src/cipher-form/services/default-cipher-form.service.ts index 719a3773c83..0145fd970df 100644 --- a/libs/vault/src/cipher-form/services/default-cipher-form.service.ts +++ b/libs/vault/src/cipher-form/services/default-cipher-form.service.ts @@ -1,10 +1,11 @@ // FIXME: Update this file to be type safe and remove this and next line // @ts-strict-ignore import { inject, Injectable } from "@angular/core"; -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { ApiService } from "@bitwarden/common/abstractions/api.service"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; import { Cipher } from "@bitwarden/common/vault/models/domain/cipher"; import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view"; @@ -22,10 +23,8 @@ export class DefaultCipherFormService implements CipherFormService { private accountService: AccountService = inject(AccountService); private apiService: ApiService = inject(ApiService); - private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id)); - async decryptCipher(cipher: Cipher): Promise { - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); return await cipher.decrypt( await this.cipherService.getKeyForCipherKeyDecryption(cipher, activeUserId), ); @@ -33,7 +32,7 @@ export class DefaultCipherFormService implements CipherFormService { async saveCipher(cipher: CipherView, config: CipherFormConfig): Promise { // Passing the original cipher is important here as it is responsible for appending to password history - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); const encryptedCipher = await this.cipherService.encrypt( cipher, activeUserId, diff --git a/libs/vault/src/cipher-view/autofill-options/autofill-options-view.component.ts b/libs/vault/src/cipher-view/autofill-options/autofill-options-view.component.ts index 178dcbcd590..3aca8682032 100644 --- a/libs/vault/src/cipher-view/autofill-options/autofill-options-view.component.ts +++ b/libs/vault/src/cipher-view/autofill-options/autofill-options-view.component.ts @@ -2,10 +2,11 @@ // @ts-strict-ignore import { CommonModule } from "@angular/common"; import { Component, Input } from "@angular/core"; -import { firstValueFrom, map } from "rxjs"; +import { firstValueFrom } from "rxjs"; import { JslibModule } from "@bitwarden/angular/jslib.module"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; +import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service"; import { LoginUriView } from "@bitwarden/common/vault/models/view/login-uri.view"; @@ -44,9 +45,7 @@ export class AutofillOptionsViewComponent { ) {} async openWebsite(selectedUri: string) { - const activeUserId = await firstValueFrom( - this.accountService.activeAccount$.pipe(map((a) => a?.id)), - ); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); await this.cipherService.updateLastLaunchedDate(this.cipherId, activeUserId); this.platformUtilsService.launchUri(selectedUri); } diff --git a/libs/vault/src/components/assign-collections.component.ts b/libs/vault/src/components/assign-collections.component.ts index 22e0ac7017e..96eb44e317b 100644 --- a/libs/vault/src/components/assign-collections.component.ts +++ b/libs/vault/src/components/assign-collections.component.ts @@ -179,7 +179,6 @@ export class AssignCollectionsComponent implements OnInit, OnDestroy, AfterViewI private get selectedOrgId(): OrganizationId { return this.formGroup.getRawValue().selectedOrg || this.params.organizationId; } - private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id)); private destroy$ = new Subject(); constructor( @@ -249,7 +248,7 @@ export class AssignCollectionsComponent implements OnInit, OnDestroy, AfterViewI .filter((i) => i.organizationId) .map((i) => i.id as CipherId); - const activeUserId = await firstValueFrom(this.activeUserId$); + const activeUserId = await firstValueFrom(getUserId(this.accountService.activeAccount$)); // Move personal items to the organization if (this.personalItemsCount > 0) {