<commit_before># coding: utf-8
"""
api_utils.auth
~~~~~~~~~~~~~~

This module provides API authentication based on Hawk scheme.

"""
from functools import wraps

from flask import request, session, current_app
from werkzeug.exceptions import BadRequest, Unauthorized
import mohawk
try:
    from flask.ext.login import current_user
except ImportError:
    pass


class Hawk(object):
    """HTTP authentication scheme using a message authentication code
    (MAC) algorithm.

    Instances are *not* bound to specific apps.

    """
    def __init__(self, app=None):
        self._client_key_loader_func = None

        if app is not None:
            self.init_app(app)

    def init_app(self, app):
        app.config.setdefault('HAWK_ALGORITHM', 'sha256')
        app.config.setdefault('HAWK_SIGN_RESPONSE', False)
        app.config.setdefault('HAWK_ALLOW_COOKIE_AUTH', False)

        if app.config['HAWK_SIGN_RESPONSE']:
            app.after_request(self._sign_response)

    def client_key_loader(self, f):
        """Registers a function to be called to find a client key.

        Function you set has to take a client id and return a client key::

            @hawk.client_key_loader
            def get_client_key(client_id):
                if client_id == 'Alice':
                    return '<KEY>'
                else:
                    raise LookupError()

        :param f: The callback for retrieving a client key.

        """
        @wraps(f)
        def wrapped_f(client_id):
            client_key = f(client_id)
            return {
                'id': client_id,
                'key': client_key,
                'algorithm': current_app.config['HAWK_ALGORITHM']
            }

        self._client_key_loader_func = wrapped_f
        return wrapped_f

    def realm(self, view_func):
        """Decorator that provides an access to view function for
        authenticated users only.

        """
        @wraps(view_func)
        def wrapped_view_func(*args, **kwargs):
            if current_app.config['HAWK_ALLOW_COOKIE_AUTH'] and session:
                self._auth_by_cookie()
            else:
                self._auth_by_signature()
            return view_func(*args, **kwargs)

        return wrapped_view_func

    def _auth_by_cookie(self):
        if not current_user.is_authenticated():
            raise Unauthorized()

    def _auth_by_signature(self):
        if self._client_key_loader_func is None:
            raise RuntimeError('Client key loader function was not defined')
        if 'Authorization' not in request.headers:
            raise Unauthorized()

        try:
            mohawk.Receiver(
                credentials_map=self._client_key_loader_func,
                request_header=request.headers['Authorization'],
                url=request.url,
                method=request.method,
                content=request.data,
                content_type=request.mimetype
            )
        except mohawk.exc.MacMismatch:
            # mohawk exception contains computed MAC.
            # We should not expose it in response.
            raise Unauthorized()
        except (
            mohawk.exc.CredentialsLookupError,
            mohawk.exc.AlreadyProcessed,
            mohawk.exc.MisComputedContentHash,
            mohawk.exc.TokenExpired
        ) as e:
            raise Unauthorized(str(e))
        except mohawk.exc.HawkFail as e:
            raise BadRequest(str(e))<commit_msg>Add few optional mohawk params<commit_after># coding: utf-8
"""
api_utils.auth
~~~~~~~~~~~~~~

This module provides API authentication based on Hawk scheme.

"""
from functools import wraps

from flask import request, session, current_app
from werkzeug.exceptions import BadRequest, Unauthorized
import mohawk
try:
    from flask.ext.login import current_user
except ImportError:
    pass


class Hawk(object):
    """HTTP authentication scheme using a message authentication code
    (MAC) algorithm.

    Instances are *not* bound to specific apps.

    """
    def __init__(self, app=None):
        self._client_key_loader_func = None

        if app is not None:
            self.init_app(app)

    def init_app(self, app):
        app.config.setdefault('HAWK_SIGN_RESPONSE', False)
        app.config.setdefault('HAWK_ALLOW_COOKIE_AUTH', False)
        app.config.setdefault('HAWK_ALGORITHM', 'sha256')
        app.config.setdefault('HAWK_ACCEPT_UNTRUSTED_CONTENT', False)
        app.config.setdefault('HAWK_LOCALTIME_OFFSET_IN_SECONDS', 0)
        app.config.setdefault('HAWK_TIMESTAMP_SKEW_IN_SECONDS', 60)

        if app.config['HAWK_SIGN_RESPONSE']:
            app.after_request(self._sign_response)

    def client_key_loader(self, f):
        """Registers a function to be called to find a client key.

        Function you set has to take a client id and return a client key::

            @hawk.client_key_loader
            def get_client_key(client_id):
                if client_id == 'Alice':
                    return '<KEY>'
                else:
                    raise LookupError()

        :param f: The callback for retrieving a client key.

        """
        @wraps(f)
        def wrapped_f(client_id):
            client_key = f(client_id)
            return {
                'id': client_id,
                'key': client_key,
                'algorithm': current_app.config['HAWK_ALGORITHM']
            }

        self._client_key_loader_func = wrapped_f
        return wrapped_f

    def realm(self, view_func):
        """Decorator that provides an access to view function for
        authenticated users only.

        """
        @wraps(view_func)
        def wrapped_view_func(*args, **kwargs):
            if current_app.config['HAWK_ALLOW_COOKIE_AUTH'] and session:
                self._auth_by_cookie()
            else:
                self._auth_by_signature()
            return view_func(*args, **kwargs)

        return wrapped_view_func

    def _auth_by_cookie(self):
        if not current_user.is_authenticated():
            raise Unauthorized()

    def _auth_by_signature(self):
        if self._client_key_loader_func is None:
            raise RuntimeError('Client key loader function was not defined')
        if 'Authorization' not in request.headers:
            raise Unauthorized()

        try:
            mohawk.Receiver(
                credentials_map=self._client_key_loader_func,
                request_header=request.headers['Authorization'],
                url=request.url,
                method=request.method,
                content=request.data,
                content_type=request.mimetype,
                accept_untrusted_content=current_app.config['HAWK_ACCEPT_UNTRUSTED_CONTENT'],
                localtime_offset_in_seconds=current_app.config['HAWK_LOCALTIME_OFFSET_IN_SECONDS'],
                timestamp_skew_in_seconds=current_app.config['HAWK_TIMESTAMP_SKEW_IN_SECONDS']
            )
        except mohawk.exc.MacMismatch:
            # mohawk exception contains computed MAC.
            # We should not expose it in response.
            raise Unauthorized()
        except (
            mohawk.exc.CredentialsLookupError,
            mohawk.exc.AlreadyProcessed,
            mohawk.exc.MisComputedContentHash,
            mohawk.exc.TokenExpired
        ) as e:
            raise Unauthorized(str(e))
        except mohawk.exc.HawkFail as e:
            raise BadRequest(str(e))
{'fraction_non_alphanumeric': 0.0820333512641205, 'fraction_numerical': 0.0014792899408284023, 'mean_word_length': 2.1313684210526316, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4904374', 'n_tokens_mistral': 2171, 'n_tokens_neox': 2076, 'n_words': 496}