diff --git a/libs/common/src/platform/models/domain/domain-base.ts b/libs/common/src/platform/models/domain/domain-base.ts index 192034254b9..5aa79946653 100644 --- a/libs/common/src/platform/models/domain/domain-base.ts +++ b/libs/common/src/platform/models/domain/domain-base.ts @@ -65,7 +65,6 @@ export default class Domain { key: SymmetricCryptoKey = null, objectContext: string = "No Domain Context", ): Promise { - const promises = []; const self: any = this; for (const prop in map) { @@ -74,27 +73,15 @@ export default class Domain { continue; } - (function (theProp) { - const p = Promise.resolve() - .then(() => { - const mapProp = map[theProp] || theProp; - if (self[mapProp]) { - return self[mapProp].decrypt( - orgId, - key, - `Property: ${prop}; ObjectContext: ${objectContext}`, - ); - } - return null; - }) - .then((val: any) => { - (viewModel as any)[theProp] = val; - }); - promises.push(p); - })(prop); + const mapProp = map[prop] || prop; + if (self[mapProp]) { + (viewModel as any)[prop] = await self[mapProp].decrypt( + orgId, + key, + `Property: ${prop}; ObjectContext: ${objectContext}`, + ); + } } - - await Promise.all(promises); return viewModel; } @@ -121,22 +108,20 @@ export default class Domain { _: Constructor = this.constructor as Constructor, objectContext: string = "No Domain Context", ): Promise> { - const promises = []; + const decryptedObjects = []; for (const prop of encryptedProperties) { const value = (this as any)[prop] as EncString; - promises.push( - this.decryptProperty( - prop, - value, - key, - encryptService, - `Property: ${prop.toString()}; ObjectContext: ${objectContext}`, - ), + const decrypted = await this.decryptProperty( + prop, + value, + key, + encryptService, + `Property: ${prop.toString()}; ObjectContext: ${objectContext}`, ); + decryptedObjects.push(decrypted); } - const decryptedObjects = await Promise.all(promises); const decryptedObject = decryptedObjects.reduce( (acc, obj) => { return { ...acc, ...obj }; diff --git a/libs/common/src/vault/models/domain/cipher.ts b/libs/common/src/vault/models/domain/cipher.ts index d82f4585e65..21538b87788 100644 --- a/libs/common/src/vault/models/domain/cipher.ts +++ b/libs/common/src/vault/models/domain/cipher.ts @@ -12,7 +12,10 @@ import { CipherRepromptType } from "../../enums/cipher-reprompt-type"; import { CipherType } from "../../enums/cipher-type"; import { CipherData } from "../data/cipher.data"; import { LocalData } from "../data/local.data"; +import { AttachmentView } from "../view/attachment.view"; import { CipherView } from "../view/cipher.view"; +import { FieldView } from "../view/field.view"; +import { PasswordHistoryView } from "../view/password-history.view"; import { Attachment } from "./attachment"; import { Card } from "./card"; @@ -136,6 +139,7 @@ export class Cipher extends Domain implements Decryptable { if (this.key != null) { const encryptService = Utils.getContainerService().getEncryptService(); + const keyBytes = await encryptService.decryptToBytes( this.key, encKey, @@ -198,44 +202,28 @@ export class Cipher extends Domain implements Decryptable { } if (this.attachments != null && this.attachments.length > 0) { - const attachments: any[] = []; - await this.attachments.reduce((promise, attachment) => { - return promise - .then(() => { - return attachment.decrypt(this.organizationId, `Cipher Id: ${this.id}`, encKey); - }) - .then((decAttachment) => { - attachments.push(decAttachment); - }); - }, Promise.resolve()); + const attachments: AttachmentView[] = []; + for (const attachment of this.attachments) { + attachments.push( + await attachment.decrypt(this.organizationId, `Cipher Id: ${this.id}`, encKey), + ); + } model.attachments = attachments; } if (this.fields != null && this.fields.length > 0) { - const fields: any[] = []; - await this.fields.reduce((promise, field) => { - return promise - .then(() => { - return field.decrypt(this.organizationId, encKey); - }) - .then((decField) => { - fields.push(decField); - }); - }, Promise.resolve()); + const fields: FieldView[] = []; + for (const field of this.fields) { + fields.push(await field.decrypt(this.organizationId, encKey)); + } model.fields = fields; } if (this.passwordHistory != null && this.passwordHistory.length > 0) { - const passwordHistory: any[] = []; - await this.passwordHistory.reduce((promise, ph) => { - return promise - .then(() => { - return ph.decrypt(this.organizationId, encKey); - }) - .then((decPh) => { - passwordHistory.push(decPh); - }); - }, Promise.resolve()); + const passwordHistory: PasswordHistoryView[] = []; + for (const ph of this.passwordHistory) { + passwordHistory.push(await ph.decrypt(this.organizationId, encKey)); + } model.passwordHistory = passwordHistory; } diff --git a/libs/key-management/src/key.service.ts b/libs/key-management/src/key.service.ts index 1a4f9374d0e..a9d63eb17d4 100644 --- a/libs/key-management/src/key.service.ts +++ b/libs/key-management/src/key.service.ts @@ -368,20 +368,20 @@ export class DefaultKeyService implements KeyServiceAbstraction { await this.stateProvider.getUser(userId, USER_ENCRYPTED_ORGANIZATION_KEYS).update(() => { const encOrgKeyData: { [orgId: string]: EncryptedOrganizationKeyData } = {}; - orgs.forEach((org) => { + for (const org of orgs) { encOrgKeyData[org.id] = { type: "organization", key: org.key, }; - }); + } - providerOrgs.forEach((org) => { + for (const org of providerOrgs) { encOrgKeyData[org.id] = { type: "provider", providerId: org.providerId, key: org.key, }; - }); + } return encOrgKeyData; }); }