OAUTH
This commit is contained in:
@@ -17,12 +17,12 @@ from django.conf.urls import url
|
||||
from django.contrib import admin
|
||||
from django.urls import path
|
||||
from rest_framework.routers import DefaultRouter
|
||||
|
||||
from app_be.views.rest_api import TestApiClass
|
||||
from app_be.views.rest_api import *
|
||||
|
||||
urlpatterns = [
|
||||
path('admin/', admin.site.urls),
|
||||
url(r'^test/', TestApiClass.test_api),
|
||||
url(r'^api/login', LoginClass.login),
|
||||
]
|
||||
|
||||
router = DefaultRouter()
|
||||
|
||||
@@ -3,6 +3,9 @@ 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__)
|
||||
|
||||
@@ -13,3 +16,44 @@ class TestApiClass:
|
||||
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,
|
||||
))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user