Additions for Django Rest Framework Simple JWT
Project description
DRF SimpleJWT additions
Additional features for django-rest-framework-simplejwt.
Features
- Full user info in TokenObtainPairSerializer
- Full user info in TokenObtainPairView
- Auto creation of user from token
Installation
pip install drf-simplejwt-additions
Usage
Note: for all these features you need to set same
SECRET_KEY
andSIMPLE_JWT['ALGORITHM']
in all services.
Full user info in TokenObtainPairSerializer
In settings.py
:
INSTALLED_APPS = [
...
'drf_simplejwt_additions',
...
]
...
SIMPLE_JWT = {
...
"TOKEN_OBTAIN_SERIALIZER": "drf_simplejwt_additions.serializers.TokenObtainPairWithFullUserSerializer",
...
}
From now on, the response of the TokenObtainPairView
will contain the full user info.
Serializer get all fields from the user model.
Then theses fields are added to the response in the user
field.
User's password is also added to the response. But it's encrypted twice:
- First time by Django itself
- Second: JWT encryption
Auto creation of user from token
This feature is useful in microservices architecture. Token, obtained from one service, can be used in another service to get user info. So, if the user doesn't exist in the second service, it will be created automatically.
In settings.py
:
INSTALLED_APPS = [
...
'drf_simplejwt_additions',
...
]
...
MIDDLEWARE = [
...
'drf_simplejwt_additions.middleware.AutoCreateUserFromTokenMiddleware',
...
]
...
REST_FRAMEWORK = {
...
'DEFAULT_AUTHENTICATION_CLASSES': (
...
'rest_framework_simplejwt.authentication.JWTAuthentication',
...
),
...
}
django-rest-framework-simplejwt has great feature: TokenUserAuthentication. But it doesn't create user automatically.
Full user info in TokenObtainPairView
In urls.py
:
from drf_simplejwt_additions.views import TokenObtainPairWithFullUserView
urlpatterns = [
...
path('api/token/', TokenObtainPairWithFullUserView.as_view(), name='token_obtain_pair'),
...
]
The TokenObtainPairWithFullUserView
is a subclass of TokenObtainPairView
with the TokenObtainPairWithFullUserSerializer
serializer.
License
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
Built Distribution
Hashes for drf-simplejwt-additions-1.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d32301120156b7df2892d1fc4731884b83912cd5ad47d51eb37335619291cbb |
|
MD5 | 4d47f5849237b49e99cbb61460015aef |
|
BLAKE2b-256 | e27ab3a3cd9d13f8a06c1665ad654579799cf1219d78d4e8d927497945cd7e61 |
Hashes for drf_simplejwt_additions-1.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 434871d31076804796a5f9a9d9dd47df93f159f81108a143daa45805d0aa4a1a |
|
MD5 | 324b6d213fe93bd065785ac3ce9fe051 |
|
BLAKE2b-256 | 1619cd7e5eb819809187f960da479c620ff68b10e10aafdf1e6f89521d9f06a1 |