This commit is contained in:
Jaya Surya 2023-07-09 23:11:14 +05:30
parent 7973e278c7
commit de5d360c1c
3 changed files with 18 additions and 6 deletions

View File

@ -10,14 +10,16 @@ logger = logging.getLogger('db')
@precheck(required_data=[AUTH_CODE]) @precheck(required_data=[AUTH_CODE])
@get_token() @get_token()
@isAuthorized(allowed_users='*') @isAuthorized(allowed_users='*')
def login(request, id, email, user_type, token, refresh_token): def login(request, email, user_type, token, refresh_token):
try: try:
return Response({'action': "Login", 'message': "Verified", "user_type": user_type, "id_token": token, "refresh_token": refresh_token}, return Response({'action': "Login", 'message': "Verified", "user_type": user_type, "id_token": token, "refresh_token": refresh_token},
status=status.HTTP_200_OK) status=status.HTTP_200_OK)
except: except:
logger.error("Login Failed")
return Response({'action': "Login", 'message': "Something Went Wrong"}, return Response({'action': "Login", 'message': "Something Went Wrong"},
status=status.HTTP_400_BAD_REQUEST) status=status.HTTP_400_BAD_REQUEST)
@api_view(['POST']) @api_view(['POST'])
@precheck(required_data=[REFRESH_TOKEN]) @precheck(required_data=[REFRESH_TOKEN])
def refresh(request): def refresh(request):
@ -43,7 +45,6 @@ def refresh(request):
status=status.HTTP_400_BAD_REQUEST) status=status.HTTP_400_BAD_REQUEST)
@api_view(['GET']) @api_view(['GET'])
@isAuthorized(allowed_users=[STUDENT]) @isAuthorized(allowed_users=[STUDENT])
def studentProfile(request, id, email, user_type): def studentProfile(request, id, email, user_type):

View File

@ -32,11 +32,12 @@ from .models import User, PrePlacementOffer, PlacementApplication, Placement, St
logger = logging.getLogger('db') logger = logging.getLogger('db')
import requests
def get_token(): def get_token():
def decorator(view_func): def decorator(view_func):
def wrapper_func(request, *args, **kwargs): def wrapper_func(request, *args, **kwargs):
try: try:
print("one")
authcode = request.data[AUTH_CODE] authcode = request.data[AUTH_CODE]
data = { data = {
'code': authcode, 'code': authcode,
@ -45,23 +46,32 @@ def get_token():
'redirect_uri': REDIRECT_URI, 'redirect_uri': REDIRECT_URI,
'grant_type': 'authorization_code' 'grant_type': 'authorization_code'
} }
r = rq.post(OAUTH2_API_ENDPOINT, data=data) print("two")
r = requests.post(OAUTH2_API_ENDPOINT, data=data)
print(r)
if r.status_code == 200: if r.status_code == 200:
print("four")
response = r.json() response = r.json()
token = response[ID_TOKEN] token = response[ID_TOKEN]
refresh_token = response[REFRESH_TOKEN] refresh_token = response[REFRESH_TOKEN]
request.META["HTTP_AUTHORIZATION"] = "Bearer " + token request.META["HTTP_AUTHORIZATION"] = "Bearer " + token
request.META["MODIFIED"] = "True" request.META["MODIFIED"] = "True"
kwargs['refresh_token'] = refresh_token kwargs['refresh_token'] = refresh_token
print("five")
return view_func(request, *args, **kwargs) return view_func(request, *args, **kwargs)
else: else:
print("six")
return Response({'action': "Get Token", 'message': "Invalid Auth Code"}, return Response({'action': "Get Token", 'message': "Invalid Auth Code"},
status=status.HTTP_400_BAD_REQUEST) status=status.HTTP_400_BAD_REQUEST)
except Exception as e: except Exception as e:
print("seven")
logger.warning("Get Token: " + str(sys.exc_info())) logger.warning("Get Token: " + str(sys.exc_info()))
return Response({'action': "Get Token", 'message': str(e)}, return Response({'action': "Get Token", 'message': str(e)},
status=status.HTTP_400_BAD_REQUEST) status=status.HTTP_400_BAD_REQUEST)
return wrapper_func return wrapper_func
return decorator return decorator
@ -117,7 +127,8 @@ def isAuthorized(allowed_users=None):
user.save() user.save()
if len(set(user.user_type).intersection(set(allowed_users))) or allowed_users == '*': if len(set(user.user_type).intersection(set(allowed_users))) or allowed_users == '*':
if "MODIFIED" in headers: if "MODIFIED" in headers:
return view_func(request, user.id, user.email, user.user_type, token_id, *args, **kwargs) return view_func(request, user.id, user.email, user.user_type, token_id, *args,
**kwargs)
else: else:
return view_func(request, user.id, user.email, user.user_type, *args, **kwargs) return view_func(request, user.id, user.email, user.user_type, *args, **kwargs)
else: else:

View File

@ -30,7 +30,7 @@ DEBUG = os.environ.get('DEBUG') == "True"
ALLOWED_HOSTS = ['cdc.iitdh.ac.in', 'localhost'] ALLOWED_HOSTS = ['cdc.iitdh.ac.in', 'localhost']
ADMINS = [ ('Karthik Mv', '200010030@iitdh.ac.in')] ADMINS = [('Karthik Mv', '200010030@iitdh.ac.in')]
# Application definition # Application definition
INSTALLED_APPS = [ INSTALLED_APPS = [