Manuel Hude 764a0c232c OAUTH
2021-03-20 15:58:02 +01:00

60 lines
1.6 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))
print(request.headers)
print(request.data)
bearer = request.headers['Authorization']
jwt = bearer.split()[1]
print(jwt)
try:
PyJwtValidator(jwt)
except PyJwtException as e:
print(f"Exception caught. Error: {e}")
return JsonResponse({},status=401)
return JsonResponse({'Result': 'success'}, safe=False, status=200)
def pretty_print_POST(req):
"""
At this point it is completely built and ready
to be fired; it is "prepared".
However pay attention at the formatting used in
this function because it is programmed to be pretty
printed and may differ from the actual request.
"""
print('{}\n{}\r\n{}\r\n\r\n{}'.format(
'-----------START-----------',
req.method + ' ' + req.url,
'\r\n'.join('{}: {}'.format(k, v) for k, v in req.headers.items()),
req.body,
))