| | | 1 | | using AsiBackbone.Core.Actors; |
| | | 2 | | using AsiBackbone.Core.Decisions; |
| | | 3 | | using AsiBackbone.Core.Handshakes; |
| | | 4 | | using Microsoft.Extensions.Options; |
| | | 5 | | |
| | | 6 | | namespace AsiBackbone.AspNetCore.Handshakes; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Provides the default ASP.NET Core-friendly acknowledgment challenge service. |
| | | 10 | | /// </summary> |
| | | 11 | | public sealed class DefaultAsiBackboneAcknowledgmentChallengeService : IAsiBackboneAcknowledgmentChallengeService |
| | | 12 | | { |
| | | 13 | | private const string ChallengeMismatchCode = "acknowledgment.challenge.mismatch"; |
| | | 14 | | private const string ChallengeCodeMismatchCode = "acknowledgment.challenge.code_mismatch"; |
| | | 15 | | |
| | | 16 | | private readonly AsiBackboneAcknowledgmentChallengeOptions options; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Initializes a new instance of the <see cref="DefaultAsiBackboneAcknowledgmentChallengeService" /> class. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="options">The acknowledgment challenge options.</param> |
| | 63 | 22 | | public DefaultAsiBackboneAcknowledgmentChallengeService(IOptions<AsiBackboneAcknowledgmentChallengeOptions> options) |
| | | 23 | | { |
| | 63 | 24 | | this.options = options?.Value ?? throw new ArgumentNullException(nameof(options)); |
| | 61 | 25 | | this.options.Validate(); |
| | 59 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | | 29 | | public AsiBackboneAcknowledgmentChallenge CreateChallenge( |
| | | 30 | | IAsiBackboneActorContext actor, |
| | | 31 | | string operationName, |
| | | 32 | | GovernanceDecision decision, |
| | | 33 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 34 | | { |
| | 46 | 35 | | ArgumentNullException.ThrowIfNull(actor); |
| | 42 | 36 | | ArgumentNullException.ThrowIfNull(decision); |
| | | 37 | | |
| | 40 | 38 | | if (!decision.RequiresAcknowledgment) |
| | | 39 | | { |
| | 2 | 40 | | throw new InvalidOperationException("Only acknowledgment-required governance decisions can be converted into |
| | | 41 | | } |
| | | 42 | | |
| | 38 | 43 | | var request = LiabilityHandshakeRequest.FromDecision( |
| | 38 | 44 | | actor, |
| | 38 | 45 | | operationName, |
| | 38 | 46 | | decision, |
| | 38 | 47 | | options.RequiredAcknowledgmentCode, |
| | 38 | 48 | | options.RequiredAcknowledgmentText, |
| | 38 | 49 | | options.RiskLevel, |
| | 38 | 50 | | options.RiskCategory, |
| | 38 | 51 | | metadata: metadata); |
| | | 52 | | |
| | 38 | 53 | | return AsiBackboneAcknowledgmentChallenge.FromHandshakeRequest(request, options); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | | 57 | | public AsiBackboneAcknowledgmentChallengeResult HandleResponse( |
| | | 58 | | AsiBackboneAcknowledgmentChallenge challenge, |
| | | 59 | | IAsiBackboneActorContext actor, |
| | | 60 | | AsiBackboneAcknowledgmentChallengeRequest response, |
| | | 61 | | DateTimeOffset? occurredUtc = null) |
| | | 62 | | { |
| | 30 | 63 | | ArgumentNullException.ThrowIfNull(challenge); |
| | 28 | 64 | | ArgumentNullException.ThrowIfNull(actor); |
| | 26 | 65 | | ArgumentNullException.ThrowIfNull(response); |
| | | 66 | | |
| | 24 | 67 | | if (!string.Equals(challenge.HandshakeId, response.HandshakeId?.Trim(), StringComparison.Ordinal)) |
| | | 68 | | { |
| | 8 | 69 | | return AsiBackboneAcknowledgmentChallengeResult.Failure( |
| | 8 | 70 | | ChallengeMismatchCode, |
| | 8 | 71 | | "The acknowledgment response did not match the active challenge."); |
| | | 72 | | } |
| | | 73 | | |
| | 16 | 74 | | if (!string.Equals(challenge.RequiredAcknowledgmentCode, response.AcknowledgmentCode?.Trim(), StringComparison.O |
| | | 75 | | { |
| | 10 | 76 | | return AsiBackboneAcknowledgmentChallengeResult.Failure( |
| | 10 | 77 | | ChallengeCodeMismatchCode, |
| | 10 | 78 | | "The acknowledgment response did not contain the required acknowledgment code."); |
| | | 79 | | } |
| | | 80 | | |
| | 6 | 81 | | var acknowledgment = LiabilityHandshakeAcknowledgment.Create( |
| | 6 | 82 | | challenge.HandshakeRequest, |
| | 6 | 83 | | actor, |
| | 6 | 84 | | response.Acknowledged, |
| | 6 | 85 | | occurredUtc: occurredUtc, |
| | 6 | 86 | | metadata: response.Metadata); |
| | | 87 | | |
| | 6 | 88 | | return AsiBackboneAcknowledgmentChallengeResult.Success(acknowledgment); |
| | | 89 | | } |
| | | 90 | | } |