| | | 1 | | using AsiBackbone.Core.CapabilityGrants; |
| | | 2 | | using AsiBackbone.Core.Signing; |
| | | 3 | | |
| | | 4 | | namespace AsiBackbone.AspNetCore.CapabilityGrants; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Configures <see cref="SignedCapabilityGrantEndpointValidator" />. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// The host owns every value here. <see cref="Issuer" /> and <see cref="Audience" /> are required; the validator binds |
| | | 11 | | /// the actor, operation, scopes, and policy context itself from the request, the endpoint descriptor, and the current |
| | | 12 | | /// decision. |
| | | 13 | | /// </remarks> |
| | | 14 | | public sealed class SignedCapabilityGrantOptions |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Gets the default request header that carries the capability grant token. |
| | | 18 | | /// </summary> |
| | | 19 | | public const string DefaultTokenHeaderName = "AsiBackbone-Capability-Grant"; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Gets or sets the issuer every accepted grant must name. |
| | | 23 | | /// </summary> |
| | | 24 | | public string? Issuer { get; set; } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets or sets the audience every accepted grant must name, typically this relying party. |
| | | 28 | | /// </summary> |
| | | 29 | | public string? Audience { get; set; } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets or sets the non-negative tolerance applied to a grant's not-before and expiry times. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <remarks> |
| | | 35 | | /// Defaults to zero. Hosts whose issuer and relying party run on different machines usually allow a few seconds. |
| | | 36 | | /// </remarks> |
| | | 37 | | public TimeSpan AllowedClockSkew { get; set; } = TimeSpan.Zero; |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Gets or sets the local use limit. The narrower of this value and the grant's signed |
| | | 41 | | /// <see cref="CapabilityGrant.MaxUseCount" /> applies. |
| | | 42 | | /// </summary> |
| | | 43 | | public int MaxUseCount { get; set; } = 1; |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets or sets a value indicating whether the grant must carry the policy version and hash of the current decision |
| | | 47 | | /// when the decision has them. |
| | | 48 | | /// </summary> |
| | | 49 | | public bool BindPolicy { get; set; } = true; |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets or sets the request header that carries the token, or <see langword="null" /> to not read a header. |
| | | 53 | | /// </summary> |
| | | 54 | | public string? TokenHeaderName { get; set; } = DefaultTokenHeaderName; |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets or sets the form field that carries the token, or <see langword="null" /> to not read the form. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <remarks> |
| | | 60 | | /// The header is read first. <see cref="FormFieldCapabilityGrantRequestBindingProvider" /> always excludes this fie |
| | | 61 | | /// from the form binding, because the token cannot bind itself. |
| | | 62 | | /// </remarks> |
| | | 63 | | public string? TokenFormFieldName { get; set; } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Gets or sets the maximum accepted token length, in characters. |
| | | 67 | | /// </summary> |
| | | 68 | | public int MaxTokenLength { get; set; } = CapabilityGrantToken.DefaultMaxTokenLength; |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Gets or sets the signing key identifier the grant proof must use, when set. |
| | | 72 | | /// </summary> |
| | | 73 | | public string? ExpectedProofKeyId { get; set; } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Gets or sets the signing key version the grant proof must use, when set. |
| | | 77 | | /// </summary> |
| | | 78 | | public string? ExpectedProofKeyVersion { get; set; } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Gets or sets the signing provider that must have produced the grant proof, when set. |
| | | 82 | | /// </summary> |
| | | 83 | | public string? RequiredProofProvider { get; set; } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Gets or sets the hash algorithm the grant proof must use, when set. |
| | | 87 | | /// </summary> |
| | | 88 | | public string? RequiredProofHashAlgorithm { get; set; } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Gets or sets the canonical payload options the issuer signed with; defaults apply when <see langword="null" />. |
| | | 92 | | /// </summary> |
| | | 93 | | public CanonicalPayloadOptions? ProofPayloadOptions { get; set; } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Validates the configured options. |
| | | 97 | | /// </summary> |
| | | 98 | | /// <exception cref="InvalidOperationException">An option is missing or out of range.</exception> |
| | | 99 | | public void Validate() |
| | | 100 | | { |
| | 94 | 101 | | if (string.IsNullOrWhiteSpace(Issuer)) |
| | | 102 | | { |
| | 1 | 103 | | throw new InvalidOperationException("Signed capability grant validation requires an issuer."); |
| | | 104 | | } |
| | | 105 | | |
| | 93 | 106 | | if (string.IsNullOrWhiteSpace(Audience)) |
| | | 107 | | { |
| | 2 | 108 | | throw new InvalidOperationException("Signed capability grant validation requires an audience."); |
| | | 109 | | } |
| | | 110 | | |
| | 91 | 111 | | if (AllowedClockSkew < TimeSpan.Zero) |
| | | 112 | | { |
| | 1 | 113 | | throw new InvalidOperationException("The allowed clock skew must not be negative."); |
| | | 114 | | } |
| | | 115 | | |
| | 90 | 116 | | if (MaxUseCount < 1) |
| | | 117 | | { |
| | 1 | 118 | | throw new InvalidOperationException("The maximum use count must be at least one."); |
| | | 119 | | } |
| | | 120 | | |
| | 89 | 121 | | if (MaxTokenLength < 1) |
| | | 122 | | { |
| | 1 | 123 | | throw new InvalidOperationException("The maximum token length must be at least one character."); |
| | | 124 | | } |
| | | 125 | | |
| | 88 | 126 | | if (string.IsNullOrWhiteSpace(TokenHeaderName) && string.IsNullOrWhiteSpace(TokenFormFieldName)) |
| | | 127 | | { |
| | 1 | 128 | | throw new InvalidOperationException("Signed capability grant validation requires a token header name, a toke |
| | | 129 | | } |
| | 87 | 130 | | } |
| | | 131 | | } |