mirror of
https://github.com/bitwarden/mobile
synced 2025-12-15 07:43:37 +00:00
Port send jslib to mobile (#1219)
* Expand Hkdf crypto functions * Add tests for hkdf crypto functions Took the testing infrastructure from bitwarden/server * Move Hkdf to cryptoFunctionService * Port changes from bitwarden/jslib#192 * Port changes from bitwarden/jslib#205 * Make Send Expiration Optional implement changes from bitwarden/jslib#242 * Bug fixes found by testing * Test helpers * Test conversion between model types * Test SendService These are mostly happy-path tests to ensure a reasonably correct implementation * Add run tests step to GitHub Actions * Test send decryption * Test Request generation from Send * Constructor dependencies on separate lines * Remove unused testing infrastructure * Rename to match class name * Move fat arrows to previous lines * Handle exceptions in App layer * PR review cleanups * Throw when attempting to save an unkown Send Type I think it's best to only throw on unknown send types here. I don't think we want to throw whenever we encounter one since that would do bad things like lock up Sync if clients get out of date relative to servers. Instead, keep the client from ruining saved data by complaining last minute that it doesn't know what it's doing.
This commit is contained in:
@@ -215,6 +215,28 @@ namespace Bit.Core.Services
|
||||
|
||||
#endregion
|
||||
|
||||
#region Send APIs
|
||||
|
||||
public Task<SendResponse> GetSendAsync(string id) =>
|
||||
SendAsync<object, SendResponse>(HttpMethod.Get, $"/sends/{id}", null, true, true);
|
||||
|
||||
public Task<SendResponse> PostSendAsync(SendRequest request) =>
|
||||
SendAsync<SendRequest, SendResponse>(HttpMethod.Post, "/sends", request, true, true);
|
||||
|
||||
public Task<SendResponse> PostSendFileAsync(MultipartFormDataContent data) =>
|
||||
SendAsync<MultipartFormDataContent, SendResponse>(HttpMethod.Post, "/sends/file", data, true, true);
|
||||
|
||||
public Task<SendResponse> PutSendAsync(string id, SendRequest request) =>
|
||||
SendAsync<SendRequest, SendResponse>(HttpMethod.Put, $"/sends/{id}", request, true, true);
|
||||
|
||||
public Task<SendResponse> PutSendRemovePasswordAsync(string id) =>
|
||||
SendAsync<object, SendResponse>(HttpMethod.Put, $"/sends/{id}", null, true, true);
|
||||
|
||||
public Task DeleteSendAsync(string id) =>
|
||||
SendAsync<object, object>(HttpMethod.Delete, $"/sends/{id}", null, true, false);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cipher APIs
|
||||
|
||||
public Task<CipherResponse> GetCipherAsync(string id)
|
||||
@@ -346,7 +368,7 @@ namespace Bit.Core.Services
|
||||
}
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var error = await HandleErrorAsync(response, false);
|
||||
var error = await HandleErrorAsync(response, false, false);
|
||||
throw new ApiException(error);
|
||||
}
|
||||
}
|
||||
@@ -398,7 +420,7 @@ namespace Bit.Core.Services
|
||||
}
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var error = await HandleErrorAsync(response, false);
|
||||
var error = await HandleErrorAsync(response, false, true);
|
||||
throw new ApiException(error);
|
||||
}
|
||||
return null;
|
||||
@@ -458,7 +480,7 @@ namespace Bit.Core.Services
|
||||
}
|
||||
else if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
var error = await HandleErrorAsync(response, false);
|
||||
var error = await HandleErrorAsync(response, false, authed);
|
||||
throw new ApiException(error);
|
||||
}
|
||||
return (TResponse)(object)null;
|
||||
@@ -506,7 +528,7 @@ namespace Bit.Core.Services
|
||||
}
|
||||
else
|
||||
{
|
||||
var error = await HandleErrorAsync(response, true);
|
||||
var error = await HandleErrorAsync(response, true, true);
|
||||
throw new ApiException(error);
|
||||
}
|
||||
}
|
||||
@@ -520,10 +542,10 @@ namespace Bit.Core.Services
|
||||
};
|
||||
}
|
||||
|
||||
private async Task<ErrorResponse> HandleErrorAsync(HttpResponseMessage response, bool tokenError)
|
||||
private async Task<ErrorResponse> HandleErrorAsync(HttpResponseMessage response, bool tokenError, bool authed)
|
||||
{
|
||||
if ((tokenError && response.StatusCode == HttpStatusCode.BadRequest) ||
|
||||
response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.Forbidden)
|
||||
if (authed && ((tokenError && response.StatusCode == HttpStatusCode.BadRequest) ||
|
||||
response.StatusCode == HttpStatusCode.Unauthorized || response.StatusCode == HttpStatusCode.Forbidden))
|
||||
{
|
||||
await _logoutCallbackAsync(true);
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user