Skip to content

Commit

Permalink
Throw VaasAuthenticationException instead of VaasClientException
Browse files Browse the repository at this point in the history
Use a separate HttpClient when uploading for the custom token
  • Loading branch information
ata-no-one committed Nov 10, 2023
1 parent 4fff377 commit b38de98
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions dotnet/Vaas/src/Vaas/Vaas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.WebSockets;
Expand Down Expand Up @@ -50,6 +51,9 @@ public class Vaas : IDisposable, IVaas
private WebsocketClient AuthenticatedClient => GetAuthenticatedWebSocket();

private readonly HttpClient _httpClient;
// Uploads use a custom token instead of the identity provider token
// TODO: Use the identity provider token for uploads
private readonly HttpClient _uploadHttpClient = new();

private string? SessionId { get; set; }
private bool AuthenticatedErrorOccured { get; set; }
Expand All @@ -76,7 +80,20 @@ public Vaas(HttpClient httpClient, IAuthenticator authenticator, VaasOptions opt

public async Task Connect(CancellationToken cancellationToken)
{
var token = await _authenticator.GetTokenAsync(cancellationToken);
string token;
try
{
token = await _authenticator.GetTokenAsync(cancellationToken);
}
catch (HttpRequestException e)
{
if (e.StatusCode != null)
{
EnsureSuccess(e.StatusCode.Value);
}
// How can this happen?
throw new VaasClientException("Unknown authentication error", e);
}
_client = new WebsocketClient(_options.Url, WebsocketClientFactory);
_client.ReconnectTimeout = null;
_client.MessageReceived.Subscribe(HandleResponseMessage);
Expand Down Expand Up @@ -197,9 +214,15 @@ private Task<HttpResponseMessage> GetAsync(Uri url, CancellationToken cancellati
private static void EnsureSuccess(HttpResponseMessage responseMessage)
{
// TODO: Parse ProblemDetails once implemented in server
var status = (int)responseMessage.StatusCode;
switch (status)
EnsureSuccess(responseMessage.StatusCode);
}

private static void EnsureSuccess(HttpStatusCode status)
{
switch ((int)status)
{
case 401 or 403:
throw new VaasAuthenticationException();
case >= 400 and < 500:
throw new VaasClientException("Client-side error");
case >= 500 and < 600:
Expand Down Expand Up @@ -255,7 +278,7 @@ private async Task UploadFile(string path, string url, string token)

request.Headers.Authorization = new AuthenticationHeaderValue(token);
request.Content = streamContent;
var httpResponse = await _httpClient.SendAsync(request);
var httpResponse = await _uploadHttpClient.SendAsync(request);

if (!httpResponse.IsSuccessStatusCode)
{
Expand Down

0 comments on commit b38de98

Please sign in to comment.