<commit_before># -*- coding: utf-8 -*-

# Akvo RSR is covered by the GNU Affero General Public License.
# See more details in the license.txt file located at the root folder of the Akvo RSR module.
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.


from django.utils.translation import ugettext_lazy as _
from rest_framework import exceptions
from rest_framework.authentication import TokenAuthentication, BaseAuthentication
from tastypie.models import ApiKey

from akvo.constants import JWT_WEB_FORMS_SCOPE


def create_api_key(sender, **kwargs):
    """
    A signal for hooking up automatic ApiKey creation.
    Slightly modified from the tastypie function to generate a key when saving an existing user if no key exists
    """
    if not kwargs.get('raw', False):
        user = kwargs.get('instance')
        ApiKey.objects.get_or_create(user=user)


class TastyTokenAuthentication(TokenAuthentication):
    """
    Override the model attribute to use Tastypie's token model

    Simple token based authentication.

    Clients should authenticate by passing the token key in the "Authorization"
    HTTP header, prepended with the string "Token ".  For example:

        Authorization: Token <PASSWORD>
    """

    model = ApiKey
    """
    A custom token model may be used, but must have the following properties.

    * key -- The string identifying the token
    * user -- The user to which the token belongs
    """


class JWTAuthentication(BaseAuthentication):

    def authenticate(self, request):
        token = getattr(request, 'token', None)
        if token is not None:
            if token.scope != JWT_WEB_FORMS_SCOPE:
                raise exceptions.AuthenticationFailed(_('Incorrect JWT token scope.'))
            try:
                token.validate_max_uses()
            except Exception:
                raise exceptions.AuthenticationFailed(_('Invalid JWT token.'))

            return self.authenticate_credentials(token, request._request)

    def authenticate_credentials(self, token, request):
        if not token.user.is_active:
            raise exceptions.AuthenticationFailed(_('User inactive or deleted.'))

        response = MockResponse()
        token.log(request, response)
        return (token.user, token)


class MockResponse():
    status_code = None
<commit_msg>[#4872] fix: Open project Results using copied form access link<commit_after># -*- coding: utf-8 -*-

# Akvo RSR is covered by the GNU Affero General Public License.
# See more details in the license.txt file located at the root folder of the Akvo RSR module.
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.


from django.utils.translation import ugettext_lazy as _
from rest_framework import exceptions
from rest_framework.authentication import TokenAuthentication, BaseAuthentication
from tastypie.models import ApiKey

from akvo.constants import JWT_WEB_FORMS_SCOPE


def create_api_key(sender, **kwargs):
    """
    A signal for hooking up automatic ApiKey creation.
    Slightly modified from the tastypie function to generate a key when saving an existing user if no key exists
    """
    if not kwargs.get('raw', False):
        user = kwargs.get('instance')
        ApiKey.objects.get_or_create(user=user)


class TastyTokenAuthentication(TokenAuthentication):
    """
    Override the model attribute to use Tastypie's token model

    Simple token based authentication.

    Clients should authenticate by passing the token key in the "Authorization"
    HTTP header, prepended with the string "Token ".  For example:

        Authorization: Token <PASSWORD>
    """

    model = ApiKey
    """
    A custom token model may be used, but must have the following properties.

    * key -- The string identifying the token
    * user -- The user to which the token belongs
    """


class JWTAuthentication(BaseAuthentication):

    def authenticate(self, request):
        token = getattr(request, 'token', None)
        if token is not None:
            if token.scope != JWT_WEB_FORMS_SCOPE:
                raise exceptions.AuthenticationFailed(_('Incorrect JWT token scope.'))
            try:
                token.validate_max_uses()
            except Exception:
                raise exceptions.AuthenticationFailed(_('Invalid JWT token.'))

            return self.authenticate_credentials(token, request._request)

    def authenticate_credentials(self, token, request):
        if not token.user.is_active:
            raise exceptions.AuthenticationFailed(_('User inactive or deleted.'))
        return (token.user, token)

{'fraction_non_alphanumeric': 0.07490877870787722, 'fraction_numerical': 0.00128783000643915, 'mean_word_length': 3.1238938053097347, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5281904', 'n_tokens_mistral': 1231, 'n_tokens_neox': 1187, 'n_words': 447}