diff --git a/src/Core/Models/Domain/Cipher.cs b/src/Core/Models/Domain/Cipher.cs index aea0c5529..c7546cd59 100644 --- a/src/Core/Models/Domain/Cipher.cs +++ b/src/Core/Models/Domain/Cipher.cs @@ -104,11 +104,14 @@ namespace Bit.Core.Models.Domain { model.Attachments = new List(); var tasks = new List(); + async Task decryptAndAddAttachmentAsync(Attachment attachment) + { + var decAttachment = await attachment.DecryptAsync(OrganizationId); + model.Attachments.Add(decAttachment); + } foreach(var attachment in Attachments) { - var t = attachment.DecryptAsync(OrganizationId) - .ContinueWith(async decAttachment => model.Attachments.Add(await decAttachment)); - tasks.Add(t); + tasks.Add(decryptAndAddAttachmentAsync(attachment)); } await Task.WhenAll(tasks); } @@ -116,11 +119,14 @@ namespace Bit.Core.Models.Domain { model.Fields = new List(); var tasks = new List(); + async Task decryptAndAddFieldAsync(Field field) + { + var decField = await field.DecryptAsync(OrganizationId); + model.Fields.Add(decField); + } foreach(var field in Fields) { - var t = field.DecryptAsync(OrganizationId) - .ContinueWith(async decField => model.Fields.Add(await decField)); - tasks.Add(t); + tasks.Add(decryptAndAddFieldAsync(field)); } await Task.WhenAll(tasks); } @@ -128,11 +134,14 @@ namespace Bit.Core.Models.Domain { model.PasswordHistory = new List(); var tasks = new List(); + async Task decryptAndAddHistoryAsync(PasswordHistory ph) + { + var decPh = await ph.DecryptAsync(OrganizationId); + model.PasswordHistory.Add(decPh); + } foreach(var ph in PasswordHistory) { - var t = ph.DecryptAsync(OrganizationId) - .ContinueWith(async decPh => model.PasswordHistory.Add(await decPh)); - tasks.Add(t); + tasks.Add(decryptAndAddHistoryAsync(ph)); } await Task.WhenAll(tasks); } diff --git a/src/Core/Models/Domain/Domain.cs b/src/Core/Models/Domain/Domain.cs index 6cb5aaac8..fc839f326 100644 --- a/src/Core/Models/Domain/Domain.cs +++ b/src/Core/Models/Domain/Domain.cs @@ -58,18 +58,14 @@ namespace Bit.Core.Models.Domain var viewModelType = viewModel.GetType(); var domainType = domain.GetType(); - Task decCs(string propName) + async Task decCsAndSetDec(string propName) { var domainPropInfo = domainType.GetProperty(propName); - var domainProp = domainPropInfo.GetValue(domain) as CipherString; - if(domainProp != null) + string val = null; + if(domainPropInfo.GetValue(domain) is CipherString domainProp) { - return domainProp.DecryptAsync(orgId); + val = await domainProp.DecryptAsync(orgId); } - return Task.FromResult((string)null); - }; - void setDec(string propName, string val) - { var viewModelPropInfo = viewModelType.GetProperty(propName); viewModelPropInfo.SetValue(viewModel, val, null); }; @@ -77,7 +73,7 @@ namespace Bit.Core.Models.Domain var tasks = new List(); foreach(var prop in map) { - tasks.Add(decCs(prop).ContinueWith(async val => setDec(prop, await val))); + tasks.Add(decCsAndSetDec(prop)); } await Task.WhenAll(tasks); return viewModel; diff --git a/src/Core/Services/CipherService.cs b/src/Core/Services/CipherService.cs index 763e1fe90..c2aa742c3 100644 --- a/src/Core/Services/CipherService.cs +++ b/src/Core/Services/CipherService.cs @@ -169,12 +169,9 @@ namespace Bit.Core.Services "Notes" }, key), EncryptCipherDataAsync(cipher, model, key), - EncryptFieldsAsync(model.Fields, key) - .ContinueWith(async fields => cipher.Fields = await fields), - EncryptPasswordHistoriesAsync(model.PasswordHistory, key) - .ContinueWith(async phs => cipher.PasswordHistory = await phs), - EncryptAttachmentsAsync(model.Attachments, key) - .ContinueWith(async attachments => cipher.Attachments = await attachments) + EncryptFieldsAsync(model.Fields, key, cipher), + EncryptPasswordHistoriesAsync(model.PasswordHistory, key, cipher), + EncryptAttachmentsAsync(model.Attachments, key, cipher) }; await Task.WhenAll(tasks); return cipher; @@ -220,11 +217,16 @@ namespace Bit.Core.Services throw new Exception("No key."); } var decCiphers = new List(); + async Task decryptAndAddCipherAsync(Cipher cipher) + { + var c = await cipher.DecryptAsync(); + decCiphers.Add(c); + } var tasks = new List(); var ciphers = await GetAllAsync(); foreach(var cipher in ciphers) { - tasks.Add(cipher.DecryptAsync().ContinueWith(async c => decCiphers.Add(await c))); + tasks.Add(decryptAndAddCipherAsync(cipher)); } await Task.WhenAll(tasks); decCiphers = decCiphers.OrderBy(c => c, new CipherLocaleComparer(_i18nService)).ToList(); @@ -872,18 +874,15 @@ namespace Bit.Core.Services var modelType = model.GetType(); var objType = obj.GetType(); - Task makeCs(string propName) + async Task makeAndSetCs(string propName) { var modelPropInfo = modelType.GetProperty(propName); var modelProp = modelPropInfo.GetValue(model) as string; + CipherString val = null; if(!string.IsNullOrWhiteSpace(modelProp)) { - return _cryptoService.EncryptAsync(modelProp, key); + val = await _cryptoService.EncryptAsync(modelProp, key); } - return Task.FromResult((CipherString)null); - }; - void setCs(string propName, CipherString val) - { var objPropInfo = objType.GetProperty(propName); objPropInfo.SetValue(obj, val, null); }; @@ -891,44 +890,44 @@ namespace Bit.Core.Services var tasks = new List(); foreach(var prop in map) { - tasks.Add(makeCs(prop).ContinueWith(async val => setCs(prop, await val))); + tasks.Add(makeAndSetCs(prop)); } return Task.WhenAll(tasks); } - private async Task> EncryptAttachmentsAsync( - List attachmentsModel, SymmetricCryptoKey key) + private async Task EncryptAttachmentsAsync(List attachmentsModel, SymmetricCryptoKey key, + Cipher cipher) { if(!attachmentsModel?.Any() ?? true) { - return null; + cipher.Attachments = null; } var tasks = new List(); var encAttachments = new List(); + async Task encryptAndAddAttachmentAsync(AttachmentView model, Attachment attachment) + { + await EncryptObjPropertyAsync(model, attachment, new HashSet + { + "FileName" + }, key); + if(model.Key != null) + { + attachment.Key = await _cryptoService.EncryptAsync(model.Key.Key, key); + } + encAttachments.Add(attachment); + } foreach(var model in attachmentsModel) { - var attachment = new Attachment + tasks.Add(encryptAndAddAttachmentAsync(model, new Attachment { Id = model.Id, Size = model.Size, SizeName = model.SizeName, Url = model.Url - }; - var task = EncryptObjPropertyAsync(model, attachment, new HashSet - { - "FileName" - }, key).ContinueWith(async (t) => - { - if(model.Key != null) - { - attachment.Key = await _cryptoService.EncryptAsync(model.Key.Key, key); - } - encAttachments.Add(attachment); - }); - tasks.Add(task); + })); } await Task.WhenAll(tasks); - return encAttachments; + cipher.Attachments = encAttachments; } private async Task EncryptCipherDataAsync(Cipher cipher, CipherView model, SymmetricCryptoKey key) @@ -1007,14 +1006,25 @@ namespace Bit.Core.Services } } - private async Task> EncryptFieldsAsync(List fieldsModel, SymmetricCryptoKey key) + private async Task EncryptFieldsAsync(List fieldsModel, SymmetricCryptoKey key, + Cipher cipher) { if(!fieldsModel?.Any() ?? true) { - return null; + cipher.Fields = null; + return; } var tasks = new List(); var encFields = new List(); + async Task encryptAndAddFieldAsync(FieldView model, Field field) + { + await EncryptObjPropertyAsync(model, field, new HashSet + { + "Name", + "Value" + }, key); + encFields.Add(field); + } foreach(var model in fieldsModel) { var field = new Field @@ -1026,46 +1036,38 @@ namespace Bit.Core.Services { model.Value = "false"; } - var task = EncryptObjPropertyAsync(model, field, new HashSet - { - "Name", - "Value" - }, key).ContinueWith((t) => - { - encFields.Add(field); - }); - tasks.Add(task); + tasks.Add(encryptAndAddFieldAsync(model, field)); } await Task.WhenAll(tasks); - return encFields; + cipher.Fields = encFields; } - private async Task> EncryptPasswordHistoriesAsync(List phModels, - SymmetricCryptoKey key) + private async Task EncryptPasswordHistoriesAsync(List phModels, + SymmetricCryptoKey key, Cipher cipher) { if(!phModels?.Any() ?? true) { - return null; + cipher.PasswordHistory = null; } var tasks = new List(); var encPhs = new List(); - foreach(var model in phModels) + async Task encryptAndAddHistoryAsync(PasswordHistoryView model, PasswordHistory ph) { - var ph = new PasswordHistory - { - LastUsedDate = model.LastUsedDate - }; - var task = EncryptObjPropertyAsync(model, ph, new HashSet + await EncryptObjPropertyAsync(model, ph, new HashSet { "Password" - }, key).ContinueWith((t) => + }, key); + encPhs.Add(ph); + } + foreach(var model in phModels) + { + tasks.Add(encryptAndAddHistoryAsync(model, new PasswordHistory { - encPhs.Add(ph); - }); - tasks.Add(task); + LastUsedDate = model.LastUsedDate + })); } await Task.WhenAll(tasks); - return encPhs; + cipher.PasswordHistory = encPhs; } private class CipherLocaleComparer : IComparer diff --git a/src/Core/Services/CollectionService.cs b/src/Core/Services/CollectionService.cs index bede65fd6..317beec51 100644 --- a/src/Core/Services/CollectionService.cs +++ b/src/Core/Services/CollectionService.cs @@ -67,10 +67,15 @@ namespace Bit.Core.Services return new List(); } var decCollections = new List(); + async Task decryptAndAddCollectionAsync(Collection collection) + { + var c = await collection.DecryptAsync(); + decCollections.Add(c); + } var tasks = new List(); foreach(var collection in collections) { - tasks.Add(collection.DecryptAsync().ContinueWith(async c => decCollections.Add(await c))); + tasks.Add(decryptAndAddCollectionAsync(collection)); } await Task.WhenAll(tasks); return decCollections.OrderBy(c => c, new CollectionLocaleComparer(_i18nService)).ToList(); diff --git a/src/Core/Services/FolderService.cs b/src/Core/Services/FolderService.cs index 554ad9aaf..878ed1eac 100644 --- a/src/Core/Services/FolderService.cs +++ b/src/Core/Services/FolderService.cs @@ -92,11 +92,16 @@ namespace Bit.Core.Services throw new Exception("No key."); } var decFolders = new List(); + async Task decryptAndAddFolderAsync(Folder folder) + { + var f = await folder.DecryptAsync(); + decFolders.Add(f); + } var tasks = new List(); var folders = await GetAllAsync(); foreach(var folder in folders) { - tasks.Add(folder.DecryptAsync().ContinueWith(async f => decFolders.Add(await f))); + tasks.Add(decryptAndAddFolderAsync(folder)); } await Task.WhenAll(tasks); decFolders = decFolders.OrderBy(f => f, new FolderLocaleComparer(_i18nService)).ToList();