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