diff --git a/hegewater/auth/authentication/__init__.py b/hegewater/auth/authentication/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hegewater/auth/authentication/admin.py b/hegewater/auth/authentication/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/hegewater/auth/authentication/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/hegewater/auth/authentication/apps.py b/hegewater/auth/authentication/apps.py new file mode 100644 index 0000000..8bab8df --- /dev/null +++ b/hegewater/auth/authentication/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class AuthenticationConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'authentication' diff --git a/hegewater/auth/authentication/migrations/__init__.py b/hegewater/auth/authentication/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hegewater/auth/authentication/models.py b/hegewater/auth/authentication/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/hegewater/auth/authentication/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/hegewater/auth/authentication/tests.py b/hegewater/auth/authentication/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/hegewater/auth/authentication/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/hegewater/auth/authentication/urls.py b/hegewater/auth/authentication/urls.py new file mode 100644 index 0000000..6ec71ba --- /dev/null +++ b/hegewater/auth/authentication/urls.py @@ -0,0 +1,13 @@ +from django.urls import path +from rest_framework_simplejwt import views as jwt_views +from . import views + +urlpatterns = [ + path('token/', + jwt_views.TokenObtainPairView.as_view(), + name ='token_obtain_pair'), + path('token/refresh/', + jwt_views.TokenRefreshView.as_view(), + name ='token_refresh'), + path('hello/', views.example_view, name ='hello'), +] \ No newline at end of file diff --git a/hegewater/auth/authentication/views.py b/hegewater/auth/authentication/views.py new file mode 100644 index 0000000..a7bf95d --- /dev/null +++ b/hegewater/auth/authentication/views.py @@ -0,0 +1,15 @@ +from rest_framework.views import APIView +from rest_framework.response import Response +from rest_framework.permissions import IsAuthenticated +from rest_framework.decorators import api_view, permission_classes +from rest_framework.parsers import JSONParser +import jwt + +@api_view(['GET']) +@permission_classes([IsAuthenticated]) +def example_view(request): + print(jwt.decode('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjY2MDY2ODI3LCJpYXQiOjE2NjYwNjMyMjcsImp0aSI6IjY0ZTc2ZDllOTBiMDRjMDM5NTAwZjFhMGQ1ZDI2YjYyIiwidXNlcl9pZCI6NH0.9jU3-E57OHtBYsjBic6a6flcJx5OTn4H3bJnqVUZmLI', key="django-insecure-1gz*2h6p@^(9)^o4%abp6aajmkn$52&&t(!-vf5$=oxgx+73&%", algorithms=['HS256', ], options={"verify_signature": True})) + content = {'message': 'Hello, GeeksforGeeks'} + return Response(content) + + \ No newline at end of file diff --git a/hegewater/auth/authorization/__init__.py b/hegewater/auth/authorization/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hegewater/auth/authorization/admin.py b/hegewater/auth/authorization/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/hegewater/auth/authorization/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/hegewater/auth/authorization/apps.py b/hegewater/auth/authorization/apps.py new file mode 100644 index 0000000..565f328 --- /dev/null +++ b/hegewater/auth/authorization/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class AuthorizationConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'authorization' diff --git a/hegewater/auth/authorization/migrations/__init__.py b/hegewater/auth/authorization/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/hegewater/auth/authorization/models.py b/hegewater/auth/authorization/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/hegewater/auth/authorization/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/hegewater/auth/authorization/tests.py b/hegewater/auth/authorization/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/hegewater/auth/authorization/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/hegewater/auth/authorization/views.py b/hegewater/auth/authorization/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/hegewater/auth/authorization/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/hegewater/settings.py b/hegewater/settings.py index 40712bb..13aa54f 100644 --- a/hegewater/settings.py +++ b/hegewater/settings.py @@ -10,6 +10,7 @@ https://docs.djangoproject.com/en/4.0/ref/settings/ """ +from datetime import timedelta from pathlib import Path import os @@ -86,9 +87,9 @@ # } 'default': { 'ENGINE': 'django.db.backends.mysql', - 'NAME': 'heroku_f8978dd3dc8112f', - 'USER': 'bc375827ce1f9f', - 'PASSWORD': '64e8c706', + 'NAME': 'heroku_e910b84b4976448', + 'USER': 'bb298870b2c072', + 'PASSWORD': '6d3f2d3a', 'HOST': 'us-cdbr-east-06.cleardb.net', 'PORT': '3306', 'OPTIONS': { @@ -149,6 +150,11 @@ ALLOWED_HOSTS = ['*'] +""" + + LOGGING START + +""" #date time logging_level | file_name - function_name - exception (in case of error logs) #method_entry method_exit LOGGING = { @@ -187,4 +193,52 @@ 'propagate': True, } }, -} \ No newline at end of file +} +""" + + LOGGING END + +""" + +""" + + JWT AUTH START + +""" +#Authentication +REST_FRAMEWORK = { + 'DEFAULT_AUTHENTICATION_CLASSES': [ + 'rest_framework_simplejwt.authentication.JWTAuthentication', + ], +} + +SIMPLE_JWT = { + 'ACCESS_TOKEN_LIFETIME': timedelta(minutes=60), + 'REFRESH_TOKEN_LIFETIME': timedelta(days=15), + 'ROTATE_REFRESH_TOKENS': False, + 'BLACKLIST_AFTER_ROTATION': True, + + 'ALGORITHM': 'HS256', + 'SIGNING_KEY': SECRET_KEY, + 'VERIFYING_KEY': None, + 'AUDIENCE': None, + 'ISSUER': None, + + 'AUTH_HEADER_TYPES': ('Bearer',), + 'USER_ID_FIELD': 'id', + 'USER_ID_CLAIM': 'user_id', + + 'AUTH_TOKEN_CLASSES': ('rest_framework_simplejwt.tokens.AccessToken',), + 'TOKEN_TYPE_CLAIM': 'token_type', + + 'JTI_CLAIM': 'jti', + + 'SLIDING_TOKEN_REFRESH_EXP_CLAIM': 'refresh_exp', + 'SLIDING_TOKEN_LIFETIME': timedelta(minutes=5), + 'SLIDING_TOKEN_REFRESH_LIFETIME': timedelta(days=1), +} +""" + + JWT AUTH END + +""" \ No newline at end of file diff --git a/hegewater/urls.py b/hegewater/urls.py index 1102981..82f6de8 100644 --- a/hegewater/urls.py +++ b/hegewater/urls.py @@ -20,5 +20,6 @@ urlpatterns = [ path('admin/', admin.site.urls), path('api/user/',include('hegewater.coreServices.user.urls')), + path('api/auth/',include('hegewater.auth.authentication.urls')), path('', include('hegewater.uiServices.react.urls')) ] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 060c396..3f2ccc2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,8 +2,10 @@ asgiref==3.5.2 Django==4.0.6 django-cors-headers==3.13.0 djangorestframework==3.13.1 +djangorestframework-simplejwt==5.2.1 gunicorn==20.1.0 mysqlclient==2.1.1 +PyJWT==2.5.0 pytz==2022.1 sqlparse==0.4.2 tzdata==2022.1