a
    xd3"                     @   s  d dl Z d dlmZ d dlmZ d dlmZ d dlmZm	Z	 d dl
mZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZmZmZmZ d dlmZm Z m!Z! d dl"m#Z#m$Z$m%Z% e Z&g dZ'e(eddZ)e(eddZ*dd Z+G dd deZ,G dd deZ-G dd deZ.G dd de,eZ/G dd  d e-eZ0G d!d" d"e.eZ1e,2 Z3e-2 Z4e.2 Z5dS )#    N)	timedelta)settings)get_user_model)validate_passwordget_password_validators)ValidationError)timezone)gettext_lazy)
exceptions)GenericAPIView)Response)GenericViewSet)ResetPasswordTokenclear_expired$get_password_reset_token_expiry_timeget_password_reset_lookup_field)EmailSerializerPasswordTokenSerializerResetTokenSerializer)reset_password_token_createdpre_password_resetpost_password_reset)	ResetPasswordValidateTokenResetPasswordConfirmResetPasswordRequestTokenreset_password_validate_tokenreset_password_confirmreset_password_request_token!ResetPasswordValidateTokenViewSetResetPasswordConfirmViewSet ResetPasswordRequestTokenViewSetZ0DJANGO_REST_PASSWORDRESET_HTTP_USER_AGENT_HEADERZHTTP_USER_AGENTZ+DJANGO_REST_PASSWORDRESET_IP_ADDRESS_HEADERZREMOTE_ADDRc                 C   s(   t d| }t d|}| | kS )z
    Perform case-insensitive comparison of two identifiers, using the
    recommended algorithm from Unicode Technical Report 36, section
    2.11.2(B)(2).
    NFKC)unicodedata	normalizecasefold)s1s2Znormalized1Znormalized2 r'   W/var/www/html/Ranjet/env/lib/python3.9/site-packages/django_rest_passwordreset/views.py_unicode_ci_compare&   s    r)   c                   @   s$   e Zd ZdZdZdZeZdd ZdS )r   zM
    An Api View which provides a method to verify that a token is valid
    r'   c                 O   s&   | j |jd}|jdd tddiS )NdataTZraise_exceptionstatusOK)serializer_classr+   is_validr   )selfrequestargskwargs
serializerr'   r'   r(   post:   s    zResetPasswordValidateToken.postN)	__name__
__module____qualname____doc__throttle_classespermission_classesr   r/   r6   r'   r'   r'   r(   r   2   s
   r   c                   @   s$   e Zd ZdZdZdZeZdd ZdS )r   zY
    An Api View which provides a method to reset a password based on a unique token
    r'   c           	   
   O   s   | j |jd}|jdd |jd }|jd }tjj|d }|j	 rt
j| j|jd zt||jttjd W n4 ty } ztd|jiW Y d }~n
d }~0 0 |j| |j  tj| j|jd tjj|jd	  td
diS )Nr*   Tr,   passwordtoken)key)senderuser)rA   Zpassword_validators)rA   r-   r.   )r/   r+   r0   validated_datar   objectsfilterfirstrA   eligible_for_resetr   send	__class__r   r   r   ZAUTH_PASSWORD_VALIDATORSr   r
   messagesset_passwordsaver   deleter   )	r1   r2   r3   r4   r5   r=   r>   reset_password_tokener'   r'   r(   r6   H   s,    




zResetPasswordConfirm.postN)	r7   r8   r9   r:   r;   r<   r   r/   r6   r'   r'   r'   r(   r   @   s
   r   c                   @   s$   e Zd ZdZdZdZeZdd ZdS )r   z
    An Api View which provides a method to request a password reset token based on an e-mail address

    Sends a signal reset_password_token_created when a reset token was created
    r'   c                 O   s2  | j |jd}|jdd |jd }t }t t|d }t| t	j
jf i dt |i}d}	|D ]}
|
 rjd}	 qqj|	sttddstdtd	gi|D ]|}
|
 rt|t|
t rd }|
j  d
kr|
j d
 }n&tj
j|
|jtd|jtdd}tj| j | |d qt!ddiS )Nr*   Tr,   email)hoursz
{}__iexactFZ0DJANGO_REST_PASSWORDRESET_NO_INFORMATION_LEAKAGEz^We couldn't find an account associated with that email. Please try a different e-mail address.r    )rA   
user_agent
ip_address)r@   instancerM   r-   r.   )"r/   r+   r0   rB   r   r   nowr   r   UserrC   rD   formatr   rF   getattrr   r
   r   _r)   Zpassword_reset_tokensallcountr   createZMETAgetHTTP_USER_AGENT_HEADERHTTP_IP_ADDRESS_HEADERr   rG   rH   r   )r1   r2   r3   r4   r5   rO   Z$password_reset_token_validation_timeZnow_minus_expiry_timeZusersZactive_user_foundrA   r>   r'   r'   r(   r6   u   s@    
zResetPasswordRequestToken.postN)	r7   r8   r9   r:   r;   r<   r   r/   r6   r'   r'   r'   r(   r   k   s
   r   c                       s    e Zd ZdZ fddZ  ZS )r   zP
    An Api ViewSet which provides a method to verify that a token is valid
    c                    s   t t| j|g|R i |S N)superr   r6   r1   r2   r3   r4   rH   r'   r(   r\      s    z(ResetPasswordValidateTokenViewSet.creater7   r8   r9   r:   r\   __classcell__r'   r'   rc   r(   r      s   r   c                       s    e Zd ZdZ fddZ  ZS )r   z\
    An Api ViewSet which provides a method to reset a password based on a unique token
    c                    s   t t| j|g|R i |S r`   )ra   r   r6   rb   rc   r'   r(   r\      s    z"ResetPasswordConfirmViewSet.createrd   r'   r'   rc   r(   r      s   r   c                       s    e Zd ZdZ fddZ  ZS )r    z
    An Api ViewSet which provides a method to request a password reset token based on an e-mail address

    Sends a signal reset_password_token_created when a reset token was created
    c                    s   t t| j|g|R i |S r`   )ra   r    r6   rb   rc   r'   r(   r\      s    z'ResetPasswordRequestTokenViewSet.createrd   r'   r'   rc   r(   r       s   r    )6r"   datetimer   Zdjango.confr   Zdjango.contrib.authr   Z'django.contrib.auth.password_validationr   r   Zdjango.core.exceptionsr   Zdjango.utilsr   Zdjango.utils.translationr	   rY   Zrest_frameworkr
   Zrest_framework.genericsr   Zrest_framework.responser   Zrest_framework.viewsetsr   Z django_rest_passwordreset.modelsr   r   r   r   Z%django_rest_passwordreset.serializersr   r   r   Z!django_rest_passwordreset.signalsr   r   r   rV   __all__rX   r^   r_   r)   r   r   r   r   r   r    Zas_viewr   r   r   r'   r'   r'   r(   <module>   s8   +G		