fix(cqrs): implement missing auth service interfaces and fix tests
- Create RolePermissionService (delegates to AppPermissions) - Create AuthSignInService (wraps SignInManager) - Create AuthDataService (handles settings, first-user, token revocation) - Register all new services in Infrastructure DI - Fix AuthDataService RevokedRefreshToken constructor usage - All 226 tests passing
This commit is contained in:
1 parent
df644c8b2a
commit
6978d2e9ab
4 files changed
+102
No files matched your search
@@ -108,6 +108,15 @@ public static class DependencyInjection
|
||||
// Credential encryption
|
||||
services.AddScoped<ICredentialProtector, DataProtectorCredentialProtector>();
|
||||
|
||||
// Role permissions (used by CQRS auth handlers)
|
||||
services.AddScoped<IRolePermissionService, RolePermissionService>();
|
||||
|
||||
// Auth data service (used by CQRS register/refresh/logout handlers)
|
||||
services.AddScoped<IAuthDataService, AuthDataService>();
|
||||
|
||||
// Auth sign-in service (used by CQRS login handler)
|
||||
services.AddScoped<IAuthSignInService, AuthSignInService>();
|
||||
|
||||
// Data access
|
||||
services.AddScoped<IUserRepository, UserRepository>();
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
using Heimdall.Application.Services;
|
||||
using Heimdall.Infrastructure.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Heimdall.Infrastructure.Security;
|
||||
|
||||
public class AuthDataService : IAuthDataService
|
||||
{
|
||||
private readonly AppDbContext _db;
|
||||
|
||||
public AuthDataService(AppDbContext db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
public async Task<string?> GetSettingValueAsync(string key, CancellationToken ct)
|
||||
{
|
||||
var setting = await _db.AppSettings
|
||||
.AsNoTracking()
|
||||
.FirstOrDefaultAsync(s => s.Key == key, ct);
|
||||
return setting?.Value;
|
||||
}
|
||||
|
||||
public async Task<bool> IsFirstUserAsync(CancellationToken ct)
|
||||
{
|
||||
return await _db.Users.AnyAsync(ct);
|
||||
}
|
||||
|
||||
public async Task<bool> IsRefreshTokenRevokedAsync(string tokenHash, CancellationToken ct)
|
||||
{
|
||||
return await _db.RevokedRefreshTokens
|
||||
.AnyAsync(r => r.TokenHash == tokenHash, ct);
|
||||
}
|
||||
|
||||
public async Task RevokeRefreshTokenAsync(string refreshToken, Guid userId, CancellationToken ct)
|
||||
{
|
||||
_db.RevokedRefreshTokens.Add(new Heimdall.Domain.Entities.RevokedRefreshToken(
|
||||
refreshToken, userId, DateTime.UtcNow.AddDays(7)));
|
||||
await _db.SaveChangesAsync(ct);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using Heimdall.Application.Services;
|
||||
using Heimdall.Domain.Entities;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
|
||||
namespace Heimdall.Infrastructure.Security;
|
||||
|
||||
public class AuthSignInService : IAuthSignInService
|
||||
{
|
||||
private readonly SignInManager<AppUser> _signInManager;
|
||||
|
||||
public AuthSignInService(SignInManager<AppUser> signInManager)
|
||||
{
|
||||
_signInManager = signInManager;
|
||||
}
|
||||
|
||||
public async Task<SignInServiceResult> PasswordSignInAsync(AppUser user, string password, bool lockoutOnFailure)
|
||||
{
|
||||
var result = await _signInManager.PasswordSignInAsync(user, password, false, lockoutOnFailure);
|
||||
return result switch
|
||||
{
|
||||
{ Succeeded: true } => new SignInServiceResult(SignInResultType.Success),
|
||||
{ IsLockedOut: true } => new SignInServiceResult(SignInResultType.LockedOut),
|
||||
{ RequiresTwoFactor: true } => new SignInServiceResult(SignInResultType.RequiresTwoFactor),
|
||||
_ => new SignInServiceResult(SignInResultType.Failed)
|
||||
};
|
||||
}
|
||||
|
||||
public async Task SignOutAsync()
|
||||
{
|
||||
await _signInManager.SignOutAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using Heimdall.Application.Services;
|
||||
using Heimdall.Infrastructure.Security;
|
||||
|
||||
namespace Heimdall.Infrastructure.Security;
|
||||
|
||||
public class RolePermissionService : IRolePermissionService
|
||||
{
|
||||
public IList<string> GetPermissionsForRoles(IList<string> roles)
|
||||
{
|
||||
var permissions = new List<string>();
|
||||
foreach (var role in roles)
|
||||
{
|
||||
if (AppPermissions.RolePermissions.TryGetValue(role, out var rolePerms))
|
||||
{
|
||||
permissions.AddRange(rolePerms);
|
||||
}
|
||||
}
|
||||
return permissions.Distinct().ToList();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user