Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add E2E test for 2-FA recovery codes (#70)
Leendert de Borstf6f33c2482ee
diff --git a/src/AliasVault.Client/Auth/Pages/Login.razor b/src/AliasVault.Client/Auth/Pages/Login.razorindex cc64dc72f..988c93566 100644--- a/src/AliasVault.Client/Auth/Pages/Login.razor+++ b/src/AliasVault.Client/Auth/Pages/Login.razor@@ -60,7 +60,7 @@ else if (ShowLoginWithRecoveryCodeStep)<ValidationSummary class="text-red-600 dark:text-red-400" role="alert"/><div><label for="two-factor-code" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Recovery Code</label>- <InputText @bind-Value="LoginModelRecoveryCode.RecoveryCode" id="two-factor-code" class="bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" autocomplete="off"/>+ <InputText @bind-Value="LoginModelRecoveryCode.RecoveryCode" id="recovery-code" class="bg-gray-50 border border-gray-300 text-gray-900 sm:text-sm rounded-lg focus:ring-primary-600 focus:border-primary-600 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" autocomplete="off"/><ValidationMessage For="() => LoginModelRecoveryCode.RecoveryCode" class="text-red-600 dark:text-red-400 text-sm mt-1"/></div><button type="submit" class="w-full text-white bg-primary-600 hover:bg-primary-700 focus:ring-4 focus:outline-none focus:ring-primary-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-primary-600 dark:hover:bg-primary-700 dark:focus:ring-primary-800">Log in</button>@@ -361,7 +361,8 @@ elseprivate async Task<List<string>> ProcessLoginVerify(ValidateLoginResponse validateLoginResponse){- // If not, then continue with login as normal and verify the proof.+ Console.WriteLine("ProcessLoginVerify called...");+// 5. Client verifies proof.Srp.VerifySession(ClientEphemeral.Public, ClientSession, validateLoginResponse.ServerSessionProof);diff --git a/src/AliasVault.Client/Auth/Pages/Logout.razor b/src/AliasVault.Client/Auth/Pages/Logout.razorindex de5162454..20794d50f 100644--- a/src/AliasVault.Client/Auth/Pages/Logout.razor+++ b/src/AliasVault.Client/Auth/Pages/Logout.razor@@ -13,7 +13,9 @@{await base.OnInitializedAsync();await AuthService.RemoveTokensAsync();+ AuthService.RemoveEncryptionKey();await AuthStateProvider.GetAuthenticationStateAsync();+// Initialize a new empty database to clear all data.DbService.InitializeEmptyDatabase();GlobalNotificationService.ClearMessages();diff --git a/src/AliasVault.Client/Main/Pages/Settings/Security/Components/ShowRecoveryCodes.razor b/src/AliasVault.Client/Main/Pages/Settings/Security/Components/ShowRecoveryCodes.razorindex 230a91bca..259423783 100644--- a/src/AliasVault.Client/Main/Pages/Settings/Security/Components/ShowRecoveryCodes.razor+++ b/src/AliasVault.Client/Main/Pages/Settings/Security/Components/ShowRecoveryCodes.razor@@ -4,7 +4,7 @@The recovery codes below are used to access your account in case you lose access to your authenticator device.Make a photo or write them down and store them in a secure location. Do not share them with anyone.</div>- <div class="bg-primary-100 border-l-4 border-primary-500 text-primary-700 p-4 mb-4" role="alert">+ <div class="bg-primary-100 border-l-4 border-primary-500 text-primary-700 p-4 mb-4"><p class="font-semibold">Store these recovery codes in a safe place.</p>@@ -14,12 +14,12 @@</p></div>- <div class="grid grid-cols-1">+ <div class="grid grid-cols-1" id="recovery-codes">@foreach (var recoveryCode in RecoveryCodes){- <div>- <code class="block p-2">@recoveryCode</code>- </div>+ <div>+ <code class="block p-2">@recoveryCode</code>+ </div>}</div></div>diff --git a/src/AliasVault.Client/Services/Auth/AuthService.cs b/src/AliasVault.Client/Services/Auth/AuthService.csindex 4b8e82738..53748e7df 100644--- a/src/AliasVault.Client/Services/Auth/AuthService.cs+++ b/src/AliasVault.Client/Services/Auth/AuthService.cs@@ -175,6 +175,14 @@ public sealed class AuthService(HttpClient httpClient, ILocalStorageService locaawait localStorage.RemoveItemAsync(RefreshTokenKey);}+ /// <summary>+ /// Removes the encryption key from memory, called during logout.+ /// </summary>+ public void RemoveEncryptionKey()+ {+ _encryptionKey = new byte[32];+ }+/// <summary>/// Revokes the access and refresh tokens on the server asynchronously./// </summary>diff --git a/src/AliasVault.Client/Services/Database/DbService.cs b/src/AliasVault.Client/Services/Database/DbService.csindex 9e3aa05eb..5bdfa4f75 100644--- a/src/AliasVault.Client/Services/Database/DbService.cs+++ b/src/AliasVault.Client/Services/Database/DbService.cs@@ -88,10 +88,6 @@ public sealed class DbService : IDisposable{_retryCount = 0;}- else- {- Console.WriteLine("Failed to load database from server.");- }}/// <summary>diff --git a/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.cs b/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.csindex ac226eb9e..f06912f34 100644--- a/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.cs+++ b/src/Tests/AliasVault.E2ETests/Common/PlaywrightTest.cs@@ -164,7 +164,7 @@ public abstract class PlaywrightTest.First.WaitForAsync(new LocatorWaitForOptions{- Timeout = TestDefaults.DefaultTimeout,+ Timeout = 300000,State = WaitForSelectorState.Attached,});}@@ -191,7 +191,7 @@ public abstract class PlaywrightTest}/// <summary>- /// Setup the Playwright test environment. This method is required to be implemented by the derived class.+ /// Setup the Playwright test environment. This method is required to be implemented by the derived class./// </summary>/// <returns>Async task.</returns>protected abstract Task SetupEnvironment();diff --git a/src/Tests/AliasVault.E2ETests/Tests/Client/TwoFactorAuthTests.cs b/src/Tests/AliasVault.E2ETests/Tests/Client/TwoFactorAuthTests.csindex fcdd3fc92..bdc9f8945 100644--- a/src/Tests/AliasVault.E2ETests/Tests/Client/TwoFactorAuthTests.cs+++ b/src/Tests/AliasVault.E2ETests/Tests/Client/TwoFactorAuthTests.cs@@ -16,13 +16,15 @@ namespace AliasVault.E2ETests.Tests.Client;public class TwoFactorAuthTests : ClientPlaywrightTest{/// <summary>- /// Test if setting up two factor authentication and then logging in works.+ /// Test if setting up two-factor authentication and then logging in works./// </summary>/// <returns>Async task.</returns>[Test]public async Task Setup2FactorAuthAndLogin(){- // Setup two factor auth.+ await DisableTwoFactorIfEnabled();++ // Setup two-factor auth.await NavigateUsingBlazorRouter("settings/security");await WaitForUrlAsync("settings/security", "Two-factor Authentication");@@ -97,4 +99,133 @@ public class TwoFactorAuthTests : ClientPlaywrightTest// Check if we get redirected to the index page.await WaitForUrlAsync(AppBaseUrl, "Welcome to AliasVault");}++ /// <summary>+ /// Test if setting up two-factor authentication and then using a recovery code to login works.+ /// </summary>+ /// <returns>Async task.</returns>+ [Test]+ public async Task Setup2FactorAuthAndLoginWithRecoveryCode()+ {+ await DisableTwoFactorIfEnabled();++ // Setup two-factor auth.+ await NavigateUsingBlazorRouter("settings/security");+ await WaitForUrlAsync("settings/security", "Two-factor Authentication");++ // Press the enable button with text "Enable Two-Factor Authentication"+ var enableButton = Page.Locator("button:has-text('Enable Two-Factor Authentication')");+ await enableButton.ClickAsync();++ // Wait for the secret to be displayed with ID "authenticator-secret"+ var secretElement = Page.Locator("div[id='authenticator-secret']");+ await secretElement.WaitForAsync(new LocatorWaitForOptions+ {+ State = WaitForSelectorState.Visible,+ Timeout = 5000,+ });++ var secret = await Page.TextContentAsync("div[id='authenticator-secret']");+ Assert.That(secret, Is.Not.Null.Or.Empty, "No secret displayed.");++ // Verify that a QR code image is displayed: #qrcode-authenticator-uri > img+ var qrCode = await Page.Locator("#qrcode-authenticator-uri img").GetAttributeAsync("src");+ Assert.That(qrCode, Does.Contain("data:image/png;base64"), "No QR code displayed.");++ // Generate a 2FA code using the secret.+ var totpCode = TotpGenerator.TotpGenerator.GenerateTotpCode(secret);+ Assert.That(totpCode, Is.Not.Null.Or.Empty, "No 2FA code generated.");++ // Fill in the 2FA code and submit the form.+ var totpField = Page.Locator("input[id='verificationCode']");+ await totpField.FillAsync(totpCode);++ var submitButton = Page.Locator("button:has-text('Verify and Enable')");+ await submitButton.ClickAsync();++ // Check if the recovery codes are displayed by id "recovery-codes"+ // wait for the recovery codes to be displayed.+ var recoveryCodesElement = Page.Locator("div[id='recovery-codes']");+ await recoveryCodesElement.WaitForAsync(new LocatorWaitForOptions+ {+ State = WaitForSelectorState.Visible,+ Timeout = 5000,+ });++ // Get the first recovery code.+ var recoveryCode = await Page.TextContentAsync("div[id='recovery-codes'] div:first-child");+ Assert.That(recoveryCode, Is.Not.Null.Or.Empty, "Recovery codes displayed but first value could not be extracted.");++ // Logout.+ await NavigateUsingBlazorRouter("user/logout");+ await WaitForUrlAsync("user/login", "AliasVault");++ // Attempt to login again with test credentials.+ var emailField = Page.Locator("input[id='email']");+ var passwordField = Page.Locator("input[id='password']");+ await emailField.FillAsync(TestUserUsername);+ await passwordField.FillAsync(TestUserPassword);++ var loginButton = Page.Locator("button[type='submit']");+ await loginButton.ClickAsync();++ // Check if we get a 2FA code prompt by checking for text "Authenticator code".+ var prompt = await Page.TextContentAsync("label:has-text('Authenticator code')");+ Assert.That(prompt, Does.Contain("Authenticator code"), "No 2FA code prompt displayed.");++ // Click the "log in with a recovery code" button.+ var useRecoveryCodeButton = Page.Locator("button:has-text('log in with a recovery code')");+ await useRecoveryCodeButton.ClickAsync();++ // Fill in the recovery code and submit the form.+ var recoveryCodeField = Page.Locator("input[id='recovery-code']");+ await recoveryCodeField.FillAsync(recoveryCode);++ submitButton = Page.Locator("button[type='submit']");+ await submitButton.ClickAsync();++ // Check if we get redirected to the index page.+ await WaitForUrlAsync(AppBaseUrl, "Welcome to AliasVault");+ }++ /// <summary>+ /// Helper method to disable two factor authentication in the client GUI if it is enabled.+ /// </summary>+ private async Task DisableTwoFactorIfEnabled()+ {+ // Ensure that two factor auth is not enabled (in case the test is run after the previous test).+ await NavigateUsingBlazorRouter("settings/security");+ await WaitForUrlAsync("settings/security", "Two-factor Authentication");++ // Check if two factor auth is enabled.+ var enableButton = Page.Locator("button:has-text('Enable Two-Factor Authentication')");+ var isTwoFactorAuthEnabled = await enableButton.IsVisibleAsync();+ if (!isTwoFactorAuthEnabled)+ {+ // Disable two factor auth.+ var disableButton = Page.Locator("button:has-text('Disable Two-Factor Authentication')");+ await disableButton.ClickAsync();++ // Wait for the confirm disable button to be displayed.+ var confirmButton = Page.Locator("button:has-text('Confirm Disable')");+ await confirmButton.WaitForAsync(new LocatorWaitForOptions+ {+ State = WaitForSelectorState.Visible,+ Timeout = 5000,+ });++ // Press the confirm disable button as well.+ await confirmButton.ClickAsync();++ // Check if the success message is displayed.+ var successMessage = Page.Locator("div[role='alert']");+ await successMessage.WaitForAsync(new LocatorWaitForOptions+ {+ State = WaitForSelectorState.Visible,+ Timeout = 5000,+ });+ var message = await Page.TextContentAsync("div[role='alert']");+ Assert.That(message, Does.Contain("Two-factor authentication is now successfully disabled."), "No 2-fa disable success message displayed.");+ }+ }}