Skip to content

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

Add merge required detection to webapi vault load (#186)

Leendert de Borste499fa9ace0b
diff --git a/src/AliasVault.Api/Controllers/VaultController.cs b/src/AliasVault.Api/Controllers/VaultController.cs
index d7feb15ea..b5d30a53b 100644
--- a/src/AliasVault.Api/Controllers/VaultController.cs
+++ b/src/AliasVault.Api/Controllers/VaultController.cs
@@ -84,29 +84,52 @@ public class VaultController(ILogger<VaultController> logger, IDbContextFactory<
// as starting point.
if (vault == null)
{
- return Ok(new Shared.Models.WebApi.Vault.Vault
+ return Ok(new Shared.Models.WebApi.Vault.VaultGetResponse
{
- Blob = string.Empty,
- Version = string.Empty,
- CurrentRevisionNumber = 0,
- EncryptionPublicKey = string.Empty,
- CredentialsCount = 0,
- EmailAddressList = new List<string>(),
- CreatedAt = DateTime.MinValue,
- UpdatedAt = DateTime.MinValue,
+ Status = VaultGetStatus.Ok,
+ Vault = new Shared.Models.WebApi.Vault.Vault
+ {
+ Blob = string.Empty,
+ Version = string.Empty,
+ CurrentRevisionNumber = 0,
+ EncryptionPublicKey = string.Empty,
+ CredentialsCount = 0,
+ EmailAddressList = new List<string>(),
+ CreatedAt = DateTime.MinValue,
+ UpdatedAt = DateTime.MinValue,
+ },
});
}
- return Ok(new Shared.Models.WebApi.Vault.Vault
+ // Check if there are no other vaults with the same revision number.
+ // If there are, return a merge required status.
+ var otherVaults = await context.Vaults
+ .Where(x => x.UserId == user.Id && x.RevisionNumber == vault.RevisionNumber)
+ .ToListAsync();
+
+ if (otherVaults.Count > 1)
{
- Blob = vault.VaultBlob,
- Version = vault.Version,
- CurrentRevisionNumber = vault.RevisionNumber,
- EncryptionPublicKey = string.Empty,
- CredentialsCount = 0,
- EmailAddressList = new List<string>(),
- CreatedAt = vault.CreatedAt,
- UpdatedAt = vault.UpdatedAt,
+ return Ok(new Shared.Models.WebApi.Vault.VaultGetResponse
+ {
+ Status = VaultGetStatus.MergeRequired,
+ Vault = null,
+ });
+ }
+
+ return Ok(new Shared.Models.WebApi.Vault.VaultGetResponse
+ {
+ Status = VaultGetStatus.Ok,
+ Vault = new Shared.Models.WebApi.Vault.Vault
+ {
+ Blob = vault.VaultBlob,
+ Version = vault.Version,
+ CurrentRevisionNumber = vault.RevisionNumber,
+ EncryptionPublicKey = string.Empty,
+ CredentialsCount = 0,
+ EmailAddressList = [],
+ CreatedAt = vault.CreatedAt,
+ UpdatedAt = vault.UpdatedAt,
+ },
});
}
diff --git a/src/AliasVault.Client/Main/Components/Loading/SmallLoadingIndicator.razor b/src/AliasVault.Client/Main/Components/Loading/SmallLoadingIndicator.razor
index 4547c0479..e996b1b98 100644
--- a/src/AliasVault.Client/Main/Components/Loading/SmallLoadingIndicator.razor
+++ b/src/AliasVault.Client/Main/Components/Loading/SmallLoadingIndicator.razor
@@ -1,22 +1,22 @@
-<div role="status" class="px-2" title="@Title">
- <svg aria-hidden="true" class="inline w-7 h-7 text-gray-200 @(Spinning ? "animate-spin fill-primary-600" : "") dark:text-gray-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
- <path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor" />
- <path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill" />
- </svg>
+<div role="status" class="px-2 flex items-center" title="@Title">
+ <div class="relative inline-flex items-center justify-center">
+ @ChildContent
+ <svg aria-hidden="true" class="inline w-8 h-8 text-gray-200 @(Spinning ? "animate-spin fill-primary-600" : "") dark:text-gray-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg">
+ <path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor" />
+ <path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill" />
+ </svg>
+ </div>
+
<span class="sr-only">Loading...</span>
</div>
@code {
- /// <summary>
- /// Optional title of the loading indicator.
- /// </summary>
[Parameter]
public string Title { get; set; } = string.Empty;
- /// <summary>
- /// Set spinning to false to stop the animation.
- /// </summary>
[Parameter]
public bool Spinning { get; set; } = true;
+ [Parameter]
+ public RenderFragment? ChildContent { get; set; }
}
diff --git a/src/AliasVault.Client/Main/Layout/DbStatusIndicator.razor b/src/AliasVault.Client/Main/Layout/DbStatusIndicator.razor
index ac527c05a..0ce134d32 100644
--- a/src/AliasVault.Client/Main/Layout/DbStatusIndicator.razor
+++ b/src/AliasVault.Client/Main/Layout/DbStatusIndicator.razor
@@ -1,17 +1,17 @@
@implements IDisposable
@inject DbService DbService
-<div class="ms-2">
- @if (Loading)
- {
- <div class="flex items-center justify-center">
- <SmallLoadingIndicator Title="@LoadingIndicatorMessage" />
- </div>
- }
- else
- {
- <SmallLoadingIndicator Title="@LoadingIndicatorMessage" Spinning="false" />
- }
+<div class="ms-2 flex items-center">
+ <SmallLoadingIndicator Title="@LoadingIndicatorMessage" Spinning="@Loading">
+ @if (!Loading)
+ {
+ <button class="absolute p-2 hover:bg-gray-200 rounded-2xl" @onclick="OnRefreshClick">
+ <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 text-gray-400" viewBox="0 0 20 20" fill="currentColor">
+ <path fill-rule="evenodd" d="M4 2a1 1 0 011 1v2.101a7.002 7.002 0 0111.601 2.566 1 1 0 11-1.885.666A5.002 5.002 0 005.999 7H9a1 1 0 010 2H4a1 1 0 01-1-1V3a1 1 0 011-1zm.008 9.057a1 1 0 011.276.61A5.002 5.002 0 0014.001 13H11a1 1 0 110-2h5a1 1 0 011 1v5a1 1 0 11-2 0v-2.101a7.002 7.002 0 01-11.601-2.566 1 1 0 01.61-1.276z" clip-rule="evenodd" />
+ </svg>
+ </button>
+ }
+ </SmallLoadingIndicator>
</div>
@code {
@@ -19,6 +19,14 @@
private string LoadingIndicatorMessage { get; set; } = "";
private bool DatabaseLoading { get; set; } = false;
+ /// <summary>
+ /// Dispose method.
+ /// </summary>
+ public void Dispose()
+ {
+ DbService.GetState().StateChanged -= OnDatabaseStateChanged;
+ }
+
/// <inheritdoc />
protected override async Task OnInitializedAsync()
{
@@ -28,6 +36,14 @@
UpdateLoadingIndicatorMessage();
}
+ /// <summary>
+ /// Refresh the database.
+ /// </summary>
+ private async Task OnRefreshClick()
+ {
+ await DbService.InitializeDatabaseAsync();
+ }
+
private async void OnDatabaseStateChanged(object? sender, DbServiceState.DatabaseState newState)
{
await InvokeAsync(StateHasChanged);
@@ -63,12 +79,4 @@
Loading = false;
StateHasChanged();
}
-
- /// <summary>
- /// Dispose method.
- /// </summary>
- public void Dispose()
- {
- DbService.GetState().StateChanged -= OnDatabaseStateChanged;
- }
}
diff --git a/src/AliasVault.Client/Main/Pages/Sync/StatusMessages/Merging.razor b/src/AliasVault.Client/Main/Pages/Sync/StatusMessages/Merging.razor
new file mode 100644
index 000000000..631b43818
--- /dev/null
+++ b/src/AliasVault.Client/Main/Pages/Sync/StatusMessages/Merging.razor
@@ -0,0 +1,57 @@
+@inject DbService DbService
+@inject GlobalNotificationService GlobalNotificationService
+
+<div class="relative p-8 bg-white dark:bg-gray-800 rounded-lg shadow-xl max-w-md mx-auto">
+ <div class="text-center">
+ <div class="space-y-4">
+ <BoldLoadingIndicator />
+ <h2 class="text-xl font-semibold text-gray-900 dark:text-white">Merge required</h2>
+ <p class="text-sm text-gray-500 dark:text-gray-400">
+ A merge is required because another client has made changes to the vault since you last synced.
+ </p>
+ <p class="text-sm text-gray-500 dark:text-gray-400">
+ Please wait while the merge is performed. This process may take a moment.
+ </p>
+
+ <div>
+ @if (ErrorMessage.Length > 0)
+ {
+ <AlertMessageError Message="@ErrorMessage" />
+ }
+ </div>
+ </div>
+ </div>
+</div>
+
+@code {
+ private string ErrorMessage { get; set; } = string.Empty;
+
+ /// <inheritdoc />
+ protected override async Task OnInitializedAsync()
+ {
+ await base.OnInitializedAsync();
+ }
+
+ /// <inheritdoc />
+ protected override async Task OnAfterRenderAsync(bool firstRender)
+ {
+ await base.OnAfterRenderAsync(firstRender);
+
+ if (firstRender)
+ {
+ // Start the database migration process
+ await MergeDatabase();
+ }
+ }
+
+ private async Task MergeDatabase()
+ {
+ // Simulate a delay.
+ await Task.Delay(1000);
+
+ // Migration failed
+ ErrorMessage = "Merge failed. Please try again or contact support.";
+
+ StateHasChanged();
+ }
+}
diff --git a/src/AliasVault.Client/Main/Pages/Sync/Sync.razor b/src/AliasVault.Client/Main/Pages/Sync/Sync.razor
index d442a2fa5..e4ea0c3a9 100644
--- a/src/AliasVault.Client/Main/Pages/Sync/Sync.razor
+++ b/src/AliasVault.Client/Main/Pages/Sync/Sync.razor
@@ -18,6 +18,10 @@
{
<PendingMigrations />
}
+ else if (CurrentDbState.Status == DbServiceState.DatabaseStatus.MergeRequired)
+ {
+ <Merging />
+ }
else if (CurrentDbState.Status == DbServiceState.DatabaseStatus.Creating)
{
<Creating />
diff --git a/src/AliasVault.Client/Services/Database/DbService.cs b/src/AliasVault.Client/Services/Database/DbService.cs
index 088bf08a8..4640ea709 100644
--- a/src/AliasVault.Client/Services/Database/DbService.cs
+++ b/src/AliasVault.Client/Services/Database/DbService.cs
@@ -11,6 +11,7 @@ using System.Data;
using System.Net.Http.Json;
using AliasClientDb;
using AliasVault.Client.Services.Auth;
+using AliasVault.Shared.Models.Enums;
using AliasVault.Shared.Models.WebApi.Vault;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
@@ -462,9 +463,17 @@ public sealed class DbService : IDisposable
// Load from webapi.
try
{
- var vault = await _httpClient.GetFromJsonAsync<Vault>("api/v1/Vault");
- if (vault is not null)
+ var response = await _httpClient.GetFromJsonAsync<VaultGetResponse>("api/v1/Vault");
+ if (response is not null)
{
+ if (response.Status == VaultGetStatus.MergeRequired)
+ {
+ _state.UpdateState(DbServiceState.DatabaseStatus.MergeRequired);
+ return false;
+ }
+
+ var vault = response.Vault!;
+
// Check if vault blob is empty, if so, we don't need to do anything and the initial vault created
// on client is sufficient.
if (string.IsNullOrEmpty(vault.Blob))
diff --git a/src/AliasVault.Client/Services/Database/DbServiceState.cs b/src/AliasVault.Client/Services/Database/DbServiceState.cs
index 2d7669612..53ff29834 100644
--- a/src/AliasVault.Client/Services/Database/DbServiceState.cs
+++ b/src/AliasVault.Client/Services/Database/DbServiceState.cs
@@ -39,6 +39,22 @@ public sealed class DbServiceState
/// </summary>
Creating,
+ /// <summary>
+ /// Database requires a merge because of multiple vaults with the same revision number.
+ /// This happens when multiple clients have made changes to the same vault without syncing in between.
+ /// </summary>
+ MergeRequired,
+
+ /// <summary>
+ /// Database is being merged.
+ /// </summary>
+ Merging,
+
+ /// <summary>
+ /// Database merge failed.
+ /// </summary>
+ MergeFailed,
+
/// <summary>
/// Database failed to decrypt. No data is accessible.
/// </summary>
diff --git a/src/AliasVault.Client/wwwroot/css/tailwind.css b/src/AliasVault.Client/wwwroot/css/tailwind.css
index f56d6630c..42e0e6974 100644
--- a/src/AliasVault.Client/wwwroot/css/tailwind.css
+++ b/src/AliasVault.Client/wwwroot/css/tailwind.css
@@ -769,10 +769,6 @@ video {
margin-inline-start: 1.25rem;
}
-.mt-0 {
- margin-top: 0px;
-}
-
.mt-1 {
margin-top: 0.25rem;
}
@@ -797,10 +793,6 @@ video {
margin-top: 2rem;
}
-.-mt-4 {
- margin-top: -1rem;
-}
-
.line-clamp-2 {
overflow: hidden;
display: -webkit-box;
@@ -1184,6 +1176,14 @@ video {
border-radius: 0.375rem;
}
+.rounded-xl {
+ border-radius: 0.75rem;
+}
+
+.rounded-2xl {
+ border-radius: 1rem;
+}
+
.rounded-l-lg {
border-top-left-radius: 0.5rem;
border-bottom-left-radius: 0.5rem;
@@ -1434,6 +1434,10 @@ video {
fill: #d68338;
}
+.p-1 {
+ padding: 0.25rem;
+}
+
.p-2 {
padding: 0.5rem;
}
@@ -1678,6 +1682,11 @@ video {
color: rgb(229 231 235 / var(--tw-text-opacity));
}
+.text-gray-300 {
+ --tw-text-opacity: 1;
+ color: rgb(209 213 219 / var(--tw-text-opacity));
+}
+
.text-gray-400 {
--tw-text-opacity: 1;
color: rgb(156 163 175 / var(--tw-text-opacity));
@@ -2583,15 +2592,6 @@ video {
padding-left: 0px;
padding-right: 0px;
}
-
- .lg\:py-0 {
- padding-top: 0px;
- padding-bottom: 0px;
- }
-
- .lg\:hover\:underline:hover {
- text-decoration-line: underline;
- }
}
@media (min-width: 1280px) {
diff --git a/src/AliasVault.Shared/Models/Enums/VaultGetStatus.cs b/src/AliasVault.Shared/Models/Enums/VaultGetStatus.cs
new file mode 100644
index 000000000..5ae120235
--- /dev/null
+++ b/src/AliasVault.Shared/Models/Enums/VaultGetStatus.cs
@@ -0,0 +1,24 @@
+//-----------------------------------------------------------------------
+// <copyright file="VaultGetStatus.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.Shared.Models.Enums;
+
+/// <summary>
+/// Enum representing the status of a vault get operation.
+/// </summary>
+public enum VaultGetStatus
+{
+ /// <summary>
+ /// The vault was retrieved successfully.
+ /// </summary>
+ Ok,
+
+ /// <summary>
+ /// A client-side merge is required before the vault can be used.
+ /// </summary>
+ MergeRequired,
+}
diff --git a/src/AliasVault.Shared/Models/WebApi/Vault/VaultGetResponse.cs b/src/AliasVault.Shared/Models/WebApi/Vault/VaultGetResponse.cs
new file mode 100644
index 000000000..f78b7f880
--- /dev/null
+++ b/src/AliasVault.Shared/Models/WebApi/Vault/VaultGetResponse.cs
@@ -0,0 +1,26 @@
+//-----------------------------------------------------------------------
+// <copyright file="VaultGetResponse.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.Shared.Models.WebApi.Vault;
+
+using AliasVault.Shared.Models.Enums;
+
+/// <summary>
+/// Vault get response model.
+/// </summary>
+public class VaultGetResponse
+{
+ /// <summary>
+ /// Gets or sets the status of the vault get operation.
+ /// </summary>
+ public VaultGetStatus Status { get; set; }
+
+ /// <summary>
+ /// Gets or sets the requested vault.
+ /// </summary>
+ public Vault? Vault { get; set; }
+}