| | | 1 | | using AsiBackbone.Core.Handshakes; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.AspNetCore.Handshakes; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents an ASP.NET Core-friendly acknowledgment challenge that a host can render using any UI or transport. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed class AsiBackboneAcknowledgmentChallenge |
| | | 9 | | { |
| | 56 | 10 | | internal AsiBackboneAcknowledgmentChallenge( |
| | 56 | 11 | | LiabilityHandshakeRequest handshakeRequest, |
| | 56 | 12 | | string handshakeId, |
| | 56 | 13 | | string operationName, |
| | 56 | 14 | | string reasonCode, |
| | 56 | 15 | | string? reasonMessage, |
| | 56 | 16 | | string requiredAcknowledgmentCode, |
| | 56 | 17 | | string requiredAcknowledgmentText, |
| | 56 | 18 | | LiabilityHandshakeRiskLevel riskLevel, |
| | 56 | 19 | | string? riskCategory, |
| | 56 | 20 | | string? correlationId, |
| | 56 | 21 | | string? traceId, |
| | 56 | 22 | | string? policyVersion, |
| | 56 | 23 | | string? policyHash, |
| | 56 | 24 | | IReadOnlyDictionary<string, string> metadata) |
| | | 25 | | { |
| | | 26 | | // Defensive validation for internal construction invariants. |
| | | 27 | | // Public inputs are validated through FromHandshakeRequest and LiabilityHandshakeRequest. |
| | 56 | 28 | | ArgumentNullException.ThrowIfNull(handshakeRequest); |
| | 54 | 29 | | ArgumentException.ThrowIfNullOrWhiteSpace(handshakeId); |
| | 52 | 30 | | ArgumentException.ThrowIfNullOrWhiteSpace(operationName); |
| | 50 | 31 | | ArgumentException.ThrowIfNullOrWhiteSpace(reasonCode); |
| | 48 | 32 | | ArgumentException.ThrowIfNullOrWhiteSpace(requiredAcknowledgmentCode); |
| | 46 | 33 | | ArgumentException.ThrowIfNullOrWhiteSpace(requiredAcknowledgmentText); |
| | | 34 | | |
| | 44 | 35 | | HandshakeRequest = handshakeRequest; |
| | 44 | 36 | | HandshakeId = handshakeId.Trim(); |
| | 44 | 37 | | OperationName = operationName.Trim(); |
| | 44 | 38 | | ReasonCode = reasonCode.Trim(); |
| | 44 | 39 | | ReasonMessage = NormalizeOptional(reasonMessage); |
| | 44 | 40 | | RequiredAcknowledgmentCode = requiredAcknowledgmentCode.Trim(); |
| | 44 | 41 | | RequiredAcknowledgmentText = requiredAcknowledgmentText.Trim(); |
| | 44 | 42 | | RiskLevel = riskLevel; |
| | 44 | 43 | | RiskCategory = NormalizeOptional(riskCategory); |
| | 44 | 44 | | CorrelationId = NormalizeOptional(correlationId); |
| | 44 | 45 | | TraceId = NormalizeOptional(traceId); |
| | 44 | 46 | | PolicyVersion = NormalizeOptional(policyVersion); |
| | 44 | 47 | | PolicyHash = NormalizeOptional(policyHash); |
| | 44 | 48 | | Metadata = metadata; |
| | 44 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets the framework-neutral Core handshake request used for round-tripping acknowledgment responses. |
| | | 53 | | /// </summary> |
| | 16 | 54 | | public LiabilityHandshakeRequest HandshakeRequest { get; } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets the stable handshake identifier. |
| | | 58 | | /// </summary> |
| | 46 | 59 | | public string HandshakeId { get; } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Gets the operation name requiring acknowledgment. |
| | | 63 | | /// </summary> |
| | 2 | 64 | | public string OperationName { get; } |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Gets the machine-readable reason code for the challenge. |
| | | 68 | | /// </summary> |
| | 4 | 69 | | public string ReasonCode { get; } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Gets the optional user-facing reason message for the challenge. |
| | | 73 | | /// </summary> |
| | 8 | 74 | | public string? ReasonMessage { get; } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Gets the required acknowledgment code. |
| | | 78 | | /// </summary> |
| | 28 | 79 | | public string RequiredAcknowledgmentCode { get; } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Gets the required acknowledgment text. |
| | | 83 | | /// </summary> |
| | 2 | 84 | | public string RequiredAcknowledgmentText { get; } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Gets the risk level associated with the challenge. |
| | | 88 | | /// </summary> |
| | 2 | 89 | | public LiabilityHandshakeRiskLevel RiskLevel { get; } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Gets the optional host-defined risk category. |
| | | 93 | | /// </summary> |
| | 2 | 94 | | public string? RiskCategory { get; } |
| | | 95 | | |
| | | 96 | | /// <summary> |
| | | 97 | | /// Gets the correlation identifier associated with the challenge. |
| | | 98 | | /// </summary> |
| | 6 | 99 | | public string? CorrelationId { get; } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Gets the optional trace identifier associated with the challenge. |
| | | 103 | | /// </summary> |
| | 8 | 104 | | public string? TraceId { get; } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// Gets the optional policy version associated with the challenge. |
| | | 108 | | /// </summary> |
| | 8 | 109 | | public string? PolicyVersion { get; } |
| | | 110 | | |
| | | 111 | | /// <summary> |
| | | 112 | | /// Gets the optional policy hash associated with the challenge. |
| | | 113 | | /// </summary> |
| | 8 | 114 | | public string? PolicyHash { get; } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Gets additional host-provided challenge metadata. |
| | | 118 | | /// </summary> |
| | 12 | 119 | | public IReadOnlyDictionary<string, string> Metadata { get; } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Creates a host-friendly challenge from a Core handshake request. |
| | | 123 | | /// </summary> |
| | | 124 | | /// <param name="request">The Core handshake request.</param> |
| | | 125 | | /// <param name="options">The challenge options.</param> |
| | | 126 | | /// <returns>A host-friendly acknowledgment challenge.</returns> |
| | | 127 | | public static AsiBackboneAcknowledgmentChallenge FromHandshakeRequest( |
| | | 128 | | LiabilityHandshakeRequest request, |
| | | 129 | | AsiBackboneAcknowledgmentChallengeOptions? options = null) |
| | | 130 | | { |
| | 48 | 131 | | ArgumentNullException.ThrowIfNull(request); |
| | 46 | 132 | | options ??= new AsiBackboneAcknowledgmentChallengeOptions(); |
| | 46 | 133 | | options.Validate(); |
| | | 134 | | |
| | 44 | 135 | | return new AsiBackboneAcknowledgmentChallenge( |
| | 44 | 136 | | request, |
| | 44 | 137 | | request.HandshakeId, |
| | 44 | 138 | | request.OperationName, |
| | 44 | 139 | | request.ReasonCode, |
| | 44 | 140 | | options.IncludeReasonMessage ? request.Message : null, |
| | 44 | 141 | | request.RequiredAcknowledgmentCode, |
| | 44 | 142 | | request.RequiredAcknowledgmentText, |
| | 44 | 143 | | request.RiskLevel, |
| | 44 | 144 | | request.RiskCategory, |
| | 44 | 145 | | request.CorrelationId, |
| | 44 | 146 | | options.IncludeTraceId ? request.TraceId : null, |
| | 44 | 147 | | options.IncludePolicyMetadata ? request.PolicyVersion : null, |
| | 44 | 148 | | options.IncludePolicyMetadata ? request.PolicyHash : null, |
| | 44 | 149 | | request.Metadata); |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | private static string? NormalizeOptional(string? value) |
| | | 153 | | { |
| | 264 | 154 | | return string.IsNullOrWhiteSpace(value) |
| | 264 | 155 | | ? null |
| | 264 | 156 | | : value.Trim(); |
| | | 157 | | } |
| | | 158 | | } |