1
0
mirror of https://github.com/bitwarden/mobile synced 2026-01-08 03:23:23 +00:00

attachments page with upload/delete

This commit is contained in:
Kyle Spearrin
2017-07-22 15:38:08 -04:00
parent b32603b472
commit f9d336a3a6
24 changed files with 786 additions and 80 deletions

View File

@@ -260,6 +260,26 @@ namespace Bit.App.Services
return Crypto.AesCbcEncrypt(plainBytes, key);
}
public byte[] EncryptToBytes(byte[] plainBytes, SymmetricCryptoKey key = null)
{
if(key == null)
{
key = EncKey ?? Key;
}
if(key == null)
{
throw new ArgumentNullException(nameof(key));
}
if(plainBytes == null)
{
throw new ArgumentNullException(nameof(plainBytes));
}
return Crypto.AesCbcEncryptToBytes(plainBytes, key);
}
public string Decrypt(CipherString encyptedValue, SymmetricCryptoKey key = null)
{
try

View File

@@ -17,6 +17,7 @@ namespace Bit.App.Services
private readonly IAttachmentRepository _attachmentRepository;
private readonly IAuthService _authService;
private readonly ILoginApiRepository _loginApiRepository;
private readonly ICipherApiRepository _cipherApiRepository;
private readonly ISettingsService _settingsService;
private readonly ICryptoService _cryptoService;
@@ -25,6 +26,7 @@ namespace Bit.App.Services
IAttachmentRepository attachmentRepository,
IAuthService authService,
ILoginApiRepository loginApiRepository,
ICipherApiRepository cipherApiRepository,
ISettingsService settingsService,
ICryptoService cryptoService)
{
@@ -32,6 +34,7 @@ namespace Bit.App.Services
_attachmentRepository = attachmentRepository;
_authService = authService;
_loginApiRepository = loginApiRepository;
_cipherApiRepository = cipherApiRepository;
_settingsService = settingsService;
_cryptoService = cryptoService;
}
@@ -238,7 +241,7 @@ namespace Bit.App.Services
{
return null;
}
if(!string.IsNullOrWhiteSpace(orgId))
{
return _cryptoService.DecryptToBytes(data, _cryptoService.GetOrgKey(orgId));
@@ -255,6 +258,47 @@ namespace Bit.App.Services
}
}
public async Task<ApiResult<CipherResponse>> EncryptAndSaveAttachmentAsync(Login login, byte[] data, string fileName)
{
var encFileName = fileName.Encrypt(login.OrganizationId);
var encBytes = _cryptoService.EncryptToBytes(data,
login.OrganizationId != null ? _cryptoService.GetOrgKey(login.OrganizationId) : null);
var response = await _cipherApiRepository.PostAttachmentAsync(login.Id, encBytes, encFileName.EncryptedString);
if(response.Succeeded)
{
var attachmentData = response.Result.Attachments.Select(a => new AttachmentData(a, login.Id));
foreach(var attachment in attachmentData)
{
await _attachmentRepository.UpsertAsync(attachment);
}
login.Attachments = response.Result.Attachments.Select(a => new Attachment(a));
}
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
|| response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
MessagingCenter.Send(Application.Current, "Logout", (string)null);
}
return response;
}
public async Task<ApiResult> DeleteAttachmentAsync(Login login, string attachmentId)
{
var response = await _cipherApiRepository.DeleteAttachmentAsync(login.Id, attachmentId);
if(response.Succeeded)
{
await _attachmentRepository.DeleteAsync(attachmentId);
}
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
|| response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
MessagingCenter.Send(Application.Current, "Logout", (string)null);
}
return response;
}
private string WebUriFromAndroidAppUri(string androidAppUriString)
{
if(!UriIsAndroidApp(androidAppUriString))