Skip to content

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

Update TOTP create and delete flow (#1404)

Leendert de Borst538b9a8c8cb5
diff --git a/apps/server/AliasVault.Client/Main/Components/TotpCodes/TotpCodes.razor b/apps/server/AliasVault.Client/Main/Components/TotpCodes/TotpCodes.razor
index ef7b3e3d3..51fe28c2b 100644
--- a/apps/server/AliasVault.Client/Main/Components/TotpCodes/TotpCodes.razor
+++ b/apps/server/AliasVault.Client/Main/Components/TotpCodes/TotpCodes.razor
@@ -143,6 +143,12 @@
// Keep track of the original TOTP codes.
OriginalTotpCodeIds = TotpCodeList.Where(t => !t.IsDeleted).Select(t => t.Id).ToList();
+
+ // Auto-show the add form if there are no existing codes
+ if (!TotpCodeList.Any(t => !t.IsDeleted))
+ {
+ ShowAddForm();
+ }
}
/// <summary>
diff --git a/apps/server/AliasVault.Client/Main/Pages/Items/View.razor b/apps/server/AliasVault.Client/Main/Pages/Items/View.razor
index 1f4164b54..a6acb726e 100644
--- a/apps/server/AliasVault.Client/Main/Pages/Items/View.razor
+++ b/apps/server/AliasVault.Client/Main/Pages/Items/View.razor
@@ -84,9 +84,9 @@ else
}
@* TOTP codes - only for items with TOTP *@
- @if (Item.TotpCodes.Count > 0)
+ @if (Item.TotpCodes.Any(t => !t.IsDeleted))
{
- <TotpViewer TotpCodeList="@Item.TotpCodes" />
+ <TotpViewer TotpCodeList="@Item.TotpCodes.Where(t => !t.IsDeleted).ToList()" />
}
@* Notes - if present (left column for non-Note types) *@
diff --git a/apps/server/AliasVault.Client/Services/ItemService.cs b/apps/server/AliasVault.Client/Services/ItemService.cs
index 57f301c17..5b718201f 100644
--- a/apps/server/AliasVault.Client/Services/ItemService.cs
+++ b/apps/server/AliasVault.Client/Services/ItemService.cs
@@ -1020,8 +1020,9 @@ public sealed class ItemService(HttpClient httpClient, DbService dbService, Conf
/// <param name="updateDateTime">The timestamp for updates.</param>
private static void UpdateAttachments(DbContext context, Item existingItem, Item newItem, DateTime updateDateTime)
{
+ // Mark existing attachments as deleted if they're not in the new list (excluding already deleted ones)
var attachmentsToRemove = existingItem.Attachments
- .Where(existingAttachment => !newItem.Attachments.Any(a => a.Id == existingAttachment.Id))
+ .Where(existingAttachment => !newItem.Attachments.Any(a => a.Id == existingAttachment.Id && !a.IsDeleted))
.ToList();
foreach (var attachmentToRemove in attachmentsToRemove)
@@ -1030,7 +1031,8 @@ public sealed class ItemService(HttpClient httpClient, DbService dbService, Conf
attachmentToRemove.UpdatedAt = updateDateTime;
}
- foreach (var attachment in newItem.Attachments)
+ // Process attachments from the new item (excluding deleted ones - they're handled above)
+ foreach (var attachment in newItem.Attachments.Where(a => !a.IsDeleted))
{
if (attachment.Id != Guid.Empty)
{
@@ -1043,11 +1045,18 @@ public sealed class ItemService(HttpClient httpClient, DbService dbService, Conf
}
else
{
- attachment.Id = Guid.NewGuid();
- attachment.ItemId = existingItem.Id;
- attachment.CreatedAt = updateDateTime;
- attachment.UpdatedAt = updateDateTime;
- existingItem.Attachments.Add(attachment);
+ // Create a new attachment entity and add via context to ensure proper tracking
+ var newAttachment = new Attachment
+ {
+ Id = Guid.NewGuid(),
+ ItemId = existingItem.Id,
+ Filename = attachment.Filename,
+ Blob = attachment.Blob,
+ CreatedAt = updateDateTime,
+ UpdatedAt = updateDateTime,
+ IsDeleted = false,
+ };
+ context.Add(newAttachment);
}
}
}
@@ -1061,8 +1070,9 @@ public sealed class ItemService(HttpClient httpClient, DbService dbService, Conf
/// <param name="updateDateTime">The timestamp for updates.</param>
private static void UpdateTotpCodes(DbContext context, Item existingItem, Item newItem, DateTime updateDateTime)
{
+ // Mark existing TOTP codes as deleted if they're not in the new list (excluding already deleted ones)
var totpCodesToRemove = existingItem.TotpCodes
- .Where(existingTotp => !newItem.TotpCodes.Any(t => t.Id == existingTotp.Id))
+ .Where(existingTotp => !newItem.TotpCodes.Any(t => t.Id == existingTotp.Id && !t.IsDeleted))
.ToList();
foreach (var totpToRemove in totpCodesToRemove)
@@ -1071,7 +1081,8 @@ public sealed class ItemService(HttpClient httpClient, DbService dbService, Conf
totpToRemove.UpdatedAt = updateDateTime;
}
- foreach (var totpCode in newItem.TotpCodes)
+ // Process TOTP codes from the new item (excluding deleted ones - they're handled above)
+ foreach (var totpCode in newItem.TotpCodes.Where(t => !t.IsDeleted))
{
if (totpCode.Id != Guid.Empty)
{
@@ -1084,11 +1095,18 @@ public sealed class ItemService(HttpClient httpClient, DbService dbService, Conf
}
else
{
- totpCode.Id = Guid.NewGuid();
- totpCode.ItemId = existingItem.Id;
- totpCode.CreatedAt = updateDateTime;
- totpCode.UpdatedAt = updateDateTime;
- existingItem.TotpCodes.Add(totpCode);
+ // Create a new TOTP code entity and add via context to ensure proper tracking
+ var newTotpCode = new TotpCode
+ {
+ Id = Guid.NewGuid(),
+ ItemId = existingItem.Id,
+ Name = totpCode.Name,
+ SecretKey = totpCode.SecretKey,
+ CreatedAt = updateDateTime,
+ UpdatedAt = updateDateTime,
+ IsDeleted = false,
+ };
+ context.Add(newTotpCode);
}
}
}
diff --git a/apps/server/Tests/AliasVault.E2ETests/Tests/Client/Shard5/TotpTests.cs b/apps/server/Tests/AliasVault.E2ETests/Tests/Client/Shard5/TotpTests.cs
index 74c2064e6..c1a431e52 100644
--- a/apps/server/Tests/AliasVault.E2ETests/Tests/Client/Shard5/TotpTests.cs
+++ b/apps/server/Tests/AliasVault.E2ETests/Tests/Client/Shard5/TotpTests.cs
@@ -29,32 +29,23 @@ public class TotpTests : ClientPlaywrightTest
new Dictionary<string, string>
{
{ "service-name", serviceName },
- });
-
- // Open the edit page again by clicking the button that contains the text "Edit"
- await Page.ClickAsync("text=Edit");
- await WaitForUrlAsync("items/**/edit", "Edit the existing item");
-
- // Click the "Add TOTP Code" button
- var addButton = Page.Locator("button[id='add-totp-code'], a[id='add-totp-code']");
- await addButton.ClickAsync();
-
- // Fill in the TOTP code details
- var secretKeyInput = Page.Locator("input[id='secretKey']");
- var nameInput = Page.Locator("input[id='name']");
-
- await nameInput.FillAsync("Test TOTP");
- await secretKeyInput.FillAsync("JBSWY3DPEHPK3PXP"); // Example secret key
+ },
+ async () =>
+ {
+ // Add the 2FA section via the + menu (form auto-opens when no codes exist)
+ await AddFieldSectionAsync("Two-Factor Authentication");
- // Submit the form
- var saveButton = Page.Locator("button[id='save-totp-code']");
- await saveButton.ClickAsync();
+ // Fill in the TOTP code details (form is already visible)
+ var nameInput = Page.Locator("input[id='name']");
+ var secretKeyInput = Page.Locator("input[id='secretKey']");
- // Save the item
- var submitButton = Page.Locator("text=Save Item").First;
- await submitButton.ClickAsync();
+ await nameInput.FillAsync("Test TOTP");
+ await secretKeyInput.FillAsync("JBSWY3DPEHPK3PXP"); // Example secret key
- await WaitForUrlAsync("items/**", "Item updated successfully");
+ // Submit the TOTP form
+ var saveButton = Page.Locator("button[id='save-totp-code']");
+ await saveButton.ClickAsync();
+ });
// Verify that the TOTP code appears in the list
var pageContent = await Page.TextContentAsync("body");
@@ -80,26 +71,19 @@ public class TotpTests : ClientPlaywrightTest
new Dictionary<string, string>
{
{ "service-name", serviceName },
- });
-
- // Open the edit page again by clicking the button that contains the text "Edit"
- await Page.ClickAsync("text=Edit");
- await WaitForUrlAsync("items/**/edit", "Edit the existing item");
-
- // Add a TOTP code
- var addButton = Page.Locator("button[id='add-totp-code'], a[id='add-totp-code']");
- await addButton.ClickAsync();
-
- await Page.Locator("input[id='name']").FillAsync("TOTP to Delete");
- await Page.Locator("input[id='secretKey']").FillAsync("JBSWY3DPEHPK3PXP");
-
- await Page.Locator("button[id='save-totp-code']").ClickAsync();
+ },
+ async () =>
+ {
+ // Add the 2FA section via the + menu (form auto-opens when no codes exist)
+ await AddFieldSectionAsync("Two-Factor Authentication");
- // Save the item
- var submitButton = Page.Locator("text=Save Item").First;
- await submitButton.ClickAsync();
+ // Fill in the TOTP code details (form is already visible)
+ await Page.Locator("input[id='name']").FillAsync("TOTP to Delete");
+ await Page.Locator("input[id='secretKey']").FillAsync("JBSWY3DPEHPK3PXP");
- await WaitForUrlAsync("items/**", "Item updated successfully");
+ // Submit the TOTP form
+ await Page.Locator("button[id='save-totp-code']").ClickAsync();
+ });
// Verify the TOTP code was added
var pageContent = await Page.TextContentAsync("body");
@@ -117,6 +101,12 @@ public class TotpTests : ClientPlaywrightTest
var confirmButton = Page.Locator("button:has-text('Confirm')");
await confirmButton.ClickAsync();
+ // Save the item to persist the deletion
+ var submitButton = Page.Locator("text=Save Item").First;
+ await submitButton.ClickAsync();
+
+ await WaitForUrlAsync("items/**", "Item updated successfully");
+
// Verify the TOTP code was deleted
pageContent = await Page.TextContentAsync("body");
Assert.That(pageContent, Does.Not.Contain("TOTP to Delete"), "TOTP code was not deleted successfully");