1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-11-23 11:45:38 +01:00

convert to local functions to avoid continuewith

This commit is contained in:
Kyle Spearrin 2019-04-19 16:15:29 -04:00
parent 6d8af4d97a
commit c6ce3e9480
5 changed files with 95 additions and 78 deletions

View File

@ -104,11 +104,14 @@ namespace Bit.Core.Models.Domain
{
model.Attachments = new List<AttachmentView>();
var tasks = new List<Task>();
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<FieldView>();
var tasks = new List<Task>();
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<PasswordHistoryView>();
var tasks = new List<Task>();
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);
}

View File

@ -58,18 +58,14 @@ namespace Bit.Core.Models.Domain
var viewModelType = viewModel.GetType();
var domainType = domain.GetType();
Task<string> 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<Task>();
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;

View File

@ -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<CipherView>();
async Task decryptAndAddCipherAsync(Cipher cipher)
{
var c = await cipher.DecryptAsync();
decCiphers.Add(c);
}
var tasks = new List<Task>();
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<CipherString> 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<Task>();
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<List<Attachment>> EncryptAttachmentsAsync(
List<AttachmentView> attachmentsModel, SymmetricCryptoKey key)
private async Task EncryptAttachmentsAsync(List<AttachmentView> attachmentsModel, SymmetricCryptoKey key,
Cipher cipher)
{
if(!attachmentsModel?.Any() ?? true)
{
return null;
cipher.Attachments = null;
}
var tasks = new List<Task>();
var encAttachments = new List<Attachment>();
foreach(var model in attachmentsModel)
async Task encryptAndAddAttachmentAsync(AttachmentView model, Attachment attachment)
{
var attachment = new Attachment
{
Id = model.Id,
Size = model.Size,
SizeName = model.SizeName,
Url = model.Url
};
var task = EncryptObjPropertyAsync(model, attachment, new HashSet<string>
await EncryptObjPropertyAsync(model, attachment, new HashSet<string>
{
"FileName"
}, key).ContinueWith(async (t) =>
{
}, key);
if(model.Key != null)
{
attachment.Key = await _cryptoService.EncryptAsync(model.Key.Key, key);
}
encAttachments.Add(attachment);
});
tasks.Add(task);
}
foreach(var model in attachmentsModel)
{
tasks.Add(encryptAndAddAttachmentAsync(model, new Attachment
{
Id = model.Id,
Size = model.Size,
SizeName = model.SizeName,
Url = model.Url
}));
}
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<List<Field>> EncryptFieldsAsync(List<FieldView> fieldsModel, SymmetricCryptoKey key)
private async Task EncryptFieldsAsync(List<FieldView> fieldsModel, SymmetricCryptoKey key,
Cipher cipher)
{
if(!fieldsModel?.Any() ?? true)
{
return null;
cipher.Fields = null;
return;
}
var tasks = new List<Task>();
var encFields = new List<Field>();
async Task encryptAndAddFieldAsync(FieldView model, Field field)
{
await EncryptObjPropertyAsync(model, field, new HashSet<string>
{
"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<string>
{
"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<List<PasswordHistory>> EncryptPasswordHistoriesAsync(List<PasswordHistoryView> phModels,
SymmetricCryptoKey key)
private async Task EncryptPasswordHistoriesAsync(List<PasswordHistoryView> phModels,
SymmetricCryptoKey key, Cipher cipher)
{
if(!phModels?.Any() ?? true)
{
return null;
cipher.PasswordHistory = null;
}
var tasks = new List<Task>();
var encPhs = new List<PasswordHistory>();
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<string>
await EncryptObjPropertyAsync(model, ph, new HashSet<string>
{
"Password"
}, key).ContinueWith((t) =>
{
}, key);
encPhs.Add(ph);
});
tasks.Add(task);
}
foreach(var model in phModels)
{
tasks.Add(encryptAndAddHistoryAsync(model, new PasswordHistory
{
LastUsedDate = model.LastUsedDate
}));
}
await Task.WhenAll(tasks);
return encPhs;
cipher.PasswordHistory = encPhs;
}
private class CipherLocaleComparer : IComparer<CipherView>

View File

@ -67,10 +67,15 @@ namespace Bit.Core.Services
return new List<CollectionView>();
}
var decCollections = new List<CollectionView>();
async Task decryptAndAddCollectionAsync(Collection collection)
{
var c = await collection.DecryptAsync();
decCollections.Add(c);
}
var tasks = new List<Task>();
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();

View File

@ -92,11 +92,16 @@ namespace Bit.Core.Services
throw new Exception("No key.");
}
var decFolders = new List<FolderView>();
async Task decryptAndAddFolderAsync(Folder folder)
{
var f = await folder.DecryptAsync();
decFolders.Add(f);
}
var tasks = new List<Task>();
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();