Skip to content

Add configurable issuer/audience validation with dynamic claim support#957

Open
diegocastrum wants to merge 15 commits into
jazzband:masterfrom
diegocastrum:feature/support_to_set_aud_and_iss_claims
Open

Add configurable issuer/audience validation with dynamic claim support#957
diegocastrum wants to merge 15 commits into
jazzband:masterfrom
diegocastrum:feature/support_to_set_aud_and_iss_claims

Conversation

@diegocastrum
Copy link
Copy Markdown
Member

@diegocastrum diegocastrum commented Dec 19, 2025

This pull request introduces enhanced support for dynamic and static validation of JWT audience and issuer claims in Simple JWT, along with improved settings validation and more flexible claim handling. It adds new settings (AUDIENCE_VALIDATION, ISSUER_CLAIM), refines how audience and issuer are validated or set, and provides comprehensive documentation and tests for these behaviors.

Settings and Validation Improvements:

  • Added the AUDIENCE_VALIDATION setting to control whether audience validation is "static" (default, enforced by PyJWT) or "dynamic" (enforced by Simple JWT logic after decoding). [1] [2] [3]
  • Added the ISSUER_CLAIM setting to allow customization of the issuer claim name (default: "iss"). [1] [2] [3]
  • Improved validation of settings: now checks that AUDIENCE_VALIDATION is a supported value and that ISSUER is a non-empty string or a sequence of non-empty strings, raising clear errors otherwise. [1] [2]

Token Claim Handling:

  • Added methods to the Token class for setting and verifying audience (aud) and issuer (iss) claims, supporting both static and dynamic configurations, and handling both single and multiple allowed issuers/audiences. [1] [2] [3]
  • Updated token creation and validation flows to ensure iss and aud claims are set and checked according to the new settings and logic. [1] [2]

Backend and Decode Logic:

  • Updated the backend to allow skipping audience/issuer validation at the PyJWT decode step, deferring to Simple JWT logic when in dynamic mode or when multiple issuers are configured. [1] [2] [3] [4]

Documentation and Testing:

  • Expanded documentation to explain the new settings and behaviors, including usage examples for dynamic audiences and issuers.
  • Added and updated tests to cover the new settings validation, claim handling, and backend decode logic. [1] [2] [3]

These changes make Simple JWT more flexible for multi-tenant or dynamic audience/issuer scenarios, and ensure that misconfigurations are detected early with clear error messages.

@diegocastrum diegocastrum self-assigned this Dec 19, 2025
@diegocastrum diegocastrum requested a review from 2ykwang December 26, 2025 06:26
@diegocastrum diegocastrum requested review from Andrew-Chen-Wang and vgrozdanic and removed request for 2ykwang and vgrozdanic February 1, 2026 19:18
@diegocastrum
Copy link
Copy Markdown
Member Author

@Andrew-Chen-Wang would you mind taking a look when you have time? Would appreciate a quick review or guidance on how to move forward

Copy link
Copy Markdown
Member

@Andrew-Chen-Wang Andrew-Chen-Wang left a comment

Choose a reason for hiding this comment

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

thanks for these changes. left some comments. have more to come pending your thoughts

Comment thread docs/settings.rst Outdated
Comment thread rest_framework_simplejwt/state.py Outdated
Comment thread rest_framework_simplejwt/settings.py Outdated
Comment thread docs/settings.rst Outdated
diegocastrum added a commit to diegocastrum/djangorestframework-simplejwt that referenced this pull request Apr 20, 2026
diegocastrum and others added 10 commits April 20, 2026 16:22
- Add `ISS_CLAIM` and `ALLOWED_ISSUERS` settings for issuer configuration
- Implement `set_iss()` and `verify_iss()` methods in `Token` class
- Add issuer verification in `Token.verify()` when configured
- Call `set_iss()` in `TokenRefreshSerializer` during token rotation
- Comment out issuer parameter from `TokenBackend` (WIP)
- Disable PyJWT's built-in issuer validation in `decode()`

This enables tokens to have dynamic issuers based on request context
while maintaining backward compatibility with static `ISSUER` setting.
Validation supports three modes:
- Static issuer (`ISSUER` setting)
- Whitelist validation (`ALLOWED_ISSUERS` setting)
- Dynamic issuers (no issuer validation beyond format checks)

Note: `TokenBackend` issuer handling still needs refactoring to fully
support the dynamic issuer workflow without double-decoding.

Fixes jazzband#899
…port

  - add `ISSUER_VALIDATION` setting (static/dynamic) defaulting to static PyJWT enforcement
  - pass issuer to `TokenBackend/PyJWT` only in static mode; skip in dynamic mode to allow per-token issuers

Add support for `aud` (audience) and `iss` (issuer) claims in tokens
Fixes jazzband#899
  - add `AUDIENCE_VALIDATION` setting (static/dynamic) and wire backend to skip PyJWT audience checks in dynamic mode
  - allow `Token.verify_aud` to accept per-token audiences when config is unset and dynamic mode is enabled
  - expose backend `verify_aud`/`verify_iss` flags and document the new setting

Add support for `aud` (audience) and `iss` (issuer) claims in tokens
Fixes jazzband#899
@diegocastrum diegocastrum force-pushed the feature/support_to_set_aud_and_iss_claims branch from b629c42 to 0a5479f Compare April 20, 2026 14:23
@diegocastrum
Copy link
Copy Markdown
Member Author

@Andrew-Chen-Wang I’ve addressed the previous feedback and updated the PR accordingly. Would you mind taking another look when you have some time?

Thanks a lot!

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.

Add support for aud (audience) and iss (issuer) claims in tokens

2 participants