Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

implementing jwt token for rest api

Rohan Karan84d1d94eee48
diff --git a/AUTHORS.rst b/AUTHORS.rst
index b088ce148..1e222c1b3 100644
--- a/AUTHORS.rst
+++ b/AUTHORS.rst
@@ -66,6 +66,7 @@ Developers
* Sofiko Alaverdashvili - https://github.com/sophiamartelli
* Kade - https://github.com/kp5431/
* Tom Bowyer - https://github.com/ImTheTom
+* Rohan Karan - https://github.com/RohanKaran
Translators
-----------
diff --git a/requirements.txt b/requirements.txt
index fd67985e2..4c8aeaa1b 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -28,6 +28,7 @@ requests==2.28.1
django-cors-headers==3.13.0
django-filter==22.1
djangorestframework~=3.14
+djangorestframework-simplejwt[crypto]==5.2.0
# Not used anymore, but needed because some modules are imported in DB migration
# files
diff --git a/wger/settings_global.py b/wger/settings_global.py
index 71a33bf42..d65a1e91d 100644
--- a/wger/settings_global.py
+++ b/wger/settings_global.py
@@ -18,7 +18,7 @@
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
import re
-
+from datetime import timedelta
"""
This file contains the global settings that don't usually need to be changed.
@@ -82,6 +82,7 @@ INSTALLED_APPS = (
'rest_framework',
'rest_framework.authtoken',
'django_filters',
+ 'rest_framework_simplejwt',
# Breadcrumbs
'django_bootstrap_breadcrumbs',
@@ -376,6 +377,7 @@ REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
+ 'rest_framework_simplejwt.authentication.JWTAuthentication',
),
'DEFAULT_FILTER_BACKENDS': (
'django_filters.rest_framework.DjangoFilterBackend',
@@ -387,6 +389,17 @@ REST_FRAMEWORK = {
}
}
+#
+# Django Rest Framework SimpleJWT
+#
+SIMPLE_JWT = {
+ 'ACCESS_TOKEN_LIFETIME': timedelta(minutes=5),
+ 'REFRESH_TOKEN_LIFETIME': timedelta(days=1),
+ 'ROTATE_REFRESH_TOKENS': False,
+ 'BLACKLIST_AFTER_ROTATION': False,
+ 'UPDATE_LAST_LOGIN': False,
+}
+
#
# CORS headers: allow all hosts to access the API
#
diff --git a/wger/urls.py b/wger/urls.py
index f060926f6..73379bd78 100644
--- a/wger/urls.py
+++ b/wger/urls.py
@@ -29,6 +29,7 @@ from django.urls import path
# Third Party
from rest_framework import routers
+from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView, TokenVerifyView
# wger
from wger.core.api import views as core_api_views
@@ -42,8 +43,7 @@ from wger.nutrition.sitemap import NutritionSitemap
from wger.utils.generic_views import TextTemplateView
from wger.weight.api import views as weight_api_views
-
-#admin.autodiscover()
+# admin.autodiscover()
#
# REST API
@@ -155,7 +155,7 @@ sitemaps = {'exercises': ExercisesSitemap, 'nutrition': NutritionSitemap}
# The actual URLs
#
urlpatterns = i18n_patterns(
- #url(r'^admin/', admin.site.urls),
+ # url(r'^admin/', admin.site.urls),
path('', include(('wger.core.urls', 'core'), namespace='core')),
path('workout/', include(('wger.manager.urls', 'manager'), namespace='manager')),
path('exercise/', include(('wger.exercises.urls', 'exercise'), namespace='exercise')),
@@ -195,6 +195,20 @@ urlpatterns += [
core_api_views.UserAPIRegistrationViewSet.as_view({'post': 'post'}),
name='api_register'
),
+ path(
+ 'api/v2/token/',
+ TokenObtainPairView.as_view(),
+ name='token_obtain_pair'
+ ),
+ path(
+ 'api/v2/token/refresh/',
+ TokenRefreshView.as_view(),
+ name='token_refresh'
+ ),
+ path(
+ 'api/v2/token/verify/',
+ TokenVerifyView.as_view(),
+ name='token_verify'),
# Others
path(