feat: adds FastEndpoints core infrastructure and contracts to the web project

This commit is contained in:
BinaryAlley committed 2026-08-17 11:52:20 -07:00
1 parent 394b33efd2
commit 733e174cac
137 files changed
+3139 -158

No files matched your search

@@ -0,0 +1,11 @@
namespace Lumina.Presentation.Web.Common.Authorization;
/// <summary>
/// Contains the names of the authorization policies defined in the Web application.
/// </summary>
public static class AuthorizationPolicies
{
public const string REQUIRE_ADMIN_ROLE = "RequireAdminRole";
public const string REQUIRE_CREATE_LIBRARIES_PERMISSION = "RequireCreateLibrariesPermission";
public const string REQUIRE_INITIALIZATION = "RequireInitialization";
}
@@ -1,7 +1,7 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Web.Common.Api;
using Lumina.Presentation.Web.Common.Enums.Authorization;
using Lumina.Presentation.Web.Common.Models.Authorization;
using Lumina.Presentation.Web.Common.Responses.Authorization;
using Microsoft.AspNetCore.Http;
using System;
using System.Linq;
@@ -55,42 +55,6 @@ public class AuthorizationService : IAuthorizationService
return authorization.Role == role;
}
/// <summary>
/// Evaluates whether the currently logged in user meets the conditions defined in the specified authorization policy.
/// </summary>
/// <typeparam name="TAuthorizationPolicy">The type of authorization policy to evaluate.</typeparam>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns><see langword="true"/> if the currently logged in user satisfies the policy, <see langword="false"/> otherwise.</returns>
public Task<bool> EvaluatePolicyAsync<TAuthorizationPolicy>(CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
/// <summary>
/// Evaluates whether the currently logged in user meets the conditions defined in the specified authorization requirement.
/// </summary>
/// <param name="requirement">The authorization requirements to check.</param>
/// <returns><see langword="true"/> if the currently logged in user satisfies the requirement, <see langword="false"/> otherwise.</returns>
public async Task<bool> EvaluateAuthorizationAsync(AuthorizationRequirementModel requirement)
{
GetAuthorizationResponse authorization = await GetUserAuthorizationAsync(CancellationToken.None);
if (requirement.Roles?.Length > 0 && !requirement.Roles.Any(role => authorization.Role?.Contains(role) == true))
return false;
if (requirement.Permissions?.Length > 0 && !requirement.Permissions.Any(permission => authorization.Permissions.Contains(permission)))
return false;
//if (_requirement.PolicyType is not null)
//{
// bool response = await _apiHttpClient.PostAsync<bool, object>($"auth/evaluate-policy/{_requirement.PolicyType.Name}", null!);
// if (!response)
// return false;
//}
return true;
}
/// <summary>
/// Retrieves user authorization details from the authorization service.
/// </summary>
@@ -1,6 +1,5 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Web.Common.Enums.Authorization;
using Lumina.Presentation.Web.Common.Models.Authorization;
using System.Threading;
using System.Threading.Tasks;
#endregion
@@ -27,19 +26,4 @@ public interface IAuthorizationService
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns><see langword="true"/> if the currently logged in user is in the specified role, <see langword="false"/> otherwise.</returns>
Task<bool> IsInRoleAsync(string role, CancellationToken cancellationToken);
/// <summary>
/// Evaluates whether the currently logged in user meets the conditions defined in the specified authorization policy.
/// </summary>
/// <typeparam name="TAuthorizationPolicy">The type of authorization policy to evaluate.</typeparam>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns><see langword="true"/> if the currently logged in user satisfies the policy, <see langword="false"/> otherwise.</returns>
Task<bool> EvaluatePolicyAsync<TAuthorizationPolicy>(CancellationToken cancellationToken);
/// <summary>
/// Evaluates whether the currently logged in user meets the conditions defined in the specified authorization requirement.
/// </summary>
/// <param name="requirement">The authorization requirements to check.</param>
/// <returns><see langword="true"/> if the currently logged in user satisfies the requirement, <see langword="false"/> otherwise.</returns>
Task<bool> EvaluateAuthorizationAsync(AuthorizationRequirementModel requirement);
}
@@ -0,0 +1,32 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Web.Common.Authorization;
/// <summary>
/// FastEndpoints pre-processor that runs the application initialization check before the endpoint handler executes.
/// </summary>
/// <remarks>
/// The check runs the <c>RequireInitialization</c> authorization policy, which queries the remote API and records the pending super admin setup state in the session.
/// Its result is deliberately ignored, because the pages that use this processor must be reachable both before and after the application is initialized.
/// </remarks>
/// <typeparam name="TRequest">The type of the request of the endpoint to which this pre-processor is applied.</typeparam>
public class InitializationCheckPreProcessor<TRequest> : IPreProcessor<TRequest> where TRequest : notnull
{
/// <summary>
/// Runs the initialization check for the current request.
/// </summary>
/// <param name="preProcessorContext">The pre-processor context for the current request.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
public async Task PreProcessAsync(IPreProcessorContext<TRequest> preProcessorContext, CancellationToken cancellationToken)
{
HttpContext httpContext = preProcessorContext.HttpContext;
Microsoft.AspNetCore.Authorization.IAuthorizationService authorizationService = httpContext.RequestServices.GetRequiredService<Microsoft.AspNetCore.Authorization.IAuthorizationService>();
await authorizationService.AuthorizeAsync(httpContext.User, httpContext, AuthorizationPolicies.REQUIRE_INITIALIZATION).ConfigureAwait(false);
}
}
@@ -1,7 +1,7 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Web.Common.Api;
using Lumina.Presentation.Web.Common.Http;
using Lumina.Presentation.Web.Common.Models.UsersManagement;
using Lumina.Presentation.Web.Common.Responses.UsersManagement;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
@@ -28,17 +28,17 @@ public class InitializationHandler : AuthorizationHandler<InitializationRequirem
/// <summary>
/// Checks the application initialization status and authorizes the requirement if initialized.
/// </summary>
/// <param name="context">The authorization context.</param>
/// <param name="requirement">The initialization requirement to be evaluated.</param>
protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, InitializationRequirement requirement)
/// <param name="authorizationHandlerContext">The authorization context.</param>
/// <param name="initializationRequirement">The initialization requirement to be evaluated.</param>
protected override async Task HandleRequirementAsync(AuthorizationHandlerContext authorizationHandlerContext, InitializationRequirement initializationRequirement)
{
InitializationModel result = await _apiHttpClient.GetAsync<InitializationModel>("initialization/").ConfigureAwait(false);
HttpContext? httpContext = context.Resource as HttpContext;
InitializationResponse result = await _apiHttpClient.GetAsync<InitializationResponse>("initialization/").ConfigureAwait(false);
HttpContext? httpContext = authorizationHandlerContext.Resource as HttpContext;
if (result.IsInitialized)
{
// admin account was registered, remove this requirement from the session
httpContext?.Session.Remove(HttpContextItemKeys.PENDING_SUPER_ADMIN_SETUP);
context.Succeed(requirement);
authorizationHandlerContext.Succeed(initializationRequirement);
}
else // store in session that super admin setup is needed
httpContext?.Session.SetString(HttpContextItemKeys.PENDING_SUPER_ADMIN_SETUP, "true");
@@ -0,0 +1,42 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Web.Common.Enums.Authorization;
using Microsoft.AspNetCore.Authorization;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Web.Common.Authorization;
/// <summary>
/// Handles the <see cref="PermissionRequirement"/> by checking the currently logged in user's permissions against the remote API.
/// </summary>
public class PermissionAuthorizationHandler : AuthorizationHandler<PermissionRequirement>
{
private readonly IAuthorizationService _authorizationService;
/// <summary>
/// Initializes a new instance of the <see cref="PermissionAuthorizationHandler"/> class.
/// </summary>
/// <param name="authorizationService">Injected service for authorization checks against the remote API.</param>
public PermissionAuthorizationHandler(IAuthorizationService authorizationService)
{
_authorizationService = authorizationService;
}
/// <summary>
/// Succeeds the requirement when the currently logged in user holds any of the required permissions.
/// </summary>
/// <param name="authorizationHandlerContext">The authorization context.</param>
/// <param name="permissionRequirement">The permission requirement to evaluate.</param>
protected override async Task HandleRequirementAsync(AuthorizationHandlerContext authorizationHandlerContext, PermissionRequirement permissionRequirement)
{
foreach (AuthorizationPermission permission in permissionRequirement.Permissions)
{
if (await _authorizationService.HasPermissionAsync(permission, CancellationToken.None).ConfigureAwait(false))
{
authorizationHandlerContext.Succeed(permissionRequirement);
return;
}
}
}
}
@@ -0,0 +1,26 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Web.Common.Enums.Authorization;
using Microsoft.AspNetCore.Authorization;
#endregion
namespace Lumina.Presentation.Web.Common.Authorization;
/// <summary>
/// Defines a requirement that the user must hold at least one of the specified permissions.
/// </summary>
public class PermissionRequirement : IAuthorizationRequirement
{
/// <summary>
/// Gets the permissions that allow access, of which the user must hold at least one.
/// </summary>
public AuthorizationPermission[] Permissions { get; }
/// <summary>
/// Initializes a new instance of the <see cref="PermissionRequirement"/> class.
/// </summary>
/// <param name="permissions">The permissions that allow access.</param>
public PermissionRequirement(params AuthorizationPermission[] permissions)
{
Permissions = permissions;
}
}
@@ -0,0 +1,42 @@
#region ========================================================================= USING =====================================================================================
using Microsoft.AspNetCore.Authorization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Web.Common.Authorization;
/// <summary>
/// Handles the <see cref="RoleRequirement"/> by checking the currently logged in user's role against the remote API.
/// </summary>
public class RoleAuthorizationHandler : AuthorizationHandler<RoleRequirement>
{
private readonly IAuthorizationService _authorizationService;
/// <summary>
/// Initializes a new instance of the <see cref="RoleAuthorizationHandler"/> class.
/// </summary>
/// <param name="authorizationService">Injected service for authorization checks against the remote API.</param>
public RoleAuthorizationHandler(IAuthorizationService authorizationService)
{
_authorizationService = authorizationService;
}
/// <summary>
/// Succeeds the requirement when the currently logged in user belongs to any of the required roles.
/// </summary>
/// <param name="authorizationHandlerContext">The authorization context.</param>
/// <param name="roleRequirement">The role requirement to evaluate.</param>
protected override async Task HandleRequirementAsync(AuthorizationHandlerContext authorizationHandlerContext, RoleRequirement roleRequirement)
{
foreach (string role in roleRequirement.Roles)
{
if (await _authorizationService.IsInRoleAsync(role, CancellationToken.None).ConfigureAwait(false))
{
authorizationHandlerContext.Succeed(roleRequirement);
return;
}
}
}
}
@@ -0,0 +1,25 @@
#region ========================================================================= USING =====================================================================================
using Microsoft.AspNetCore.Authorization;
#endregion
namespace Lumina.Presentation.Web.Common.Authorization;
/// <summary>
/// Defines a requirement that the user must belong to at least one of the specified roles.
/// </summary>
public class RoleRequirement : IAuthorizationRequirement
{
/// <summary>
/// Gets the roles that allow access, of which the user must belong to at least one.
/// </summary>
public string[] Roles { get; }
/// <summary>
/// Initializes a new instance of the <see cref="RoleRequirement"/> class.
/// </summary>
/// <param name="roles">The roles that allow access.</param>
public RoleRequirement(params string[] roles)
{
Roles = roles;
}
}