<commit_before>"""
Security token generator for the change email app.
Cloned from django.contrib.auth.tokens.
"""

from datetime import date

from django.utils import six
from django.utils.crypto import constant_time_compare, salted_hmac
from django.utils.http import base36_to_int, int_to_base36

from .settings import CHANGE_EMAIL_TIMEOUT_DAYS


class EmailChangeTokenGenerator(object):
    """
    Strategy object used to generate and check tokens for the change email mechanism.
    """
    key_salt = "apps.changemail.tokens.EmailChangeTokenGenerator"

    def make_token(self, user):
        """
        Returns a token that can be used once to do an email change for the given user.
        """
        return self._make_token_with_timestamp(user, self._num_days(self._today()))

    def check_token(self, user, token):
        """
        Check that an email change token is correct for a given user.
        """
        # Parse the token
        try:
            ts_b36, hash = token.split("-")
        except ValueError:
            return False

        try:
            ts = base36_to_int(ts_b36)
        except ValueError:
            return False

        # Check that the timestamp/uid has not been tampered with
        if not constant_time_compare(self._make_token_with_timestamp(user, ts), token):
            return False

        # Check the timestamp is within limit
        if (self._num_days(self._today()) - ts) >= CHANGE_EMAIL_TIMEOUT_DAYS:
            return False

        return True

    def _make_token_with_timestamp(self, user, timestamp):
        # timestamp is number of days since 2001-1-1.  Converted to
        # base 36, this gives us a 3 digit string until about 2121
        ts_b36 = int_to_base36(timestamp)
<commit_msg>Update docstring<commit_after>"""
Security token generator for the change email app.
Cloned from django.contrib.auth.tokens.
"""

from datetime import date

from django.utils import six
from django.utils.crypto import constant_time_compare, salted_hmac
from django.utils.http import base36_to_int, int_to_base36

from .settings import CHANGE_EMAIL_TIMEOUT_DAYS


class EmailChangeTokenGenerator(object):
    """
    Strategy object used to generate and check tokens for the change email mechanism.
    """

    key_salt = "apps.changemail.tokens.EmailChangeTokenGenerator"

    def make_token(self, user):
        """
        Returns a token that can be used once to do an email change for the given user.
        :param user: The user which request the token.
        """
        return self._make_token_with_timestamp(user, self._num_days(self._today()))

    def check_token(self, user, token):
        """
        Check that an email change token is correct for a given user.
        :param user: The user which request the token.
        :param token: The token to be checked.
        """

        # Parse the token
        try:
            ts_b36, hash = token.split("-")
        except ValueError:
            return False

        try:
            ts = base36_to_int(ts_b36)
        except ValueError:
            return False

        # Check that the timestamp/uid has not been tampered with
        if not constant_time_compare(self._make_token_with_timestamp(user, ts), token):
            return False

        # Check the timestamp is within limit
        if (self._num_days(self._today()) - ts) >= CHANGE_EMAIL_TIMEOUT_DAYS:
            return False

        return True

    def _make_token_with_timestamp(self, user, timestamp):
        # timestamp is number of days since 2001-1-1.  Converted to
        # base 36, this gives us a 3 digit string until about 2121
        ts_b36 = int_to_base36(timestamp)

{'fraction_non_alphanumeric': 0.06976112920738328, 'fraction_numerical': 0.014657980456026058, 'mean_word_length': 2.591617933723197, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2454710', 'n_tokens_mistral': 1057, 'n_tokens_neox': 1000, 'n_words': 354}