Skip to content

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

Add recent auth logs to client (#80)

Leendert de Borste6feafcb8736
diff --git a/src/AliasVault.Admin/Auth/Pages/Login.razor b/src/AliasVault.Admin/Auth/Pages/Login.razor
index e728d3336..6e71a9103 100644
--- a/src/AliasVault.Admin/Auth/Pages/Login.razor
+++ b/src/AliasVault.Admin/Auth/Pages/Login.razor
@@ -1,4 +1,5 @@
@page "/user/login"
+@using AliasVault.Shared.Models.Enums
<LayoutPageTitle>Log in</LayoutPageTitle>
@@ -63,6 +64,7 @@
var user = await UserManager.FindByNameAsync(Input.UserName);
if (user == null)
{
+
await AuthLoggingService.LogAuthEventFailAsync(Input.UserName, AuthEventType.Login, AuthFailureReason.InvalidUsername);
ServerValidationErrors.AddError("Error: Invalid login attempt.");
return;
diff --git a/src/AliasVault.Admin/Auth/Pages/LoginWith2fa.razor b/src/AliasVault.Admin/Auth/Pages/LoginWith2fa.razor
index 72e4dbd4e..08860cf2b 100644
--- a/src/AliasVault.Admin/Auth/Pages/LoginWith2fa.razor
+++ b/src/AliasVault.Admin/Auth/Pages/LoginWith2fa.razor
@@ -1,4 +1,5 @@
@page "/user/loginWith2fa"
+@using AliasVault.Shared.Models.Enums
<LayoutPageTitle>Two-factor authentication</LayoutPageTitle>
diff --git a/src/AliasVault.Admin/Auth/Pages/LoginWithRecoveryCode.razor b/src/AliasVault.Admin/Auth/Pages/LoginWithRecoveryCode.razor
index 4a9a35112..6c770ccf3 100644
--- a/src/AliasVault.Admin/Auth/Pages/LoginWithRecoveryCode.razor
+++ b/src/AliasVault.Admin/Auth/Pages/LoginWithRecoveryCode.razor
@@ -1,4 +1,5 @@
@page "/user/loginWithRecoveryCode"
+@using AliasVault.Shared.Models.Enums
<LayoutPageTitle>Recovery code verification</LayoutPageTitle>
diff --git a/src/AliasVault.Admin/Auth/Pages/Logout.razor b/src/AliasVault.Admin/Auth/Pages/Logout.razor
index aa00da829..b0b3787e7 100644
--- a/src/AliasVault.Admin/Auth/Pages/Logout.razor
+++ b/src/AliasVault.Admin/Auth/Pages/Logout.razor
@@ -1,4 +1,5 @@
@page "/user/logout"
+@using AliasVault.Shared.Models.Enums
@inject UserService UserService
@inject GlobalNotificationService GlobalNotificationService
diff --git a/src/AliasVault.Admin/Main/Pages/Account/Manage/Disable2fa.razor b/src/AliasVault.Admin/Main/Pages/Account/Manage/Disable2fa.razor
index 3f8cb1379..8eb7cb96b 100644
--- a/src/AliasVault.Admin/Main/Pages/Account/Manage/Disable2fa.razor
+++ b/src/AliasVault.Admin/Main/Pages/Account/Manage/Disable2fa.razor
@@ -1,5 +1,6 @@
@page "/account/manage/disable-2fa"
+@using AliasVault.Shared.Models.Enums
@using Microsoft.AspNetCore.Identity
@inject UserManager<AdminUser> UserManager
diff --git a/src/AliasVault.Admin/Main/Pages/Account/Manage/EnableAuthenticator.razor b/src/AliasVault.Admin/Main/Pages/Account/Manage/EnableAuthenticator.razor
index ee5afef53..bf41931bd 100644
--- a/src/AliasVault.Admin/Main/Pages/Account/Manage/EnableAuthenticator.razor
+++ b/src/AliasVault.Admin/Main/Pages/Account/Manage/EnableAuthenticator.razor
@@ -4,6 +4,7 @@
@using System.Globalization
@using System.Text
@using System.Text.Encodings.Web
+@using AliasVault.Shared.Models.Enums
@using Microsoft.AspNetCore.Identity
@inject UserManager<AdminUser> UserManager
diff --git a/src/AliasVault.Api/Controllers/AuthController.cs b/src/AliasVault.Api/Controllers/AuthController.cs
index ccc0bdde2..84c49022c 100644
--- a/src/AliasVault.Api/Controllers/AuthController.cs
+++ b/src/AliasVault.Api/Controllers/AuthController.cs
@@ -13,6 +13,7 @@ using System.Security.Cryptography;
using System.Text;
using AliasServerDb;
using AliasVault.AuthLogging;
+using AliasVault.Shared.Models.Enums;
using AliasVault.Shared.Models.WebApi;
using AliasVault.Shared.Models.WebApi.Auth;
using AliasVault.Shared.Providers.Time;
diff --git a/src/AliasVault.Api/Controllers/Security/SecurityController.cs b/src/AliasVault.Api/Controllers/Security/SecurityController.cs
index 07b3dcd9c..316d327a9 100644
--- a/src/AliasVault.Api/Controllers/Security/SecurityController.cs
+++ b/src/AliasVault.Api/Controllers/Security/SecurityController.cs
@@ -9,7 +9,7 @@ namespace AliasVault.Api.Controllers.Security;
using AliasServerDb;
using AliasVault.Api.Controllers.Abstracts;
-using AliasVault.AuthLogging;
+using AliasVault.Shared.Models.Enums;
using AliasVault.Shared.Models.WebApi.Security;
using Asp.Versioning;
using Microsoft.AspNetCore.Identity;
@@ -83,4 +83,39 @@ public class SecurityController(IDbContextFactory<AliasServerDbContext> dbContex
return Ok();
}
+
+ /// <summary>
+ /// Returns the last 50 authentication logs for the current user.
+ /// </summary>
+ /// <returns>Task with a list of the last 50 authentication logs.</returns>
+ [HttpGet("authlogs")]
+ public async Task<IActionResult> GetUserAuthLogs()
+ {
+ var user = await GetCurrentUserAsync();
+ if (user is null)
+ {
+ return Unauthorized("Not authenticated.");
+ }
+
+ await using var context = await dbContextFactory.CreateDbContextAsync();
+
+ var authLogs = await context.AuthLogs
+ .Where(x => x.Username == user.UserName)
+ .Where(x => x.EventType != AuthEventType.TokenRefresh)
+ .OrderByDescending(x => x.Timestamp)
+ .Take(50)
+ .Select(x => new AuthLogModel
+ {
+ Id = x.Id,
+ Timestamp = x.Timestamp,
+ EventType = x.EventType,
+ Username = x.Username,
+ IpAddress = x.IpAddress ?? string.Empty,
+ UserAgent = x.UserAgent ?? string.Empty,
+ IsSuccess = x.IsSuccess,
+ })
+ .ToListAsync();
+
+ return Ok(authLogs);
+ }
}
diff --git a/src/AliasVault.Api/Controllers/Security/TwoFactorAuthController.cs b/src/AliasVault.Api/Controllers/Security/TwoFactorAuthController.cs
index 4ead944e1..41bb3574b 100644
--- a/src/AliasVault.Api/Controllers/Security/TwoFactorAuthController.cs
+++ b/src/AliasVault.Api/Controllers/Security/TwoFactorAuthController.cs
@@ -11,6 +11,7 @@ using System.Text.Encodings.Web;
using AliasServerDb;
using AliasVault.Api.Controllers.Abstracts;
using AliasVault.AuthLogging;
+using AliasVault.Shared.Models.Enums;
using Asp.Versioning;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
diff --git a/src/AliasVault.Client/Main/Pages/Settings/Security/Components/ActiveSessionsSection.razor b/src/AliasVault.Client/Main/Pages/Settings/Security/Components/ActiveSessionsSection.razor
index 13c81eb3b..7b1e8fe8b 100644
--- a/src/AliasVault.Client/Main/Pages/Settings/Security/Components/ActiveSessionsSection.razor
+++ b/src/AliasVault.Client/Main/Pages/Settings/Security/Components/ActiveSessionsSection.razor
@@ -53,7 +53,7 @@
/// Gets or sets the list of active sessions (refresh tokens) for the current user.
/// </summary>
[Parameter]
- public List<RefreshTokenModel> Sessions { get; set; } = new List<RefreshTokenModel>();
+ public List<RefreshTokenModel> Sessions { get; set; } = [];
/// <summary>
/// Event callback that is invoked when the list of active sessions changes.
diff --git a/src/AliasVault.Client/Main/Pages/Settings/Security/Components/RecentAuthLogsSection.razor b/src/AliasVault.Client/Main/Pages/Settings/Security/Components/RecentAuthLogsSection.razor
new file mode 100644
index 000000000..9f3eb96a1
--- /dev/null
+++ b/src/AliasVault.Client/Main/Pages/Settings/Security/Components/RecentAuthLogsSection.razor
@@ -0,0 +1,112 @@
+@using AliasVault.Shared.Models.Enums
+@using AliasVault.Shared.Models.WebApi.Security
+@inject HttpClient Http
+@inject GlobalNotificationService GlobalNotificationService
+
+<div class="p-4 mb-4 mx-4 bg-white border border-gray-200 rounded-lg shadow-sm dark:border-gray-700 sm:p-6 dark:bg-gray-800">
+ <h3 class="mb-2 text-lg font-medium text-gray-900 dark:text-white">Recent Auth Logs</h3>
+ <p class="text-sm text-gray-600 dark:text-gray-400 mb-4">Below you can find an overview of recent login attempts to your account.</p>
+
+ @if (IsLoading)
+ {
+ <LoadingIndicator />
+ }
+ else
+ {
+ @if (!AuthLogs.Any())
+ {
+ <p class="text-sm text-gray-600 dark:text-gray-400">No authentication logs found.</p>
+ }
+ else
+ {
+ <div class="overflow-x-auto">
+ <table class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
+ <thead class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
+ <tr>
+ <th scope="col" class="px-6 py-3">Timestamp</th>
+ <th scope="col" class="px-6 py-3">Event Type</th>
+ <th scope="col" class="px-6 py-3">Username</th>
+ <th scope="col" class="px-6 py-3">IP Address</th>
+ <th scope="col" class="px-6 py-3">Device</th>
+ <th scope="col" class="px-6 py-3">Success</th>
+ </tr>
+ </thead>
+ <tbody>
+ @foreach (var authLog in AuthLogs)
+ {
+ <tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700">
+ <td class="px-6 py-4">@authLog.Timestamp.ToLocalTime().ToString("g")</td>
+ <td class="px-6 py-4">@authLog.EventType</td>
+ <td class="px-6 py-4">@authLog.Username</td>
+ <td class="px-6 py-4">@authLog.IpAddress</td>
+ <td class="px-6 py-4">@authLog.UserAgent</td>
+ <td class="px-4 py-4"><StatusPill Enabled="authLog.IsSuccess" TextTrue="Success" TextFalse="Failed" /></td>
+ </tr>
+ }
+ </tbody>
+ </table>
+ </div>
+ }
+ }
+</div>
+
+@code {
+
+ /// <summary>
+ /// Gets or sets the list of active sessions (refresh tokens) for the current user.
+ /// </summary>
+ [Parameter]
+ public List<AuthLogModel> AuthLogs { get; set; } = [];
+
+ /// <summary>
+ /// Event callback that is invoked when the list of active sessions changes.
+ /// </summary>
+ [Parameter]
+ public EventCallback OnSessionsChanged { get; set; }
+
+ private bool IsLoading { get; set; } = true;
+
+ /// <summary>
+ /// Loads the active sessions data from the server.
+ /// </summary>
+ /// <returns>A task representing the asynchronous operation.</returns>
+ public async Task LoadData()
+ {
+ IsLoading = true;
+ StateHasChanged();
+ var authlogResponse = await Http.GetFromJsonAsync<List<AuthLogModel>>("api/v1/Security/authlogs");
+ if (authlogResponse is not null)
+ {
+ AuthLogs = authlogResponse;
+ }
+
+ IsLoading = false;
+ StateHasChanged();
+ }
+
+ /// <summary>
+ /// Revokes a specific session (refresh token) for the current user.
+ /// </summary>
+ /// <param name="id">The unique identifier of the session to revoke.</param>
+ /// <returns>A task representing the asynchronous operation.</returns>
+ private async Task RevokeSession(Guid id)
+ {
+ try
+ {
+ var response = await Http.DeleteAsync($"api/v1/Security/sessions/{id}");
+ if (response.IsSuccessStatusCode)
+ {
+ GlobalNotificationService.AddSuccessMessage("Session revoked successfully.", true);
+ await OnSessionsChanged.InvokeAsync();
+ }
+ else
+ {
+ GlobalNotificationService.AddErrorMessage("Failed to revoke session.", true);
+ }
+ }
+ catch (Exception ex)
+ {
+ GlobalNotificationService.AddErrorMessage($"Failed to revoke session: {ex.Message}.", true);
+ }
+ }
+}
diff --git a/src/AliasVault.Client/Main/Pages/Settings/Security/Security.razor b/src/AliasVault.Client/Main/Pages/Settings/Security/Security.razor
index dbe3f1f30..e802ea587 100644
--- a/src/AliasVault.Client/Main/Pages/Settings/Security/Security.razor
+++ b/src/AliasVault.Client/Main/Pages/Settings/Security/Security.razor
@@ -17,10 +17,12 @@
<TwoFactorAuthenticationSection @ref="TwoFactorSection" OnStatusChanged="LoadData" />
<ActiveSessionsSection @ref="SessionsSection" OnSessionsChanged="LoadData" />
+<RecentAuthLogsSection @ref="RecentAuthLogsSection" OnSessionsChanged="LoadData" />
@code {
private TwoFactorAuthenticationSection? TwoFactorSection;
private ActiveSessionsSection? SessionsSection;
+ private RecentAuthLogsSection? RecentAuthLogsSection;
/// <inheritdoc />
protected override async Task OnInitializedAsync()
@@ -51,5 +53,6 @@
// When switched over to PostgreSQL, we can load the data concurrently.
await TwoFactorSection!.LoadData();
await SessionsSection!.LoadData();
+ await RecentAuthLogsSection!.LoadData();
}
}
diff --git a/src/AliasVault.Client/wwwroot/css/tailwind.css b/src/AliasVault.Client/wwwroot/css/tailwind.css
index baa99ff52..736e6ff22 100644
--- a/src/AliasVault.Client/wwwroot/css/tailwind.css
+++ b/src/AliasVault.Client/wwwroot/css/tailwind.css
@@ -1319,6 +1319,11 @@ video {
background-color: rgb(31 41 55 / var(--tw-bg-opacity));
}
+.bg-green-100 {
+ --tw-bg-opacity: 1;
+ background-color: rgb(220 252 231 / var(--tw-bg-opacity));
+}
+
.bg-green-50 {
--tw-bg-opacity: 1;
background-color: rgb(240 253 244 / var(--tw-bg-opacity));
@@ -1339,11 +1344,6 @@ video {
background-color: rgb(253 222 133 / var(--tw-bg-opacity));
}
-.bg-primary-200 {
- --tw-bg-opacity: 1;
- background-color: rgb(251 203 116 / var(--tw-bg-opacity));
-}
-
.bg-primary-300 {
--tw-bg-opacity: 1;
background-color: rgb(248 185 99 / var(--tw-bg-opacity));
@@ -1364,6 +1364,11 @@ video {
background-color: rgb(184 112 47 / var(--tw-bg-opacity));
}
+.bg-red-100 {
+ --tw-bg-opacity: 1;
+ background-color: rgb(254 226 226 / var(--tw-bg-opacity));
+}
+
.bg-red-50 {
--tw-bg-opacity: 1;
background-color: rgb(254 242 242 / var(--tw-bg-opacity));
@@ -2206,6 +2211,11 @@ video {
color: rgb(248 113 113 / var(--tw-text-opacity));
}
+.dark\:text-red-500:is(.dark *) {
+ --tw-text-opacity: 1;
+ color: rgb(239 68 68 / var(--tw-text-opacity));
+}
+
.dark\:text-sky-300:is(.dark *) {
--tw-text-opacity: 1;
color: rgb(125 211 252 / var(--tw-text-opacity));
diff --git a/src/AliasVault.Shared/Models/Enums/AuthEventType.cs b/src/AliasVault.Shared/Models/Enums/AuthEventType.cs
new file mode 100644
index 000000000..bf86f018b
--- /dev/null
+++ b/src/AliasVault.Shared/Models/Enums/AuthEventType.cs
@@ -0,0 +1,54 @@
+//-----------------------------------------------------------------------
+// <copyright file="AuthEventType.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>
+/// Represents the type of authentication event.
+/// </summary>
+public enum AuthEventType
+{
+ /// <summary>
+ /// Represents a standard login attempt.
+ /// </summary>
+ Login = 1,
+
+ /// <summary>
+ /// Represents a two-factor authentication attempt.
+ /// </summary>
+ TwoFactorAuthentication = 2,
+
+ /// <summary>
+ /// Represents a user logout event.
+ /// </summary>
+ Logout = 3,
+
+ /// <summary>
+ /// Represents JWT access token refresh event issued by client to API.
+ /// </summary>
+ TokenRefresh = 10,
+
+ /// <summary>
+ /// Represents a password reset event.
+ /// </summary>
+ PasswordReset = 20,
+
+ /// <summary>
+ /// Represents a password change event.
+ /// </summary>
+ PasswordChange = 21,
+
+ /// <summary>
+ /// Represents enabling two-factor authentication in settings.
+ /// </summary>
+ TwoFactorAuthEnable = 22,
+
+ /// <summary>
+ /// Represents disabling two-factor authentication in settings.
+ /// </summary>
+ TwoFactorAuthDisable = 23,
+}
diff --git a/src/AliasVault.Shared/Models/WebApi/Security/AuthLogModel.cs b/src/AliasVault.Shared/Models/WebApi/Security/AuthLogModel.cs
new file mode 100644
index 000000000..e0c0059be
--- /dev/null
+++ b/src/AliasVault.Shared/Models/WebApi/Security/AuthLogModel.cs
@@ -0,0 +1,51 @@
+//-----------------------------------------------------------------------
+// <copyright file="AuthLogModel.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.Security;
+
+using AliasVault.Shared.Models.Enums;
+
+/// <summary>
+/// Auth Log model.
+/// </summary>
+public class AuthLogModel
+{
+ /// <summary>
+ /// Gets or sets the primary key for the auth log entry.
+ /// </summary>
+ public int Id { get; set; }
+
+ /// <summary>
+ /// Gets or sets the timestamp of the auth log entry.
+ /// </summary>
+ public DateTime Timestamp { get; set; }
+
+ /// <summary>
+ /// Gets or sets the type of authentication event.
+ /// </summary>
+ public AuthEventType EventType { get; set; }
+
+ /// <summary>
+ /// Gets or sets the username associated with the auth log entry.
+ /// </summary>
+ public string Username { get; set; } = string.Empty;
+
+ /// <summary>
+ /// Gets or sets the IP address from which the authentication attempt was made.
+ /// </summary>
+ public string IpAddress { get; set; } = string.Empty;
+
+ /// <summary>
+ /// Gets or sets the user agent string of the device used for the authentication attempt.
+ /// </summary>
+ public string UserAgent { get; set; } = string.Empty;
+
+ /// <summary>
+ /// Gets or sets a value indicating whether the authentication attempt was successful.
+ /// </summary>
+ public bool IsSuccess { get; set; }
+}
diff --git a/src/Databases/AliasServerDb/AliasServerDb.csproj b/src/Databases/AliasServerDb/AliasServerDb.csproj
index 02980efdd..624357d10 100644
--- a/src/Databases/AliasServerDb/AliasServerDb.csproj
+++ b/src/Databases/AliasServerDb/AliasServerDb.csproj
@@ -44,6 +44,7 @@
</ItemGroup>
<ItemGroup>
+ <ProjectReference Include="..\..\AliasVault.Shared\AliasVault.Shared.csproj" />
<ProjectReference Include="..\..\Utilities\AliasVault.WorkerStatus\AliasVault.WorkerStatus.csproj" />
</ItemGroup>
diff --git a/src/Databases/AliasServerDb/AuthLog.cs b/src/Databases/AliasServerDb/AuthLog.cs
index de0a0d954..6e6eb6c4e 100644
--- a/src/Databases/AliasServerDb/AuthLog.cs
+++ b/src/Databases/AliasServerDb/AuthLog.cs
@@ -8,54 +8,9 @@
namespace AliasServerDb;
using System.ComponentModel.DataAnnotations;
+using AliasVault.Shared.Models.Enums;
using Microsoft.EntityFrameworkCore;
-/// <summary>
-/// Represents the type of authentication event.
-/// </summary>
-public enum AuthEventType
-{
- /// <summary>
- /// Represents a standard login attempt.
- /// </summary>
- Login = 1,
-
- /// <summary>
- /// Represents a two-factor authentication attempt.
- /// </summary>
- TwoFactorAuthentication = 2,
-
- /// <summary>
- /// Represents a user logout event.
- /// </summary>
- Logout = 3,
-
- /// <summary>
- /// Represents JWT access token refresh event issued by client to API.
- /// </summary>
- TokenRefresh = 10,
-
- /// <summary>
- /// Represents a password reset event.
- /// </summary>
- PasswordReset = 20,
-
- /// <summary>
- /// Represents a password change event.
- /// </summary>
- PasswordChange = 21,
-
- /// <summary>
- /// Represents enabling two-factor authentication in settings.
- /// </summary>
- TwoFactorAuthEnable = 22,
-
- /// <summary>
- /// Represents disabling two-factor authentication in settings.
- /// </summary>
- TwoFactorAuthDisable = 23,
-}
-
/// <summary>
/// Represents the reason for an authentication failure.
/// </summary>
diff --git a/src/Utilities/AliasVault.AuthLogging/AuthLoggingService.cs b/src/Utilities/AliasVault.AuthLogging/AuthLoggingService.cs
index 9c9cd5e40..b113fe04a 100644
--- a/src/Utilities/AliasVault.AuthLogging/AuthLoggingService.cs
+++ b/src/Utilities/AliasVault.AuthLogging/AuthLoggingService.cs
@@ -8,6 +8,7 @@
namespace AliasVault.AuthLogging;
using AliasServerDb;
+using AliasVault.Shared.Models.Enums;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;