Skip to content

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

Abstract Login and Unlock duplicated logic (#63)

Leendert de Borst8243213028fb
diff --git a/src/AliasVault.WebApp/Auth/Pages/Base/LoginBase.cs b/src/AliasVault.WebApp/Auth/Pages/Base/LoginBase.cs
new file mode 100644
index 000000000..4c75e65e8
--- /dev/null
+++ b/src/AliasVault.WebApp/Auth/Pages/Base/LoginBase.cs
@@ -0,0 +1,149 @@
+//-----------------------------------------------------------------------
+// <copyright file="LoginBase.cs" company="lanedirt">
+// Copyright (c) lanedirt. All rights reserved.
+// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace AliasVault.WebApp.Auth.Pages.Base;
+
+using System.Net.Http.Json;
+using System.Text.Json;
+using AliasVault.Shared.Models.WebApi.Auth;
+using AliasVault.WebApp.Auth.Services;
+using AliasVault.WebApp.Components;
+using AliasVault.WebApp.Services;
+using AliasVault.WebApp.Services.Database;
+using Blazored.LocalStorage;
+using Cryptography;
+using Microsoft.AspNetCore.Components;
+using Microsoft.AspNetCore.Components.Authorization;
+using Microsoft.JSInterop;
+
+/// <summary>
+/// Base authorize page that all pages that are part of the logged in website should inherit from.
+/// All pages that inherit from this class will require the user to be logged in and have a confirmed email.
+/// Also, a default set of breadcrumbs is added in the parent OnInitialized method.
+/// </summary>
+public class LoginBase : OwningComponentBase
+{
+ /// <summary>
+ /// Gets or sets the NavigationManager.
+ /// </summary>
+ [Inject]
+ public NavigationManager NavigationManager { get; set; } = null!;
+
+ /// <summary>
+ /// Gets or sets the HttpClient.
+ /// </summary>
+ [Inject]
+ public HttpClient Http { get; set; } = null!;
+
+ /// <summary>
+ /// Gets or sets the AuthenticationStateProvider.
+ /// </summary>
+ [Inject]
+ public AuthenticationStateProvider AuthStateProvider { get; set; } = null!;
+
+ /// <summary>
+ /// Gets or sets the GlobalNotificationService.
+ /// </summary>
+ [Inject]
+ public GlobalNotificationService GlobalNotificationService { get; set; } = null!;
+
+ /// <summary>
+ /// Gets or sets the IJSRuntime.
+ /// </summary>
+ [Inject]
+ public IJSRuntime Js { get; set; } = null!;
+
+ /// <summary>
+ /// Gets or sets the DbService.
+ /// </summary>
+ [Inject]
+ public DbService DbService { 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 the username from the authentication state asynchronously.
+ /// </summary>
+ /// <param name="email">Email address.</param>
+ /// <param name="password">Password.</param>
+ /// <param name="serverValidationErrors">ServerValidationErrors Blazor component reference.</param>
+ /// <returns>The username.</returns>
+ protected async Task ProcessLoginAsync(string email, string password, ServerValidationErrors serverValidationErrors)
+ {
+ // Send request to server with email to get server ephemeral public key.
+ var result = await Http.PostAsJsonAsync("api/v1/Auth/login", new LoginRequest(email));
+ var responseContent = await result.Content.ReadAsStringAsync();
+
+ if (!result.IsSuccessStatusCode)
+ {
+ serverValidationErrors.ParseResponse(responseContent);
+ return;
+ }
+
+ var loginResponse = JsonSerializer.Deserialize<LoginResponse>(responseContent);
+ if (loginResponse == null)
+ {
+ serverValidationErrors.AddError("An error occurred while processing the login request.");
+ return;
+ }
+
+ // 3. Client derives shared session key.
+ byte[] passwordHash = await Encryption.DeriveKeyFromPasswordAsync(password, loginResponse.Salt);
+ var passwordHashString = BitConverter.ToString(passwordHash).Replace("-", string.Empty);
+
+ var clientEphemeral = Srp.GenerateEphemeralClient();
+ var privateKey = Srp.DerivePrivateKey(loginResponse.Salt, email, passwordHashString);
+ var clientSession = Srp.DeriveSessionClient(
+ privateKey,
+ clientEphemeral.Secret,
+ loginResponse.ServerEphemeral,
+ loginResponse.Salt,
+ email);
+
+ // 4. Client sends proof of session key to server.
+ result = await Http.PostAsJsonAsync("api/v1/Auth/validate", new ValidateLoginRequest(email, clientEphemeral.Public, clientSession.Proof));
+ responseContent = await result.Content.ReadAsStringAsync();
+
+ if (!result.IsSuccessStatusCode)
+ {
+ serverValidationErrors.ParseResponse(responseContent);
+ return;
+ }
+
+ var validateLoginResponse = JsonSerializer.Deserialize<ValidateLoginResponse>(responseContent);
+ if (validateLoginResponse == null)
+ {
+ serverValidationErrors.AddError("An error occurred while processing the login request.");
+ return;
+ }
+
+ // 5. Client verifies proof.
+ Srp.VerifySession(clientEphemeral.Public, clientSession, validateLoginResponse.ServerSessionProof);
+
+ // Store the tokens in local storage.
+ await AuthService.StoreAccessTokenAsync(validateLoginResponse.Token.Token);
+ await AuthService.StoreRefreshTokenAsync(validateLoginResponse.Token.RefreshToken);
+
+ // Store the encryption key in memory.
+ AuthService.StoreEncryptionKey(passwordHash);
+
+ // Redirect to home page.
+ await AuthStateProvider.GetAuthenticationStateAsync();
+ GlobalNotificationService.ClearMessages();
+ NavigationManager.NavigateTo("/");
+ }
+}
diff --git a/src/AliasVault.WebApp/Auth/Pages/Login.razor b/src/AliasVault.WebApp/Auth/Pages/Login.razor
index 63424f1cd..81a952e33 100644
--- a/src/AliasVault.WebApp/Auth/Pages/Login.razor
+++ b/src/AliasVault.WebApp/Auth/Pages/Login.razor
@@ -1,17 +1,9 @@
@page "/user/login"
-@attribute [AllowAnonymous]
+@inherits AliasVault.WebApp.Auth.Pages.Base.LoginBase
@layout Auth.Layout.MainLayout
-@inject HttpClient Http
-@inject AuthenticationStateProvider AuthStateProvider
-@inject NavigationManager NavigationManager
-@inject AuthService AuthService
-@inject GlobalNotificationService GlobalNotificationService
-@using System.Text.Json
+@attribute [AllowAnonymous]
@using AliasVault.Shared.Models
-@using AliasVault.Shared.Models.WebApi.Auth
@using AliasVault.WebApp.Auth.Components
-@using AliasVault.WebApp.Auth.Services
-@using Cryptography
<h2 class="text-2xl font-bold text-gray-900 dark:text-white">
Sign in to AliasVault
@@ -72,69 +64,7 @@
try
{
- // Send request to server with email to get server ephemeral public key.
- var result = await Http.PostAsJsonAsync("api/v1/Auth/login", new LoginRequest(_loginModel.Email));
- var responseContent = await result.Content.ReadAsStringAsync();
-
- if (!result.IsSuccessStatusCode)
- {
- _serverValidationErrors.ParseResponse(responseContent);
- StateHasChanged();
- return;
- }
-
- var loginResponse = JsonSerializer.Deserialize<LoginResponse>(responseContent);
- if (loginResponse == null)
- {
- _serverValidationErrors.AddError("An error occurred while processing the login request.");
- return;
- }
-
- // 3. Client derives shared session key.
- byte[] passwordHash = await Encryption.DeriveKeyFromPasswordAsync(_loginModel.Password, loginResponse.Salt);
- var passwordHashString = BitConverter.ToString(passwordHash).Replace("-", string.Empty);
-
- var clientEphemeral = Srp.GenerateEphemeralClient();
- var privateKey = Srp.DerivePrivateKey(loginResponse.Salt, _loginModel.Email, passwordHashString);
- var clientSession = Srp.DeriveSessionClient(
- privateKey,
- clientEphemeral.Secret,
- loginResponse.ServerEphemeral,
- loginResponse.Salt,
- _loginModel.Email);
-
- // 4. Client sends proof of session key to server.
- result = await Http.PostAsJsonAsync("api/v1/Auth/validate", new ValidateLoginRequest(_loginModel.Email, clientEphemeral.Public, clientSession.Proof));
- responseContent = await result.Content.ReadAsStringAsync();
-
- if (!result.IsSuccessStatusCode)
- {
- _serverValidationErrors.ParseResponse(responseContent);
- StateHasChanged();
- return;
- }
-
- var validateLoginResponse = JsonSerializer.Deserialize<ValidateLoginResponse>(responseContent);
- if (validateLoginResponse == null)
- {
- _serverValidationErrors.AddError("An error occurred while processing the login request.");
- return;
- }
-
- // 5. Client verifies proof.
- Srp.VerifySession(clientEphemeral.Public, clientSession, validateLoginResponse.ServerSessionProof);
-
- // Store the tokens in local storage.
- await AuthService.StoreAccessTokenAsync(validateLoginResponse.Token.Token);
- await AuthService.StoreRefreshTokenAsync(validateLoginResponse.Token.RefreshToken);
-
- // Store the encryption key in memory.
- AuthService.StoreEncryptionKey(passwordHash);
-
- // Redirect to home page.
- await AuthStateProvider.GetAuthenticationStateAsync();
- GlobalNotificationService.ClearMessages();
- NavigationManager.NavigateTo("/");
+ await ProcessLoginAsync(_loginModel.Email, _loginModel.Password, _serverValidationErrors);
}
#if DEBUG
catch (Exception ex)
diff --git a/src/AliasVault.WebApp/Auth/Pages/Unlock.razor b/src/AliasVault.WebApp/Auth/Pages/Unlock.razor
index 84eb269fb..85bb4e50a 100644
--- a/src/AliasVault.WebApp/Auth/Pages/Unlock.razor
+++ b/src/AliasVault.WebApp/Auth/Pages/Unlock.razor
@@ -1,18 +1,8 @@
@page "/unlock"
+@inherits AliasVault.WebApp.Auth.Pages.Base.LoginBase
@layout Auth.Layout.MainLayout
-@inject HttpClient Http
-@inject AuthenticationStateProvider AuthStateProvider
-@inject NavigationManager NavigationManager
-@inject AuthService AuthService
-@inject GlobalNotificationService GlobalNotificationService
-@inject DbService DbService
-@inject ILocalStorageService LocalStorage
-@using System.Text.Json
@using AliasVault.Shared.Models
-@using AliasVault.Shared.Models.WebApi.Auth
@using AliasVault.WebApp.Auth.Components
-@using AliasVault.WebApp.Auth.Services
-@using Cryptography
<div class="flex space-x-4">
<img class="w-8 h-8 rounded-full" src="/img/avatar.webp" alt="Bonnie image">
@@ -70,7 +60,7 @@
NavigationManager.NavigateTo("/user/login");
}
- Email = authState.User.Identity?.Name!;
+ Email = email;
}
private async Task HandleLogin()
@@ -85,83 +75,8 @@
try
{
- // Send request to server with email to get server ephemeral public key.
- var result = await Http.PostAsJsonAsync("api/v1/Auth/login", new LoginRequest(Email));
- var responseContent = await result.Content.ReadAsStringAsync();
-
- if (!result.IsSuccessStatusCode)
- {
- _serverValidationErrors.ParseResponse(responseContent);
- StateHasChanged();
- return;
- }
-
- var loginResponse = JsonSerializer.Deserialize<LoginResponse>(responseContent);
- if (loginResponse == null)
- {
- _serverValidationErrors.AddError("An error occurred while processing the login request.");
- return;
- }
-
- // 3. Client derives shared session key.
- byte[] passwordHash = await Encryption.DeriveKeyFromPasswordAsync(_unlockModel.Password, loginResponse.Salt);
- var passwordHashString = BitConverter.ToString(passwordHash).Replace("-", string.Empty);
-
- var clientEphemeral = Srp.GenerateEphemeralClient();
- var privateKey = Srp.DerivePrivateKey(loginResponse.Salt, Email, passwordHashString);
- var clientSession = Srp.DeriveSessionClient(
- privateKey,
- clientEphemeral.Secret,
- loginResponse.ServerEphemeral,
- loginResponse.Salt,
- Email);
-
- // 4. Client sends proof of session key to server.
- result = await Http.PostAsJsonAsync("api/v1/Auth/validate", new ValidateLoginRequest(Email, clientEphemeral.Public, clientSession.Proof));
- responseContent = await result.Content.ReadAsStringAsync();
-
- if (!result.IsSuccessStatusCode)
- {
- _serverValidationErrors.ParseResponse(responseContent);
- StateHasChanged();
- return;
- }
-
- var validateLoginResponse = JsonSerializer.Deserialize<ValidateLoginResponse>(responseContent);
- if (validateLoginResponse == null)
- {
- _serverValidationErrors.AddError("An error occurred while processing the login request.");
- return;
- }
-
- // 5. Client verifies proof.
- Srp.VerifySession(clientEphemeral.Public, clientSession, validateLoginResponse.ServerSessionProof);
-
- // Store the changed access/refresh tokens in local storage.
- await AuthService.StoreAccessTokenAsync(validateLoginResponse.Token.Token);
- await AuthService.StoreRefreshTokenAsync(validateLoginResponse.Token.RefreshToken);
-
- // Store the encryption key in memory.
- AuthService.StoreEncryptionKey(passwordHash);
-
- // Try to retrieve vault again from remote.
- await DbService.GetDbContextAsync();
-
- // Redirect to home page.
- await AuthStateProvider.GetAuthenticationStateAsync();
- GlobalNotificationService.ClearMessages();
-
- // 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("/");
- }
+ await ProcessLoginAsync(Email, _unlockModel.Password, _serverValidationErrors);
+ StateHasChanged();
}
#if DEBUG
catch (Exception ex)