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'))