Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Tweak lock/unlock flow (#233)

Leendert de Borstdb6357a84516
diff --git a/src/AliasVault.Api/Controllers/AuthController.cs b/src/AliasVault.Api/Controllers/AuthController.cs
index c5c4ae3e8..1dd73a7b9 100644
--- a/src/AliasVault.Api/Controllers/AuthController.cs
+++ b/src/AliasVault.Api/Controllers/AuthController.cs
@@ -69,6 +69,17 @@ public class AuthController(IDbContextFactory<AliasServerDbContext> dbContextFac
/// </summary>
private static readonly SemaphoreSlim _semaphore = new(1, 1);
+ /// <summary>
+ /// Status endpoint called by client to check if user is still authenticated.
+ /// </summary>
+ /// <returns>Returns OK if valid authentication is provided, otherwise it will return 401 unauthorized.</returns>
+ [Authorize]
+ [HttpGet("status")]
+ public IActionResult Status()
+ {
+ return Ok();
+ }
+
/// <summary>
/// Login endpoint used to process login attempt using credentials.
/// </summary>
diff --git a/src/AliasVault.Client/Auth/Layout/MainLayout.razor b/src/AliasVault.Client/Auth/Layout/MainLayout.razor
index 0856eb1f7..ee6fbe510 100644
--- a/src/AliasVault.Client/Auth/Layout/MainLayout.razor
+++ b/src/AliasVault.Client/Auth/Layout/MainLayout.razor
@@ -4,6 +4,7 @@
<div class="flex flex-col items-center justify-center px-6 pt-8 mx-auto md:h-screen pt:mt-0 dark:bg-gray-900">
<Logo />
<div class="w-full max-w-xl p-6 space-y-4 sm:p-8 bg-white rounded-lg shadow dark:bg-gray-800">
+ <GlobalNotificationDisplay />
@Body
</div>
</div>
diff --git a/src/AliasVault.Client/Auth/Pages/Unlock.razor b/src/AliasVault.Client/Auth/Pages/Unlock.razor
index 0d0b3f06e..7c5bafba0 100644
--- a/src/AliasVault.Client/Auth/Pages/Unlock.razor
+++ b/src/AliasVault.Client/Auth/Pages/Unlock.razor
@@ -9,7 +9,7 @@
<div class="flex space-x-4">
<img class="w-8 h-8 rounded-full" src="/img/avatar.webp" alt="Bonnie image">
- <h2 class="mb-3 text-2xl font-bold text-gray-900 dark:text-white">@Email</h2>
+ <h2 class="mb-3 text-2xl font-bold text-gray-900 dark:text-white">@Username</h2>
</div>
<p class="text-base font-normal text-gray-500 dark:text-gray-400">
@@ -19,7 +19,7 @@
<FullScreenLoadingIndicator @ref="LoadingIndicator" />
<ServerValidationErrors @ref="ServerValidationErrors" />
-<EditForm Model="UnlockModel" OnValidSubmit="HandleLogin" class="mt-8 space-y-6">
+<EditForm Model="UnlockModel" OnValidSubmit="UnlockSubmit" class="mt-8 space-y-6">
<DataAnnotationsValidator/>
<div>
<label asp-for="Input.Password" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Your password</label>
@@ -38,7 +38,7 @@
</EditForm>
@code {
- private string? Email { get; set; }
+ private string? Username { get; set; }
private readonly UnlockModel UnlockModel = new();
private FullScreenLoadingIndicator LoadingIndicator = new();
private ServerValidationErrors ServerValidationErrors = new();
@@ -46,40 +46,28 @@
/// <inheritdoc />
protected override async Task OnInitializedAsync()
{
- await AuthStateProvider.GetAuthenticationStateAsync();
- var authState = await AuthStateProvider.GetAuthenticationStateAsync();
- if (authState.User.Identity?.IsAuthenticated == false) {
- // Not authenticated (anymore), redirect to login page.
- NavigationManager.NavigateTo("/user/login");
- }
+ // Do not await this call, as it is only to check if the user is still authenticated
+ // and does not need to block the page from rendering.
+ _ = Http.GetAsync("api/v1/Auth/status");
- var email = authState.User.Identity?.Name;
- if (email is null)
- {
- // Clear all tokens and redirect to login page.
- await AuthService.RemoveTokensAsync();
- await AuthStateProvider.GetAuthenticationStateAsync();
- GlobalNotificationService.ClearMessages();
- NavigationManager.NavigateTo("/user/login");
- }
-
- Email = email;
+ await StatusCheck();
}
- private async Task HandleLogin()
+ /// <summary>
+ /// Execute the unlock form submission.
+ /// </summary>
+ private async Task UnlockSubmit()
{
- if (Email == null)
- {
- return;
- }
-
LoadingIndicator.Show();
ServerValidationErrors.Clear();
try
{
+ await StatusCheck();
+ await Http.GetAsync("api/v1/Auth/status");
+
// Send request to server with email to get user salt.
- var result = await Http.PostAsJsonAsync("api/v1/Auth/login", new LoginInitiateRequest(Email));
+ var result = await Http.PostAsJsonAsync("api/v1/Auth/login", new LoginInitiateRequest(Username!));
var responseContent = await result.Content.ReadAsStringAsync();
if (!result.IsSuccessStatusCode)
@@ -143,4 +131,45 @@
LoadingIndicator.Hide();
}
}
+
+ /// <summary>
+ /// Make a request to the server to check if access token is still valid.
+ /// If not, then this call will automatically result in redirect to the login page.
+ /// </summary>
+ private async Task StatusCheck()
+ {
+ await AuthStateProvider.GetAuthenticationStateAsync();
+ var authState = await AuthStateProvider.GetAuthenticationStateAsync();
+ if (authState.User.Identity?.IsAuthenticated == false) {
+ // Not authenticated (anymore), redirect to login page.
+ GlobalNotificationService.AddErrorMessage("Your session has timed out. Please log in again.");
+ NavigationManager.NavigateTo("/user/login");
+ return;
+ }
+
+ // Check if encryption key test string is available. If not
+ // user should login again.
+ if (!await AuthService.HasEncryptionKeyTestStringAsync())
+ {
+ // Clear all tokens and redirect to login page.
+ await AuthService.RemoveTokensAsync();
+ GlobalNotificationService.ClearMessages();
+ GlobalNotificationService.AddErrorMessage("Your session has timed out. Please log in again.");
+ NavigationManager.NavigateTo("/user/login");
+ return;
+ }
+
+ // Check if username is set.
+ // If not, redirect to login page.
+ Username = authState.User.Identity?.Name;
+ if (Username is null)
+ {
+ // Clear all tokens and redirect to login page.
+ await AuthService.RemoveTokensAsync();
+ await AuthStateProvider.GetAuthenticationStateAsync();
+ GlobalNotificationService.ClearMessages();
+ GlobalNotificationService.AddErrorMessage("Your session has timed out. Please log in again.");
+ NavigationManager.NavigateTo("/user/login");
+ }
+ }
}
diff --git a/src/AliasVault.Client/Main/Pages/MainBase.cs b/src/AliasVault.Client/Main/Pages/MainBase.cs
index 2f932bbee..4dcff94ab 100644
--- a/src/AliasVault.Client/Main/Pages/MainBase.cs
+++ b/src/AliasVault.Client/Main/Pages/MainBase.cs
@@ -12,7 +12,6 @@ using AliasVault.Client.Services.Auth;
using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
-using Microsoft.JSInterop;
/// <summary>
/// Base authorized page that all pages that are part of the logged in website should inherit from.
diff --git a/src/AliasVault.Client/Main/Pages/Test/Test3.razor b/src/AliasVault.Client/Main/Pages/Test/Test3.razor
index 6ec46b4b8..d4ce0b3fe 100644
--- a/src/AliasVault.Client/Main/Pages/Test/Test3.razor
+++ b/src/AliasVault.Client/Main/Pages/Test/Test3.razor
@@ -7,6 +7,7 @@
@code {
private int SoftDeletedCredentialCount { get; set; }
+ /// <inheritdoc />
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
@@ -14,4 +15,4 @@
var context = await DbService.GetDbContextAsync();
SoftDeletedCredentialCount = await context.Credentials.CountAsync(c => c.IsDeleted);
}
-}
\ No newline at end of file
+}
diff --git a/src/AliasVault.Client/Services/Auth/AliasVaultApiHandlerService.cs b/src/AliasVault.Client/Services/Auth/AliasVaultApiHandlerService.cs
index 620184aa8..a835d7f72 100644
--- a/src/AliasVault.Client/Services/Auth/AliasVaultApiHandlerService.cs
+++ b/src/AliasVault.Client/Services/Auth/AliasVaultApiHandlerService.cs
@@ -61,6 +61,8 @@ public sealed class AliasVaultApiHandlerService(IServiceProvider serviceProvider
// Remove the token from local storage and redirect to the login page.
await authService.RemoveTokensAsync();
+ var globalNotificationService = serviceProvider.GetRequiredService<GlobalNotificationService>();
+ globalNotificationService.AddErrorMessage("Your session has expired. Please log in again.");
var navigationManager = serviceProvider.GetRequiredService<NavigationManager>();
navigationManager.NavigateTo("/user/login");
}
diff --git a/src/AliasVault.Client/Services/Auth/AuthService.cs b/src/AliasVault.Client/Services/Auth/AuthService.cs
index 9e461fbf1..7c505c36f 100644
--- a/src/AliasVault.Client/Services/Auth/AuthService.cs
+++ b/src/AliasVault.Client/Services/Auth/AuthService.cs
@@ -160,6 +160,17 @@ public sealed class AuthService(HttpClient httpClient, ILocalStorageService loca
await localStorage.SetItemAsStringAsync("encryptionTestString", encryptedTestString);
}
+ /// <summary>
+ /// Check if the encryption test string is stored in local storage which is used to validate
+ /// the encryption key locally during future vault unlocks. If its not stored the unlock
+ /// attempts will fail and user should login again instead.
+ /// </summary>
+ /// <returns>Task.</returns>
+ public async Task<bool> HasEncryptionKeyTestStringAsync()
+ {
+ return await localStorage.GetItemAsStringAsync("encryptionTestString") != null;
+ }
+
/// <summary>
/// Validate the encryption locally by attempting to decrypt test string stored in local storage.
/// </summary>