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