public async Task <ApiResult <CipherResponse> > EncryptAndSaveAttachmentAsync(Cipher cipher, byte[] data, string fileName) { var key = cipher.OrganizationId != null?_cryptoService.GetOrgKey(cipher.OrganizationId) : null; var encFileName = fileName.Encrypt(cipher.OrganizationId); var dataKey = _cryptoService.MakeEncKey(key); var encBytes = _cryptoService.EncryptToBytes(data, dataKey.Item1); var response = await _cipherApiRepository.PostAttachmentAsync(cipher.Id, encBytes, dataKey.Item2.EncryptedString, encFileName.EncryptedString); if (response.Succeeded) { var attachmentData = response.Result.Attachments.Select(a => new AttachmentData(a, cipher.Id)); await UpsertAttachmentDataAsync(attachmentData); cipher.Attachments = response.Result.Attachments.Select(a => new Attachment(a)); } else if (response.StatusCode == System.Net.HttpStatusCode.Forbidden || response.StatusCode == System.Net.HttpStatusCode.Unauthorized) { _authService.LogOut(); } return(response); }
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); }