Skip to content

feat(jwt): [#4191] extend Token.encode() to support custom headers #4192

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

Conversation

s-aleshin
Copy link

@s-aleshin s-aleshin commented May 31, 2025

Description

Allows passing custom JWT headers (e.g., kid) to Token.encode(). This improves flexibility in cases where custom JWT headers (like kid) are required.

Closes

Fixes #4191

@s-aleshin s-aleshin requested review from a team as code owners May 31, 2025 14:45
@s-aleshin s-aleshin force-pushed the issue-4191-token-encode-enhancement branch from 75fa443 to 20cec6f Compare May 31, 2025 14:54
Copy link
Member

@provinzkraut provinzkraut left a comment

Choose a reason for hiding this comment

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

Two things:

  1. This is a breaking change (for users that subclass Token), so it would have to be targeted at the v3 branch
  2. I don't think we should add json_encoder and sort_headers, as they are specific to pyjwt, and this is meant as an abstraction over pyjwt, not a drop-in replacement. If you do need to customise how decode interacts with pyjwt, you can simply subclass Token

@s-aleshin s-aleshin force-pushed the issue-4191-token-encode-enhancement branch from db00474 to a98ad84 Compare May 31, 2025 14:57
Copy link

codecov bot commented May 31, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.30%. Comparing base (61e9c18) to head (4692923).
Report is 1 commits behind head on v3.0.

Additional details and impacted files
@@            Coverage Diff             @@
##             v3.0    #4192      +/-   ##
==========================================
- Coverage   98.30%   98.30%   -0.01%     
==========================================
  Files         343      343              
  Lines       15815    15810       -5     
  Branches     1744     1743       -1     
==========================================
- Hits        15547    15542       -5     
  Misses        130      130              
  Partials      138      138              

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@s-aleshin s-aleshin force-pushed the issue-4191-token-encode-enhancement branch from a98ad84 to 5842b20 Compare May 31, 2025 15:17
@s-aleshin s-aleshin requested a review from JacobCoffee as a code owner May 31, 2025 15:17
@github-actions github-actions bot added area/asgi area/background-tasks area/channels area/ci This PR involves changes to the CI/Infra area/connection area/constants This PR involves changes to the constants area/contrib This PR involves changes to the contrib (Deprecated) area/controller area/datastructures area/dependencies This PR involves changes to the dependencies area/di area/docs This PR involves changes to the documentation area/dto This PR involves changes to the DTOs area/enums This PR involves changes to the enums area/events area/file-system area/handlers This PR involves changes to the handlers and removed size: small labels May 31, 2025
@github-actions github-actions bot removed area/enums This PR involves changes to the enums area/channels area/constants This PR involves changes to the constants area/plugins This PR involves changes to the plugins area/logging area/handlers This PR involves changes to the handlers area/datastructures area/contrib This PR involves changes to the contrib (Deprecated) area/dependencies This PR involves changes to the dependencies area/multipart area/ci This PR involves changes to the CI/Infra area/security area/file-system labels May 31, 2025
@s-aleshin s-aleshin force-pushed the issue-4191-token-encode-enhancement branch from e5df94f to 1f872fb Compare May 31, 2025 15:35
@github-actions github-actions bot removed area/params This PR involves changes to the params area/openapi This PR involves changes to the OpenAPI schema area/private-api This PR involves changes to the privatized API labels May 31, 2025
@s-aleshin
Copy link
Author

Two things:

  1. This is a breaking change (for users that subclass Token), so it would have to be targeted at the v3 branch
  2. I don't think we should add json_encoder and sort_headers, as they are specific to pyjwt, and this is meant as an abstraction over pyjwt, not a drop-in replacement. If you do need to customise how decode interacts with pyjwt, you can simply subclass Token
  1. I’ve rebased the branch onto v3.0 as you suggested — the PR now targets the correct base.
  2. Ok, I agree. json_encoder and sort_headers are specific for pyjwt and use rarely. Deleted from the PR.

@s-aleshin s-aleshin marked this pull request as ready for review May 31, 2025 15:43
@s-aleshin s-aleshin changed the title feat(jwt): [#4191] extend Token.encode() to support custom headers and encoder options feat(jwt): [#4191] extend Token.encode() to support custom headers May 31, 2025
@s-aleshin s-aleshin force-pushed the issue-4191-token-encode-enhancement branch from 1f872fb to b45cbe7 Compare May 31, 2025 15:53
@s-aleshin s-aleshin force-pushed the issue-4191-token-encode-enhancement branch from 83037bd to 5fa2892 Compare June 1, 2025 10:34
@s-aleshin s-aleshin force-pushed the issue-4191-token-encode-enhancement branch from 727b3ec to 4692923 Compare June 1, 2025 10:41
Copy link

github-actions bot commented Jun 1, 2025

Documentation preview will be available shortly at https://litestar-org.github.io/litestar-docs-preview/4192

Copy link
Member

@provinzkraut provinzkraut left a comment

Choose a reason for hiding this comment

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

Thanks @s-aleshin!

@provinzkraut provinzkraut enabled auto-merge (squash) June 1, 2025 11:17
@provinzkraut provinzkraut merged commit 986ca0a into litestar-org:v3.0 Jun 1, 2025
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enhancement: Allow passing custom headers to Token.encode() for improved JWT flexibility
2 participants