diff --git a/backend/webapp/prototype/filehandler/userModule.py b/backend/webapp/prototype/filehandler/userModule.py index d5fb506..8c3af2e 100644 --- a/backend/webapp/prototype/filehandler/userModule.py +++ b/backend/webapp/prototype/filehandler/userModule.py @@ -1,4 +1,7 @@ +import jwt +import time from django.contrib.auth import authenticate +from django.conf import settings from django.contrib.auth.models import User from prototype.filehandler.exceptions import UserAuthFailed @@ -20,8 +23,14 @@ def loginUser(login, password): try: user = authenticate(username = login, password = password) if isinstance(user, User): + payload = {"iss": "NKADF", + "iat": int(time.time()), + "sub": user.id, + "exp": int(time.time()) + 86400} + jwt_token = jwt.encode(payload, settings.SECRET_KEY, algorithm = "HS256").decode("utf-8") result = {"message": "User authenticated successfully", - "statusCode": 200} + "statusCode": 200, + "token": jwt_token} else: raise UserAuthFailed("Error: User authentication failed") except UserAuthFailed as error: @@ -29,3 +38,14 @@ def loginUser(login, password): "statusCode": 401} finally: return result + +def decodeToken(token): + try: + payload = jwt.decode(token, settings.SECRET_KEY, algorith = "HS256") + result = payload + return payload + except Exception as error: + result = {"message": str(error), + "statusCode": 500} + finally: + return result diff --git a/backend/webapp/prototype/filehandler/views.py b/backend/webapp/prototype/filehandler/views.py index fddb322..52f01e4 100644 --- a/backend/webapp/prototype/filehandler/views.py +++ b/backend/webapp/prototype/filehandler/views.py @@ -9,7 +9,7 @@ from prototype.filehandler.models import Document, Forum from prototype.filehandler.forms import DocumentForm from prototype.filehandler.xmlParser import parseData from prototype.filehandler.functions import addToDatabase, listDiscussionsFromFile, listParagraphsFromDiscussion, createLabels, listPostsFromDiscussion, updateLabelsByParagraphId -from prototype.filehandler.userModule import registerNewUser, loginUser +from prototype.filehandler.userModule import registerNewUser, loginUser, decodeToken from prototype.filehandler.errorCodes import MethodNotAllowed @@ -89,3 +89,11 @@ def login(request): return JsonResponse(result, status = result["statusCode"]) else: return JsonResponse(MethodNotAllowed, status = MethodNotAllowed["statusCode"]) + +@csrf_exempt +def testToken(request): + if request.method == 'POST': + result = decodeToken(request.POST["token"]) + return JsonResponse(result, status = 200) + else: + return JsonResponse(MethodNotAllowed, status = MethodNotAllowed["statusCode"]) diff --git a/backend/webapp/prototype/urls.py b/backend/webapp/prototype/urls.py index db8c7d7..2f4cef4 100644 --- a/backend/webapp/prototype/urls.py +++ b/backend/webapp/prototype/urls.py @@ -27,7 +27,8 @@ urlpatterns = [ path('discussions/', views.discussions), path('visualize/', views.visualize), path('user/', views.user), - path('login/', views.login) + path('login/', views.login), + path('testToken/', views.testToken) ] if settings.DEBUG: diff --git a/backend/webapp/requirements.txt b/backend/webapp/requirements.txt index 30a93cd..1458d9b 100644 --- a/backend/webapp/requirements.txt +++ b/backend/webapp/requirements.txt @@ -1 +1,2 @@ Django~=3.0.5 +pyjwt