Skip to main content

A Django package for JSON Web Token validation and verification. Using PyJWT.

Project description

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",
   } 

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dj_jwt_auth-1.10.0.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dj_jwt_auth-1.10.0-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file dj_jwt_auth-1.10.0.tar.gz.

File metadata

  • Download URL: dj_jwt_auth-1.10.0.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for dj_jwt_auth-1.10.0.tar.gz
Algorithm Hash digest
SHA256 feafb94b12adf0c5bd3e681ccc1dc7e946dd7d03c8a110c3679b999c4e01db08
MD5 dffa32b448dec99ab67db62ff1cb4c54
BLAKE2b-256 dab0caf2a5f0be33718c3103095f9ad28db64c4f14902ba2e966b0bbd4a4c106

See more details on using hashes here.

File details

Details for the file dj_jwt_auth-1.10.0-py3-none-any.whl.

File metadata

  • Download URL: dj_jwt_auth-1.10.0-py3-none-any.whl
  • Upload date:
  • Size: 16.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for dj_jwt_auth-1.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 29cea5952d8871f2dc4f31b5851b9635e6e1cef9ced86c6158aea817f59b1e35
MD5 fb30823264f26eb24c7e8ffe354025be
BLAKE2b-256 03f35a8a088574da45c08cc4031f5ab7aad983c88df92ae123a3f94d7a54f913

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page