From dbea1c2c4d4e6871c0f27fcf5d053f81740249d5 Mon Sep 17 00:00:00 2001 From: Leendert de Borst Date: Tue, 18 Jun 2024 20:43:07 +0200 Subject: [PATCH] Add api version prefix to WASM app URLs (#25) --- src/AliasVault.WebApp/Auth/Pages/Login.razor | 3 +- .../Auth/Pages/Register.razor | 2 +- .../Auth/Services/AuthService.cs | 40 ++++++++----------- .../Services/AliasService.cs | 14 +++---- 4 files changed, 25 insertions(+), 34 deletions(-) diff --git a/src/AliasVault.WebApp/Auth/Pages/Login.razor b/src/AliasVault.WebApp/Auth/Pages/Login.razor index cf867beae..0e5d21a30 100644 --- a/src/AliasVault.WebApp/Auth/Pages/Login.razor +++ b/src/AliasVault.WebApp/Auth/Pages/Login.razor @@ -1,7 +1,6 @@ @page "/user/login" @attribute [AllowAnonymous] @layout Auth.Layout.MainLayout - @inject HttpClient Http @inject AuthenticationStateProvider AuthStateProvider @inject NavigationManager NavigationManager @@ -71,7 +70,7 @@ try { - var result = await Http.PostAsJsonAsync("api/Auth/login", _loginModel); + var result = await Http.PostAsJsonAsync("api/v1/Auth/login", _loginModel); var responseContent = await result.Content.ReadAsStringAsync(); if (!result.IsSuccessStatusCode) diff --git a/src/AliasVault.WebApp/Auth/Pages/Register.razor b/src/AliasVault.WebApp/Auth/Pages/Register.razor index ff73f3bbb..bc56f00c0 100644 --- a/src/AliasVault.WebApp/Auth/Pages/Register.razor +++ b/src/AliasVault.WebApp/Auth/Pages/Register.razor @@ -63,7 +63,7 @@ try { - var result = await Http.PostAsJsonAsync("api/Auth/register", _registerModel); + var result = await Http.PostAsJsonAsync("api/v1/Auth/register", _registerModel); var responseContent = await result.Content.ReadAsStringAsync(); if (!result.IsSuccessStatusCode) diff --git a/src/AliasVault.WebApp/Auth/Services/AuthService.cs b/src/AliasVault.WebApp/Auth/Services/AuthService.cs index f4c6a0304..8414cc186 100644 --- a/src/AliasVault.WebApp/Auth/Services/AuthService.cs +++ b/src/AliasVault.WebApp/Auth/Services/AuthService.cs @@ -16,23 +16,15 @@ using Blazored.LocalStorage; /// This service is responsible for handling authentication-related operations such as refreshing tokens, /// storing tokens, and revoking tokens. /// -public class AuthService +/// +/// Initializes a new instance of the class. +/// +/// The HTTP client. +/// The local storage service. +public class AuthService(HttpClient httpClient, ILocalStorageService localStorage) { private const string AccessTokenKey = "token"; private const string RefreshTokenKey = "refreshToken"; - private readonly HttpClient _httpClient; - private readonly ILocalStorageService _localStorage; - - /// - /// Initializes a new instance of the class. - /// - /// The HTTP client. - /// The local storage service. - public AuthService(HttpClient httpClient, ILocalStorageService localStorage) - { - _httpClient = httpClient; - _localStorage = localStorage; - } /// /// Refreshes the access token asynchronously. @@ -44,14 +36,14 @@ public class AuthService var accessToken = await GetAccessTokenAsync(); var refreshToken = await GetRefreshTokenAsync(); var tokenInput = new TokenModel { Token = accessToken, RefreshToken = refreshToken }; - using var request = new HttpRequestMessage(HttpMethod.Post, "api/Auth/refresh") + using var request = new HttpRequestMessage(HttpMethod.Post, "api/v1/Auth/refresh") { Content = JsonContent.Create(tokenInput), }; // Add the X-Ignore-Failure header to the request so any failure does not trigger another refresh token request. request.Headers.Add("X-Ignore-Failure", "true"); - var response = await _httpClient.SendAsync(request); + var response = await httpClient.SendAsync(request); if (response.IsSuccessStatusCode) { @@ -77,7 +69,7 @@ public class AuthService /// The stored access token. public async Task GetAccessTokenAsync() { - return await _localStorage.GetItemAsStringAsync(AccessTokenKey) ?? string.Empty; + return await localStorage.GetItemAsStringAsync(AccessTokenKey) ?? string.Empty; } /// @@ -87,7 +79,7 @@ public class AuthService /// A representing the asynchronous operation. public async Task StoreAccessTokenAsync(string newToken) { - await _localStorage.SetItemAsStringAsync(AccessTokenKey, newToken); + await localStorage.SetItemAsStringAsync(AccessTokenKey, newToken); } /// @@ -96,7 +88,7 @@ public class AuthService /// The stored refresh token. public async Task GetRefreshTokenAsync() { - return await _localStorage.GetItemAsStringAsync(RefreshTokenKey) ?? string.Empty; + return await localStorage.GetItemAsStringAsync(RefreshTokenKey) ?? string.Empty; } /// @@ -106,7 +98,7 @@ public class AuthService /// A representing the asynchronous operation. public async Task StoreRefreshTokenAsync(string newToken) { - await _localStorage.SetItemAsStringAsync(RefreshTokenKey, newToken); + await localStorage.SetItemAsStringAsync(RefreshTokenKey, newToken); } /// @@ -115,8 +107,8 @@ public class AuthService /// A representing the asynchronous operation. public async Task RemoveTokensAsync() { - await _localStorage.RemoveItemAsync(AccessTokenKey); - await _localStorage.RemoveItemAsync(RefreshTokenKey); + await localStorage.RemoveItemAsync(AccessTokenKey); + await localStorage.RemoveItemAsync(RefreshTokenKey); // If the remote call fails we catch the exception and ignore it. // This is because the user is already logged out and we don't want to trigger another refresh token request. @@ -142,13 +134,13 @@ public class AuthService RefreshToken = await GetRefreshTokenAsync(), }; - using var request = new HttpRequestMessage(HttpMethod.Post, "api/Auth/revoke") + using var request = new HttpRequestMessage(HttpMethod.Post, "api/v1/Auth/revoke") { Content = JsonContent.Create(tokenInput), }; // Add the X-Ignore-Failure header to the request so any failure does not trigger another refresh token request. request.Headers.Add("X-Ignore-Failure", "true"); - await _httpClient.SendAsync(request); + await httpClient.SendAsync(request); } } diff --git a/src/AliasVault.WebApp/Services/AliasService.cs b/src/AliasVault.WebApp/Services/AliasService.cs index 0c4cfec85..a8d892202 100644 --- a/src/AliasVault.WebApp/Services/AliasService.cs +++ b/src/AliasVault.WebApp/Services/AliasService.cs @@ -22,8 +22,8 @@ public class AliasService(HttpClient httpClient) /// Identity object. public async Task GenerateRandomIdentityAsync() { - var identity = await httpClient.GetFromJsonAsync("api/Identity/generate"); - if (identity == null) + var identity = await httpClient.GetFromJsonAsync("api/v1/Identity/generate"); + if (identity is null) { throw new InvalidOperationException("Failed to generate random identity."); } @@ -40,7 +40,7 @@ public class AliasService(HttpClient httpClient) { try { - var returnObject = await httpClient.PutAsJsonAsync("api/Alias", aliasObject); + var returnObject = await httpClient.PutAsJsonAsync("api/v1/Alias", aliasObject); return await returnObject.Content.ReadFromJsonAsync(); } catch @@ -59,7 +59,7 @@ public class AliasService(HttpClient httpClient) { try { - var returnObject = await httpClient.PostAsJsonAsync("api/Alias/" + id, aliasObject); + var returnObject = await httpClient.PostAsJsonAsync("api/v1/Alias/" + id, aliasObject); return await returnObject.Content.ReadFromJsonAsync(); } catch @@ -77,7 +77,7 @@ public class AliasService(HttpClient httpClient) { try { - return await httpClient.GetFromJsonAsync("api/Alias/" + aliasId); + return await httpClient.GetFromJsonAsync("api/v1/Alias/" + aliasId); } catch { @@ -93,7 +93,7 @@ public class AliasService(HttpClient httpClient) { try { - return await httpClient.GetFromJsonAsync>("api/Alias/items"); + return await httpClient.GetFromJsonAsync>("api/v1/Alias/items"); } catch { @@ -111,7 +111,7 @@ public class AliasService(HttpClient httpClient) // Delete from webapi. try { - await httpClient.DeleteAsync("api/Alias/" + id); + await httpClient.DeleteAsync("api/v1/Alias/" + id); } catch {