Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dotnet: Fix authententication #323

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading