| | | 1 | | using Microsoft.AspNetCore.Builder; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.AspNetCore.Endpoints; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides Minimal API / endpoint route-builder helpers for adding AsiBackbone governance metadata. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class AsiBackboneEndpointGovernanceRouteBuilderExtensions |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Adds a host-defined governance policy marker to a Minimal API route handler endpoint. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <typeparam name="TPolicy">The host-defined policy marker or resolver type.</typeparam> |
| | | 14 | | /// <param name="builder">The route handler builder.</param> |
| | | 15 | | /// <returns>The same builder so calls can be chained.</returns> |
| | | 16 | | public static RouteHandlerBuilder RequireGovernancePolicy<TPolicy>(this RouteHandlerBuilder builder) |
| | | 17 | | { |
| | 2 | 18 | | return builder.RequireGovernancePolicy(typeof(TPolicy)); |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Adds a host-defined governance policy marker to an endpoint. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <typeparam name="TBuilder">The endpoint convention builder type.</typeparam> |
| | | 25 | | /// <param name="builder">The endpoint convention builder.</param> |
| | | 26 | | /// <param name="policyType">The host-defined policy marker or resolver type.</param> |
| | | 27 | | /// <returns>The same builder so calls can be chained.</returns> |
| | | 28 | | public static TBuilder RequireGovernancePolicy<TBuilder>(this TBuilder builder, Type policyType) |
| | | 29 | | where TBuilder : IEndpointConventionBuilder |
| | | 30 | | { |
| | 6 | 31 | | ArgumentNullException.ThrowIfNull(policyType); |
| | 4 | 32 | | return AddEndpointMetadata(builder, new RequireGovernancePolicyAttribute(policyType)); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Adds endpoint-scoped metadata requesting latency-optimized fast-abort policy evaluation after the first denied c |
| | | 37 | | /// </summary> |
| | | 38 | | /// <typeparam name="TBuilder">The endpoint convention builder type.</typeparam> |
| | | 39 | | /// <param name="builder">The endpoint convention builder.</param> |
| | | 40 | | /// <param name="enabled">Whether first-denial short-circuit metadata is enabled for the endpoint.</param> |
| | | 41 | | /// <returns>The same builder so calls can be chained.</returns> |
| | | 42 | | public static TBuilder ShortCircuitOnFirstDenial<TBuilder>(this TBuilder builder, bool enabled = true) |
| | | 43 | | where TBuilder : IEndpointConventionBuilder |
| | | 44 | | { |
| | 4 | 45 | | return AddEndpointMetadata(builder, new ShortCircuitOnFirstDenialAttribute(enabled)); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Adds liability-handshake metadata to an endpoint. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <typeparam name="TBuilder">The endpoint convention builder type.</typeparam> |
| | | 52 | | /// <param name="builder">The endpoint convention builder.</param> |
| | | 53 | | /// <returns>The same builder so calls can be chained.</returns> |
| | | 54 | | public static TBuilder RequireLiabilityHandshake<TBuilder>(this TBuilder builder) |
| | | 55 | | where TBuilder : IEndpointConventionBuilder |
| | | 56 | | { |
| | 4 | 57 | | return AddEndpointMetadata(builder, new RequireLiabilityHandshakeAttribute()); |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Adds a required capability-grant scope to an endpoint. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <typeparam name="TBuilder">The endpoint convention builder type.</typeparam> |
| | | 64 | | /// <param name="builder">The endpoint convention builder.</param> |
| | | 65 | | /// <param name="scope">The required capability-grant scope.</param> |
| | | 66 | | /// <returns>The same builder so calls can be chained.</returns> |
| | | 67 | | public static TBuilder RequireCapabilityGrant<TBuilder>(this TBuilder builder, string scope) |
| | | 68 | | where TBuilder : IEndpointConventionBuilder |
| | | 69 | | { |
| | 2 | 70 | | return AddEndpointMetadata(builder, new RequireCapabilityGrantAttribute(scope)); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Adds governance-audit emission metadata to an endpoint. |
| | | 75 | | /// </summary> |
| | | 76 | | /// <typeparam name="TBuilder">The endpoint convention builder type.</typeparam> |
| | | 77 | | /// <param name="builder">The endpoint convention builder.</param> |
| | | 78 | | /// <returns>The same builder so calls can be chained.</returns> |
| | | 79 | | public static TBuilder EmitGovernanceAudit<TBuilder>(this TBuilder builder) |
| | | 80 | | where TBuilder : IEndpointConventionBuilder |
| | | 81 | | { |
| | 2 | 82 | | return AddEndpointMetadata(builder, new EmitGovernanceAuditAttribute()); |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Allows an endpoint to pass through endpoint governance middleware when strict governance metadata enforcement is |
| | | 87 | | /// </summary> |
| | | 88 | | /// <typeparam name="TBuilder">The endpoint convention builder type.</typeparam> |
| | | 89 | | /// <param name="builder">The endpoint convention builder.</param> |
| | | 90 | | /// <returns>The same builder so calls can be chained.</returns> |
| | | 91 | | public static TBuilder AllowMissingGovernanceMetadata<TBuilder>(this TBuilder builder) |
| | | 92 | | where TBuilder : IEndpointConventionBuilder |
| | | 93 | | { |
| | 2 | 94 | | return AddEndpointMetadata(builder, new AllowMissingGovernanceMetadataAttribute()); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | private static TBuilder AddEndpointMetadata<TBuilder>(TBuilder builder, object metadata) |
| | | 98 | | where TBuilder : IEndpointConventionBuilder |
| | | 99 | | { |
| | 20 | 100 | | ArgumentNullException.ThrowIfNull(builder); |
| | 18 | 101 | | ArgumentNullException.ThrowIfNull(metadata); |
| | | 102 | | |
| | 30 | 103 | | builder.Add(endpointBuilder => endpointBuilder.Metadata.Add(metadata)); |
| | 16 | 104 | | return builder; |
| | | 105 | | } |
| | | 106 | | } |