U
    8ÄT_Ù  ã                   @   sP   d dl m Z  d dlmZ d dlmZmZ d dlmZmZ G dd„ dƒZ	e	ƒ Z
dS )é    )Údatetime)Úsettings)Úconstant_time_compareÚsalted_hmac)Úbase36_to_intÚint_to_base36c                   @   sX   e Zd ZdZdZdZejZdd„ Z	dd„ Z
dd	„ Zddd„Zdd„ Zdd„ Zdd„ ZdS )ÚPasswordResetTokenGeneratorza
    Strategy object used to generate and check tokens for the password
    reset mechanism.
    z6django.contrib.auth.tokens.PasswordResetTokenGeneratorNc                 C   s   | j p
tj| _ d S ©N)Ú	algorithmr   ZDEFAULT_HASHING_ALGORITHM©Úself© r   ú>/tmp/pip-unpacked-wheel-dv63caxw/django/contrib/auth/tokens.pyÚ__init__   s    z$PasswordResetTokenGenerator.__init__c                 C   s   |   ||  |  ¡ ¡¡S )zi
        Return a token that can be used once to do a password reset
        for the given user.
        )Ú_make_token_with_timestampÚ_num_secondsÚ_now)r   Úuserr   r   r   Ú
make_token   s    z&PasswordResetTokenGenerator.make_tokenc                 C   s¦   |r|sdS z|  d¡\}}W n tk
r4   Y dS X zt|ƒ}W n tk
rX   Y dS X t|  ||¡|ƒs†t| j||dd�|ƒs†dS |  |  ¡ ¡| tjkr¢dS dS )zP
        Check that a password reset token is correct for a given user.
        Fú-T)Úlegacy)	ÚsplitÚ
ValueErrorr   r   r   r   r   r   ZPASSWORD_RESET_TIMEOUT)r   r   ÚtokenÚts_b36Ú_Útsr   r   r   Úcheck_token   s&    þz'PasswordResetTokenGenerator.check_tokenFc                 C   sH   t |ƒ}t| j|  ||¡| j|r$dn| jd� ¡ d d d… }d||f S )NÚsha1)Úsecretr
   é   z%s-%s)r   r   Úkey_saltÚ_make_hash_valuer   r
   Ú	hexdigest)r   r   Ú	timestampr   r   Zhash_stringr   r   r   r   ?   s    
ùø	z6PasswordResetTokenGenerator._make_token_with_timestampc                 C   s>   |j dkrdn|j jddd�}t|jƒ|j t|ƒ t|ƒ S )a¨  
        Hash the user's primary key and some user state that's sure to change
        after a password reset to produce a token that invalidated when it's
        used:
        1. The password field will change upon a password reset (even if the
           same password is chosen, due to password salting).
        2. The last_login field will usually be updated very shortly after
           a password reset.
        Failing those things, settings.PASSWORD_RESET_TIMEOUT eventually
        invalidates the token.

        Running this data through salted_hmac() prevents password cracking
        attempts using the reset token, provided the secret isn't compromised.
        NÚ r   )ÚmicrosecondÚtzinfo)Z
last_loginÚreplaceÚstrÚpkÚpassword)r   r   r$   Zlogin_timestampr   r   r   r"   N   s    z,PasswordResetTokenGenerator._make_hash_valuec                 C   s   t |tdddƒ  ¡ ƒS )NiÑ  é   )Úintr   Útotal_seconds)r   Údtr   r   r   r   b   s    z(PasswordResetTokenGenerator._num_secondsc                 C   s   t  ¡ S r	   )r   Únowr   r   r   r   r   e   s    z PasswordResetTokenGenerator._now)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r!   r
   r   Z
SECRET_KEYr   r   r   r   r   r"   r   r   r   r   r   r   r      s   "
r   N)r   Zdjango.confr   Zdjango.utils.cryptor   r   Zdjango.utils.httpr   r   r   Zdefault_token_generatorr   r   r   r   Ú<module>   s
   b