2021-03-20 16:22:25 +01:00

45 lines
1.1 KiB
Python

import logging
from django.http import JsonResponse
from rest_framework.decorators import api_view
from oauthlib import openid
from py_jwt_validator import PyJwtValidator, PyJwtException
import requests
logger = logging.getLogger(__name__)
class TestApiClass:
@staticmethod
@api_view(['GET'])
def test_api(request):
logger.debug('Test api call: {}'.format(request))
return JsonResponse({'Result': 'success'}, safe=False)
class LoginClass:
@staticmethod
@api_view(['GET'])
def login(request:requests.Request):
logger.debug('Validating request: {}'.format(request))
bearer = request.headers['Authorization']
if len(bearer.split()) < 2:
return JsonResponse({},status=401)
jwt = bearer.split()[1]
print(jwt)
try:
PyJwtValidator(jwt)
except PyJwtException as e:
print(f"Exception caught. Error: {e}")
logger.error(f"Exception caught. Error: {e}")
return JsonResponse({},status=401)
return JsonResponse({}, safe=False, status=200)