azure-activedirectory-identitymodel-extensions-for-dotnet icon indicating copy to clipboard operation
azure-activedirectory-identitymodel-extensions-for-dotnet copied to clipboard

Add ReadOnlyMemory based overload for CanReadToken

Open henrikwidlund opened this issue 6 months ago • 15 comments
trafficstars

Add Memory based overload for CanReadToken

  • [x] You've read the Contributor Guide and Code of Conduct.
  • [x] You've included unit or integration tests for your change, where applicable.
  • [x] You've included inline docs for your change, where applicable.
  • [ ] If any gains or losses in performance are possible, you've included benchmarks for your changes. More info
  • [x] There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Description

Added an overload for the CanReadToken in JsonWebTokenHandler and JwtSecurityTokenHandler with ReadOnlyMemory<char> to reduce allocations and adjusted the string based overload to call the new method.

New overload is only available on dotnet 8 and above as the span based regex methods used to match JWS/JWE isn't available in dotnet 6.

Fixes #3221

henrikwidlund avatar May 07 '25 08:05 henrikwidlund

@microsoft-github-policy-service agree

henrikwidlund avatar May 07 '25 08:05 henrikwidlund

I did not add benchmarks, even though this is a performance improvement. The reason being that the string based API calls the span based one.

henrikwidlund avatar May 07 '25 08:05 henrikwidlund

Thanks for your contribution @henrikwidlund . Are you planning to add unit tests?

kllysng avatar May 08 '25 19:05 kllysng

@kllysng I was thinking that the regular string based ones would cover it since the string based method calls the memory based one. Let me know if you want explicit tests.

henrikwidlund avatar May 09 '25 07:05 henrikwidlund

I've added tests now either way. Also added the method to JwtSecurityTokenHandler. Was thinking of adding the memory based overload to JwtSecurityTokenHandler.ReadToken, but feels out of scope.

henrikwidlund avatar May 09 '25 14:05 henrikwidlund

Good points @brentschmaltz, I've applied your suggestions.

henrikwidlund avatar May 12 '25 19:05 henrikwidlund

I see that a few unit tests are failing because of accessibility. I'll make the method internal again tomorrow

henrikwidlund avatar May 13 '25 21:05 henrikwidlund

@sruke fixed the compiler error

henrikwidlund avatar May 14 '25 07:05 henrikwidlund

@henrikwidlund, a few unit tests are still failing. We’re planning to release IdentityModel today—would it be okay to include this PR in the next release (in a month) instead?

sruke avatar May 15 '25 17:05 sruke

@sruke I believe I've fixed the test (was a real bug). I leave it up to you if it's getting to tight for your deadline (although, it would be nice to have it :))

henrikwidlund avatar May 15 '25 18:05 henrikwidlund

I noticed that the PR build is still failing, but I don't understand if it's related to my code changes?

henrikwidlund avatar May 19 '25 18:05 henrikwidlund

I noticed that the PR build is still failing, but I don't understand if it's related to my code changes?

The failure (Resource not accessible by integration) appears unrelated to recent changes. All tests passed. I’m rerunning the build to check if the issue resolves. This change wasn’t included in the previous IdentityModel release but will be part of the upcoming one.

sruke avatar May 20 '25 17:05 sruke

I noticed that the PR build is still failing, but I don't understand if it's related to my code changes?

The failure (Resource not accessible by integration) appears unrelated to recent changes. All tests passed. I’m rerunning the build to check if the issue resolves. This change wasn’t included in the previous IdentityModel release but will be part of the upcoming one.

Notice that it didn't help 😕

henrikwidlund avatar May 20 '25 18:05 henrikwidlund

@sruke @brentschmaltz ping, any ideas what the problem is and when this can be merged?

henrikwidlund avatar May 23 '25 18:05 henrikwidlund

@sruke @brentschmaltz ping again :/

henrikwidlund avatar Jun 04 '25 17:06 henrikwidlund

Is this PR still wanted? It's been open for four months now. @brentschmaltz @sruke @jennyf19

henrikwidlund avatar Sep 23 '25 09:09 henrikwidlund

@henrikwidlund Sorry about the delay. I'm on holiday next week. I'll bring this up with the team, if there's available bandwidth; otherwise, I'll follow up on this after I'm back.

pmaytak avatar Sep 26 '25 07:09 pmaytak