From 7cad429a2c1e3a6a45d4fadb961d15896c3828a6 Mon Sep 17 00:00:00 2001 From: Rushil Umaretiya Date: Sat, 16 Apr 2022 21:12:58 -0400 Subject: [PATCH] feat(backend): added jwt auth support --- server/Pipfile | 1 + server/Pipfile.lock | 18 +++++++++++++++++- server/api/urls.py | 7 +++++++ server/config/settings.py | 10 ++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/server/Pipfile b/server/Pipfile index 2f290e1..c171e0a 100644 --- a/server/Pipfile +++ b/server/Pipfile @@ -9,6 +9,7 @@ gunicorn = "*" python-dotenv = "*" djangorestframework = "*" django-cors-headers = "*" +djangorestframework-simplejwt = "*" [dev-packages] diff --git a/server/Pipfile.lock b/server/Pipfile.lock index ff4f634..bee726c 100644 --- a/server/Pipfile.lock +++ b/server/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "6b09e5def16da359280b38cfa9690ef7fb07897b5befd3c0f5b16de35d664690" + "sha256": "d2db7358afc491233105dbd0ca11ab1f16efa6e70de3e44270a99ce7acde40b6" }, "pipfile-spec": 6, "requires": { @@ -48,6 +48,14 @@ "index": "pypi", "version": "==3.13.1" }, + "djangorestframework-simplejwt": { + "hashes": [ + "sha256:75323528a7b910843b879194746f0ebc3481c4ad9f354de2dd162160662fb95a", + "sha256:cd3ee3aabdc98de104957c136e74ba2a16571b151c88ecd97421fce171a03ea4" + ], + "index": "pypi", + "version": "==5.1.0" + }, "gunicorn": { "hashes": [ "sha256:9dcc4547dbb1cb284accfb15ab5667a0e5d1881cc443e0677b4882a4067a807e", @@ -56,6 +64,14 @@ "index": "pypi", "version": "==20.1.0" }, + "pyjwt": { + "hashes": [ + "sha256:b888b4d56f06f6dcd777210c334e69c737be74755d3e5e9ee3fe67dc18a0ee41", + "sha256:e0c4bb8d9f0af0c7f5b1ec4c5036309617d03d56932877f2f7a0beeb5318322f" + ], + "markers": "python_version >= '3.6'", + "version": "==2.3.0" + }, "python-dotenv": { "hashes": [ "sha256:b7e3b04a59693c42c36f9ab1cc2acc46fa5df8c78e178fc33a8d4cd05c8d498f", diff --git a/server/api/urls.py b/server/api/urls.py index 4e2a81e..6a91efa 100644 --- a/server/api/urls.py +++ b/server/api/urls.py @@ -1,10 +1,17 @@ from django.urls import path from . import views +from rest_framework_simplejwt.views import ( + TokenObtainPairView, + TokenRefreshView, +) + urlpatterns = [ path('profile/create', views.ConsumerCreate.as_view()), path('profile/', views.ConsumerDetail.as_view()), path('foundation/', views.FoundationViewSet.as_view({'get': 'list', 'post': 'create'})), path('foundation//', views.FoundationViewSet.as_view({'get': 'retrieve', 'put': 'update', 'delete': 'destroy'})), + path('token/', TokenObtainPairView.as_view(), name='token_obtain_pair'), + path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'), ] \ No newline at end of file diff --git a/server/config/settings.py b/server/config/settings.py index 97ccd5f..80722f2 100644 --- a/server/config/settings.py +++ b/server/config/settings.py @@ -106,6 +106,16 @@ else: } } +REST_FRAMEWORK = { + 'DEFAULT_PERMISSION_CLASSES': ( + 'rest_framework.permissions.IsAuthenticated', + ), + 'DEFAULT_AUTHENTICATION_CLASSES': ( + 'rest_framework_simplejwt.authentication.JWTAuthentication', + 'rest_framework.authentication.SessionAuthentication', + 'rest_framework.authentication.BasicAuthentication', + ), +} # Password validation