Skip to content

Commit 011e97b

Browse files
authored
Account authorization (#277)
* Account activation has been enabled. On branch account_authorization Changes to be committed: modified: api/urls.py modified: api/views.py modified: authentication/apis.py modified: authentication/selectors.py modified: authentication/services.py modified: authentication/urls.py * get_new_user
1 parent b3abd90 commit 011e97b

File tree

10 files changed

+120
-188
lines changed

10 files changed

+120
-188
lines changed

api/scripts/method_specific/GET_activate_account.py

Lines changed: 0 additions & 53 deletions
This file was deleted.

api/scripts/method_specific/POST_api_accounts_describe.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

api/urls.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from drf_yasg import openapi
1919

2020
from api.views import (
21-
ApiAccountsDescribe,
2221
ApiGroupsCreate,
2322
ApiGroupsInfo,
2423
ApiGroupsDelete,
@@ -126,7 +125,6 @@
126125
ObjectIdRootObjectIdVersion.as_view(),
127126
),
128127
path("<str:object_id_root>", ObjectIdRootObjectId.as_view()),
129-
path("api/accounts/describe/", ApiAccountsDescribe.as_view()),
130128
path("api/groups/group_info/", ApiGroupsInfo.as_view()),
131129
path("api/groups/create/", ApiGroupsCreate.as_view()),
132130
path("api/groups/delete/", ApiGroupsDelete.as_view()),

api/views.py

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,13 @@
44
Django views for BCODB API
55
"""
66

7-
import jwt
8-
from django.contrib.auth.models import User
97
from drf_yasg import openapi
108
from drf_yasg.utils import swagger_auto_schema
119
from rest_framework import status
1210
from rest_framework.permissions import IsAuthenticated
13-
from rest_framework.renderers import TemplateHTMLRenderer
1411
from rest_framework.response import Response
1512
from rest_framework.views import APIView
16-
from rest_framework.authtoken.models import Token
1713
from api.permissions import RequestorInPrefixAdminsGroup
18-
from api.scripts.method_specific.GET_activate_account import GET_activate_account
1914
from api.scripts.method_specific.GET_draft_object_by_id import get_draft_object_by_id
2015
from api.scripts.method_specific.GET_published_object_by_id import (
2116
GET_published_object_by_id,
@@ -42,9 +37,6 @@
4237
post_api_prefixes_token_flat,
4338
)
4439

45-
from api.scripts.method_specific.POST_api_accounts_describe import (
46-
POST_api_accounts_describe,
47-
)
4840
from api.scripts.method_specific.POST_api_objects_drafts_create import (
4941
post_api_objects_drafts_create,
5042
)
@@ -130,55 +122,6 @@ def check_get(request) -> Response:
130122
# Placeholder
131123
return Response(status=status.HTTP_200_OK)
132124

133-
# Source: https://www.django-rest-framework.org/api-guide/authentication/#by-exposing-an-api-endpoint
134-
class ApiAccountsDescribe(APIView):
135-
"""
136-
Account details
137-
138-
--------------------
139-
No schema for this request since only the Authorization header is required.
140-
The word 'Token' must be included in the header.
141-
For example: 'Token 627626823549f787c3ec763ff687169206626149'
142-
"""
143-
144-
auth = [
145-
openapi.Parameter(
146-
"Authorization",
147-
openapi.IN_HEADER,
148-
description="Authorization Token",
149-
type=openapi.TYPE_STRING,
150-
)
151-
]
152-
153-
@swagger_auto_schema(
154-
manual_parameters=auth,
155-
responses={
156-
200: "Authorization is successful.",
157-
403: "Forbidden. Authentication credentials were not provided.",
158-
403: "Invalid token"
159-
},
160-
tags=["Account Management"],
161-
)
162-
def post(self, request):
163-
"""
164-
Pass the request to the handling function
165-
Source: https://stackoverflow.com/a/31813810
166-
"""
167-
168-
if request.headers["Authorization"].split(" ")[0] == "Token" or request.headers["Authorization"].split(" ")[0] == "TOKEN":
169-
return POST_api_accounts_describe(
170-
token=request.META.get("HTTP_AUTHORIZATION")
171-
)
172-
if request.headers["Authorization"].split(" ")[0] == "Bearer":
173-
jw_token=request.META.get("HTTP_AUTHORIZATION").split(" ")[1]
174-
unverified_payload = jwt.decode(jw_token, None, False)
175-
user = User.objects.get(email=unverified_payload['email'])
176-
token = "Thing "+ str(Token.objects.get(user=user))
177-
return POST_api_accounts_describe(token)
178-
else:
179-
return Response(status=status.HTTP_400_BAD_REQUEST)
180-
181-
182125
class ApiGroupsInfo(APIView):
183126
"""Group Info
184127

authentication/apis.py

Lines changed: 82 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# authentication/apis.py
22

33
import json
4+
import jwt
45
import uuid
56
from django.contrib.auth.models import User
67
from drf_yasg import openapi
@@ -12,7 +13,11 @@
1213
from rest_framework.views import APIView
1314
from api.scripts.utilities.UserUtils import UserUtils
1415
from authentication.models import Authentication, NewUser
15-
from authentication.selectors import check_user_email, get_user_info, check_new_user
16+
from authentication.selectors import (
17+
check_user_email,
18+
get_user_info,
19+
check_new_user
20+
)
1621
from authentication.services import (
1722
validate_token,
1823
create_bcodb_user,
@@ -99,7 +104,10 @@ def post(self, request) -> Response:
99104
if email == "[email protected]":
100105
return Response(
101106
status=status.HTTP_201_CREATED,
102-
data={"message":"Testing account request successful!!"}
107+
data={
108+
"message":"Testing account request successful. Check" \
109+
+ " your email fro the activation link."
110+
}
103111
)
104112

105113
if check_user_email(email) is True:
@@ -155,9 +163,9 @@ class AccountActivateApi(APIView):
155163
auth = []
156164
auth.append(
157165
openapi.Parameter(
158-
"username",
166+
"email",
159167
openapi.IN_PATH,
160-
description="Username to be authenticated.",
168+
description="Email to be authenticated.",
161169
type=openapi.TYPE_STRING,
162170
163171
)
@@ -177,28 +185,49 @@ class AccountActivateApi(APIView):
177185
responses={
178186
200: "Account has been activated.",
179187
403: "Requestor's credentials were rejected.",
188+
404: "That account, {email}, was not found.",
189+
409: "CONFLICT: That account, {email}, has already been activated"
180190
},
181191
tags=["Authentication and Account Management"],
182192
)
183193

184-
def get(self, request, username: str, temp_identifier: str) -> Response:
185-
if check_user_email(username) is True:
194+
def get(self, request, email: str, temp_identifier: str) -> Response:
195+
if email == "[email protected]":
196+
return Response(
197+
status=status.HTTP_200_OK,
198+
data={"message":f"Account for {email} has been activated"}
199+
)
200+
if check_user_email(email) is True:
186201
return Response(
187202
status=status.HTTP_409_CONFLICT,
188203
data={
189-
"message":f"CONFLICT: That account, {username}, has already "\
204+
"message":f"CONFLICT: That account, {email}, has already "\
190205
+ "been activated."
191206
}
192207
)
193-
new_user = check_new_user(username, temp_identifier)
194-
print(new_user)
195-
create_bcodb_user(new_user.email)
196-
new_user.delete()
197-
return Response(
198-
status=status.HTTP_200_OK,
199-
data={"message":f"Account for {username} has been activated"}
200-
)
201-
208+
if check_new_user(email) == False:
209+
return Response(
210+
status=status.HTTP_404_NOT_FOUND,
211+
data={
212+
"message":f"That account, {email}, was not found."\
213+
}
214+
)
215+
try:
216+
new_user = NewUser.objects.get(
217+
email=email,
218+
temp_identifier=temp_identifier
219+
)
220+
create_bcodb_user(new_user.email)
221+
new_user.delete()
222+
return Response(
223+
status=status.HTTP_200_OK,
224+
data={"message":f"Account for {email} has been activated"}
225+
)
226+
except NewUser.DoesNotExist:
227+
return Response(
228+
status=status.HTTP_403_FORBIDDEN,
229+
data={"message": "Requestor's credentials were rejected."}
230+
)
202231

203232
class RegisterUserNoVerificationAPI(APIView):
204233
"""Register BCODB
@@ -260,6 +289,43 @@ def post(self, request):
260289
if response.status_code == 200:
261290
return Response(status=status.HTTP_201_CREATED, data={"message": "user account created"})
262291

292+
class AccountDescribeApi(APIView):
293+
"""
294+
Account details
295+
296+
--------------------
297+
The word 'Token' or 'Bearer' must be included in the header.
298+
For example: 'Token 627626823549f787c3ec763ff687169206626149'
299+
'Bearer' indicates a JWT that will be verified with another service.
300+
'Token' is the API token for this service.
301+
"""
302+
303+
auth = [
304+
openapi.Parameter(
305+
"Authorization",
306+
openapi.IN_HEADER,
307+
description="Authorization Token",
308+
type=openapi.TYPE_STRING,
309+
default="Token 627626823549f787c3ec763ff687169206626149"
310+
)
311+
]
312+
313+
@swagger_auto_schema(
314+
manual_parameters=auth,
315+
responses={
316+
200: "Authorization is successful.",
317+
403: "Forbidden. Authentication credentials were not provided.",
318+
403: "Invalid token"
319+
},
320+
tags=["Authentication and Account Management"],
321+
)
322+
323+
def post(self, request):
324+
user = request._user
325+
user_info = get_user_info(user)
326+
327+
return Response(status=status.HTTP_200_OK, data=user_info)
328+
263329
class AddAuthenticationApi(APIView):
264330
"""
265331
Add Authentication Object
@@ -319,7 +385,6 @@ class Meta:
319385
)
320386

321387
def post(self, request):
322-
""""""
323388

324389
result = validate_auth_service(request.data)
325390

authentication/selectors.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,16 @@ def check_user_email(email: str)-> bool:
4444
return True
4545
except User.DoesNotExist:
4646
return False
47-
48-
def check_new_user(email: str, temp_identifier:str=None):
47+
48+
def check_new_user(email: str) -> bool:
4949
"""Check for new user
5050
5151
Using the provided email check for a new user in the DB.
52-
If the temp id is supplied and matches it will return the new user object.
5352
"""
5453

5554
try:
56-
new_user = NewUser.objects.get(email=email)
57-
if new_user.temp_identifier == temp_identifier:
58-
return new_user
59-
else:
60-
return True
55+
NewUser.objects.get(email=email)
56+
return True
6157
except NewUser.DoesNotExist:
6258
return False
6359

authentication/services.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,6 @@ def authenticate_google(token: str) -> bool:
127127
except User.DoesNotExist:
128128
return None
129129

130-
def custom_jwt_handler(token, user=None, request=None, public_key=None):
131-
"""Custom JWT Handler
132-
Triggered by any user authentication. This will gater all the associated
133-
user information and return that along with the validated JWT
134-
"""
135-
136-
print('hadley', token)
137-
return request
138-
139130
def validate_token(token: str, url: str)-> bool:
140131
"""
141132
"""

0 commit comments

Comments
 (0)