Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hono/jwk): Extended with allow_anon option & passing Context to callbacks #3961

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Beyondo
Copy link
Contributor

@Beyondo Beyondo commented Feb 27, 2025

Adding two main useful features people have been asking for in the original PR + some slight changes.

New features:

Example:

app.use(
  '/auth/*',
  jwk({
    jwks_uri: (c) => `https://${c.env.authServer}/.well-known/jwks.json`,
    allow_anon: true
  })
)

Note 1: This PR allows having a single endpoint for both authenticated and non-authenticated requests through allow_anon. The variable c.get("jwtPayload") can be used to differentiate authenticated requests from anonymous requests since hono requires requests to have exp and iat in the payload (in the verify stage) which means the payload would always be defined for authenticated requests and be reliably used as indicator.

Note 2: Setting allow_anon to true means requests without a token present (in either header/cookie) would be allowed to pass through the middleware successfully.

Tests: Added 3 extra tests + modified one test to cover jwks_uri being a function.

✓ src/middleware/jwk/index.test.ts (34)

This PR breaks no code at all except for Jwt.verifyFromJwks(...)—specifically removed callbacks support from it since they're redundant and can be called outside, and also since only the middleware can provide the Context variable to the callbacks.

…_token` option

One slightly breaking change:
Removed function callbacks from `Jwt.verifyFromJwks` for two reasons:
1. It’s always called from functions, so `jwks_uri`/`keys` can be grabbed before calling `Jwt.verifyFromJwks` anyways
2. Jwt.verifyFromJwks cannot provide the Context to the new callbacks—only the middleware can

Callbacks now also accept either a Promise or a synchronous function, not only async.
@Beyondo
Copy link
Contributor Author

Beyondo commented Feb 27, 2025

Hmm, I don't think that Deno error is caused by my PR as I was so sure I pulled from the upstream, but I will attempt to fix it.

Edit: Turns out nope; I accidentally overridden the upstream change of that .buffer line de3db7d cause my editor kept an old downstream version when I saved and I of course didn't notice that as it was a small diff in the same file I was comitting 🤦 (all unit tests passing didn't help me notice either), but should be all good now for Deno and would be more careful next time!

Copy link

codecov bot commented Feb 27, 2025

Codecov Report

Attention: Patch coverage is 92.30769% with 1 line in your changes missing coverage. Please review.

Project coverage is 91.33%. Comparing base (a2ec848) to head (5a70d82).

Files with missing lines Patch % Lines
src/utils/jwt/jwt.ts 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3961      +/-   ##
==========================================
+ Coverage   91.32%   91.33%   +0.01%     
==========================================
  Files         168      168              
  Lines       10688    10692       +4     
  Branches     3070     3059      -11     
==========================================
+ Hits         9761     9766       +5     
+ Misses        926      925       -1     
  Partials        1        1              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@flexchar
Copy link

Looks very promising, thank you a lot for your prompt contribution, wise person across the world! 👌

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.

2 participants