| | | 1 | | using Microsoft.AspNetCore.Http; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.AspNetCore.Endpoints; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides options for ergonomic ASP.NET Core endpoint governance integration. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed class AsiBackboneEndpointGovernanceOptions |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Gets or sets the policy version attached to generated endpoint governance evaluation contexts. |
| | | 12 | | /// </summary> |
| | 9 | 13 | | public string? PolicyVersion { get; set; } |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Gets or sets the policy hash attached to generated endpoint governance evaluation contexts. |
| | | 17 | | /// </summary> |
| | 9 | 18 | | public string? PolicyHash { get; set; } |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Gets or sets a value indicating whether policy metadata should fail closed when no policy evaluator is configure |
| | | 22 | | /// </summary> |
| | 29 | 23 | | public bool FailClosedWhenPolicyEvaluatorMissing { get; set; } = true; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Gets or sets a value indicating whether capability metadata should fail closed when no capability validator is c |
| | | 27 | | /// </summary> |
| | 33 | 28 | | public bool FailClosedWhenCapabilityValidatorMissing { get; set; } = true; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Gets or sets a value indicating whether audit metadata should fail closed when no host-owned audit sink is confi |
| | | 32 | | /// </summary> |
| | 29 | 33 | | public bool FailClosedWhenAuditSinkMissing { get; set; } = true; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets or sets the HTTP status code used when endpoint governance is missing required host configuration. |
| | | 37 | | /// </summary> |
| | 67 | 38 | | public int ConfigurationFailureStatusCode { get; set; } = StatusCodes.Status500InternalServerError; |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets or sets the HTTP status code used when endpoint capability validation fails before policy evaluation return |
| | | 42 | | /// </summary> |
| | 71 | 43 | | public int CapabilityFailureStatusCode { get; set; } = StatusCodes.Status403Forbidden; |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets or sets the HTTP status code used when a governance decision requires acknowledgment and the endpoint reque |
| | | 47 | | /// </summary> |
| | 67 | 48 | | public int AcknowledgmentChallengeStatusCode { get; set; } = StatusCodes.Status428PreconditionRequired; |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets or sets a value indicating whether selected endpoints without AsiBackbone governance metadata should fail c |
| | | 52 | | /// </summary> |
| | 10 | 53 | | public bool RequireGovernanceMetadata { get; set; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets or sets a value indicating whether local-development ProblemDetails diagnostics should be emitted for endpo |
| | | 57 | | /// </summary> |
| | 22 | 58 | | public bool EnableDevelopmentDiagnostics { get; set; } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets or sets the documentation base URL used when development diagnostics include a troubleshooting link. |
| | | 62 | | /// </summary> |
| | 8 | 63 | | public string? DevelopmentDiagnosticsDocumentationBaseUrl { get; set; } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Gets a collection of metadata keys whose values should be redacted from development diagnostics. |
| | | 67 | | /// </summary> |
| | 47 | 68 | | public ISet<string> DevelopmentDiagnosticsRedactedMetadataKeys { get; } = new HashSet<string>(StringComparer.Ordinal |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Gets or sets a value indicating whether non-sensitive metadata values may be included in development diagnostics |
| | | 72 | | /// </summary> |
| | 47 | 73 | | public bool IncludeDevelopmentDiagnosticsMetadataValues { get; set; } = true; |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Gets or sets an optional factory for the generic 403 response used by middleware when no explicit failure result |
| | | 77 | | /// </summary> |
| | | 78 | | /// <remarks> |
| | | 79 | | /// Leave this unset for the low-allocation, bodyless default 403 response. Hosts that prefer richer API responses |
| | | 80 | | /// may provide a safe factory, such as a ProblemDetails result, while avoiding sensitive governance details. |
| | | 81 | | /// </remarks> |
| | 16 | 82 | | public Func<HttpContext, IResult>? DefaultForbiddenResultFactory { get; set; } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Validates endpoint governance options. |
| | | 86 | | /// </summary> |
| | | 87 | | public void Validate() |
| | | 88 | | { |
| | 38 | 89 | | ValidateStatusCode(ConfigurationFailureStatusCode, nameof(ConfigurationFailureStatusCode)); |
| | 38 | 90 | | ValidateStatusCode(CapabilityFailureStatusCode, nameof(CapabilityFailureStatusCode)); |
| | 38 | 91 | | ValidateStatusCode(AcknowledgmentChallengeStatusCode, nameof(AcknowledgmentChallengeStatusCode)); |
| | 38 | 92 | | } |
| | | 93 | | |
| | | 94 | | private static void ValidateStatusCode(int statusCode, string propertyName) |
| | | 95 | | { |
| | 114 | 96 | | if (statusCode is < 100 or > 599) |
| | | 97 | | { |
| | 0 | 98 | | throw new InvalidOperationException($"{propertyName} must be a valid HTTP status code."); |
| | | 99 | | } |
| | 114 | 100 | | } |
| | | 101 | | } |