Fix admin bugs (#113)

This commit is contained in:
Leendert de Borst
2024-07-22 23:57:37 +02:00
parent d87800f370
commit bf68e380bc
4 changed files with 9 additions and 14 deletions

View File

@@ -35,9 +35,6 @@ else
</li>
<li>
<p class="text-gray-700 dark:text-gray-300">Scan the QR Code or enter this key <kbd class="px-2 py-1.5 text-xs font-semibold text-gray-800 bg-gray-100 border border-gray-200 rounded-lg dark:bg-gray-600 dark:text-gray-100 dark:border-gray-500">@sharedKey</kbd> into your two factor authenticator app. Spaces and casing do not matter.</p>
<div class="mt-2 p-4 bg-blue-100 text-blue-700 rounded-md dark:bg-blue-900 dark:text-blue-300">
Learn how to <a href="https://go.microsoft.com/fwlink/?Linkid=852423" class="text-blue-800 hover:underline dark:text-blue-200">enable QR code generation</a>.
</div>
<div id="authenticator-uri" data-url="@authenticatorUri" class="mt-4"></div>
</li>
<li>
@@ -129,8 +126,8 @@ else
sharedKey = FormatKey(unformattedKey!);
var email = await UserManager.GetEmailAsync(user);
authenticatorUri = GenerateQrCodeUri(email!, unformattedKey!);
var username = await UserManager.GetUserNameAsync(user);
authenticatorUri = GenerateQrCodeUri(username!, unformattedKey!);
}
private string FormatKey(string unformattedKey)
@@ -151,13 +148,13 @@ else
return result.ToString().ToLowerInvariant();
}
private string GenerateQrCodeUri(string email, string unformattedKey)
private string GenerateQrCodeUri(string username, string unformattedKey)
{
return string.Format(
CultureInfo.InvariantCulture,
AuthenticatorUriFormat,
UrlEncoder.Encode("AliasVault Admin"),
UrlEncoder.Encode(email),
UrlEncoder.Encode(username),
unformattedKey);
}

View File

@@ -33,7 +33,7 @@
private string? username;
private string? phoneNumber;
[SupplyParameterFromForm] private InputModel Input { get; } = new();
[SupplyParameterFromForm] private InputModel Input { get; set; } = new();
/// <inheritdoc />
protected override async Task OnInitializedAsync()

View File

@@ -3,7 +3,6 @@
@using Microsoft.AspNetCore.Identity
@inject UserManager<AdminUser> UserManager
@inject SignInManager<AdminUser> SignInManager
@inject ILogger<ResetAuthenticator> Logger
<LayoutPageTitle>Reset authenticator key</LayoutPageTitle>
@@ -36,12 +35,10 @@
var userId = await UserManager.GetUserIdAsync(UserService.User());
Logger.LogInformation("User with ID '{UserId}' has reset their authentication app key.", userId);
await SignInManager.RefreshSignInAsync(UserService.User());
GlobalNotificationService.AddSuccessMessage("Your authenticator app key has been reset, you will need to configure your authenticator app using the new key.");
GlobalNotificationService.AddSuccessMessage("Your authenticator app key has been reset, you will need to re-configure your authenticator app using the new key.", true);
NavigationService.RedirectTo(
"account/manage/enable-authenticator");
"account/manage/2fa");
}
}

View File

@@ -52,6 +52,7 @@ public static class StartupTasks
await userManager.CreateAsync(adminUser);
adminUser.PasswordHash = adminPasswordHash;
adminUser.LastPasswordChanged = DateTime.UtcNow;
await userManager.UpdateAsync(adminUser);
Console.WriteLine("Admin user created.");
@@ -60,7 +61,7 @@ public static class StartupTasks
{
// Check if the password hash is different AND the password in .env file is newer than the password of user.
// If so, update the password hash of the user in the database so it matches the one in the .env file.
if (adminUser.PasswordHash != config.AdminPasswordHash && config.LastPasswordChanged > adminUser.LastPasswordChanged)
if (adminUser.PasswordHash != config.AdminPasswordHash && (adminUser.LastPasswordChanged is null || config.LastPasswordChanged > adminUser.LastPasswordChanged))
{
// The password has been changed in the .env file, update the user's password hash.
adminUser.PasswordHash = config.AdminPasswordHash;