Separate dashboard port, first-run password setup and default trusted proxies
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a34ff96490
commit
c22d452720
25 files changed
+654
-109
No files matched your search
@@ -1,22 +1,64 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using Flypaper.Storage.Settings;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Flypaper.Web.Auth;
|
||||
|
||||
public sealed class AdminAuthenticator(IOptions<AdminOptions> options)
|
||||
public sealed class AdminAuthenticator(IOptions<AdminOptions> options, AdminCredentialStore store)
|
||||
{
|
||||
public bool IsConfigured => !string.IsNullOrEmpty(options.Value.Password);
|
||||
public const int MinimumPasswordLength = 12;
|
||||
|
||||
public bool Verify(string? candidate)
|
||||
private static readonly PasswordHasher<AdminAccount> Hasher = new();
|
||||
private static readonly AdminAccount Account = new();
|
||||
|
||||
public bool UsesEnvironmentPassword => !string.IsNullOrEmpty(options.Value.Password);
|
||||
|
||||
public async Task<bool> IsConfiguredAsync(CancellationToken ct = default) =>
|
||||
UsesEnvironmentPassword || await store.GetPasswordHashAsync(ct) is not null;
|
||||
|
||||
public async Task<bool> VerifyAsync(string? candidate, CancellationToken ct = default)
|
||||
{
|
||||
if (!IsConfigured || string.IsNullOrEmpty(candidate))
|
||||
if (string.IsNullOrEmpty(candidate))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return CryptographicOperations.FixedTimeEquals(Hash(candidate), Hash(options.Value.Password!));
|
||||
if (UsesEnvironmentPassword)
|
||||
{
|
||||
return CryptographicOperations.FixedTimeEquals(Sha256(candidate), Sha256(options.Value.Password!));
|
||||
}
|
||||
|
||||
return await store.GetPasswordHashAsync(ct) is { } hash
|
||||
&& Hasher.VerifyHashedPassword(Account, hash, candidate) != PasswordVerificationResult.Failed;
|
||||
}
|
||||
|
||||
private static byte[] Hash(string value) => SHA256.HashData(Encoding.UTF8.GetBytes(value));
|
||||
public async Task<bool> TrySetInitialPasswordAsync(string password, CancellationToken ct = default) =>
|
||||
!UsesEnvironmentPassword && await store.TryCreatePasswordHashAsync(Hasher.HashPassword(Account, password), ct);
|
||||
|
||||
public async Task<bool> TryChangePasswordAsync(string currentPassword, string newPassword, CancellationToken ct = default)
|
||||
{
|
||||
if (UsesEnvironmentPassword || !await VerifyAsync(currentPassword, ct))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
await store.UpdatePasswordHashAsync(Hasher.HashPassword(Account, newPassword), ct);
|
||||
return true;
|
||||
}
|
||||
|
||||
public static string? ValidateNewPassword(string? password, string? confirmation)
|
||||
{
|
||||
if (string.IsNullOrEmpty(password) || password.Length < MinimumPasswordLength)
|
||||
{
|
||||
return $"Mindestens {MinimumPasswordLength} Zeichen.";
|
||||
}
|
||||
|
||||
return password == confirmation ? null : "Die Passwörter stimmen nicht überein.";
|
||||
}
|
||||
|
||||
private static byte[] Sha256(string value) => SHA256.HashData(Encoding.UTF8.GetBytes(value));
|
||||
|
||||
public sealed class AdminAccount;
|
||||
}
|
||||
Reference in new issue
Block a user