#region ========================================================================= USING ===================================================================================== using Lumina.Presentation.Web.Common.Api; using Lumina.Presentation.Web.Common.Authorization; using Lumina.Presentation.Web.Common.Enums.Authorization; using Microsoft.AspNetCore.Http; using System; using System.Linq; using System.Security.Claims; using System.Threading; using System.Threading.Tasks; #endregion namespace Lumina.Presentation.Web.Common.Models.Authorization; /// /// Implements authorization logic for API endpoints by validating user roles, permissions, and policies. /// public class AuthorizationService : IAuthorizationService { private readonly IApiHttpClient _apiHttpClient; private readonly IHttpContextAccessor _httpContextAccessor; /// /// Initializes a new instance of the class. /// /// Injected HTTP typed client for interactions with the API. /// The used to access the current HTTP context. public AuthorizationService(IApiHttpClient apiHttpClient, IHttpContextAccessor httpContextAccessor) { _apiHttpClient = apiHttpClient; _httpContextAccessor = httpContextAccessor; } /// /// Determines whether the currently logged in user has a specific permission. /// /// The permission to check. /// Cancellation token that can be used to stop the execution. /// if the currently logged in user has the specified permission, otherwise. public async Task HasPermissionAsync(AuthorizationPermission permission, CancellationToken cancellationToken) { GetAuthorizationResponse authorization = await GetUserAuthorizationAsync(cancellationToken).ConfigureAwait(false); return authorization.Permissions.Contains(permission); } /// /// Determines whether the currently logged in user belongs to a specific role. /// /// The role to check. /// Cancellation token that can be used to stop the execution. /// if the currently logged in user is in the specified role, otherwise. public async Task IsInRoleAsync(AuthorizationRole role, CancellationToken cancellationToken) { GetAuthorizationResponse authorization = await GetUserAuthorizationAsync(cancellationToken).ConfigureAwait(false); return authorization.Roles.Contains(role); } /// /// Evaluates whether the currently logged in user meets the conditions defined in the specified authorization policy. /// /// The type of authorization policy to evaluate. /// Cancellation token that can be used to stop the execution. /// if the currently logged in user satisfies the policy, otherwise. public Task EvaluatePolicyAsync(CancellationToken cancellationToken) { throw new NotImplementedException(); } /// /// Evaluates whether the currently logged in user meets the conditions defined in the specified authorization requirement. /// /// The authorization requirements to check. /// if the currently logged in user satisfies the requirement, otherwise. public async Task EvaluateAuthorizationAsync(AuthorizationRequirementModel requirement) { GetAuthorizationResponse authorization = await GetUserAuthorizationAsync(CancellationToken.None); if (requirement.Roles?.Length > 0 && !requirement.Roles.Any(role => authorization.Roles?.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($"auth/evaluate-policy/{_requirement.PolicyType.Name}", null!); // if (!response) // return false; //} return true; } /// /// Retrieves user authorization details from the authorization service. /// /// Cancellation token that can be used to stop the execution. /// The user's authorization details. private async Task GetUserAuthorizationAsync(CancellationToken cancellationToken) { // get the id of the currently logged in user string userId = _httpContextAccessor.HttpContext!.User.FindFirst(ClaimTypes.NameIdentifier)!.Value; return await _apiHttpClient.GetAsync($"auth/get-authorization?userId={Uri.EscapeDataString(userId.ToString())}", cancellationToken); } }