Skip to main content

Django-JWT

This is a package to verify and validate JSON Web Tokens (JWT) in Django.

Installation

  1. Install the package using pip:
    pip install dj-jwt-auth
  1. Add "django_jwt" to your INSTALLED_APPS setting like this::
    INSTALLED_APPS = [
        ...
        "django_jwt",
    ]
  1. Add "django_jwt.middleware.JWTAuthMiddleware" to your MIDDLEWARE setting like this::
    MIDDLEWARE = [
        ...
        "django_jwt.middleware.JWTAuthMiddleware",
    ]

Configuration:

Required variables:

  • OIDC_CONFIG_ROUTES - dict of "algorithm": "config_url". Required for using JWTAuthMiddleware. Example:
   OIDC_CONFIG_ROUTES = {
       "RS256": "https://keyCloak/realms/h/.well-known/openid-configuration",
       "HS256": "https://keyCloak/realms/h/.well-known/openid-configuration",
   } 

A value may also be a list/tuple of URLs to support multiple realms during a migration. The token's iss claim selects the realm that validates it, so a token is accepted if it is valid in whichever configured realm issued it. The first URL is the primary one, used by the admin login flow. The string form above still works. The iss claim is verified only when the token has one; a token without iss is accepted by the first realm whose key verifies its signature. Example:

   OIDC_CONFIG_ROUTES = {
       "ES256": ["https://<host>/realms/internal/", "https://<host>/realms/h/"],
   }

Optional variables:

  • OIDC_AUDIENCE - by default ["account", "broker"]

User retated variables:

  • OIDC_USER_UPDATE - if True, user model will be updated from userinfo endpoint if MODIFIED date has changed, by default True
  • OIDC_USER_MODIFIED_FIELD - user model field to store last modified date, by default modified_timestamp
  • OIDC_TOKEN_MODIFIED_FIELD - access token field to store last modified date, by default updated_at
  • OIDC_USER_UID - User model" unique identifier, by default kc_id
  • OIDC_TOKEN_USER_UID - access token field to store user UID, by default sub
  • OIDC_USER_MAPPING - mapping between JWT claims and user model fields. Can be dict or function. By default:
    OIDC_USER_MAPPING = {
        "given_name": "first_name",
        "family_name": "last_name",
        "name": "username",
    }

OR

    def OIDC_USER_MAPPING(userinfo):
        return {
            "first_name": userinfo.get("given_name"),
            "last_name": userinfo.get("family_name"),
            "username": userinfo.get("name"),
        }
- OIDC_USER_DEFAULTS - default values for user model fields, by default:
OIDC_USER_DEFAULTS = {
    "is_active": True,
}

- OIDC_USER_ON_CREATE and OIDC_USER_ON_UPDATE - functions to be called on user creation and update, by default:
OIDC_USER_ON_CREATE = None
OIDC_USER_ON_UPDATE = None
These functions should accept two arguments: user and request.

### Admin panel integration:
To integrate admin panel with OIDC, add OIDC_ADMIN_CLIENT_ID to settings. Example:
- OIDC_ADMIN_CLIENT_ID - by default "complete-anatomy"
By default will be used 'ES256' from OIDC_CONFIG_ROUTES as Issuer.
To mapping roles to admin panel permissions, use OIDC_ADMIN_ROLES. Example:

```python

from django_jwt.roles import ROLE

OIDC_ADMIN_ROLES = [
    ROLE(
        name="admin",  # name from token
        is_superuser=True,
    ),
    ROLE(
        name="staff",
        groups=["LMS (Full)", "Organizations (Full)", "Customer Support (Full)"],
        permissions=["Can add user"],
    ),
]

And add login view to urls.py:

urlpatterns = [
    path("admin/", include("django_jwt.urls")),
    ...
]

Login URL will be available at /admin/oidc/.

Testing:

Run command python runtests.py to run tests.

Metadata

Release files for dj-jwt-auth 1.11.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dj-jwt-auth 1.11.2
File Size Uploaded Provenance Actions
dj_jwt_auth-1.11.2.tar.gz 16.8 kB Without provenance Details for dj_jwt_auth-1.11.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dj-jwt-auth 1.11.2
File Interpreter ABI Platform Actions
dj_jwt_auth-1.11.2-py3-none-any.whl Python 3 none any Details for dj_jwt_auth-1.11.2-py3-none-any.whl

Total release size: 37.1 kB

Release files / dj_jwt_auth-1.11.2.tar.gz

Download URL dj_jwt_auth-1.11.2.tar.gz
Size 16.8 kB
Tags Source
SHA-256 checksum
How to use checksums
ab0745dfb6c33cead42f8e5838b5c84f517495a1cb868ebdd647c995abc9f694
BLAKE2b-256 checksum
How to use checksums
aef57c7e78606a585a2f31224b0cd28e7be92b5cb7afece9c2d8d7a766c9c285
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.8

Release files / dj_jwt_auth-1.11.2-py3-none-any.whl

Download URL dj_jwt_auth-1.11.2-py3-none-any.whl
Size 20.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d26c512a199407b2736d908b3a0b784eaf21354549cc5a24659404cc231f32d2
BLAKE2b-256 checksum
How to use checksums
ab62c959cea57a81693accb435037a19e0c5472f2ead167fa74278cf098878fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.8

Security for dj-jwt-auth 1.11.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.11.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page