Skip to content

Commit

Permalink
Fix Pre-release Regression: Correct JSON Web Key (JWK) Encoding for R…
Browse files Browse the repository at this point in the history
…SA Public Key Export (#4878)

* Revert JWK format

* Update src/client/Microsoft.Identity.Client/AuthScheme/PoP/InMemoryCryptoProvider.cs

Co-authored-by: Gladwin Johnson <[email protected]>

* Adding JWK test
disabling failing test

---------

Co-authored-by: trwalke <[email protected]>
Co-authored-by: Gladwin Johnson <[email protected]>
  • Loading branch information
3 people authored Aug 6, 2024
1 parent 83725aa commit dd337e2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ public byte[] Sign(byte[] payload)
/// </summary>
private static string ComputeCanonicalJwk(RSAParameters rsaPublicKey)
{
return $@"{{""{JsonWebKeyParameterNames.E}"":""{Base64UrlHelpers.Encode(rsaPublicKey.Exponent)}"",
""{JsonWebKeyParameterNames.Kty}"":""{JsonWebAlgorithmsKeyTypes.RSA}"",
""{JsonWebKeyParameterNames.N}"":""{Base64UrlHelpers.Encode(rsaPublicKey.Modulus)}""}}";
//Important: This format cannot be modified as it needs to be the same as what is used in the service when calculating hashes.
return $@"{{""{JsonWebKeyParameterNames.E}"":""{Base64UrlHelpers.Encode(rsaPublicKey.Exponent)}"",""{JsonWebKeyParameterNames.Kty}"":""{JsonWebAlgorithmsKeyTypes.RSA}"",""{JsonWebKeyParameterNames.N}"":""{Base64UrlHelpers.Encode(rsaPublicKey.Modulus)}""}}";
}

/// <summary>
Expand Down
18 changes: 18 additions & 0 deletions tests/Microsoft.Identity.Test.Unit/pop/PoPTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using NSubstitute;
using JsonWebAlgorithmsKeyTypes = Microsoft.Identity.Client.AuthScheme.PoP.JsonWebAlgorithmsKeyTypes;
using JsonWebKeyParameterNames = Microsoft.Identity.Client.AuthScheme.PoP.JsonWebKeyParameterNames;

namespace Microsoft.Identity.Test.Unit.Pop
{
Expand Down Expand Up @@ -678,5 +680,21 @@ public async Task TokenGenerationAndValidation_Async()
AssertSingedHttpRequestClaims(provider, claims);
}
}

[TestMethod]
public void ValidateCanonicalJwkFormat()
{
// Arrange
var provider = PoPProviderFactory.GetOrCreateProvider();
var actualCanonicaljwk = provider.CannonicalPublicKeyJwk;

// Act and Assert

// Parse the JWK to get the RSA parameters so that we can create a new canonical JWK in expected format
var jsonWebKey = JsonWebKey.Create(actualCanonicaljwk);
var expectedCanonicalJwk = $@"{{""{JsonWebKeyParameterNames.E}"":""{jsonWebKey.E}"",""{JsonWebKeyParameterNames.Kty}"":""{JsonWebAlgorithmsKeyTypes.RSA}"",""{JsonWebKeyParameterNames.N}"":""{jsonWebKey.N}""}}";

Assert.AreEqual(expectedCanonicalJwk, actualCanonicaljwk);
}
}
}

0 comments on commit dd337e2

Please sign in to comment.