NetDevPack / NetDevPack/Security.Jwt

Prefer using IssuerSigningKeyResolver over cleaning TokenHandlers

Open
#66 0 comments 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.