|
| 1 | +#if NET |
| 2 | +using System.Text.Json.Nodes; |
| 3 | + |
| 4 | +namespace Openiddict.Contrib.Client; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Helper class for setting parameters matching https://openid.net/specs/openid-connect-core-1_0.html#ClaimsParameter. |
| 8 | +/// </summary> |
| 9 | +public class RequestClaimsParameterValue |
| 10 | +{ |
| 11 | + private readonly Dictionary<string, JsonNode?> _userinfo = new(); |
| 12 | + private readonly Dictionary<string, JsonNode?> _claims = new(); |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// Specify a user info claim that should be returned from the authentication request. |
| 16 | + /// </summary> |
| 17 | + /// <param name="claim">See <see cref="Claims"/>.</param> |
| 18 | + /// <param name="essential">Indicates whether the Claim being requested is an Essential Claim. If the value is true, this indicates that the Claim is an Essential Claim.</param> |
| 19 | + /// <returns>This class for chaining.</returns> |
| 20 | + public RequestClaimsParameterValue UserInfoClaim(string claim, bool essential = false) |
| 21 | + { |
| 22 | + _userinfo.Add(claim, AsNode(essential, null, null)); |
| 23 | + return this; |
| 24 | + } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Specify a user info claim that should be returned from the authentication request. |
| 28 | + /// </summary> |
| 29 | + /// <param name="claim">See <see cref="Claims"/>.</param> |
| 30 | + /// <param name="value">Requests that the Claim be returned with a particular value.</param> |
| 31 | + /// <param name="essential">Indicates whether the Claim being requested is an Essential Claim. If the value is true, this indicates that the Claim is an Essential Claim.</param> |
| 32 | + /// <returns>This class for chaining.</returns> |
| 33 | + public RequestClaimsParameterValue UserInfoClaim(string claim, string value, bool essential = false) |
| 34 | + { |
| 35 | + _userinfo.Add(claim, AsNode(essential, value, null)); |
| 36 | + return this; |
| 37 | + } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Specify a user info claim that should be returned from the authentication request. |
| 41 | + /// </summary> |
| 42 | + /// <param name="claim">See <see cref="Claims"/>.</param> |
| 43 | + /// <param name="values">Requests that the Claim be returned with one of a set of values, with the values appearing in order of preference.</param> |
| 44 | + /// <param name="essential">Indicates whether the Claim being requested is an Essential Claim. If the value is true, this indicates that the Claim is an Essential Claim.</param> |
| 45 | + /// <returns>This class for chaining.</returns> |
| 46 | + public RequestClaimsParameterValue UserInfoClaim(string claim, IReadOnlyCollection<string> values, bool essential) |
| 47 | + { |
| 48 | + _userinfo.Add(claim, AsNode(essential, null, values)); |
| 49 | + return this; |
| 50 | + } |
| 51 | + |
| 52 | + private static JsonObject? AsNode(bool essential, string? value, IReadOnlyCollection<string?>? values) |
| 53 | + { |
| 54 | + var json = new JsonObject(GetJsonNode(essential, value, values)); |
| 55 | + return json.Count == 0 ? default : json; |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Specify a claim that should be returned from the authentication request. |
| 60 | + /// </summary> |
| 61 | + /// <param name="claim">See <see cref="Claims"/>.</param> |
| 62 | + /// <param name="essential">Indicates whether the Claim being requested is an Essential Claim. If the value is true, this indicates that the Claim is an Essential Claim.</param> |
| 63 | + /// <returns>This class for chaining.</returns> |
| 64 | + public RequestClaimsParameterValue IdTokenClaim(string claim, bool essential = false) |
| 65 | + { |
| 66 | + _claims.Add(claim, AsNode(essential, null, null)); |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Specify a claim that should be returned from the authentication request. |
| 72 | + /// </summary> |
| 73 | + /// <param name="claim">See <see cref="Claims"/>.</param> |
| 74 | + /// <param name="value">Requests that the Claim be returned with a particular value.</param> |
| 75 | + /// <param name="essential">Indicates whether the Claim being requested is an Essential Claim. If the value is true, this indicates that the Claim is an Essential Claim.</param> |
| 76 | + /// <returns>This class for chaining.</returns> |
| 77 | + public RequestClaimsParameterValue IdTokenClaim(string claim, string value, bool essential = false) |
| 78 | + { |
| 79 | + _claims.Add(claim, AsNode(essential, value, null)); |
| 80 | + return this; |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Specify a claim that should be returned from the authentication request. |
| 85 | + /// </summary> |
| 86 | + /// <param name="claim">See <see cref="Claims"/>.</param> |
| 87 | + /// <param name="values">Requests that the Claim be returned with one of a set of values, with the values appearing in order of preference.</param> |
| 88 | + /// <param name="essential">Indicates whether the Claim being requested is an Essential Claim. If the value is true, this indicates that the Claim is an Essential Claim.</param> |
| 89 | + /// <returns>This class for chaining.</returns> |
| 90 | + public RequestClaimsParameterValue IdTokenClaim(string claim, IReadOnlyCollection<string> values, bool essential) |
| 91 | + { |
| 92 | + _claims.Add(claim, AsNode(essential, null, values)); |
| 93 | + return this; |
| 94 | + } |
| 95 | + |
| 96 | + /// <summary> |
| 97 | + /// Convert the current settings to a <see cref="OpenIddictParameter"/> that can be used in a request. |
| 98 | + /// <example> |
| 99 | + /// <code> |
| 100 | + /// new AuthenticationProperties().SetParameter(Parameters.Claims, new RequestClaimsParameterValue().IdTokenClaim(Claims.AuthenticationTime, true).AsOpenIddictParameter()); |
| 101 | + /// </code></example> |
| 102 | + /// </summary> |
| 103 | + /// <returns></returns> |
| 104 | + public OpenIddictParameter? AsOpenIddictParameter() |
| 105 | + { |
| 106 | + if (_userinfo.Count == 0 && _claims.Count == 0) |
| 107 | + return null; |
| 108 | + var lst = new List<KeyValuePair<string, JsonNode?>>(); |
| 109 | + |
| 110 | + if (_userinfo.Count != 0) |
| 111 | + lst.Add(new("userinfo", new JsonObject(_userinfo))); |
| 112 | + if (_claims.Count != 0) |
| 113 | + lst.Add(new(Parameters.IdToken, new JsonObject(_claims))); |
| 114 | + return new OpenIddictParameter(new JsonObject(lst)); |
| 115 | + } |
| 116 | + |
| 117 | + private static IEnumerable<KeyValuePair<string, JsonNode?>> GetJsonNode(bool essential, string? value, IReadOnlyCollection<string?>? values) |
| 118 | + { |
| 119 | + if (essential) |
| 120 | + yield return new("essential", true); |
| 121 | + if (value is not null) |
| 122 | + yield return new("value", value!); |
| 123 | + if (values is not null) |
| 124 | + yield return new("values", new JsonArray(values.Select(x => (JsonNode?)JsonValue.Create(x)).ToArray())); |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | +} |
| 129 | +#endif |
0 commit comments