Skip to content

Commit

Permalink
Used getUserId utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
gbubemismith committed Jan 24, 2025
1 parent 11e4771 commit c643176
Show file tree
Hide file tree
Showing 52 changed files with 191 additions and 303 deletions.
19 changes: 9 additions & 10 deletions apps/browser/src/autofill/background/notification.background.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -600,15 +599,15 @@ 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);
}

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),
Expand Down Expand Up @@ -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));
}

Expand Down
13 changes: 4 additions & 9 deletions apps/browser/src/autofill/background/overlay.background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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));
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 3 additions & 9 deletions apps/browser/src/autofill/browser/cipher-context-menu-handler.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion apps/browser/src/autofill/popup/fido2/fido2.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 5 additions & 6 deletions apps/browser/src/autofill/services/autofill.service.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -67,8 +68,6 @@ export default class AutofillService implements AutofillServiceInterface {
private currentlyOpeningPasswordRepromptPopout = false;
private autofillScriptPortsSet = new Set<chrome.runtime.Port>();

private activeUserId$ = this.accountService.activeAccount$.pipe(map((a) => a?.id));

static searchFieldNamesSet = new Set(AutoFillConstants.SearchFieldNames);

constructor(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -533,7 +532,7 @@ export default class AutofillService implements AutofillServiceInterface {
): Promise<string | null> {
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);
Expand Down Expand Up @@ -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";
Expand Down
7 changes: 3 additions & 4 deletions apps/browser/src/platform/listeners/update-badge.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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);
Expand All @@ -217,7 +216,7 @@ export class ViewV2Component {

restore = async (): Promise<void> => {
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);
Expand Down
Loading

0 comments on commit c643176

Please sign in to comment.