-
Notifications
You must be signed in to change notification settings - Fork 453
Move CNF from SHR to M.IM.Tokens #3168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
81 changes: 8 additions & 73 deletions
81
src/Microsoft.IdentityModel.Protocols.SignedHttpRequest/Cnf.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,88 +1,23 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Text; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using Microsoft.IdentityModel.Logging; | ||
| using Microsoft.IdentityModel.Tokens; | ||
| using Microsoft.IdentityModel.Tokens.Json; | ||
|
|
||
| namespace Microsoft.IdentityModel.Protocols.SignedHttpRequest | ||
| { | ||
| /// <summary> | ||
| /// Represents the Cnf Claim | ||
| /// </summary> | ||
| internal class Cnf | ||
| internal class Cnf : Tokens.Cnf | ||
| { | ||
| internal const string ClassName = "Microsoft.IdentityModel.Protocols.SignedHttpRequest.Cnf"; | ||
|
|
||
| public Cnf() { } | ||
| private readonly string _shrClassName = "Microsoft.IdentityModel.Protocols.SignedHttpRequest.Cnf"; | ||
|
|
||
| public Cnf(string json) | ||
| public Cnf() : base() | ||
| { | ||
| if (string.IsNullOrEmpty(json)) | ||
| throw LogHelper.LogArgumentNullException(nameof(json)); | ||
|
|
||
| Utf8JsonReader reader = new(Encoding.UTF8.GetBytes(json).AsSpan()); | ||
| if (!JsonSerializerPrimitives.IsReaderAtTokenType(ref reader, JsonTokenType.StartObject, true)) | ||
| throw LogHelper.LogExceptionMessage( | ||
| new JsonException( | ||
| LogHelper.FormatInvariant( | ||
| Tokens.LogMessages.IDX11023, | ||
| LogHelper.MarkAsNonPII("JsonTokenType.StartObject"), | ||
| LogHelper.MarkAsNonPII(reader.TokenType), | ||
| LogHelper.MarkAsNonPII(ClassName), | ||
| LogHelper.MarkAsNonPII(reader.TokenStartIndex), | ||
| LogHelper.MarkAsNonPII(reader.CurrentDepth), | ||
| LogHelper.MarkAsNonPII(reader.BytesConsumed)))); | ||
|
|
||
| while (true) | ||
| { | ||
| if (reader.TokenType == JsonTokenType.PropertyName) | ||
| { | ||
| if (reader.ValueTextEquals(ConfirmationClaimTypesUtf8Bytes.Jwk)) | ||
| { | ||
| reader.Read(); | ||
| JsonWebKey = JsonWebKeySerializer.Read(ref reader, new JsonWebKey()); | ||
| } | ||
| else if (reader.ValueTextEquals(ConfirmationClaimTypesUtf8Bytes.Kid)) | ||
| { | ||
| Kid = JsonSerializerPrimitives.ReadString(ref reader, ConfirmationClaimTypes.Kid, ClassName, true); | ||
| } | ||
| else if (reader.ValueTextEquals(ConfirmationClaimTypesUtf8Bytes.Jku)) | ||
| { | ||
| Jku = JsonSerializerPrimitives.ReadString(ref reader, ConfirmationClaimTypes.Kid, ClassName, true); | ||
| } | ||
| else if (reader.ValueTextEquals(ConfirmationClaimTypesUtf8Bytes.Jwe)) | ||
| { | ||
| Jwe = JsonSerializerPrimitives.ReadString(ref reader, ConfirmationClaimTypes.Jwe, ClassName, true); | ||
| } | ||
| else | ||
| { | ||
| // this is not a property we recognize, skip it. | ||
| reader.Skip(); | ||
| } | ||
| } | ||
| // We read a JsonTokenType.StartObject above, exiting and positioning reader at next token. | ||
| else if (JsonSerializerPrimitives.IsReaderAtTokenType(ref reader, JsonTokenType.EndObject, true)) | ||
| break; | ||
| else if (!reader.Read()) | ||
| break; | ||
| } | ||
| ClassName = _shrClassName; | ||
| } | ||
|
|
||
| [JsonPropertyName("kid")] | ||
| public string Kid { get; set; } | ||
|
|
||
| [JsonPropertyName("jwe")] | ||
| public string Jwe { get; set; } | ||
|
|
||
| [JsonPropertyName("jku")] | ||
| public string Jku { get; set; } | ||
|
|
||
| [JsonPropertyName("jwk")] | ||
| public JsonWebKey JsonWebKey { get; set; } | ||
| public Cnf(string json) : base(json) | ||
| { | ||
| ClassName = _shrClassName; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Text; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using Microsoft.IdentityModel.Logging; | ||
| using Microsoft.IdentityModel.Tokens.Json; | ||
|
|
||
| namespace Microsoft.IdentityModel.Tokens | ||
| { | ||
| /// <summary> | ||
| /// Represents the Cnf Claim | ||
| /// </summary> | ||
| public class Cnf | ||
| { | ||
| private const string DefaultClassName = "Microsoft.IdentityModel.Tokens.Cnf"; | ||
|
|
||
| /// <summary> | ||
| /// The class name used for logging and exception messages. | ||
| /// </summary> | ||
| protected internal string ClassName { get; set; } = DefaultClassName; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="Cnf"/> class. | ||
| /// </summary> | ||
| public Cnf() | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="Cnf"/> class. | ||
| /// </summary> | ||
| /// <param name="json"> | ||
| /// JSON representation of the Cnf Claim. | ||
| /// </param> | ||
| public Cnf(string json) | ||
| { | ||
| if (string.IsNullOrEmpty(json)) | ||
| throw LogHelper.LogArgumentNullException(nameof(json)); | ||
|
|
||
| Utf8JsonReader reader = new(Encoding.UTF8.GetBytes(json).AsSpan()); | ||
| if (!JsonSerializerPrimitives.IsReaderAtTokenType(ref reader, JsonTokenType.StartObject, true)) | ||
| throw LogHelper.LogExceptionMessage( | ||
| new JsonException( | ||
| LogHelper.FormatInvariant( | ||
| LogMessages.IDX11023, | ||
| LogHelper.MarkAsNonPII("JsonTokenType.StartObject"), | ||
| LogHelper.MarkAsNonPII(reader.TokenType), | ||
| LogHelper.MarkAsNonPII(ClassName), | ||
| LogHelper.MarkAsNonPII(reader.TokenStartIndex), | ||
| LogHelper.MarkAsNonPII(reader.CurrentDepth), | ||
| LogHelper.MarkAsNonPII(reader.BytesConsumed)))); | ||
|
|
||
| while (true) | ||
| { | ||
| if (reader.TokenType == JsonTokenType.PropertyName) | ||
| { | ||
| if (reader.ValueTextEquals(ConfirmationClaimTypesUtf8Bytes.Jwk)) | ||
| { | ||
| reader.Read(); | ||
| JsonWebKey = JsonWebKeySerializer.Read(ref reader, new JsonWebKey()); | ||
| } | ||
| else if (reader.ValueTextEquals(ConfirmationClaimTypesUtf8Bytes.Kid)) | ||
| { | ||
| Kid = JsonSerializerPrimitives.ReadString(ref reader, ConfirmationClaimTypes.Kid, ClassName, true); | ||
| } | ||
| else if (reader.ValueTextEquals(ConfirmationClaimTypesUtf8Bytes.Jku)) | ||
| { | ||
| Jku = JsonSerializerPrimitives.ReadString(ref reader, ConfirmationClaimTypes.Kid, ClassName, true); | ||
| } | ||
| else if (reader.ValueTextEquals(ConfirmationClaimTypesUtf8Bytes.Jwe)) | ||
| { | ||
| Jwe = JsonSerializerPrimitives.ReadString(ref reader, ConfirmationClaimTypes.Jwe, ClassName, true); | ||
| } | ||
| else | ||
| { | ||
| // this is not a property we recognize, skip it. | ||
| reader.Skip(); | ||
| } | ||
| } | ||
| // We read a JsonTokenType.StartObject above, exiting and positioning reader at next token. | ||
| else if (JsonSerializerPrimitives.IsReaderAtTokenType(ref reader, JsonTokenType.EndObject, true)) | ||
| break; | ||
| else if (!reader.Read()) | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The Kid property is used to specify the key ID of the public key used to verify the signature of the JWT. | ||
| /// </summary> | ||
| [JsonPropertyName("kid")] | ||
| public string Kid { get; internal set; } | ||
|
|
||
| /// <summary> | ||
| /// The Jwe property is used to specify an encrypted JSON web key. | ||
| /// </summary> | ||
| [JsonPropertyName("jwe")] | ||
| public string Jwe { get; internal set; } | ||
|
|
||
| /// <summary> | ||
| /// The Jku property is used to specify the location of the public key used to verify the signature of the JWT. | ||
| /// </summary> | ||
| [JsonPropertyName("jku")] | ||
| public string Jku { get; internal set; } | ||
|
|
||
| /// <summary> | ||
| /// The Jwk property is used to specify the public key used to verify the signature of the JWT. | ||
| /// </summary> | ||
| [JsonPropertyName("jwk")] | ||
| public JsonWebKey JsonWebKey { get; internal set; } | ||
| } | ||
| } |
48 changes: 48 additions & 0 deletions
48
src/Microsoft.IdentityModel.Tokens/ConfirmationClaimTypes.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
|
|
||
| namespace Microsoft.IdentityModel.Tokens | ||
| { | ||
| /// <summary> | ||
| /// Confirmation Claim ("cnf") related constants | ||
| /// https://datatracker.ietf.org/doc/html/rfc7800 | ||
| /// </summary> | ||
| public static class ConfirmationClaimTypes | ||
| { | ||
| /// <summary> | ||
| /// https://datatracker.ietf.org/doc/html/rfc7800#section-6.1.1 | ||
| /// </summary> | ||
| public const string Cnf = "cnf"; | ||
|
|
||
| /// <summary> | ||
| /// https://datatracker.ietf.org/doc/html/rfc7800#section-6.2.2 | ||
| /// </summary> | ||
| public const string Jwk = "jwk"; | ||
|
|
||
| /// <summary> | ||
| /// https://datatracker.ietf.org/doc/html/rfc7800#section-6.2.2 | ||
| /// </summary> | ||
| public const string Jwe = "jwe"; | ||
|
|
||
| /// <summary> | ||
| /// https://datatracker.ietf.org/doc/html/rfc7800#section-6.2.2 | ||
| /// </summary> | ||
| public const string Jku = "jku"; | ||
|
|
||
| /// <summary> | ||
| /// https://datatracker.ietf.org/doc/html/rfc7800#section-6.2.2 | ||
| /// </summary> | ||
| public const string Kid = "kid"; | ||
| } | ||
|
|
||
| internal static class ConfirmationClaimTypesUtf8Bytes | ||
| { | ||
| public static ReadOnlySpan<byte> Cnf => "cnf"u8; | ||
| public static ReadOnlySpan<byte> Jwk => "jwk"u8; | ||
| public static ReadOnlySpan<byte> Jwe => "jwe"u8; | ||
| public static ReadOnlySpan<byte> Jku => "jku"u8; | ||
| public static ReadOnlySpan<byte> Kid => "kid"u8; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| Microsoft.IdentityModel.Tokens.Cnf.Jku.set -> void | ||
| Microsoft.IdentityModel.Tokens.Cnf.JsonWebKey.set -> void | ||
| Microsoft.IdentityModel.Tokens.Cnf.Jwe.set -> void | ||
| Microsoft.IdentityModel.Tokens.Cnf.Kid.set -> void | ||
| Microsoft.IdentityModel.Protocols.SignedHttpRequest.ConfirmationClaimTypesUtf8Bytes | ||
| Microsoft.IdentityModel.Tokens.Cnf.Jku.set -> void | ||
| Microsoft.IdentityModel.Tokens.Cnf.JsonWebKey.set -> void | ||
| Microsoft.IdentityModel.Tokens.Cnf.Jwe.set -> void | ||
| Microsoft.IdentityModel.Tokens.Cnf.Kid.set -> void |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| Microsoft.IdentityModel.Tokens.Cnf | ||
| Microsoft.IdentityModel.Tokens.Cnf.Cnf() -> void | ||
| Microsoft.IdentityModel.Tokens.Cnf.Cnf(string json) -> void | ||
| Microsoft.IdentityModel.Tokens.Cnf.Jku.get -> string | ||
| Microsoft.IdentityModel.Tokens.Cnf.JsonWebKey.get -> Microsoft.IdentityModel.Tokens.JsonWebKey | ||
| Microsoft.IdentityModel.Tokens.Cnf.Jwe.get -> string | ||
| Microsoft.IdentityModel.Tokens.Cnf.Kid.get -> string | ||
| const Microsoft.IdentityModel.Tokens.ConfirmationClaimTypes.Cnf = "cnf" -> string | ||
| const Microsoft.IdentityModel.Tokens.ConfirmationClaimTypes.Jku = "jku" -> string | ||
| const Microsoft.IdentityModel.Tokens.ConfirmationClaimTypes.Jwe = "jwe" -> string | ||
| const Microsoft.IdentityModel.Tokens.ConfirmationClaimTypes.Jwk = "jwk" -> string | ||
| const Microsoft.IdentityModel.Tokens.ConfirmationClaimTypes.Kid = "kid" -> string | ||
| Microsoft.IdentityModel.Tokens.ConfirmationClaimTypes | ||
| Microsoft.IdentityModel.Tokens.Cnf.ClassName.get -> string | ||
| Microsoft.IdentityModel.Tokens.Cnf.ClassName.set -> void |
25 changes: 25 additions & 0 deletions
25
test/Microsoft.IdentityModel.Protocols.SignedHttpRequest.Tests/CnfTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Xunit; | ||
|
|
||
| namespace Microsoft.IdentityModel.Protocols.SignedHttpRequest.Tests | ||
| { | ||
| public class CnfTests | ||
| { | ||
| [Fact] | ||
| public void CtorWithJson_ExpectedClassName() | ||
| { | ||
| var json = "{\"jwk\":{\"kty\":\"oct\",\"k\":\"AQIDBAUGBwgJCgsMDQ4PEC==\"}}"; | ||
| var cnf = new Cnf(json); | ||
| Assert.Equal("Microsoft.IdentityModel.Protocols.SignedHttpRequest.Cnf", cnf.ClassName); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Ctor_ExpectedClassName() | ||
| { | ||
| var cnf = new Cnf(); | ||
| Assert.Equal("Microsoft.IdentityModel.Protocols.SignedHttpRequest.Cnf", cnf.ClassName); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Xunit; | ||
|
|
||
| namespace Microsoft.IdentityModel.Tokens.Tests | ||
| { | ||
| public class CnfTests | ||
| { | ||
| [Fact] | ||
| public void CtorWithJson_ExpectedClassName() | ||
| { | ||
| var json = "{\"jwk\":{\"kty\":\"oct\",\"k\":\"AQIDBAUGBwgJCgsMDQ4PEC==\"}}"; | ||
| var cnf = new Cnf(json); | ||
| Assert.Equal("Microsoft.IdentityModel.Tokens.Cnf", cnf.ClassName); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Ctor_ExpectedClassName() | ||
| { | ||
| var cnf = new Cnf(); | ||
| Assert.Equal("Microsoft.IdentityModel.Tokens.Cnf", cnf.ClassName); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.