| | | 1 | | using AsiBackbone.Core.Decisions; |
| | | 2 | | using AsiBackbone.Core.Results; |
| | | 3 | | using Microsoft.AspNetCore.Http; |
| | | 4 | | using Microsoft.AspNetCore.Mvc; |
| | | 5 | | |
| | | 6 | | namespace AsiBackbone.AspNetCore.Results; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Provides helpers for translating AsiBackbone Core decisions and operation results into ASP.NET Core HTTP results. |
| | | 10 | | /// </summary> |
| | | 11 | | public static class AsiBackboneHttpResultMappingExtensions |
| | | 12 | | { |
| | | 13 | | private const string OutcomeExtensionName = "outcome"; |
| | | 14 | | private const string ReasonCodesExtensionName = "reasonCodes"; |
| | | 15 | | private const string ReasonMessagesExtensionName = "reasonMessages"; |
| | | 16 | | private const string CorrelationIdExtensionName = "correlationId"; |
| | | 17 | | private const string TraceIdExtensionName = "traceId"; |
| | | 18 | | private const string PolicyVersionExtensionName = "policyVersion"; |
| | | 19 | | private const string PolicyHashExtensionName = "policyHash"; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Maps a governance decision into an ASP.NET Core result using default safe HTTP mapping options. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="decision">The governance decision to map.</param> |
| | | 25 | | /// <returns>An ASP.NET Core result.</returns> |
| | | 26 | | public static IResult ToHttpResult(this GovernanceDecision decision) |
| | | 27 | | { |
| | 12 | 28 | | return decision.ToHttpResult(new AsiBackboneHttpResultMappingOptions()); |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Maps a governance decision into an ASP.NET Core result using host-provided HTTP mapping options. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="decision">The governance decision to map.</param> |
| | | 35 | | /// <param name="options">The mapping options.</param> |
| | | 36 | | /// <returns>An ASP.NET Core result.</returns> |
| | | 37 | | public static IResult ToHttpResult(this GovernanceDecision decision, AsiBackboneHttpResultMappingOptions options) |
| | | 38 | | { |
| | 20 | 39 | | ArgumentNullException.ThrowIfNull(decision); |
| | 18 | 40 | | ArgumentNullException.ThrowIfNull(options); |
| | 16 | 41 | | options.Validate(); |
| | | 42 | | |
| | 16 | 43 | | return decision.CanProceed |
| | 16 | 44 | | ? Microsoft.AspNetCore.Http.Results.Json(CreateDecisionBody(decision, options, allowed: true), statusCode: R |
| | 16 | 45 | | : Microsoft.AspNetCore.Http.Results.Problem(CreateDecisionProblemDetails(decision, options)); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Maps an operation result into an ASP.NET Core result using default safe HTTP mapping options. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="result">The operation result to map.</param> |
| | | 52 | | /// <returns>An ASP.NET Core result.</returns> |
| | | 53 | | public static IResult ToHttpResult(this OperationResult result) |
| | | 54 | | { |
| | 8 | 55 | | return result.ToHttpResult(new AsiBackboneHttpResultMappingOptions()); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Maps an operation result into an ASP.NET Core result using host-provided HTTP mapping options. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="result">The operation result to map.</param> |
| | | 62 | | /// <param name="options">The mapping options.</param> |
| | | 63 | | /// <returns>An ASP.NET Core result.</returns> |
| | | 64 | | public static IResult ToHttpResult(this OperationResult result, AsiBackboneHttpResultMappingOptions options) |
| | | 65 | | { |
| | 12 | 66 | | ArgumentNullException.ThrowIfNull(result); |
| | 10 | 67 | | ArgumentNullException.ThrowIfNull(options); |
| | 8 | 68 | | options.Validate(); |
| | | 69 | | |
| | 8 | 70 | | return result.Succeeded |
| | 8 | 71 | | ? Microsoft.AspNetCore.Http.Results.Json(CreateOperationBody(result, options), statusCode: options.SuccessSt |
| | 8 | 72 | | : Microsoft.AspNetCore.Http.Results.Problem(CreateOperationProblemDetails(result, options)); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | private static int ResolveDecisionStatusCode(GovernanceDecision decision, AsiBackboneHttpResultMappingOptions option |
| | | 76 | | { |
| | 16 | 77 | | return decision.Outcome switch |
| | 16 | 78 | | { |
| | 4 | 79 | | GovernanceDecisionOutcome.Allowed => options.SuccessStatusCode, |
| | 2 | 80 | | GovernanceDecisionOutcome.Warning => options.WarningStatusCode, |
| | 4 | 81 | | GovernanceDecisionOutcome.Denied => options.DeniedStatusCode, |
| | 2 | 82 | | GovernanceDecisionOutcome.Deferred => options.DeferredStatusCode, |
| | 2 | 83 | | GovernanceDecisionOutcome.AcknowledgmentRequired => options.AcknowledgmentRequiredStatusCode, |
| | 2 | 84 | | GovernanceDecisionOutcome.EscalationRecommended => options.EscalationRecommendedStatusCode, |
| | 0 | 85 | | _ => options.OperationFailureStatusCode, |
| | 16 | 86 | | }; |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | private static ProblemDetails CreateDecisionProblemDetails( |
| | | 90 | | GovernanceDecision decision, |
| | | 91 | | AsiBackboneHttpResultMappingOptions options) |
| | | 92 | | { |
| | 10 | 93 | | int statusCode = ResolveDecisionStatusCode(decision, options); |
| | 10 | 94 | | ProblemDetails problemDetails = new() |
| | 10 | 95 | | { |
| | 10 | 96 | | Status = statusCode, |
| | 10 | 97 | | Title = ResolveDecisionTitle(decision.Outcome), |
| | 10 | 98 | | Detail = options.GovernanceDecisionNotAllowedMessage.Trim(), |
| | 10 | 99 | | Type = "https://tools.ietf.org/html/rfc9110", |
| | 10 | 100 | | }; |
| | | 101 | | |
| | 10 | 102 | | AddDecisionExtensions(problemDetails.Extensions, decision, options); |
| | | 103 | | |
| | 10 | 104 | | return problemDetails; |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | private static ProblemDetails CreateOperationProblemDetails( |
| | | 108 | | OperationResult result, |
| | | 109 | | AsiBackboneHttpResultMappingOptions options) |
| | | 110 | | { |
| | 4 | 111 | | ProblemDetails problemDetails = new() |
| | 4 | 112 | | { |
| | 4 | 113 | | Status = options.OperationFailureStatusCode, |
| | 4 | 114 | | Title = "Operation failed.", |
| | 4 | 115 | | Detail = options.OperationFailureMessage.Trim(), |
| | 4 | 116 | | Type = "https://tools.ietf.org/html/rfc9110", |
| | 4 | 117 | | }; |
| | | 118 | | |
| | 4 | 119 | | AddOperationExtensions(problemDetails.Extensions, result, options); |
| | | 120 | | |
| | 4 | 121 | | return problemDetails; |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | private static Dictionary<string, object?> CreateDecisionBody( |
| | | 125 | | GovernanceDecision decision, |
| | | 126 | | AsiBackboneHttpResultMappingOptions options, |
| | | 127 | | bool allowed) |
| | | 128 | | { |
| | 6 | 129 | | Dictionary<string, object?> body = new(StringComparer.Ordinal) |
| | 6 | 130 | | { |
| | 6 | 131 | | ["allowed"] = allowed, |
| | 6 | 132 | | [OutcomeExtensionName] = decision.Outcome.ToString(), |
| | 6 | 133 | | }; |
| | | 134 | | |
| | 6 | 135 | | AddDecisionPayload(body, decision, options); |
| | | 136 | | |
| | 6 | 137 | | return body; |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | private static Dictionary<string, object?> CreateOperationBody(OperationResult result, AsiBackboneHttpResultMappingO |
| | | 141 | | { |
| | 4 | 142 | | Dictionary<string, object?> body = new(StringComparer.Ordinal) |
| | 4 | 143 | | { |
| | 4 | 144 | | ["succeeded"] = result.Succeeded, |
| | 4 | 145 | | }; |
| | | 146 | | |
| | 4 | 147 | | AddOperationPayload(body, result, options); |
| | | 148 | | |
| | 4 | 149 | | return body; |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | private static string ResolveDecisionTitle(GovernanceDecisionOutcome outcome) |
| | | 153 | | { |
| | 10 | 154 | | return outcome switch |
| | 10 | 155 | | { |
| | 0 | 156 | | GovernanceDecisionOutcome.Allowed => "Governance decision allowed execution.", |
| | 0 | 157 | | GovernanceDecisionOutcome.Warning => "Governance decision allowed execution with warnings.", |
| | 4 | 158 | | GovernanceDecisionOutcome.Denied => "Governance decision denied execution.", |
| | 2 | 159 | | GovernanceDecisionOutcome.Deferred => "Governance decision deferred execution.", |
| | 2 | 160 | | GovernanceDecisionOutcome.AcknowledgmentRequired => "Governance decision requires acknowledgment.", |
| | 2 | 161 | | GovernanceDecisionOutcome.EscalationRecommended => "Governance decision recommends escalation.", |
| | 0 | 162 | | _ => "Governance decision did not allow immediate execution.", |
| | 10 | 163 | | }; |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | private static void AddDecisionExtensions( |
| | | 167 | | IDictionary<string, object?> extensions, |
| | | 168 | | GovernanceDecision decision, |
| | | 169 | | AsiBackboneHttpResultMappingOptions options) |
| | | 170 | | { |
| | 10 | 171 | | extensions[OutcomeExtensionName] = decision.Outcome.ToString(); |
| | 10 | 172 | | AddDecisionPayload(extensions, decision, options); |
| | 10 | 173 | | } |
| | | 174 | | |
| | | 175 | | private static void AddOperationExtensions( |
| | | 176 | | IDictionary<string, object?> extensions, |
| | | 177 | | OperationResult result, |
| | | 178 | | AsiBackboneHttpResultMappingOptions options) |
| | | 179 | | { |
| | 4 | 180 | | AddOperationPayload(extensions, result, options); |
| | 4 | 181 | | } |
| | | 182 | | |
| | | 183 | | private static void AddDecisionPayload( |
| | | 184 | | IDictionary<string, object?> payload, |
| | | 185 | | GovernanceDecision decision, |
| | | 186 | | AsiBackboneHttpResultMappingOptions options) |
| | | 187 | | { |
| | 16 | 188 | | if (decision.ReasonCodes.Count > 0) |
| | | 189 | | { |
| | 12 | 190 | | payload[ReasonCodesExtensionName] = decision.ReasonCodes; |
| | | 191 | | } |
| | | 192 | | |
| | 16 | 193 | | if (options.IncludeReasonMessages && decision.Reasons.Count > 0) |
| | | 194 | | { |
| | 4 | 195 | | payload[ReasonMessagesExtensionName] = decision.Reasons.Select(reason => reason.Message).ToArray(); |
| | | 196 | | } |
| | | 197 | | |
| | 16 | 198 | | if (!string.IsNullOrWhiteSpace(decision.CorrelationId)) |
| | | 199 | | { |
| | 6 | 200 | | payload[CorrelationIdExtensionName] = decision.CorrelationId; |
| | | 201 | | } |
| | | 202 | | |
| | 16 | 203 | | if (options.IncludeTraceId && !string.IsNullOrWhiteSpace(decision.TraceId)) |
| | | 204 | | { |
| | 2 | 205 | | payload[TraceIdExtensionName] = decision.TraceId; |
| | | 206 | | } |
| | | 207 | | |
| | 16 | 208 | | if (options.IncludePolicyMetadata) |
| | | 209 | | { |
| | 4 | 210 | | if (!string.IsNullOrWhiteSpace(decision.PolicyVersion)) |
| | | 211 | | { |
| | 2 | 212 | | payload[PolicyVersionExtensionName] = decision.PolicyVersion; |
| | | 213 | | } |
| | | 214 | | |
| | 4 | 215 | | if (!string.IsNullOrWhiteSpace(decision.PolicyHash)) |
| | | 216 | | { |
| | 2 | 217 | | payload[PolicyHashExtensionName] = decision.PolicyHash; |
| | | 218 | | } |
| | | 219 | | } |
| | 16 | 220 | | } |
| | | 221 | | |
| | | 222 | | private static void AddOperationPayload( |
| | | 223 | | IDictionary<string, object?> payload, |
| | | 224 | | OperationResult result, |
| | | 225 | | AsiBackboneHttpResultMappingOptions options) |
| | | 226 | | { |
| | 8 | 227 | | if (result.ReasonCodes.Count > 0) |
| | | 228 | | { |
| | 4 | 229 | | payload[ReasonCodesExtensionName] = result.ReasonCodes; |
| | | 230 | | } |
| | | 231 | | |
| | 8 | 232 | | if (result.HasWarnings) |
| | | 233 | | { |
| | 2 | 234 | | payload["warnings"] = result.Warnings; |
| | | 235 | | } |
| | | 236 | | |
| | 8 | 237 | | if (options.IncludeReasonMessages && result.Reasons.Count > 0) |
| | | 238 | | { |
| | 4 | 239 | | payload[ReasonMessagesExtensionName] = result.Reasons.Select(reason => reason.Message).ToArray(); |
| | | 240 | | } |
| | 8 | 241 | | } |
| | | 242 | | } |