Skip to content

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

Merged
provinzkraut merged 1 commit intolitestar-org:v3.0from
s-aleshin:issue-4191-token-encode-enhancement
Jun 1, 2025
Merged

feat(jwt): [#4191] extend Token.encode() to support custom headers#4192
provinzkraut merged 1 commit intolitestar-org:v3.0from
s-aleshin:issue-4191-token-encode-enhancement

Conversation

@s-aleshin
Copy link
Copy Markdown
Contributor

@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
Copy Markdown
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
@codecov
Copy link
Copy Markdown

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
@s-aleshin s-aleshin marked this pull request as draft May 31, 2025 15:17
@s-aleshin s-aleshin changed the base branch from main to v3.0 May 31, 2025 15:18
@s-aleshin
Copy link
Copy Markdown
Contributor 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.

Comment thread litestar/security/jwt/token.py Outdated
@github-actions
Copy link
Copy Markdown

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
Copy Markdown
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!

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