Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add client version check and vault revision number to status endpoint (#541)
Leendert de Borst84d3a253046e
diff --git a/src/AliasVault.Api/AliasVault.Api.csproj b/src/AliasVault.Api/AliasVault.Api.csprojindex b3798364c..c90f42152 100644--- a/src/AliasVault.Api/AliasVault.Api.csproj+++ b/src/AliasVault.Api/AliasVault.Api.csproj@@ -35,6 +35,7 @@<ItemGroup><ProjectReference Include="..\Databases\AliasServerDb\AliasServerDb.csproj" /><ProjectReference Include="..\Generators\AliasVault.Generators.Identity\AliasVault.Generators.Identity.csproj" />+ <ProjectReference Include="..\Shared\AliasVault.Shared.Core\AliasVault.Shared.Core.csproj" /><ProjectReference Include="..\Shared\AliasVault.Shared.Server\AliasVault.Shared.Server.csproj" /><ProjectReference Include="..\Shared\AliasVault.Shared\AliasVault.Shared.csproj" /><ProjectReference Include="..\Utilities\AliasVault.Auth\AliasVault.Auth.csproj" />diff --git a/src/AliasVault.Api/Controllers/AuthController.cs b/src/AliasVault.Api/Controllers/AuthController.csindex a6393461d..5628e44f1 100644--- a/src/AliasVault.Api/Controllers/AuthController.cs+++ b/src/AliasVault.Api/Controllers/AuthController.cs@@ -15,6 +15,7 @@ using AliasServerDb;using AliasVault.Api.Helpers;using AliasVault.Auth;using AliasVault.Cryptography.Client;+using AliasVault.Shared.Core;using AliasVault.Shared.Models.Enums;using AliasVault.Shared.Models.WebApi;using AliasVault.Shared.Models.WebApi.Auth;@@ -78,14 +79,60 @@ public class AuthController(IAliasServerDbContextFactory dbContextFactory, UserMprivate static readonly SemaphoreSlim Semaphore = new(1, 1);/// <summary>- /// Status endpoint called by client to check if user is still authenticated.+ /// Status endpoint called by client to check if user is still authenticated and get sync status./// </summary>- /// <returns>Returns OK if valid authentication is provided, otherwise it will return 401 unauthorized.</returns>+ /// <param name="clientHeader">Client header.</param>+ /// <returns>Returns status response if valid authentication is provided, otherwise it will return 401 unauthorized.</returns>[Authorize][HttpGet("status")]- public IActionResult Status()+ public async Task<IActionResult> Status([FromHeader(Name = "X-AliasVault-Client")] string? clientHeader){- return Ok();+ var user = await userManager.GetUserAsync(User);+ if (user == null)+ {+ return Unauthorized();+ }++ // Get latest vault revision number+ var latestVault = user.Vaults.OrderByDescending(x => x.RevisionNumber).FirstOrDefault();+ var latestRevision = latestVault?.RevisionNumber ?? 0;++ // Check client version compatibility if header is provided+ var isSupported = false;+ if (!string.IsNullOrEmpty(clientHeader))+ {+ // Client header format should be "{platform}-{version}" e.g. "chrome-1.4.0"+ var parts = clientHeader.Split('-');+ if (parts.Length == 2)+ {+ var platform = parts[0].ToLowerInvariant();+ var clientVersion = parts[1];++ if (AppInfo.MinimumClientVersions.TryGetValue(platform, out var minimumVersion))+ {+ if (VersionHelper.IsVersionEqualOrNewer(clientVersion, minimumVersion))+ {+ isSupported = true;+ }+ }+ else+ {+ // Unknown platform+ isSupported = false;+ }+ }+ else+ {+ // Invalid header format+ isSupported = false;+ }+ }++ return Ok(new StatusResponse+ {+ Supported = isSupported,+ VaultRevision = latestRevision,+ });}/// <summary>diff --git a/src/AliasVault.Api/Helpers/VersionHelper.cs b/src/AliasVault.Api/Helpers/VersionHelper.csindex b692bd470..686ad4ede 100644--- a/src/AliasVault.Api/Helpers/VersionHelper.cs+++ b/src/AliasVault.Api/Helpers/VersionHelper.cs@@ -35,4 +35,28 @@ public static class VersionHelper// Compare the versionsreturn v1 < v2;}++ /// <summary>+ /// Checks if version1 is equal or newer than version2.+ /// </summary>+ /// <param name="version1">The first version.</param>+ /// <param name="version2">The second version.</param>+ /// <returns>True if version1 is older than version2, false otherwise.</returns>+ public static bool IsVersionEqualOrNewer(string version1, string version2)+ {+ if (string.IsNullOrEmpty(version1) || string.IsNullOrEmpty(version2))+ {+ return false;+ }++ // Try parsing both versions+ if (!Version.TryParse(version1, out Version? v1) || !Version.TryParse(version2, out Version? v2))+ {+ // If one of the versions is not a valid version string, throw an exception.+ throw new ArgumentException("Invalid version string.");+ }++ // Compare the versions+ return v1 >= v2;+ }}diff --git a/src/Shared/AliasVault.Shared.Core/AppInfo.cs b/src/Shared/AliasVault.Shared.Core/AppInfo.csindex fb649f7a5..66427d1f4 100644--- a/src/Shared/AliasVault.Shared.Core/AppInfo.cs+++ b/src/Shared/AliasVault.Shared.Core/AppInfo.cs@@ -32,6 +32,16 @@ public static class AppInfo/// </summary>public const int VersionPatch = 1;+ /// <summary>+ /// List of minimum supported client versions. If client version is lower than the minimum supported version,+ /// the client will be shown a message to update to the minimum supported version.+ /// </summary>+ public static readonly Dictionary<string, string> MinimumClientVersions = new()+ {+ { "chrome", "0.12.0" },+ { "web", "0.12.0" },+ };+/// <summary>/// Gets the build number, typically used in CI/CD pipelines./// Can be overridden at build time.diff --git a/src/Shared/AliasVault.Shared/Models/WebApi/Auth/StatusResponse.cs b/src/Shared/AliasVault.Shared/Models/WebApi/Auth/StatusResponse.csnew file mode 100644index 000000000..49a877018--- /dev/null+++ b/src/Shared/AliasVault.Shared/Models/WebApi/Auth/StatusResponse.cs@@ -0,0 +1,25 @@+//-----------------------------------------------------------------------+// <copyright file="StatusResponse.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.Auth;++/// <summary>+/// Response model for the status endpoint.+/// </summary>+public class StatusResponse+{+ /// <summary>+ /// Gets or sets a value indicating whether the client version is supported by this API.+ /// </summary>+ public required bool Supported { get; set; }++ /// <summary>+ /// Gets or sets the latest vault revision number for the authenticated user, which the client+ /// can use to determine if it should refresh the vault from the server.+ /// </summary>+ public required long VaultRevision { get; set; }+}diff --git a/src/Tests/AliasVault.UnitTests/Vault/VersionTests.cs b/src/Tests/AliasVault.UnitTests/Vault/VersionTests.csindex 5607a552b..45e67aaea 100644--- a/src/Tests/AliasVault.UnitTests/Vault/VersionTests.cs+++ b/src/Tests/AliasVault.UnitTests/Vault/VersionTests.cs@@ -46,4 +46,26 @@ public class VersionTestsvar version2 = "1.0.1";Assert.Throws<ArgumentException>(() => VersionHelper.IsVersionOlder(version1, version2));}++ /// <summary>+ /// Test the version comparison throws an exception for illegal version strings.+ /// </summary>+ [Test]+ public void VersionEqualOrNewerComparisonExceptionTest()+ {+ var version1 = "1.2.0.5.1";+ var version2 = "1.0.1";+ Assert.Throws<ArgumentException>(() => VersionHelper.IsVersionOlder(version1, version2));+ }++ /// <summary>+ /// Test the version comparison returns true for equal versions.+ /// </summary>+ [Test]+ public void VersionEqualComparisonTest()+ {+ var version1 = "1.2.0";+ var version2 = "1.1.0";+ Assert.That(VersionHelper.IsVersionEqualOrNewer(version1, version2), Is.True);+ }}