NetDevPack / NetDevPack/Security.Jwt
Prefer using IssuerSigningKeyResolver over cleaning TokenHandlers
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 297
- Forks
- 48
- PR merge metrics
- No merged PRs in 30d
Description
TokenValidationParameters has IssuerSigningKeyResolver that provides you a kid of the required key immediately.
Adjusting TokenHandlers in JwtServiceValidationHandler has an issue.
It gets a list of keys to check while the amount of keys to retrieve is unknown. In the perfect world you should validate against any key in the database unless it was explicitly revoked.
Other smaller issue is that it's a bit intrusive. Because what if a user added his own validator there and you just deleted it without telling a user about it.
My easy naive approach would be something like this:
IssuerSigningKeyResolver = (string token, SecurityToken securityToken, string kid, TokenValidationParameters validationParameters) =>
{
using var scope = serviceProvider.CreateScope();
var service = scope.ServiceProvider.GetRequiredService<IJsonWebKeyStore>();
var key = service.Get(kid).Result;
return key != null ? [key.GetSecurityKey()] : [];
},
In this case I'm not sure if GetLastKeys function is needed at all
In this case IJsonWebKeyStore can have a cache on kid directly and doesn't have to cache whole set of keys.
Note in .NET 9 there will be possiblity to make it async
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in JwtServiceValidationHandler, where TokenValidationParameters and TokenHandlers are configured, and inspect how GetLastKeys and IJsonWebKeyStore are used. The change is complete when key lookup uses the requested kid without removing a user's custom TokenHandlers, while preserving the existing token-validation behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- authentication, security
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100