@@ -0,0 +1,2564 @@
+# coding: utf-8%0Afrom functools import wraps%0A%0Afrom flask import request, current_app%0Afrom werkzeug.exceptions import BadRequest, Forbidden%0Aimport mohawk%0A%0A%0Aclass Hawk(object):%0A    %22%22%22HTTP authentication scheme using a message authentication code%0A    (MAC) algorithm.%0A%0A    Instances are *not* bound to specific apps.%0A%0A    %22%22%22%0A    def __init__(self, app=None):%0A        self.lookup_client_key_func = None%0A%0A        if app is not None:%0A            self.init_app(app)%0A%0A    def init_app(self, app):%0A        app.config.setdefault('HAWK_ALGORITHM', 'sha256')%0A%0A    def client_key_loader(self, f):%0A        %22%22%22Registers a function to be called to find a client key.%0A%0A        Function you set has to take a client id and return a client key::%0A%0A            @hawk.client_key_loader%0A            def lookup_client_key(client_id):%0A                if client_id == 'Steve':%0A                    return 'werxhqb98rpaxn39848xrunpaw3489ruxnpa98w4rxn'%0A%0A        :param f: The callback for retrieving a client key.%0A%0A        %22%22%22%0A        @wraps(f)%0A        def wrapped_f(client_id):%0A            client_key = f(client_id)%0A            return %7B%0A                'id': client_id,%0A                'key': client_key,%0A                'algorithm': current_app.config%5B'HAWK_ALGORITHM'%5D%0A            %7D%0A%0A        self.lookup_client_key_func = wrapped_f%0A        return wrapped_f%0A%0A    def verify(self, view_func):%0A        %22%22%22Decorator that verifies HTTP requests.%22%22%22%0A        @wraps(view_func)%0A        def wrapped_view_func(*args, **kwargs):%0A            if 'Authorization' not in request.headers:%0A                raise BadRequest('Authorization header is required')%0A            if 'Content-Type' not in request.headers:%0A                raise BadRequest('Content-Type header is required')%0A%0A            try:%0A                mohawk.Receiver(%0A                    credentials_map=self.lookup_client_key_func,%0A                    request_header=request.headers%5B'Authorization'%5D,%0A                    url=request.url,%0A                    method=request.method,%0A                    content=request.data,%0A                    content_type=request.headers%5B'Content-Type'%5D%0A                )%0A            except (%0A                mohawk.exc.AlreadyProcessed,%0A                mohawk.exc.MacMismatch,%0A                mohawk.exc.MisComputedContentHash,%0A                mohawk.exc.TokenExpired%0A            ) as e:%0A                raise Forbidden(e)%0A            except mohawk.exc.HawkFail as e:%0A                raise BadRequest(e)%0A            else:%0A                return view_func(*args, **kwargs)%0A%0A        return wrapped_view_func%0A

Add Hawk authentication scheme