authentication - DRF: how to integrate django-rest-framework-jwt to Djoser -


i planning build application django rest framework. i'm more interested in using django-rest-framework-jwt authentication mechanism session or token authentication mechanism.

but other packages django-rest-auth , djoser (which helps in registrations process) uses session , token authentication system.

how override token authentication mechanism in djoser or django-rest-auth django-rest-framework-jwt?

i know question year old, figured out how djoser , django-rest-knox play along , sure enough same technique worked djangorestframework-jwt well. trick knowing you can use djoser's account endpoints without using auth-related endpoints. have put each library on own endpoint.

here's how set django rest framework use jwts log in , authenticate against djoser endpoints (i'm going take start finish):

first, install djangorestframework-jwt , djoser:

pip install djangorestframework-jwt djoser 

specify want use jwts authenticate adding jsonwebtokenauthentication default_authentication_classes in django project's settings.py:

rest_framework = {     'default_permission_classes': (         'rest_framework.permissions.isauthenticated',     ),     'default_authentication_classes': (         'rest_framework_jwt.authentication.jsonwebtokenauthentication',     ), } 

next, add djoser.urls , rest_framework_jwt's obtain_jwt_token view urls:

from django.conf.urls import url, include rest_framework_jwt import views jwt_views  urlpatterns = [     url(r'^account/', include('djoser.urls')),     url(r'^auth/login/', jwt_views.obtain_jwt_token, name='auth'), ] 

that should need started. safe, run migrate (i spun brand-new instance of django rest framework post , hadn't yet run initial commits before point):

python manage.py migrate 

to test things out, create new user if don't have one:

python manage.py createsuperuser 

once have user account, runserver , try logging in jwt:

http post http://localhost:800/auth/login/ username=admin password=password

you should token:

{     "token": "eyjhbgcioijiuzi1niisinr5cci6ikpxvcj9.eyjlehaioje0ntg2odi3mzysinvzzxjuyw1lijoiywrtaw4ilcjlbwfpbci6iiisinvzzxjfawqiojj9.jdovcpfie0ughsv9oqfpgpc-wxjjqtcejwai6btlwrm" } 

you can use token authenticate against djoser's /me/ endpoint profile information. include token within request's header authorization: jwt:

http http://localhost:8000/account/me/ "authorization: jwt eyjhbgcioijiuzi1niisinr5cci6ikpxvcj9.eyjlehaioje0ntg2odi3mzysinvzzxjuyw1lijoiywrtaw4ilcjlbwfpbci6iiisinvzzxjfawqiojj9.jdovcpfie0ughsv9oqfpgpc-wxjjqtcejwai6btlwrm"

here's got back:

{     "email": "",     "id": 2,     "username": "admin" } 

as can see, it's pretty easy start using jwts authentication. guess libraries djoser , django-rest-auth focus on basic, session, or token authentication because they're included out of drf box , common method people authenticate calls against server.

the beauty of it's easy implement more secure authentication scheme because djoser isn't tightly coupled own authentication classes - it'll happily respect whatever set default_authentication_classes.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -