1
0
mirror of https://github.com/bitwarden/mobile synced 2026-01-08 19:43:56 +00:00

PM-2572 Code refactor

This commit is contained in:
Carlos Gonçalves
2024-05-28 10:59:40 +01:00
parent 25e394eb5e
commit 265c95b494
4 changed files with 19 additions and 38 deletions

View File

@@ -34,7 +34,6 @@ namespace Bit.Core.Models.Domain
public string SizeName { get; set; }
public EncString Key { get; set; }
public EncString FileName { get; set; }
public EncString CipherKey { get; set; }
public async Task<AttachmentView> DecryptAsync(string orgId, SymmetricCryptoKey key = null)
{

View File

@@ -134,24 +134,14 @@ namespace Bit.Core.Models.Domain
{
model.Attachments = new List<AttachmentView>();
var tasks = new List<Task>();
async Task decryptAndAddAttachmentAsync(Attachment attachment, SymmetricCryptoKey decKey)
async Task decryptAndAddAttachmentAsync(Attachment attachment)
{
var decAttachment = await attachment.DecryptAsync(OrganizationId, model.Key ?? decKey);
var decAttachment = await attachment.DecryptAsync(OrganizationId, model.Key);
model.Attachments.Add(decAttachment);
}
var cryptoService = ServiceContainer.Resolve<ICryptoService>();
foreach (var attachment in Attachments)
{
SymmetricCryptoKey decKey = null;
//If the cipher.key is null but the attachment.cipherKey has a value we will use it to decrypt the attachment
if (Key == null && attachment.CipherKey != null)
{
var orgKey = await cryptoService.GetOrgKeyAsync(OrganizationId);
var key = await cryptoService.DecryptToBytesAsync(attachment.CipherKey, orgKey);
decKey = new CipherKey(key);
}
tasks.Add(decryptAndAddAttachmentAsync(attachment, decKey));
tasks.Add(decryptAndAddAttachmentAsync(attachment));
}
await Task.WhenAll(tasks);
}

View File

@@ -20,7 +20,6 @@ namespace Bit.Core.Models.View
public string SizeName { get; set; }
public string FileName { get; set; }
public SymmetricCryptoKey Key { get; set; }
public CipherKey CipherKey { get; set; }
public long FileSize
{

View File

@@ -579,34 +579,25 @@ namespace Bit.Core.Services
if(cipherView.Key == null)
{
cipher = await EncryptAsync(cipherView);
var putCipherRequest = new CipherRequest(cipher);
var putCipherResponse = await _apiService.PutCipherAsync(cipherView.Id, putCipherRequest);
var cipherData = new CipherData(putCipherResponse, await _stateService.GetActiveUserIdAsync());
await UpsertAsync(cipherData);
await UpdateAndUpsertAsync(async () => await _apiService.PutCipherAsync(cipherView.Id, new CipherRequest(cipher)));
cipher = await GetAsync(cipherView.Id);
cipherView = await cipher.DecryptAsync();
}
if (cipherView.Attachments != null)
{
foreach (var attachment in cipherView.Attachments)
{
if (attachment.Key == null)
{
attachmentTasks.Add(ShareAttachmentWithServerAsync(attachment, cipherView.Id, organizationId, cipherView.Key, cipher?.Key));
attachment.CipherKey = cipherView.Key;
attachmentTasks.Add(ShareAttachmentWithServerAsync(attachment, cipherView.Id, organizationId));
}
}
}
await Task.WhenAll(attachmentTasks);
cipherView.OrganizationId = organizationId;
cipherView.CollectionIds = collectionIds;
var encCipher = await EncryptAsync(cipherView);
var request = new CipherShareRequest(encCipher);
var response = await _apiService.PutShareCipherAsync(cipherView.Id, request);
var userId = await _stateService.GetActiveUserIdAsync();
var data = new CipherData(response, userId, collectionIds);
await UpsertAsync(data);
cipher = await EncryptAsync(cipherView);
await UpdateAndUpsertAsync(async () => await _apiService.PutShareCipherAsync(cipherView.Id, new CipherShareRequest(cipher)), collectionIds);
}
public async Task<Cipher> SaveAttachmentRawWithServerAsync(Cipher cipher, CipherView cipherView, string filename, byte[] data)
@@ -869,13 +860,12 @@ namespace Bit.Core.Services
// Helpers
private async Task<Tuple<SymmetricCryptoKey, EncString, SymmetricCryptoKey>> MakeAttachmentKeyAsync(string organizationId, Cipher cipher = null, CipherView cipherView = null, SymmetricCryptoKey cipherKey = null)
private async Task<Tuple<SymmetricCryptoKey, EncString, SymmetricCryptoKey>> MakeAttachmentKeyAsync(string organizationId, Cipher cipher = null, CipherView cipherView = null)
{
var orgKey = await _cryptoService.GetOrgKeyAsync(organizationId);
//We give priority to the use of cipher.key if it exists
SymmetricCryptoKey encryptionKey = cipherKey ?? orgKey;
if (cipher != null && cipherView != null && cipher.Key == null)
SymmetricCryptoKey encryptionKey = orgKey;
if (cipher != null && cipherView != null)
{
encryptionKey = await UpdateCipherAndGetCipherKeyAsync(cipher, cipherView, orgKey, false);
}
@@ -887,7 +877,7 @@ namespace Bit.Core.Services
}
private async Task ShareAttachmentWithServerAsync(AttachmentView attachmentView, string cipherId,
string organizationId, SymmetricCryptoKey cipherKey = null, EncString cipherKeyProtected = null)
string organizationId)
{
var attachmentResponse = await _httpClient.GetAsync(attachmentView.Url);
if (!attachmentResponse.IsSuccessStatusCode)
@@ -898,7 +888,7 @@ namespace Bit.Core.Services
var bytes = await attachmentResponse.Content.ReadAsByteArrayAsync();
var decBytes = await _cryptoService.DecryptFromBytesAsync(bytes, null);
var (attachmentKey, protectedAttachmentKey, encKey) = await MakeAttachmentKeyAsync(organizationId, cipherKey:cipherKey);
var (attachmentKey, protectedAttachmentKey, encKey) = await MakeAttachmentKeyAsync(organizationId);
var encFileName = await _cryptoService.EncryptAsync(attachmentView.FileName, encKey);
var encFileData = await _cryptoService.EncryptToBytesAsync(decBytes, attachmentKey);
@@ -907,10 +897,6 @@ namespace Bit.Core.Services
var fd = new MultipartFormDataContent(boundary);
fd.Add(new StringContent(protectedAttachmentKey.EncryptedString), "key");
fd.Add(new StreamContent(new MemoryStream(encFileData.Buffer)), "data", encFileName.EncryptedString);
if(cipherKey != null && cipherKeyProtected != null)
{
fd.Add(new StringContent(cipherKeyProtected.EncryptedString), "cipherKey");
}
await _apiService.PostShareCipherAttachmentAsync(cipherId, attachmentView.Id, fd, organizationId);
}
@@ -1374,6 +1360,13 @@ namespace Bit.Core.Services
}
}
private async Task UpdateAndUpsertAsync(Func<Task<CipherResponse>> func, HashSet<string> collectionIds = null)
{
var response = await func();
var data = new CipherData(response, await _stateService.GetActiveUserIdAsync(), collectionIds);
await UpsertAsync(data);
}
private class CipherLocaleComparer : IComparer<CipherView>
{
private readonly II18nService _i18nService;