| | | 1 | | using AsiBackbone.AspNetCore.Handshakes; |
| | | 2 | | using AsiBackbone.Core.Decisions; |
| | | 3 | | using Microsoft.AspNetCore.Http; |
| | | 4 | | |
| | | 5 | | namespace AsiBackbone.AspNetCore.Endpoints; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Represents the outcome of ASP.NET Core endpoint governance evaluation before endpoint execution. |
| | | 9 | | /// </summary> |
| | | 10 | | public sealed class AsiBackboneEndpointGovernanceResult |
| | | 11 | | { |
| | 23 | 12 | | private AsiBackboneEndpointGovernanceResult( |
| | 23 | 13 | | bool canExecute, |
| | 23 | 14 | | GovernanceDecision? decision, |
| | 23 | 15 | | IResult? failureResult, |
| | 23 | 16 | | AsiBackboneAcknowledgmentChallenge? acknowledgmentChallenge) |
| | | 17 | | { |
| | 23 | 18 | | CanExecute = canExecute; |
| | 23 | 19 | | Decision = decision; |
| | 23 | 20 | | FailureResult = failureResult; |
| | 23 | 21 | | AcknowledgmentChallenge = acknowledgmentChallenge; |
| | 23 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Gets a value indicating whether the ASP.NET Core endpoint may continue execution. |
| | | 26 | | /// </summary> |
| | 23 | 27 | | public bool CanExecute { get; } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Gets the governance decision associated with the endpoint evaluation, when one was produced. |
| | | 31 | | /// </summary> |
| | 20 | 32 | | public GovernanceDecision? Decision { get; } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets the HTTP result that should be returned instead of executing the endpoint, when execution is blocked. |
| | | 36 | | /// </summary> |
| | 22 | 37 | | public IResult? FailureResult { get; } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Gets the acknowledgment challenge produced when a decision required acknowledgment. |
| | | 41 | | /// </summary> |
| | 0 | 42 | | public AsiBackboneAcknowledgmentChallenge? AcknowledgmentChallenge { get; } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Creates a result that allows endpoint execution to continue. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="decision">The governance decision that allowed execution.</param> |
| | | 48 | | /// <returns>An allow result.</returns> |
| | | 49 | | public static AsiBackboneEndpointGovernanceResult Allow(GovernanceDecision? decision = null) |
| | | 50 | | { |
| | 1 | 51 | | return new AsiBackboneEndpointGovernanceResult( |
| | 1 | 52 | | canExecute: true, |
| | 1 | 53 | | decision, |
| | 1 | 54 | | failureResult: null, |
| | 1 | 55 | | acknowledgmentChallenge: null); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Creates a result that blocks endpoint execution and lets middleware write the configured generic failure respons |
| | | 60 | | /// </summary> |
| | | 61 | | /// <param name="decision">The governance decision that blocked execution.</param> |
| | | 62 | | /// <returns>A blocked result without a custom failure response.</returns> |
| | | 63 | | public static AsiBackboneEndpointGovernanceResult BlockWithDefaultFailure(GovernanceDecision? decision = null) |
| | | 64 | | { |
| | 14 | 65 | | return new AsiBackboneEndpointGovernanceResult( |
| | 14 | 66 | | canExecute: false, |
| | 14 | 67 | | decision, |
| | 14 | 68 | | failureResult: null, |
| | 14 | 69 | | acknowledgmentChallenge: null); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Creates a result that blocks endpoint execution and returns a host-safe failure result. |
| | | 74 | | /// </summary> |
| | | 75 | | /// <param name="failureResult">The HTTP result to execute.</param> |
| | | 76 | | /// <param name="decision">The governance decision that blocked execution.</param> |
| | | 77 | | /// <returns>A blocked result.</returns> |
| | | 78 | | public static AsiBackboneEndpointGovernanceResult Block(IResult failureResult, GovernanceDecision? decision = null) |
| | | 79 | | { |
| | 8 | 80 | | ArgumentNullException.ThrowIfNull(failureResult); |
| | | 81 | | |
| | 8 | 82 | | return new AsiBackboneEndpointGovernanceResult( |
| | 8 | 83 | | canExecute: false, |
| | 8 | 84 | | decision, |
| | 8 | 85 | | failureResult, |
| | 8 | 86 | | acknowledgmentChallenge: null); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Creates a result that blocks execution with an acknowledgment challenge. |
| | | 91 | | /// </summary> |
| | | 92 | | /// <param name="challenge">The acknowledgment challenge.</param> |
| | | 93 | | /// <param name="failureResult">The HTTP result to execute.</param> |
| | | 94 | | /// <param name="decision">The governance decision that required acknowledgment.</param> |
| | | 95 | | /// <returns>An acknowledgment challenge result.</returns> |
| | | 96 | | public static AsiBackboneEndpointGovernanceResult Challenge( |
| | | 97 | | AsiBackboneAcknowledgmentChallenge challenge, |
| | | 98 | | IResult failureResult, |
| | | 99 | | GovernanceDecision decision) |
| | | 100 | | { |
| | 0 | 101 | | ArgumentNullException.ThrowIfNull(challenge); |
| | 0 | 102 | | ArgumentNullException.ThrowIfNull(failureResult); |
| | 0 | 103 | | ArgumentNullException.ThrowIfNull(decision); |
| | | 104 | | |
| | 0 | 105 | | return new AsiBackboneEndpointGovernanceResult( |
| | 0 | 106 | | canExecute: false, |
| | 0 | 107 | | decision, |
| | 0 | 108 | | failureResult, |
| | 0 | 109 | | challenge); |
| | | 110 | | } |
| | | 111 | | } |