From fe21ac3cd8f71a662f6bc349efd8d7f293a58017 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 6 Apr 2022 19:43:35 +0200 Subject: [PATCH] new validate api with out teal --- ereuse_devicehub/api/__init__.py | 0 ereuse_devicehub/api/views.py | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 ereuse_devicehub/api/__init__.py create mode 100644 ereuse_devicehub/api/views.py diff --git a/ereuse_devicehub/api/__init__.py b/ereuse_devicehub/api/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ereuse_devicehub/api/views.py b/ereuse_devicehub/api/views.py new file mode 100644 index 00000000..5aa7ccc5 --- /dev/null +++ b/ereuse_devicehub/api/views.py @@ -0,0 +1,38 @@ +from binascii import Error as asciiError + +from flask import Blueprint, jsonify, request +from flask.views import View +from werkzeug.exceptions import Unauthorized + +from ereuse_devicehub.auth import Auth + +api = Blueprint('api', __name__, url_prefix='/api') + + +class LoginMix(View): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.authenticate() + + def authenticate(self): + unauthorized = Unauthorized('Provide a suitable token.') + basic_token = request.headers.get('Authorization', " ").split(" ") + if not len(basic_token) == 2: + raise unauthorized + + token = basic_token[1] + try: + token = Auth.decode(token) + except asciiError: + raise unauthorized + self.user = Auth().authenticate(token) + + +class InventoryView(LoginMix): + methods = ['POST'] + + def dispatch_request(self): + return jsonify("Ok") + + +api.add_url_rule('/inventory/', view_func=InventoryView.as_view('inventory'))