|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Text; |
| 6 | +using System.Text.Json; |
| 7 | +using System.Text.Json.Serialization; |
| 8 | +using Microsoft.IdentityModel.Logging; |
| 9 | +using Microsoft.IdentityModel.Tokens.Json; |
| 10 | + |
| 11 | +namespace Microsoft.IdentityModel.Tokens |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Represents the Cnf Claim |
| 15 | + /// </summary> |
| 16 | + public class Cnf |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// The class name used for logging and exception messages. |
| 20 | + /// </summary> |
| 21 | + internal string ClassName { get; } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Initializes a new instance of the <see cref="Cnf"/> class. |
| 25 | + /// </summary> |
| 26 | + public Cnf() |
| 27 | + { |
| 28 | + // GetType returns the runtime type, e.g. if derived, |
| 29 | + // it will return the derived type. |
| 30 | + ClassName = GetType().FullName; |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Initializes a new instance of the <see cref="Cnf"/> class. |
| 35 | + /// </summary> |
| 36 | + /// <param name="json"> |
| 37 | + /// JSON representation of the Cnf Claim. |
| 38 | + /// </param> |
| 39 | + public Cnf(string json) : this() |
| 40 | + { |
| 41 | + if (string.IsNullOrEmpty(json)) |
| 42 | + throw LogHelper.LogArgumentNullException(nameof(json)); |
| 43 | + |
| 44 | + Utf8JsonReader reader = new(Encoding.UTF8.GetBytes(json).AsSpan()); |
| 45 | + if (!JsonSerializerPrimitives.IsReaderAtTokenType(ref reader, JsonTokenType.StartObject, true)) |
| 46 | + throw LogHelper.LogExceptionMessage( |
| 47 | + new JsonException( |
| 48 | + LogHelper.FormatInvariant( |
| 49 | + LogMessages.IDX11023, |
| 50 | + LogHelper.MarkAsNonPII("JsonTokenType.StartObject"), |
| 51 | + LogHelper.MarkAsNonPII(reader.TokenType), |
| 52 | + LogHelper.MarkAsNonPII(ClassName), |
| 53 | + LogHelper.MarkAsNonPII(reader.TokenStartIndex), |
| 54 | + LogHelper.MarkAsNonPII(reader.CurrentDepth), |
| 55 | + LogHelper.MarkAsNonPII(reader.BytesConsumed)))); |
| 56 | + |
| 57 | + while (true) |
| 58 | + { |
| 59 | + if (reader.TokenType == JsonTokenType.PropertyName) |
| 60 | + { |
| 61 | + if (reader.ValueTextEquals(ConfirmationClaimTypesUtf8Bytes.Jwk)) |
| 62 | + { |
| 63 | + reader.Read(); |
| 64 | + JsonWebKey = JsonWebKeySerializer.Read(ref reader, new JsonWebKey()); |
| 65 | + } |
| 66 | + else if (reader.ValueTextEquals(ConfirmationClaimTypesUtf8Bytes.Kid)) |
| 67 | + { |
| 68 | + Kid = JsonSerializerPrimitives.ReadString(ref reader, ConfirmationClaimTypes.Kid, ClassName, true); |
| 69 | + } |
| 70 | + else if (reader.ValueTextEquals(ConfirmationClaimTypesUtf8Bytes.Jku)) |
| 71 | + { |
| 72 | + Jku = JsonSerializerPrimitives.ReadString(ref reader, ConfirmationClaimTypes.Kid, ClassName, true); |
| 73 | + } |
| 74 | + else if (reader.ValueTextEquals(ConfirmationClaimTypesUtf8Bytes.Jwe)) |
| 75 | + { |
| 76 | + Jwe = JsonSerializerPrimitives.ReadString(ref reader, ConfirmationClaimTypes.Jwe, ClassName, true); |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + // this is not a property we recognize, skip it. |
| 81 | + reader.Skip(); |
| 82 | + } |
| 83 | + } |
| 84 | + // We read a JsonTokenType.StartObject above, exiting and positioning reader at next token. |
| 85 | + else if (JsonSerializerPrimitives.IsReaderAtTokenType(ref reader, JsonTokenType.EndObject, true)) |
| 86 | + break; |
| 87 | + else if (!reader.Read()) |
| 88 | + break; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// The Kid property is used to specify the key ID of the public key used to verify the signature of the JWT. |
| 94 | + /// </summary> |
| 95 | + [JsonPropertyName("kid")] |
| 96 | + public string Kid { get; internal set; } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// The Jwe property is used to specify an encrypted JSON web key. |
| 100 | + /// </summary> |
| 101 | + [JsonPropertyName("jwe")] |
| 102 | + public string Jwe { get; internal set; } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// The Jku property is used to specify the location of the public key used to verify the signature of the JWT. |
| 106 | + /// </summary> |
| 107 | + [JsonPropertyName("jku")] |
| 108 | + public string Jku { get; internal set; } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// The Jwk property is used to specify the public key used to verify the signature of the JWT. |
| 112 | + /// </summary> |
| 113 | + [JsonPropertyName("jwk")] |
| 114 | + public JsonWebKey JsonWebKey { get; internal set; } |
| 115 | + } |
| 116 | +} |
0 commit comments