Skip to content

Commit

Permalink
Add reset_token API
Browse files Browse the repository at this point in the history
Issue #158
  • Loading branch information
HadleyKing committed May 22, 2023
1 parent 74f6d5b commit 3ed55a3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
48 changes: 46 additions & 2 deletions authentication/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from rest_framework import status, serializers
from rest_framework.authtoken.models import Token
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from api.scripts.utilities.UserUtils import UserUtils
from authentication.selectors import check_user_email, get_user_info
from authentication.services import validate_token, create_bcodb, send_bcodb, validate_auth_service
from authentication.models import Authentication
Expand Down Expand Up @@ -134,7 +136,7 @@ def post(self, request):
"""
result = validate_auth_service(request.data)

if result is not 1:
if result != 1:
return Response(status=status.HTTP_400_BAD_REQUEST, data=result)
try:
auth_object = Authentication.objects.get(username=request.user.username)
Expand Down Expand Up @@ -208,7 +210,7 @@ def post(self, request):
""""""
result = validate_auth_service(request.data)

if result is not 1:
if result != 1:
return Response(status=status.HTTP_400_BAD_REQUEST, data=result)
try:
auth_object = Authentication.objects.get(username=request.user.username)
Expand All @@ -225,3 +227,45 @@ def post(self, request):
auth_object.auth_service.remove(request.data)
auth_object.save()
return Response(status=status.HTTP_200_OK, data={"message": "Authentication object removed."})

class ResetTokenApi(APIView):
"""Reset Token
-----------------------------
Resets the user's token and returns the new one.
"""

permission_classes = [IsAuthenticated,]

# schema = openapi.Schema()

auth = [
openapi.Parameter(
"Authorization",
openapi.IN_HEADER,
description="Authorization Token",
type=openapi.TYPE_STRING,
)
]

@swagger_auto_schema(
manual_parameters=auth,
responses={
200: "Token reset is successful.",
400: "Bad request.",
},
tags=["Authentication"],
)

def post(self, request):
try:
token = Token.objects.get(user=request.user)
token.delete()
Token.objects.create(user=request.user)
return Response(
status=status.HTTP_200_OK,
data=UserUtils().get_user_info(username=request.user)
)

except Exception as error:
return Response(status=status.HTTP_400_BAD_REQUEST, data={"message": f"{error}"})

5 changes: 3 additions & 2 deletions authentication/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# authentication/urls.py

from django.urls import path
from authentication.apis import RegisterBcodbAPI, AddAuthenticationApi, RemoveAuthenticationApi
from authentication.apis import RegisterBcodbAPI, AddAuthenticationApi, RemoveAuthenticationApi, ResetTokenApi

urlpatterns = [
path("auth/register/", RegisterBcodbAPI.as_view()),
path("auth/add/", AddAuthenticationApi.as_view()),
path("auth/remove/", RemoveAuthenticationApi.as_view())
path("auth/remove/", RemoveAuthenticationApi.as_view()),
path("auth/reset_token/", ResetTokenApi.as_view())
]

0 comments on commit 3ed55a3

Please sign in to comment.