| | | 1 | | using Microsoft.AspNetCore.Http; |
| | | 2 | | using Microsoft.Extensions.Options; |
| | | 3 | | |
| | | 4 | | namespace AsiBackbone.AspNetCore.Endpoints; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Middleware that evaluates AsiBackbone endpoint governance metadata before endpoint execution. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// Initializes a new instance of the <see cref="AsiBackboneEndpointGovernanceMiddleware" /> class. |
| | | 11 | | /// </remarks> |
| | | 12 | | /// <param name="next">The next request delegate.</param> |
| | 20 | 13 | | public sealed class AsiBackboneEndpointGovernanceMiddleware(RequestDelegate next) |
| | | 14 | | { |
| | | 15 | | // Example: use a precomputed static result for the most common forbidden response |
| | 2 | 16 | | private static readonly IResult LightweightForbiddenResult = |
| | 2 | 17 | | Microsoft.AspNetCore.Http.Results.StatusCode(StatusCodes.Status403Forbidden); |
| | | 18 | | // Use this for default cases instead of constructing new ProblemDetails every time. |
| | | 19 | | |
| | 20 | 20 | | private readonly RequestDelegate next = next ?? throw new ArgumentNullException(nameof(next)); |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Evaluates endpoint governance metadata and either continues execution or writes a failure result. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="httpContext">The current HTTP context.</param> |
| | | 26 | | /// <param name="governanceService">The endpoint governance service.</param> |
| | | 27 | | /// <param name="endpointOptions">The endpoint governance options.</param> |
| | | 28 | | /// <returns>A task that completes when the middleware has run.</returns> |
| | | 29 | | public async Task InvokeAsync( |
| | | 30 | | HttpContext httpContext, |
| | | 31 | | IAsiBackboneEndpointGovernanceService governanceService, |
| | | 32 | | IOptions<AsiBackboneEndpointGovernanceOptions> endpointOptions) |
| | | 33 | | { |
| | 20 | 34 | | ArgumentNullException.ThrowIfNull(httpContext); |
| | 20 | 35 | | ArgumentNullException.ThrowIfNull(governanceService); |
| | 20 | 36 | | ArgumentNullException.ThrowIfNull(endpointOptions); |
| | | 37 | | |
| | 20 | 38 | | AsiBackboneEndpointGovernanceOptions options = endpointOptions.Value; |
| | 20 | 39 | | options.Validate(); |
| | | 40 | | |
| | 20 | 41 | | Endpoint? endpoint = httpContext.GetEndpoint(); |
| | 20 | 42 | | var descriptor = AsiBackboneEndpointGovernanceDescriptor.FromEndpoint(endpoint); |
| | | 43 | | |
| | 20 | 44 | | bool endpointAllowsMissingGovernance = endpoint?.Metadata |
| | 20 | 45 | | .GetMetadata<AllowMissingGovernanceMetadataAttribute>() is not null; |
| | | 46 | | |
| | 20 | 47 | | if (!descriptor.HasGovernanceMetadata) |
| | | 48 | | { |
| | 6 | 49 | | if (options.RequireGovernanceMetadata && !endpointAllowsMissingGovernance) |
| | | 50 | | { |
| | 2 | 51 | | IResult missingGovernanceResult = CreateDefaultForbiddenResult( |
| | 2 | 52 | | httpContext, |
| | 2 | 53 | | options, |
| | 2 | 54 | | descriptor, |
| | 2 | 55 | | result: null, |
| | 2 | 56 | | decisionStage: "aspnetcore.endpoint.governance.metadata"); |
| | | 57 | | |
| | 2 | 58 | | await missingGovernanceResult.ExecuteAsync(httpContext).ConfigureAwait(false); |
| | 2 | 59 | | return; |
| | | 60 | | } |
| | | 61 | | |
| | 4 | 62 | | await next(httpContext).ConfigureAwait(false); |
| | 4 | 63 | | return; |
| | | 64 | | } |
| | | 65 | | |
| | 14 | 66 | | AsiBackboneEndpointGovernanceResult result = await governanceService |
| | 14 | 67 | | .EvaluateAsync(httpContext, descriptor, httpContext.RequestAborted) |
| | 14 | 68 | | .ConfigureAwait(false); |
| | | 69 | | |
| | 14 | 70 | | if (result.CanExecute) |
| | | 71 | | { |
| | 0 | 72 | | await next(httpContext).ConfigureAwait(false); |
| | 0 | 73 | | return; |
| | | 74 | | } |
| | | 75 | | |
| | 14 | 76 | | IResult failureResult = result.FailureResult ?? CreateDefaultForbiddenResult( |
| | 14 | 77 | | httpContext, |
| | 14 | 78 | | options, |
| | 14 | 79 | | descriptor, |
| | 14 | 80 | | result, |
| | 14 | 81 | | "aspnetcore.endpoint.governance.decision"); |
| | | 82 | | |
| | 14 | 83 | | await failureResult.ExecuteAsync(httpContext).ConfigureAwait(false); |
| | 20 | 84 | | } |
| | | 85 | | |
| | | 86 | | private static IResult CreateDefaultForbiddenResult( |
| | | 87 | | HttpContext httpContext, |
| | | 88 | | AsiBackboneEndpointGovernanceOptions options, |
| | | 89 | | AsiBackboneEndpointGovernanceDescriptor descriptor, |
| | | 90 | | AsiBackboneEndpointGovernanceResult? result, |
| | | 91 | | string decisionStage) |
| | | 92 | | { |
| | 12 | 93 | | return AsiBackboneEndpointGovernanceDevelopmentDiagnostics.IsEnabled(httpContext, options) |
| | 12 | 94 | | ? AsiBackboneEndpointGovernanceDevelopmentDiagnostics.CreateProblem( |
| | 12 | 95 | | httpContext, |
| | 12 | 96 | | options, |
| | 12 | 97 | | descriptor, |
| | 12 | 98 | | result?.Decision, |
| | 12 | 99 | | decisionStage, |
| | 12 | 100 | | title: "Endpoint governance blocked execution.", |
| | 12 | 101 | | detail: "Endpoint governance blocked this request before the selected endpoint executed.", |
| | 12 | 102 | | statusCode: StatusCodes.Status403Forbidden) |
| | 12 | 103 | | : options.DefaultForbiddenResultFactory is null |
| | 12 | 104 | | ? LightweightForbiddenResult |
| | 12 | 105 | | : options.DefaultForbiddenResultFactory(httpContext) |
| | 12 | 106 | | ?? throw new InvalidOperationException($"{nameof(AsiBackboneEndpointGovernanceOptions.DefaultForbiddenRe |
| | | 107 | | } |
| | | 108 | | } |