diff --git a/apps/cli/src/tools/send/commands/receive.command.ts b/apps/cli/src/tools/send/commands/receive.command.ts index 3a8f38dc3b1..a412f7c1667 100644 --- a/apps/cli/src/tools/send/commands/receive.command.ts +++ b/apps/cli/src/tools/send/commands/receive.command.ts @@ -5,8 +5,6 @@ import * as inquirer from "inquirer"; 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 { CryptoFunctionService } from "@bitwarden/common/key-management/crypto/abstractions/crypto-function.service"; import { EncryptService } from "@bitwarden/common/key-management/crypto/abstractions/encrypt.service"; import { ErrorResponse } from "@bitwarden/common/models/response/error.response"; @@ -39,7 +37,6 @@ export class SendReceiveCommand extends DownloadCommand { private platformUtilsService: PlatformUtilsService, private environmentService: EnvironmentService, private sendApiService: SendApiService, - private accountService: AccountService, apiService: ApiService, ) { super(encryptService, apiService); @@ -155,8 +152,6 @@ export class SendReceiveCommand extends DownloadCommand { key: Uint8Array, ): Promise { try { - const activeUserId = await firstValueFrom(this.accountService.activeAccount$.pipe(getUserId)); - const sendResponse = await this.sendApiService.postSendAccess( id, this.sendAccessRequest, @@ -165,7 +160,7 @@ export class SendReceiveCommand extends DownloadCommand { const sendAccess = new SendAccess(sendResponse); this.decKey = await this.keyService.makeSendKey(key); - return await sendAccess.decrypt(activeUserId, this.decKey); + return await sendAccess.decrypt(this.decKey); } catch (e) { if (e instanceof ErrorResponse) { if (e.statusCode === 401) { diff --git a/apps/cli/src/tools/send/send.program.ts b/apps/cli/src/tools/send/send.program.ts index 9cbb5d9526b..6c643e04cd0 100644 --- a/apps/cli/src/tools/send/send.program.ts +++ b/apps/cli/src/tools/send/send.program.ts @@ -123,7 +123,6 @@ export class SendProgram extends BaseProgram { this.serviceContainer.platformUtilsService, this.serviceContainer.environmentService, this.serviceContainer.sendApiService, - this.serviceContainer.accountService, this.serviceContainer.apiService, ); const response = await cmd.run(url, options); diff --git a/apps/web/src/app/tools/send/send-access/access.component.ts b/apps/web/src/app/tools/send/send-access/access.component.ts index 9b12587d099..85168e96f6d 100644 --- a/apps/web/src/app/tools/send/send-access/access.component.ts +++ b/apps/web/src/app/tools/send/send-access/access.component.ts @@ -3,10 +3,8 @@ import { Component, OnInit } from "@angular/core"; import { FormBuilder } from "@angular/forms"; import { ActivatedRoute } from "@angular/router"; -import { firstValueFrom } from "rxjs"; import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; -import { getUserId } from "@bitwarden/common/auth/services/account.service"; import { CryptoFunctionService } from "@bitwarden/common/key-management/crypto/abstractions/crypto-function.service"; import { ErrorResponse } from "@bitwarden/common/models/response/error.response"; import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; @@ -122,8 +120,7 @@ export class AccessComponent implements OnInit { this.passwordRequired = false; const sendAccess = new SendAccess(sendResponse); this.decKey = await this.keyService.makeSendKey(keyArray); - const activeUserId = await firstValueFrom(this.accountService.activeAccount$.pipe(getUserId)); - this.send = await sendAccess.decrypt(activeUserId, this.decKey); + this.send = await sendAccess.decrypt(this.decKey); } catch (e) { if (e instanceof ErrorResponse) { if (e.statusCode === 401) { diff --git a/libs/common/src/platform/models/domain/domain-base.ts b/libs/common/src/platform/models/domain/domain-base.ts index d9c4dccc5e3..9d251e9d79d 100644 --- a/libs/common/src/platform/models/domain/domain-base.ts +++ b/libs/common/src/platform/models/domain/domain-base.ts @@ -87,7 +87,7 @@ export default class Domain { domain: DomainEncryptableKeys, viewModel: ViewEncryptableKeys, props: EncryptableKeys[], - userId: UserId, + userId: UserId | null, orgId: string | null, key: SymmetricCryptoKey | null = null, _objectContext: string = "No Domain Context", @@ -100,8 +100,10 @@ export default class Domain { .orgKeys$(userId) .pipe(map((orgKeys) => orgKeys![orgId as OrganizationId] ?? null)), ); - } else { + } else if (userId != null) { key = await firstValueFrom(keyService.userKey$(userId)); + } else { + throw new Error("No key or context provided for decryption"); } } diff --git a/libs/common/src/tools/send/models/domain/send-access.ts b/libs/common/src/tools/send/models/domain/send-access.ts index 87efa4eb84d..9a931918d23 100644 --- a/libs/common/src/tools/send/models/domain/send-access.ts +++ b/libs/common/src/tools/send/models/domain/send-access.ts @@ -1,6 +1,5 @@ // FIXME: Update this file to be type safe and remove this and next line // @ts-strict-ignore -import { UserId } from "@bitwarden/user-core"; import { EncString } from "../../../../key-management/crypto/models/enc-string"; import Domain from "../../../../platform/models/domain/domain-base"; @@ -53,17 +52,17 @@ export class SendAccess extends Domain { } } - async decrypt(userId: UserId, key: SymmetricCryptoKey): Promise { + async decrypt(key: SymmetricCryptoKey): Promise { const model = new SendAccessView(this); - await this.decryptObj(this, model, ["name"], userId, null, key); + await this.decryptObj(this, model, ["name"], null, null, key); switch (this.type) { case SendType.File: - model.file = await this.file.decrypt(userId, key); + model.file = await this.file.decrypt(key); break; case SendType.Text: - model.text = await this.text.decrypt(userId, key); + model.text = await this.text.decrypt(key); break; default: break; diff --git a/libs/common/src/tools/send/models/domain/send-file.ts b/libs/common/src/tools/send/models/domain/send-file.ts index ba07a130fbd..b0d1d12203b 100644 --- a/libs/common/src/tools/send/models/domain/send-file.ts +++ b/libs/common/src/tools/send/models/domain/send-file.ts @@ -2,8 +2,6 @@ // @ts-strict-ignore import { Jsonify } from "type-fest"; -import { UserId } from "@bitwarden/user-core"; - import { EncString } from "../../../../key-management/crypto/models/enc-string"; import Domain from "../../../../platform/models/domain/domain-base"; import { SymmetricCryptoKey } from "../../../../platform/models/domain/symmetric-crypto-key"; @@ -35,12 +33,12 @@ export class SendFile extends Domain { ); } - async decrypt(userId: UserId, key: SymmetricCryptoKey): Promise { + async decrypt(key: SymmetricCryptoKey): Promise { return await this.decryptObj( this, new SendFileView(this), ["fileName"], - userId, + null, null, key, ); diff --git a/libs/common/src/tools/send/models/domain/send-text.ts b/libs/common/src/tools/send/models/domain/send-text.ts index 882def46165..93a3e3b803b 100644 --- a/libs/common/src/tools/send/models/domain/send-text.ts +++ b/libs/common/src/tools/send/models/domain/send-text.ts @@ -2,8 +2,6 @@ // @ts-strict-ignore import { Jsonify } from "type-fest"; -import { UserId } from "@bitwarden/user-core"; - import { EncString } from "../../../../key-management/crypto/models/enc-string"; import Domain from "../../../../platform/models/domain/domain-base"; import { SymmetricCryptoKey } from "../../../../platform/models/domain/symmetric-crypto-key"; @@ -31,12 +29,12 @@ export class SendText extends Domain { ); } - decrypt(userId: UserId, key: SymmetricCryptoKey): Promise { + decrypt(key: SymmetricCryptoKey): Promise { return this.decryptObj( this, new SendTextView(this), ["text"], - userId, + null, null, key, ); diff --git a/libs/importer/src/importers/bitwarden/bitwarden-json-importer.ts b/libs/importer/src/importers/bitwarden/bitwarden-json-importer.ts index 96b452a3122..e610de43dbe 100644 --- a/libs/importer/src/importers/bitwarden/bitwarden-json-importer.ts +++ b/libs/importer/src/importers/bitwarden/bitwarden-json-importer.ts @@ -53,10 +53,11 @@ export class BitwardenJsonImporter extends BaseImporter implements Importer { return this.result; } + const userId = await firstValueFrom(this.accountService.activeAccount$.pipe(getUserId)); if (results.encrypted) { - await this.parseEncrypted(results as any); + await this.parseEncrypted(results as any, userId); } else { - await this.parseDecrypted(results as any); + await this.parseDecrypted(results as any, userId); } return this.result; @@ -64,14 +65,13 @@ export class BitwardenJsonImporter extends BaseImporter implements Importer { private async parseEncrypted( results: BitwardenEncryptedIndividualJsonExport | BitwardenEncryptedOrgJsonExport, + userId: UserId, ) { - const account = await firstValueFrom(this.accountService.activeAccount$); - if (results.encKeyValidation_DO_NOT_EDIT != null) { - const orgKeys = await firstValueFrom(this.keyService.orgKeys$(account.id)); + const orgKeys = await firstValueFrom(this.keyService.orgKeys$(userId)); let keyForDecryption: SymmetricCryptoKey = orgKeys?.[this.organizationId]; if (keyForDecryption == null) { - keyForDecryption = await firstValueFrom(this.keyService.userKey$(account.id)); + keyForDecryption = await firstValueFrom(this.keyService.userKey$(userId)); } const encKeyValidation = new EncString(results.encKeyValidation_DO_NOT_EDIT); try { @@ -85,7 +85,7 @@ export class BitwardenJsonImporter extends BaseImporter implements Importer { const groupingsMap = this.organization ? await this.parseCollections(results as BitwardenEncryptedOrgJsonExport) - : await this.parseFolders(results as BitwardenEncryptedIndividualJsonExport); + : await this.parseFolders(results as BitwardenEncryptedIndividualJsonExport, userId); for (const c of results.items) { const cipher = CipherWithIdExport.toDomain(c); @@ -115,7 +115,7 @@ export class BitwardenJsonImporter extends BaseImporter implements Importer { }); } - const view = await this.cipherService.decrypt(cipher, account.id); + const view = await this.cipherService.decrypt(cipher, userId); this.cleanupCipher(view); this.result.ciphers.push(view); } @@ -125,10 +125,11 @@ export class BitwardenJsonImporter extends BaseImporter implements Importer { private async parseDecrypted( results: BitwardenUnEncryptedIndividualJsonExport | BitwardenUnEncryptedOrgJsonExport, + userId: UserId, ) { const groupingsMap = this.organization ? await this.parseCollections(results as BitwardenUnEncryptedOrgJsonExport) - : await this.parseFolders(results as BitwardenUnEncryptedIndividualJsonExport); + : await this.parseFolders(results as BitwardenUnEncryptedIndividualJsonExport, userId); results.items.forEach((c) => { const cipher = CipherWithIdExport.toView(c); @@ -167,9 +168,8 @@ export class BitwardenJsonImporter extends BaseImporter implements Importer { private async parseFolders( data: BitwardenUnEncryptedIndividualJsonExport | BitwardenEncryptedIndividualJsonExport, + userId: UserId, ): Promise> | null { - const userId: UserId = await firstValueFrom(this.accountService.activeAccount$.pipe(getUserId)); - if (data.folders == null) { return null; }