Skip to content

Commit

Permalink
Add resetToken API for server
Browse files Browse the repository at this point in the history
For biocompute-objects/bco_api#158
Changes to be committed:
	modified:   server/bcodb/apis.py
	modified:   server/bcodb/services.py
	modified:   server/bcodb/urls.py
  • Loading branch information
HadleyKing committed May 23, 2023
1 parent cc95697 commit 269924b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
41 changes: 40 additions & 1 deletion server/bcodb/apis.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

from django.db import transaction
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from rest_framework import status
from rest_framework.views import APIView
Expand All @@ -12,7 +13,7 @@
from datetime import datetime
from users.selectors import profile_from_username, user_from_username
from authentication.services import custom_jwt_handler
from bcodb.services import create_bcodb
from bcodb.services import create_bcodb, reset_token
from bcodb.selectors import get_bcodb


Expand Down Expand Up @@ -98,4 +99,42 @@ def post(self, request):
request._auth, user_from_username(request.user.username)
)

return Response(status=status.HTTP_200_OK, data=user_info)

class ResetBcodbTokenApi(APIView):
"""Remove a BCODB from a user account"""
schema = openapi.Schema(
type=openapi.TYPE_OBJECT,
title="Reset Token",
description="Will reset the BCODB token",
required=["token", "public_hostname"],
properties={
"token": openapi.Schema(
type=openapi.TYPE_STRING,
description="The BCODB token to be reset."
),
"public_hostname": openapi.Schema(
type=openapi.TYPE_STRING,
description="The BCODB public hostname (URL)."
)
}
)

@swagger_auto_schema(
request_body=schema,
responses={
200: "BCODB token reset is successful.",
409: "Conflict.",
},
tags=["BCODB Management"],
)

# @transaction.atomic
def post(self, request):
""""""
public_hostname, token = request.data['public_hostname'], request.data['token']
reset_token(public_hostname, token)
user_info = custom_jwt_handler(
request._auth, user_from_username(request.user.username)
)
return Response(status=status.HTTP_200_OK, data=user_info)
18 changes: 17 additions & 1 deletion server/bcodb/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,20 @@ def remove_authentication(token: str, auth_object: dict, bcodb: BcoDb):
except Exception as err:
print(err)


def reset_token(public_hostname: str, token: str) -> BcoDb:
"""Reset BCODB Token"""

try:
bco_api_response = requests.post(
url=public_hostname + "/api/auth/reset_token/",
data={},
headers= {
"Authorization": "Token " + token,
"Content-type": "application/json; charset=UTF-8",
},
)
BcoDb.objects.filter(token=token).update(token=bco_api_response.json()['token'])

return bco_api_response.json()
except:
return bco_api_response.json()
3 changes: 2 additions & 1 deletion server/bcodb/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

from django.urls import path
from rest_framework_jwt.views import obtain_jwt_token
from bcodb.apis import getRouts, AddBcodbApi, RemoveBcodbApi # getBcoDb
from bcodb.apis import getRouts, AddBcodbApi, RemoveBcodbApi, ResetBcodbTokenApi # getBcoDb

urlpatterns = [
path("", getRouts),
path("bcodb/add/", AddBcodbApi.as_view()),
path("bcodb/remove/", RemoveBcodbApi.as_view()),
path("bcodb/reset_token/", ResetBcodbTokenApi.as_view()),
]

0 comments on commit 269924b

Please sign in to comment.