mirror of
https://github.com/bitwarden/mobile
synced 2025-12-26 05:03:39 +00:00
PM-2149 WIP adding cancellation token to calls for the attachment upload
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Models.Domain;
|
||||
@@ -47,16 +48,16 @@ namespace Bit.Core.Abstractions
|
||||
Task RefreshIdentityTokenAsync();
|
||||
Task<SsoPrevalidateResponse> PreValidateSso(string identifier);
|
||||
Task<TResponse> SendAsync<TRequest, TResponse>(HttpMethod method, string path,
|
||||
TRequest body, bool authed, bool hasResponse, Action<HttpRequestMessage> alterRequest, bool logoutOnUnauthorized = true);
|
||||
TRequest body, bool authed, bool hasResponse, Action<HttpRequestMessage> alterRequest, bool logoutOnUnauthorized = true, CancellationToken cancellationToken = default);
|
||||
void SetUrls(EnvironmentUrls urls);
|
||||
[Obsolete("Mar 25 2021: This method has been deprecated in favor of direct uploads. This method still exists for backward compatibility with old server versions.")]
|
||||
Task<CipherResponse> PostCipherAttachmentLegacyAsync(string id, MultipartFormDataContent data);
|
||||
Task<AttachmentUploadDataResponse> PostCipherAttachmentAsync(string id, AttachmentRequest request);
|
||||
Task<AttachmentUploadDataResponse> PostCipherAttachmentAsync(string id, AttachmentRequest request, CancellationToken cancellationToken);
|
||||
Task<AttachmentResponse> GetAttachmentData(string cipherId, string attachmentId);
|
||||
Task PostShareCipherAttachmentAsync(string id, string attachmentId, MultipartFormDataContent data,
|
||||
string organizationId);
|
||||
Task<AttachmentUploadDataResponse> RenewAttachmentUploadUrlAsync(string id, string attachmentId);
|
||||
Task PostAttachmentFileAsync(string id, string attachmentId, MultipartFormDataContent data);
|
||||
Task<AttachmentUploadDataResponse> RenewAttachmentUploadUrlAsync(string id, string attachmentId, CancellationToken cancellationToken);
|
||||
Task PostAttachmentFileAsync(string id, string attachmentId, MultipartFormDataContent data, CancellationToken cancellationToken);
|
||||
Task<List<BreachAccountResponse>> GetHibpBreachAsync(string username);
|
||||
Task PostTwoFactorEmailAsync(TwoFactorEmailRequest request);
|
||||
Task PutDeviceTokenAsync(string identifier, DeviceTokenRequest request);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Models.Domain;
|
||||
|
||||
@@ -6,6 +7,6 @@ namespace Bit.Core.Abstractions
|
||||
{
|
||||
public interface IAzureFileUploadService
|
||||
{
|
||||
Task Upload(string uri, EncByteArray data, Func<Task<string>> renewalCallback);
|
||||
Task Upload(string uri, EncByteArray data, Func<CancellationToken, Task<string>> renewalCallback, CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Models.Data;
|
||||
@@ -27,7 +28,7 @@ namespace Bit.Core.Abstractions
|
||||
Task<Cipher> GetAsync(string id);
|
||||
Task<CipherView> GetLastUsedForUrlAsync(string url);
|
||||
Task ReplaceAsync(Dictionary<string, CipherData> ciphers);
|
||||
Task<Cipher> SaveAttachmentRawWithServerAsync(Cipher cipher, string filename, byte[] data);
|
||||
Task<Cipher> SaveAttachmentRawWithServerAsync(Cipher cipher, string filename, byte[] data, CancellationToken cancellationToken);
|
||||
Task SaveCollectionsWithServerAsync(Cipher cipher);
|
||||
Task SaveNeverDomainAsync(string domain);
|
||||
Task SaveWithServerAsync(Cipher cipher);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Models.Domain;
|
||||
using Bit.Core.Models.Response;
|
||||
|
||||
@@ -6,7 +7,7 @@ namespace Bit.Core.Abstractions
|
||||
{
|
||||
public interface IFileUploadService
|
||||
{
|
||||
Task UploadCipherAttachmentFileAsync(AttachmentUploadDataResponse uploadData, EncString fileName, EncByteArray encryptedFileData);
|
||||
Task UploadCipherAttachmentFileAsync(AttachmentUploadDataResponse uploadData, EncString fileName, EncByteArray encryptedFileData, CancellationToken cancellationToken);
|
||||
Task UploadSendFileAsync(SendFileUploadDataResponse uploadData, EncString fileName, EncByteArray encryptedFileData);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Abstractions;
|
||||
using Bit.Core.Enums;
|
||||
@@ -342,10 +343,10 @@ namespace Bit.Core.Services
|
||||
string.Concat("/ciphers/", id, "/attachment"), data, true, true);
|
||||
}
|
||||
|
||||
public Task<AttachmentUploadDataResponse> PostCipherAttachmentAsync(string id, AttachmentRequest request)
|
||||
public Task<AttachmentUploadDataResponse> PostCipherAttachmentAsync(string id, AttachmentRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
return SendAsync<AttachmentRequest, AttachmentUploadDataResponse>(HttpMethod.Post,
|
||||
$"/ciphers/{id}/attachment/v2", request, true, true);
|
||||
$"/ciphers/{id}/attachment/v2", request, true, true, cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
public Task<AttachmentResponse> GetAttachmentData(string cipherId, string attachmentId) =>
|
||||
@@ -365,12 +366,12 @@ namespace Bit.Core.Services
|
||||
data, true, false);
|
||||
}
|
||||
|
||||
public Task<AttachmentUploadDataResponse> RenewAttachmentUploadUrlAsync(string cipherId, string attachmentId) =>
|
||||
SendAsync<AttachmentUploadDataResponse>(HttpMethod.Get, $"/ciphers/{cipherId}/attachment/{attachmentId}/renew", true);
|
||||
public Task<AttachmentUploadDataResponse> RenewAttachmentUploadUrlAsync(string cipherId, string attachmentId, CancellationToken cancellationToken) =>
|
||||
SendAsync<AttachmentUploadDataResponse>(HttpMethod.Get, $"/ciphers/{cipherId}/attachment/{attachmentId}/renew", true, cancellationToken);
|
||||
|
||||
public Task PostAttachmentFileAsync(string cipherId, string attachmentId, MultipartFormDataContent data) =>
|
||||
public Task PostAttachmentFileAsync(string cipherId, string attachmentId, MultipartFormDataContent data, CancellationToken cancellationToken) =>
|
||||
SendAsync(HttpMethod.Post,
|
||||
$"/ciphers/{cipherId}/attachment/{attachmentId}", data, true);
|
||||
$"/ciphers/{cipherId}/attachment/{attachmentId}", data, true, cancellationToken);
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -629,12 +630,12 @@ namespace Bit.Core.Services
|
||||
|
||||
public Task SendAsync(HttpMethod method, string path, bool authed) =>
|
||||
SendAsync<object, object>(method, path, null, authed, false);
|
||||
public Task SendAsync<TRequest>(HttpMethod method, string path, TRequest body, bool authed) =>
|
||||
SendAsync<TRequest, object>(method, path, body, authed, false);
|
||||
public Task<TResponse> SendAsync<TResponse>(HttpMethod method, string path, bool authed) =>
|
||||
SendAsync<object, TResponse>(method, path, null, authed, true);
|
||||
public Task SendAsync<TRequest>(HttpMethod method, string path, TRequest body, bool authed, CancellationToken cancellationToken = default) =>
|
||||
SendAsync<TRequest, object>(method, path, body, authed, false, cancellationToken: cancellationToken);
|
||||
public Task<TResponse> SendAsync<TResponse>(HttpMethod method, string path, bool authed, CancellationToken cancellationToken = default) =>
|
||||
SendAsync<object, TResponse>(method, path, null, authed, true, cancellationToken: cancellationToken);
|
||||
public async Task<TResponse> SendAsync<TRequest, TResponse>(HttpMethod method, string path, TRequest body,
|
||||
bool authed, bool hasResponse, Action<HttpRequestMessage> alterRequest = null, bool logoutOnUnauthorized = true)
|
||||
bool authed, bool hasResponse, Action<HttpRequestMessage> alterRequest = null, bool logoutOnUnauthorized = true, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using (var requestMessage = new HttpRequestMessage())
|
||||
{
|
||||
@@ -686,7 +687,7 @@ namespace Bit.Core.Services
|
||||
HttpResponseMessage response;
|
||||
try
|
||||
{
|
||||
response = await _httpClient.SendAsync(requestMessage);
|
||||
response = await _httpClient.SendAsync(requestMessage, cancellationToken);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using Bit.Core.Abstractions;
|
||||
@@ -29,19 +30,19 @@ namespace Bit.Core.Services
|
||||
};
|
||||
}
|
||||
|
||||
public async Task Upload(string uri, EncByteArray data, Func<Task<string>> renewalCallback)
|
||||
public async Task Upload(string uri, EncByteArray data, Func<CancellationToken, Task<string>> renewalCallback, CancellationToken cancellationToken)
|
||||
{
|
||||
if (data?.Buffer?.Length <= MAX_SINGLE_BLOB_UPLOAD_SIZE)
|
||||
{
|
||||
await AzureUploadBlob(uri, data);
|
||||
await AzureUploadBlob(uri, data, cancellationToken);
|
||||
}
|
||||
else
|
||||
{
|
||||
await AzureUploadBlocks(uri, data, renewalCallback);
|
||||
await AzureUploadBlocks(uri, data, renewalCallback, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task AzureUploadBlob(string uri, EncByteArray data)
|
||||
private async Task AzureUploadBlob(string uri, EncByteArray data, CancellationToken cancellationToken)
|
||||
{
|
||||
using (var requestMessage = new HttpRequestMessage())
|
||||
{
|
||||
@@ -57,7 +58,7 @@ namespace Bit.Core.Services
|
||||
requestMessage.Method = HttpMethod.Put;
|
||||
requestMessage.RequestUri = uriBuilder.Uri;
|
||||
|
||||
var blobResponse = await _httpClient.SendAsync(requestMessage);
|
||||
var blobResponse = await _httpClient.SendAsync(requestMessage, cancellationToken);
|
||||
|
||||
if (blobResponse.StatusCode != HttpStatusCode.Created)
|
||||
{
|
||||
@@ -66,7 +67,7 @@ namespace Bit.Core.Services
|
||||
}
|
||||
}
|
||||
|
||||
private async Task AzureUploadBlocks(string uri, EncByteArray data, Func<Task<string>> renewalFunc)
|
||||
private async Task AzureUploadBlocks(string uri, EncByteArray data, Func<CancellationToken, Task<string>> renewalFunc, CancellationToken cancellationToken)
|
||||
{
|
||||
_httpClient.Timeout = TimeSpan.FromHours(3);
|
||||
var baseParams = HttpUtility.ParseQueryString(CoreHelpers.GetUri(uri).Query);
|
||||
@@ -82,7 +83,7 @@ namespace Bit.Core.Services
|
||||
|
||||
while (blockIndex < numBlocks)
|
||||
{
|
||||
uri = await RenewUriIfNecessary(uri, renewalFunc);
|
||||
uri = await RenewUriIfNecessary(uri, renewalFunc, cancellationToken);
|
||||
var blockUriBuilder = new UriBuilder(uri);
|
||||
var blockId = EncodeBlockId(blockIndex);
|
||||
var blockParams = HttpUtility.ParseQueryString(blockUriBuilder.Query);
|
||||
@@ -101,7 +102,7 @@ namespace Bit.Core.Services
|
||||
requestMessage.Method = HttpMethod.Put;
|
||||
requestMessage.RequestUri = blockUriBuilder.Uri;
|
||||
|
||||
var blockResponse = await _httpClient.SendAsync(requestMessage);
|
||||
var blockResponse = await _httpClient.SendAsync(requestMessage, cancellationToken);
|
||||
|
||||
if (blockResponse.StatusCode != HttpStatusCode.Created)
|
||||
{
|
||||
@@ -115,7 +116,7 @@ namespace Bit.Core.Services
|
||||
|
||||
using (var requestMessage = new HttpRequestMessage())
|
||||
{
|
||||
uri = await RenewUriIfNecessary(uri, renewalFunc);
|
||||
uri = await RenewUriIfNecessary(uri, renewalFunc, cancellationToken);
|
||||
var blockListXml = GenerateBlockListXml(blocksStaged);
|
||||
var blockListUriBuilder = new UriBuilder(uri);
|
||||
var blockListParams = HttpUtility.ParseQueryString(blockListUriBuilder.Query);
|
||||
@@ -130,7 +131,7 @@ namespace Bit.Core.Services
|
||||
requestMessage.Method = HttpMethod.Put;
|
||||
requestMessage.RequestUri = blockListUriBuilder.Uri;
|
||||
|
||||
var blockListResponse = await _httpClient.SendAsync(requestMessage);
|
||||
var blockListResponse = await _httpClient.SendAsync(requestMessage, cancellationToken);
|
||||
|
||||
if (blockListResponse.StatusCode != HttpStatusCode.Created)
|
||||
{
|
||||
@@ -139,13 +140,13 @@ namespace Bit.Core.Services
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<string> RenewUriIfNecessary(string uri, Func<Task<string>> renewalFunc)
|
||||
private async Task<string> RenewUriIfNecessary(string uri, Func<CancellationToken, Task<string>> renewalFunc, CancellationToken cancellationToken)
|
||||
{
|
||||
var uriParams = HttpUtility.ParseQueryString(CoreHelpers.GetUri(uri).Query);
|
||||
|
||||
if (DateTime.TryParse(uriParams.Get("se") ?? "", out DateTime expiry) && expiry < DateTime.UtcNow.AddSeconds(1))
|
||||
{
|
||||
return await renewalFunc();
|
||||
return await renewalFunc(cancellationToken);
|
||||
}
|
||||
return uri;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Net.Mime;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Models.Domain;
|
||||
|
||||
@@ -9,21 +10,21 @@ namespace Bit.Core.Services
|
||||
{
|
||||
public class BitwardenFileUploadService
|
||||
{
|
||||
private readonly ApiService _apiService;
|
||||
|
||||
public BitwardenFileUploadService(ApiService apiService)
|
||||
{
|
||||
_apiService = apiService;
|
||||
}
|
||||
|
||||
private readonly ApiService _apiService;
|
||||
|
||||
public async Task Upload(string encryptedFileName, EncByteArray encryptedFileData, Func<MultipartFormDataContent, Task> apiCall)
|
||||
public async Task Upload(string encryptedFileName, EncByteArray encryptedFileData, Func<MultipartFormDataContent, CancellationToken, Task> apiCall, CancellationToken cancellationToken)
|
||||
{
|
||||
var fd = new MultipartFormDataContent($"--BWMobileFormBoundary{DateTime.UtcNow.Ticks}")
|
||||
{
|
||||
{ new ByteArrayContent(encryptedFileData.Buffer) { Headers = { ContentType = new MediaTypeHeaderValue(MediaTypeNames.Application.Octet) } }, "data", encryptedFileName }
|
||||
};
|
||||
|
||||
await apiCall(fd);
|
||||
await apiCall(fd, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Abstractions;
|
||||
using Bit.Core.Enums;
|
||||
@@ -555,13 +556,15 @@ namespace Bit.Core.Services
|
||||
await UpsertAsync(data);
|
||||
}
|
||||
|
||||
public async Task<Cipher> SaveAttachmentRawWithServerAsync(Cipher cipher, string filename, byte[] data)
|
||||
public async Task<Cipher> SaveAttachmentRawWithServerAsync(Cipher cipher, string filename, byte[] data, CancellationToken cancellationToken)
|
||||
{
|
||||
var orgKey = await _cryptoService.GetOrgKeyAsync(cipher.OrganizationId);
|
||||
var encFileName = await _cryptoService.EncryptAsync(filename, orgKey);
|
||||
var (attachmentKey, orgEncAttachmentKey) = await _cryptoService.MakeEncKeyAsync(orgKey);
|
||||
var encFileData = await _cryptoService.EncryptToBytesAsync(data, attachmentKey);
|
||||
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
CipherResponse response;
|
||||
try
|
||||
{
|
||||
@@ -572,9 +575,9 @@ namespace Bit.Core.Services
|
||||
FileSize = encFileData.Buffer.Length,
|
||||
};
|
||||
|
||||
var uploadDataResponse = await _apiService.PostCipherAttachmentAsync(cipher.Id, request);
|
||||
var uploadDataResponse = await _apiService.PostCipherAttachmentAsync(cipher.Id, request, cancellationToken);
|
||||
response = uploadDataResponse.CipherResponse;
|
||||
await _fileUploadService.UploadCipherAttachmentFileAsync(uploadDataResponse, encFileName, encFileData);
|
||||
await _fileUploadService.UploadCipherAttachmentFileAsync(uploadDataResponse, encFileName, encFileData, cancellationToken);
|
||||
}
|
||||
catch (ApiException e) when (e.Error.StatusCode == System.Net.HttpStatusCode.NotFound || e.Error.StatusCode == System.Net.HttpStatusCode.MethodNotAllowed)
|
||||
{
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Abstractions;
|
||||
using Bit.Core.Enums;
|
||||
@@ -21,7 +22,7 @@ namespace Bit.Core.Services
|
||||
private readonly ApiService _apiService;
|
||||
|
||||
public async Task UploadCipherAttachmentFileAsync(AttachmentUploadDataResponse uploadData,
|
||||
EncString encryptedFileName, EncByteArray encryptedFileData)
|
||||
EncString encryptedFileName, EncByteArray encryptedFileData, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -29,15 +30,15 @@ namespace Bit.Core.Services
|
||||
{
|
||||
case FileUploadType.Direct:
|
||||
await _bitwardenFileUploadService.Upload(encryptedFileName.EncryptedString, encryptedFileData,
|
||||
fd => _apiService.PostAttachmentFileAsync(uploadData.CipherResponse.Id, uploadData.AttachmentId, fd));
|
||||
(fd, ct) => _apiService.PostAttachmentFileAsync(uploadData.CipherResponse.Id, uploadData.AttachmentId, fd, ct), cancellationToken);
|
||||
break;
|
||||
case FileUploadType.Azure:
|
||||
Func<Task<string>> renewalCallback = async () =>
|
||||
Func<CancellationToken, Task<string>> renewalCallback = async ct =>
|
||||
{
|
||||
var response = await _apiService.RenewAttachmentUploadUrlAsync(uploadData.CipherResponse.Id, uploadData.AttachmentId);
|
||||
var response = await _apiService.RenewAttachmentUploadUrlAsync(uploadData.CipherResponse.Id, uploadData.AttachmentId, ct);
|
||||
return response.Url;
|
||||
};
|
||||
await _azureFileUploadService.Upload(uploadData.Url, encryptedFileData, renewalCallback);
|
||||
await _azureFileUploadService.Upload(uploadData.Url, encryptedFileData, renewalCallback, cancellationToken);
|
||||
break;
|
||||
default:
|
||||
throw new Exception($"Unkown file upload type: {uploadData.FileUploadType}");
|
||||
@@ -58,16 +59,16 @@ namespace Bit.Core.Services
|
||||
{
|
||||
case FileUploadType.Direct:
|
||||
await _bitwardenFileUploadService.Upload(fileName.EncryptedString, encryptedFileData,
|
||||
fd => _apiService.PostSendFileAsync(uploadData.SendResponse.Id, uploadData.SendResponse.File.Id, fd));
|
||||
(fd, _) => _apiService.PostSendFileAsync(uploadData.SendResponse.Id, uploadData.SendResponse.File.Id, fd), default);
|
||||
break;
|
||||
case FileUploadType.Azure:
|
||||
Func<Task<string>> renewalCallback = async () =>
|
||||
Func<CancellationToken, Task<string>> renewalCallback = async ct =>
|
||||
{
|
||||
var response = await _apiService.RenewFileUploadUrlAsync(uploadData.SendResponse.Id, uploadData.SendResponse.File.Id);
|
||||
return response.Url;
|
||||
};
|
||||
|
||||
await _azureFileUploadService.Upload(uploadData.Url, encryptedFileData, renewalCallback);
|
||||
await _azureFileUploadService.Upload(uploadData.Url, encryptedFileData, renewalCallback, default);
|
||||
break;
|
||||
default:
|
||||
throw new Exception("Unknown file upload type");
|
||||
|
||||
Reference in New Issue
Block a user