| | | 1 | | namespace AsiBackbone.Core.CapabilityTokens; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Represents the outcome returned by a capability grant use-control store. |
| | | 5 | | /// </summary> |
| | | 6 | | public sealed class CapabilityGrantUseResult |
| | | 7 | | { |
| | 13 | 8 | | private CapabilityGrantUseResult(GrantUseState state, int useCount, string? failureCode, string? failureMessage) |
| | | 9 | | { |
| | 13 | 10 | | if (!Enum.IsDefined(state)) |
| | | 11 | | { |
| | 0 | 12 | | throw new ArgumentOutOfRangeException(nameof(state), state, "Use state must be defined."); |
| | | 13 | | } |
| | | 14 | | |
| | 13 | 15 | | State = state; |
| | 13 | 16 | | UseCount = useCount < 0 |
| | 13 | 17 | | ? throw new ArgumentOutOfRangeException(nameof(useCount), useCount, "Use count must be greater than or equal |
| | 13 | 18 | | : useCount; |
| | 11 | 19 | | FailureCode = NormalizeOptional(failureCode); |
| | 11 | 20 | | FailureMessage = NormalizeOptional(failureMessage); |
| | 11 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Gets the use-control state. |
| | | 25 | | /// </summary> |
| | 18 | 26 | | public GrantUseState State { get; } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Gets the observed use count after the store checked or consumed the grant. |
| | | 30 | | /// </summary> |
| | 5 | 31 | | public int UseCount { get; } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Gets the provider-neutral failure code when the use-control check did not accept the grant. |
| | | 35 | | /// </summary> |
| | 10 | 36 | | public string? FailureCode { get; } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets the provider-neutral failure message when the use-control check did not accept the grant. |
| | | 40 | | /// </summary> |
| | 10 | 41 | | public string? FailureMessage { get; } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Gets a value indicating whether the grant was accepted for use. |
| | | 45 | | /// </summary> |
| | 5 | 46 | | public bool IsAccepted => State is GrantUseState.Accepted; |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Creates an accepted use result. |
| | | 50 | | /// </summary> |
| | | 51 | | public static CapabilityGrantUseResult Accepted(int useCount) |
| | | 52 | | { |
| | 4 | 53 | | return new CapabilityGrantUseResult(GrantUseState.Accepted, useCount, null, null); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Creates a result indicating the configured use limit was exceeded. |
| | | 58 | | /// </summary> |
| | | 59 | | public static CapabilityGrantUseResult UseLimitExceeded(int useCount, string? failureMessage = null) |
| | | 60 | | { |
| | 3 | 61 | | return new CapabilityGrantUseResult(GrantUseState.UseLimitExceeded, useCount, "capability.use-limit-exceeded", f |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Creates a result indicating the grant was administratively stopped. |
| | | 66 | | /// </summary> |
| | | 67 | | public static CapabilityGrantUseResult Stopped(string? failureMessage = null) |
| | | 68 | | { |
| | 2 | 69 | | return new CapabilityGrantUseResult(GrantUseState.Stopped, 0, "capability.grant-stopped", failureMessage); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Creates a result indicating the grant was cancelled. |
| | | 74 | | /// </summary> |
| | | 75 | | public static CapabilityGrantUseResult Cancelled(string? failureMessage = null) |
| | | 76 | | { |
| | 2 | 77 | | return new CapabilityGrantUseResult(GrantUseState.Cancelled, 0, "capability.grant-cancelled", failureMessage); |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Creates a result indicating the use-control store was unavailable. |
| | | 82 | | /// </summary> |
| | | 83 | | public static CapabilityGrantUseResult Unavailable(string? failureMessage = null) |
| | | 84 | | { |
| | 2 | 85 | | return new CapabilityGrantUseResult(GrantUseState.Unavailable, 0, "capability.use-store-unavailable", failureMes |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | private static string? NormalizeOptional(string? value) |
| | | 89 | | { |
| | 22 | 90 | | return string.IsNullOrWhiteSpace(value) |
| | 22 | 91 | | ? null |
| | 22 | 92 | | : value.Trim(); |
| | | 93 | | } |
| | | 94 | | } |