feat(mvp): complete MVP hardening — API, Web, config system, SignalR, tests
This commit is contained in:
1 parent
3fc42be4b2
commit
c2837af089
59 files changed
+4920
-81
No files matched your search
@@ -1,58 +1,82 @@
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
|
||||
using Heimdall.Infrastructure.Security;
|
||||
|
||||
namespace Heimdall.Web.Services;
|
||||
|
||||
public class HeimdallAuthStateProvider : AuthenticationStateProvider
|
||||
{
|
||||
private readonly ProtectedLocalStorage _storage;
|
||||
private readonly AuthState _authState;
|
||||
|
||||
public HeimdallAuthStateProvider(AuthState authState)
|
||||
private const string StorageKey = "heimdall_auth";
|
||||
|
||||
public HeimdallAuthStateProvider(ProtectedLocalStorage storage, AuthState authState)
|
||||
{
|
||||
_storage = storage;
|
||||
_authState = authState;
|
||||
}
|
||||
|
||||
public override Task<AuthenticationState> GetAuthenticationStateAsync()
|
||||
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
|
||||
{
|
||||
if (!_authState.IsLoggedIn || _authState.User is null)
|
||||
try
|
||||
{
|
||||
return Task.FromResult(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())));
|
||||
var result = await _storage.GetAsync<AuthResponse>(StorageKey);
|
||||
if (!result.Success || result.Value is null)
|
||||
return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
|
||||
|
||||
var authResponse = result.Value;
|
||||
_authState.Login(authResponse);
|
||||
|
||||
return CreateAuthenticationState(authResponse);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
|
||||
}
|
||||
}
|
||||
|
||||
public async Task NotifyLoginAsync(AuthResponse response)
|
||||
{
|
||||
await _storage.SetAsync(StorageKey, response);
|
||||
_authState.Login(response);
|
||||
NotifyAuthenticationStateChanged(Task.FromResult(CreateAuthenticationState(response)));
|
||||
}
|
||||
|
||||
public async Task NotifyLogoutAsync()
|
||||
{
|
||||
await _storage.DeleteAsync(StorageKey);
|
||||
_authState.Logout();
|
||||
NotifyAuthenticationStateChanged(
|
||||
Task.FromResult(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()))));
|
||||
}
|
||||
|
||||
private static AuthenticationState CreateAuthenticationState(AuthResponse response)
|
||||
{
|
||||
if (response.User is null)
|
||||
return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
|
||||
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
new(ClaimTypes.NameIdentifier, _authState.User.Id.ToString()),
|
||||
new(ClaimTypes.Name, _authState.User.DisplayName),
|
||||
new(ClaimTypes.Email, _authState.User.Email),
|
||||
new(ClaimTypes.NameIdentifier, response.User.Id.ToString()),
|
||||
new(ClaimTypes.Name, response.User.DisplayName),
|
||||
new(ClaimTypes.Email, response.User.Email),
|
||||
new(ClaimTypes.AuthenticationMethod, "Bearer")
|
||||
};
|
||||
|
||||
if (_authState.User.Roles is not null)
|
||||
{
|
||||
foreach (var role in _authState.User.Roles)
|
||||
{
|
||||
claims.Add(new Claim(ClaimTypes.Role, role));
|
||||
claims.Add(new Claim("permission", role)); // Map role to permission for policies
|
||||
}
|
||||
}
|
||||
foreach (var role in response.User.Roles ?? [])
|
||||
claims.Add(new Claim(ClaimTypes.Role, role));
|
||||
|
||||
var identity = new ClaimsIdentity(claims, "HeimdallAuth");
|
||||
return Task.FromResult(new AuthenticationState(new ClaimsPrincipal(identity)));
|
||||
}
|
||||
// Map roles → permissions via the AppPermissions dictionary (same as AuthService)
|
||||
var permissions = response.User.Roles?
|
||||
.SelectMany(r => AppPermissions.RolePermissions.TryGetValue(r, out var perms) ? perms : [])
|
||||
.ToList() ?? [];
|
||||
|
||||
public void NotifyAuthStateChanged()
|
||||
{
|
||||
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
|
||||
}
|
||||
foreach (var permission in permissions)
|
||||
claims.Add(new Claim("permission", permission));
|
||||
|
||||
public void NotifyLogin(AuthResponse response)
|
||||
{
|
||||
_authState.Login(response);
|
||||
NotifyAuthStateChanged();
|
||||
}
|
||||
|
||||
public void NotifyLogout()
|
||||
{
|
||||
_authState.Logout();
|
||||
NotifyAuthStateChanged();
|
||||
var identity = new ClaimsIdentity(claims, "Bearer");
|
||||
return new AuthenticationState(new ClaimsPrincipal(identity));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user