<commit_before>from rest_framework import authentication


class DoacAuthentication(authentication.BaseAuthentication):

    def authenticate(self, request):
        """
        Send the request through the authentication middleware that
        is provided with DOAC and grab the user and token from it.
        """

        from doac.middleware import AuthenticationMiddleware

        AuthenticationMiddleware().process_request(request._request)

        return request._request.user, request._request.access_token

    def authenticate_header(self, request):
        """
        DOAC specifies the realm as Bearer by default.
        """

        return 'Bearer realm="%s"' % self.www_authenticate_realm
<commit_msg>Fixed authenticator for invalid permissions<commit_after>from rest_framework import authentication, exceptions


class DoacAuthentication(authentication.BaseAuthentication):

    def authenticate(self, request):
        """
        Send the request through the authentication middleware that
        is provided with DOAC and grab the user and token from it.
        """

        from doac.middleware import AuthenticationMiddleware

        try:
            response = AuthenticationMiddleware().process_request(request._request)
        except:
            raise exceptions.AuthenticationFailed("Invalid handler")

        if not hasattr(request._request, "user") or not request._request.user.is_authenticated():
            raise exceptions.AuthenticationFailed("Could not authenticate")

        if not hasattr(request._request, "access_token"):
            raise exceptions.AuthenticationFailed("Access token was not valid")

        return request._request.user, request._request.access_token

    def authenticate_header(self, request):
        """
        DOAC specifies the realm as Bearer by default.
        """

        from doac.conf import options

        return 'Bearer realm="%s"' % options.realm

{'fraction_non_alphanumeric': 0.07083764219234746, 'fraction_numerical': 0.0, 'mean_word_length': 3.0822784810126582, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '2358147', 'n_tokens_mistral': 463, 'n_tokens_neox': 436, 'n_words': 140}