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
- Install the package using pip:
pip install dj-jwt-auth
- Add "django_jwt" to your INSTALLED_APPS setting like this::
INSTALLED_APPS = [
...
"django_jwt",
]
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dj_jwt_auth-1.10.1.tar.gz.
File metadata
- Download URL: dj_jwt_auth-1.10.1.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
59f4741bbb476a930c961e5f45284d8d419a03126b519a69c0f84babeb8d4684
|
|
| MD5 |
d1b3cc06a285ceec057420495ea5da61
|
|
| BLAKE2b-256 |
2b3cd5f9bd1bed4ee8de02a7186ab4328d91263ef6a0f16cb952e4c4981fac37
|
File details
Details for the file dj_jwt_auth-1.10.1-py3-none-any.whl.
File metadata
- Download URL: dj_jwt_auth-1.10.1-py3-none-any.whl
- Upload date:
- Size: 16.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7563bdd3b0250745416e55d50410fc42180de2d0729831513fa7d8e80a5a4763
|
|
| MD5 |
84c58e2f83e9157eb6485e7a7f1b423c
|
|
| BLAKE2b-256 |
23572f1e4893848351458e234312cae9eba03eef7b1e729978af8a531f308d35
|