Skip to content

Conversation

@Avery-Dunn
Copy link
Contributor

This PR removes various com.nimbusds helpers and utils, as per #909. In short, the following changes were made:

  • Refactor JwtHelper to use built-in Java functionality rather than com.nimbusds imports
  • Use Java's StandardCharsets instead of nimbus's equivalent
  • Throw Java MSAL-specific exception types
  • Unit tests for the changes to JwtHelper

It also removes unused code in IdToken, and the unused class SAML11BearerGrant (no longer needed due to the changes in #926)

@Avery-Dunn Avery-Dunn requested a review from a team as a code owner April 23, 2025 00:36
Copy link
Contributor

@neha-bhargava neha-bhargava left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approve with comments.

String idTokenJson;

try {
idTokenJson = new String(Base64.getDecoder().decode(idToken.split("\\.")[1]), StandardCharsets.UTF_8);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jwt's are base64url encoded, not base64. THis already results in a P1.

Can we add some tests around it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was mistakenly copied from a branch that had not yet been updated with the fix for the previous issue.

In the latest commit the logic both here and in TokenRequestExecutor has been moved to a helper method in JsonHelper, and new tests were added to confirm the behavior.

String idTokenJson;

try {
idTokenJson = new String(Base64.getDecoder().decode(idToken.split("\\.")[1]), StandardCharsets.UTF_8);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very strange operation (extracting only the body). Why are you doing this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was copied from similar code in TokenRequestExecutor, which had been parsing the body since that file was first added: https://github.com/AzureAD/microsoft-authentication-library-for-java/blame/bbdc93d59d54916cdbac4016f1b174822e9bb51c/msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/TokenRequestExecutor.java#L123

An IdToken is created from the JSON of claims in the body: https://github.com/AzureAD/microsoft-authentication-library-for-java/blob/dev/msal4j-sdk/src/main/java/com/microsoft/aad/msal4j/IdToken.java

I'm not sure exactly why just the body is decoded, however from some testing now it seems like Java's built-in base64 URL decoded cannot parse the signature so that may be why.


// Build header
Map<String, Object> header = new HashMap<>();
header.put("alg", "RS256");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alg is PS256 not RS256 to suggest PSS padding instead of PKCS1 padding. Please have a look at the .NET code for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current version of the library uses RS256 here, and has apparently done so since the 'sendX5C' API was first added in 2020 by PR #285:

So this might need a more thorough investigation. Pretty much every integration test sends that "alg=RS256" header when retrieving the token to access ID labs, so the endpoint is either accepting or ignoring it.


// Decode and verify headers
String headerJson = new String(Base64.getUrlDecoder().decode(jwtParts[0]));
assertTrue(headerJson.contains("\"alg\":\"RS256\""), "Header should specify RS256 algorithm");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be PS256 . RS256 means PKCS1 padding, which is not approved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These comments have more context: #941 (comment)

Copy link
Member

@bgavrilMS bgavrilMS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PSS

…crosoft-authentication-library-for-java into avdunn/nimbus-utils

# Conflicts:
#	msal4j-sdk/src/test/java/com/microsoft/aad/msal4j/HelperAndUtilityTests.java
@Avery-Dunn Avery-Dunn changed the base branch from avdunn/nimbus-grants to avdunn/nimbus-removal April 25, 2025 21:25
@Avery-Dunn Avery-Dunn requested a review from bgavrilMS April 25, 2025 22:48
@Avery-Dunn Avery-Dunn merged commit f0d8e24 into avdunn/nimbus-removal May 7, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants