AuthServiceFix
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Server.Data;
|
||||
using Server.Services.Auth;
|
||||
|
||||
namespace Server.Services.Auth;
|
||||
|
||||
@@ -21,7 +21,7 @@ public sealed class AdminAuthResult
|
||||
public bool IsAuthorized { get; init; }
|
||||
public Account? Account { get; init; }
|
||||
public string? Error { get; init; }
|
||||
|
||||
|
||||
public static AdminAuthResult Success(Account account) => new() { IsAuthorized = true, Account = account };
|
||||
public static AdminAuthResult Fail(string error) => new() { IsAuthorized = false, Error = error };
|
||||
}
|
||||
@@ -29,12 +29,12 @@ public sealed class AdminAuthResult
|
||||
public sealed class AdminAuthService : IAdminAuthService
|
||||
{
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
private readonly IPasswordHasher _hasher;
|
||||
private readonly ILogger<AdminAuthService> _log;
|
||||
|
||||
public AdminAuthService(IServiceScopeFactory scopeFactory, IPasswordHasher hasher)
|
||||
public AdminAuthService(IServiceScopeFactory scopeFactory, ILogger<AdminAuthService> log)
|
||||
{
|
||||
_scopeFactory = scopeFactory;
|
||||
_hasher = hasher;
|
||||
_log = log;
|
||||
}
|
||||
|
||||
public async Task<AdminAuthResult> ValidateAsync(string? token, AccountRole requiredRole = AccountRole.Admin)
|
||||
@@ -46,18 +46,23 @@ public sealed class AdminAuthService : IAdminAuthService
|
||||
var db = scope.ServiceProvider.GetRequiredService<GameDbContext>();
|
||||
|
||||
// Hash the token to match against stored hash
|
||||
var tokenHash = _hasher.HashToken(token);
|
||||
var tokenHash = HashTokenToString(token);
|
||||
|
||||
_log.LogDebug("Validating token hash: {Hash}", tokenHash);
|
||||
|
||||
// Find valid session with this token
|
||||
var session = await db.Sessions
|
||||
.Include(s => s.Account)
|
||||
.Where(s => s.TokenHash == tokenHash.ToString()
|
||||
&& s.RevokedAt == null
|
||||
.Where(s => s.TokenHash == tokenHash
|
||||
&& s.RevokedAt == null
|
||||
&& s.ExpiresAt > DateTime.UtcNow)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
if (session == null)
|
||||
{
|
||||
_log.LogDebug("No session found for hash {Hash}", tokenHash);
|
||||
return AdminAuthResult.Fail("Invalid or expired token");
|
||||
}
|
||||
|
||||
var account = session.Account;
|
||||
|
||||
@@ -70,19 +75,14 @@ public sealed class AdminAuthService : IAdminAuthService
|
||||
if (account.Role < requiredRole)
|
||||
return AdminAuthResult.Fail($"Insufficient permissions. Required: {requiredRole}, Current: {account.Role}");
|
||||
|
||||
_log.LogDebug("Token validated for user {User} with role {Role}", account.Username, account.Role);
|
||||
return AdminAuthResult.Success(account);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extension methods for IPasswordHasher to support token hashing
|
||||
/// </summary>
|
||||
public static class PasswordHasherExtensions
|
||||
{
|
||||
public static byte[] HashToken(this IPasswordHasher hasher, string token)
|
||||
private static string HashTokenToString(string token)
|
||||
{
|
||||
// Use SHA256 for token hashing (deterministic, no salt needed for lookup)
|
||||
using var sha = System.Security.Cryptography.SHA256.Create();
|
||||
return sha.ComputeHash(System.Text.Encoding.UTF8.GetBytes(token));
|
||||
var hashBytes = sha.ComputeHash(System.Text.Encoding.UTF8.GetBytes(token));
|
||||
return Convert.ToHexString(hashBytes).ToLowerInvariant(); // Hex is safer than base64
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user