Skip to content

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

Add encryption key check to generic PageBase (#60)

Leendert de Borst2a834eeb387f
diff --git a/src/AliasVault.WebApp/Auth/Pages/Unlock.razor b/src/AliasVault.WebApp/Auth/Pages/Unlock.razor
index 34ab03a28..41f26bda9 100644
--- a/src/AliasVault.WebApp/Auth/Pages/Unlock.razor
+++ b/src/AliasVault.WebApp/Auth/Pages/Unlock.razor
@@ -6,6 +6,7 @@
@inject AuthService AuthService
@inject GlobalNotificationService GlobalNotificationService
@inject AliasClientDbService AliasClientDbService
+@inject ILocalStorageService LocalStorage
@using System.Text.Json
@using AliasVault.Shared.Models
@using AliasVault.Shared.Models.WebApi.Auth
@@ -149,7 +150,18 @@
// Redirect to home page.
await AuthStateProvider.GetAuthenticationStateAsync();
GlobalNotificationService.ClearMessages();
- NavigationManager.NavigateTo("/");
+
+ // Redirect to the page the user was trying to access before if set.
+ var localStorageReturnUrl = await LocalStorage.GetItemAsync<string>("returnUrl");
+ if (!string.IsNullOrEmpty(localStorageReturnUrl))
+ {
+ await LocalStorage.RemoveItemAsync("returnUrl");
+ NavigationManager.NavigateTo(localStorageReturnUrl);
+ }
+ else
+ {
+ NavigationManager.NavigateTo("/");
+ }
}
#if DEBUG
catch (Exception ex)
diff --git a/src/AliasVault.WebApp/Pages/Base/PageBase.cs b/src/AliasVault.WebApp/Pages/Base/PageBase.cs
index 506d74e65..64b0c5b39 100644
--- a/src/AliasVault.WebApp/Pages/Base/PageBase.cs
+++ b/src/AliasVault.WebApp/Pages/Base/PageBase.cs
@@ -7,8 +7,10 @@
namespace AliasVault.WebApp.Pages.Base;
+using AliasVault.WebApp.Auth.Services;
using AliasVault.WebApp.Components.Models;
using AliasVault.WebApp.Services;
+using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.JSInterop;
@@ -52,6 +54,18 @@ public class PageBase : OwningComponentBase
[Inject]
public AliasClientDbService AliasClientDbService { get; set; } = null!;
+ /// <summary>
+ /// Gets or sets the AuthService.
+ /// </summary>
+ [Inject]
+ public AuthService AuthService { get; set; } = null!;
+
+ /// <summary>
+ /// Gets or sets the LocalStorage.
+ /// </summary>
+ [Inject]
+ public ILocalStorageService LocalStorage { get; set; } = null!;
+
/// <summary>
/// Gets or sets the breadcrumb items for the page. A default set of breadcrumbs is added in the parent OnInitialized method.
/// </summary>
@@ -68,6 +82,15 @@ public class PageBase : OwningComponentBase
// Add base breadcrumbs
BreadcrumbItems.Add(new BreadcrumbItem { DisplayName = "Home", Url = NavigationManager.BaseUri });
+
+ await RedirectIfNoEncryptionKey();
+ }
+
+ /// <inheritdoc />
+ protected override async Task OnAfterRenderAsync(bool firstRender)
+ {
+ await base.OnAfterRenderAsync(firstRender);
+ await RedirectIfNoEncryptionKey();
}
/// <summary>
@@ -94,4 +117,22 @@ public class PageBase : OwningComponentBase
_parametersInitialSet = true;
}
}
+
+ /// <summary>
+ /// Checks if the encryption key is set. If not, redirect to the unlock screen
+ /// where the user can re-enter the master password so the encryption key gets refreshed.
+ ///
+ /// This method should be called on every authenticated page load.
+ /// </summary>
+ private async Task RedirectIfNoEncryptionKey()
+ {
+ // Sanity check: check that encryption key is set. If not, redirect to lock screen.
+ if (string.IsNullOrEmpty(AuthService.GetEncryptionKeyAsBase64Async()) ||
+ AuthService.GetEncryptionKeyAsBase64Async() == "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")
+ {
+ var currentUrl = NavigationManager.Uri;
+ await LocalStorage.SetItemAsync("returnUrl", currentUrl);
+ NavigationManager.NavigateTo("/unlock");
+ }
+ }
}
diff --git a/src/AliasVault.WebApp/Pages/Dbtest.razor b/src/AliasVault.WebApp/Pages/Dbtest.razor
index 09be0261b..0ea294b66 100644
--- a/src/AliasVault.WebApp/Pages/Dbtest.razor
+++ b/src/AliasVault.WebApp/Pages/Dbtest.razor
@@ -44,6 +44,8 @@
/// <inheritdoc />
protected override async Task OnAfterRenderAsync(bool firstRender)
{
+ await base.OnAfterRenderAsync(firstRender);
+
if (firstRender)
{
await LoadAliasFromDbAsync();
diff --git a/src/AliasVault.WebApp/Services/AliasClientDbService.cs b/src/AliasVault.WebApp/Services/AliasClientDbService.cs
index 47902ea0b..7dfd57df9 100644
--- a/src/AliasVault.WebApp/Services/AliasClientDbService.cs
+++ b/src/AliasVault.WebApp/Services/AliasClientDbService.cs
@@ -232,14 +232,6 @@ public class AliasClientDbService
/// <returns>Task.</returns>
private async Task<bool> LoadDatabaseFromServerAsync()
{
- // Sanity check: check that encryption key is set. If not, redirect to lock screen.
- if (string.IsNullOrEmpty(_authService.GetEncryptionKeyAsBase64Async()) ||
- _authService.GetEncryptionKeyAsBase64Async() == "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")
- {
- _navigationManager.NavigateTo("/unlock");
- return false;
- }
-
// Load from webapi.
try
{