Skip to content

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

Add single token revoke method to API to fix mobile unlock flow (#1683)

Leendert de Borstba744b8e9390
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx b/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx
index 4e25222e3..ba96abc5e 100644
--- a/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx
+++ b/apps/browser-extension/src/entrypoints/popup/pages/auth/Unlock.tsx
@@ -472,8 +472,11 @@ const Unlock: React.FC = () => {
const handleMobileUnlockSuccess = async (result: MobileLoginResult): Promise<void> => {
showLoading();
try {
- // Revoke current tokens before setting new ones (since we're already logged in)
- await webApi.revokeTokens();
+ /*
+ * Revoke the old refresh token (from existing logged in session) before setting new ones
+ * that we get from the mobile login request.
+ */
+ await webApi.revokeCurrentTokens();
// Set new auth tokens
await authContext.setAuthTokens(result.username, result.token, result.refreshToken);
diff --git a/apps/browser-extension/src/utils/WebApiService.ts b/apps/browser-extension/src/utils/WebApiService.ts
index cb8f447e2..abc059ec6 100644
--- a/apps/browser-extension/src/utils/WebApiService.ts
+++ b/apps/browser-extension/src/utils/WebApiService.ts
@@ -200,6 +200,7 @@ export class WebApiService {
/**
* Revoke tokens via WebApi called when logging out.
+ * This revokes all tokens for the current device.
*/
public async revokeTokens(): Promise<void> {
// Revoke tokens via WebApi.
@@ -216,6 +217,26 @@ export class WebApiService {
}
}
+ /**
+ * Revoke only the current specific token via WebApi.
+ * Unlike revokeTokens(), this does NOT revoke other sessions for the same device.
+ * Used for mobile unlock flow where we want to replace the current session without
+ * affecting other browser sessions.
+ */
+ public async revokeCurrentTokens(): Promise<void> {
+ try {
+ const refreshToken = await this.getRefreshToken();
+ if (refreshToken) {
+ await this.post('Auth/revoke-token', {
+ token: await this.getAccessToken(),
+ refreshToken: refreshToken,
+ }, false);
+ }
+ } catch (err) {
+ console.error('WebApi revoke current token error:', err);
+ }
+ }
+
/**
* Calls the status endpoint to check if the auth tokens are still valid, app is supported and the vault is up to date.
* Returns offline indicator (serverVersion: '0.0.0') for network failures and server errors (5xx, 404, etc.).
diff --git a/apps/server/AliasVault.Api/Controllers/AuthController.cs b/apps/server/AliasVault.Api/Controllers/AuthController.cs
index 391f46dbd..8f4c6f266 100644
--- a/apps/server/AliasVault.Api/Controllers/AuthController.cs
+++ b/apps/server/AliasVault.Api/Controllers/AuthController.cs
@@ -367,7 +367,7 @@ public class AuthController(IAliasServerDbContextFactory dbContextFactory, UserM
}
/// <summary>
- /// Revoke endpoint used to revoke a refresh token.
+ /// Revoke endpoint used to revoke all refresh tokens for the current device.
/// </summary>
/// <param name="model">Token model.</param>
/// <returns>IActionResult.</returns>
@@ -406,6 +406,41 @@ public class AuthController(IAliasServerDbContextFactory dbContextFactory, UserM
return Ok();
}
+ /// <summary>
+ /// Revoke endpoint used to revoke only a specific refresh token (not all device tokens).
+ /// This is useful for scenarios like mobile unlock where we want to revoke the old token
+ /// before receiving a new one, without affecting other sessions.
+ /// </summary>
+ /// <param name="model">Token model.</param>
+ /// <returns>IActionResult.</returns>
+ [HttpPost("revoke-token")]
+ public async Task<IActionResult> RevokeToken([FromBody] TokenModel model)
+ {
+ await using var context = await dbContextFactory.CreateDbContextAsync();
+
+ // If the refresh token is not provided, return bad request.
+ if (string.IsNullOrWhiteSpace(model.RefreshToken))
+ {
+ return BadRequest(ApiErrorCodeHelper.CreateErrorResponse(ApiErrorCode.REFRESH_TOKEN_REQUIRED, 400));
+ }
+
+ // Look up the refresh token directly.
+ var refreshTokenEntry = await context.AliasVaultUserRefreshTokens.Include(t => t.User).FirstOrDefaultAsync(t => t.Value == model.RefreshToken);
+
+ if (refreshTokenEntry == null)
+ {
+ // Token doesn't exist - could already be revoked or never existed.
+ // Return success to avoid leaking information about token validity.
+ return Ok();
+ }
+
+ // Remove only the specific token, not other device tokens
+ context.AliasVaultUserRefreshTokens.Remove(refreshTokenEntry);
+ await context.SaveChangesAsync();
+
+ return Ok();
+ }
+
/// <summary>
/// Register endpoint used to register a new user.
/// </summary>
diff --git a/apps/server/AliasVault.Client/Auth/Pages/Unlock.razor b/apps/server/AliasVault.Client/Auth/Pages/Unlock.razor
index e8e10e4b3..c3560de51 100644
--- a/apps/server/AliasVault.Client/Auth/Pages/Unlock.razor
+++ b/apps/server/AliasVault.Client/Auth/Pages/Unlock.razor
@@ -384,8 +384,8 @@ else
{
await StatusCheck();
- // Revoke existing tokens
- await AuthService.RemoveTokensAsync();
+ // Revoke only the current specific token before setting new ones.
+ await AuthService.RevokeCurrentTokenAsync();
// Store the new tokens
await AuthService.StoreAccessTokenAsync(result.Token);
diff --git a/apps/server/AliasVault.Client/Services/Auth/AuthService.cs b/apps/server/AliasVault.Client/Services/Auth/AuthService.cs
index d95c0de69..041b9ca0b 100644
--- a/apps/server/AliasVault.Client/Services/Auth/AuthService.cs
+++ b/apps/server/AliasVault.Client/Services/Auth/AuthService.cs
@@ -373,8 +373,34 @@ public sealed class AuthService(HttpClient httpClient, ILocalStorageService loca
_encryptionKey = new byte[32];
}
+ /// <summary>
+ /// Revokes only the current specific token on the server asynchronously.
+ /// Unlike RemoveTokensAsync/RevokeTokenAsync, this does NOT revoke other sessions for the same device.
+ /// Used for mobile unlock flow where we want to replace the current session without
+ /// affecting other browser sessions.
+ /// </summary>
+ /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
+ public async Task RevokeCurrentTokenAsync()
+ {
+ var tokenInput = new TokenModel
+ {
+ Token = await GetAccessTokenAsync(),
+ RefreshToken = await GetRefreshTokenAsync(),
+ };
+
+ using var request = new HttpRequestMessage(HttpMethod.Post, "v1/Auth/revoke-token")
+ {
+ Content = JsonContent.Create(tokenInput),
+ };
+
+ // Add the X-Ignore-Failure header to the request so any failure does not trigger another refresh token request.
+ request.Headers.Add("X-Ignore-Failure", "true");
+ await httpClient.SendAsync(request);
+ }
+
/// <summary>
/// Revokes the access and refresh tokens on the server asynchronously.
+ /// This revokes all tokens for the current device.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
private async Task RevokeTokenAsync()