U
    8ÄT_š4  ã                   @   sÞ  d dl mZmZ d dlmZ d dlmZmZmZ	m
ZmZ d dlmZ d dlmZmZmZm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$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/m0Z0 d dl1m2Z2 eƒ Z3G dd„ dƒZ4G dd„ de4e2ƒZ5G dd„ de4e0ƒZ6d.dd„Z7defdd„Z8G dd „ d ƒZ9G d!d"„ d"e9e2ƒZ:d#Z;G d$d%„ d%e9e0ƒZ<G d&d'„ d'e9e2ƒZ=G d(d)„ d)e9e0ƒZ>G d*d+„ d+e9e2ƒZ?G d,d-„ d-e9e0ƒZ@dS )/é    )ÚurlparseÚ
urlunparse)Úsettings)ÚREDIRECT_FIELD_NAMEÚget_user_modelÚloginÚlogoutÚupdate_session_auth_hash)Úlogin_required)ÚAuthenticationFormÚPasswordChangeFormÚPasswordResetFormÚSetPasswordForm)Údefault_token_generator)Úget_current_site)ÚValidationError)ÚHttpResponseRedirectÚ	QueryDict)Úresolve_url)Úreverse_lazy)Úmethod_decorator)Úurl_has_allowed_host_and_schemeÚurlsafe_base64_decode)Úgettext_lazy)Únever_cache)Úcsrf_protect)Úsensitive_post_parameters)ÚTemplateView)ÚFormViewc                   @   s   e Zd Zeƒ Zdd„ ZdS )ÚSuccessURLAllowedHostsMixinc                 C   s   | j  ¡ h| j™S ©N)ÚrequestÚget_hostÚsuccess_url_allowed_hosts©Úself© r&   ú=/tmp/pip-unpacked-wheel-dv63caxw/django/contrib/auth/views.pyÚget_success_url_allowed_hosts$   s    z9SuccessURLAllowedHostsMixin.get_success_url_allowed_hostsN)Ú__name__Ú
__module__Ú__qualname__Úsetr#   r(   r&   r&   r&   r'   r   !   s   r   c                       sŠ   e Zd ZdZeZdZeZdZ	dZ
dZeeƒ ƒeeƒeeƒ‡ fdd„ƒƒƒZdd„ Zd	d
„ Zdd„ Z‡ fdd„Zdd„ Z‡ fdd„Z‡  ZS )Ú	LoginViewz=
    Display the login form and handle the login action.
    Nzregistration/login.htmlFc                    sH   | j r4| jjjr4|  ¡ }|| jjkr,tdƒ‚t|ƒS tƒ j	|f|ž|ŽS )NzsRedirection loop for authenticated user detected. Check that your LOGIN_REDIRECT_URL doesn't point to a login page.)
Úredirect_authenticated_userr!   ÚuserÚis_authenticatedÚget_success_urlÚpathÚ
ValueErrorr   ÚsuperÚdispatch)r%   r!   ÚargsÚkwargsÚredirect_to©Ú	__class__r&   r'   r5   3   s    ÿzLoginView.dispatchc                 C   s   |   ¡ }|pttjƒS r    )Úget_redirect_urlr   r   ZLOGIN_REDIRECT_URL)r%   Úurlr&   r&   r'   r1   A   s    zLoginView.get_success_urlc                 C   sD   | j j | j| j j | jd¡¡}t||  ¡ | j  ¡ d�}|r@|S dS )z6Return the user-originating redirect URL if it's safe.Ú ©r<   Zallowed_hostsZrequire_https)r!   ÚPOSTÚgetÚredirect_field_nameÚGETr   r(   Ú	is_secure)r%   r8   Úurl_is_safer&   r&   r'   r;   E   s    þýzLoginView.get_redirect_urlc                 C   s   | j p
| jS r    )Úauthentication_formÚ
form_classr$   r&   r&   r'   Úget_form_classR   s    zLoginView.get_form_classc                    s   t ƒ  ¡ }| j|d< |S )Nr!   )r4   Úget_form_kwargsr!   ©r%   r7   r9   r&   r'   rH   U   s    

zLoginView.get_form_kwargsc                 C   s   t | j| ¡ ƒ t|  ¡ ƒS )z)Security check complete. Log the user in.)Ú
auth_loginr!   Úget_userr   r1   ©r%   Úformr&   r&   r'   Ú
form_validZ   s    zLoginView.form_validc                    sD   t ƒ jf |Ž}t| jƒ}| | j|  ¡ d|d|ji| jp:i –¡ |S )NÚsiteÚ	site_name)	r4   Úget_context_datar   r!   ÚupdaterA   r;   ÚnameÚextra_context©r%   r7   ÚcontextZcurrent_siter9   r&   r'   rQ   _   s    
   ýüzLoginView.get_context_data)r)   r*   r+   Ú__doc__r   rF   rE   r   rA   Útemplate_namer.   rT   r   r   r   r   r5   r1   r;   rG   rH   rN   rQ   Ú__classcell__r&   r&   r9   r'   r-   (   s"   r-   c                       sT   e Zd ZdZdZeZdZdZe	e
ƒ‡ fdd„ƒZdd„ Zdd	„ Z‡ fd
d„Z‡  ZS )Ú
LogoutViewzH
    Log out the user and display the 'You are logged out' message.
    Nzregistration/logged_out.htmlc                    s0   t |ƒ |  ¡ }|rt|ƒS tƒ j|f|ž|ŽS r    )Úauth_logoutÚget_next_pager   r4   r5   )r%   r!   r6   r7   Ú	next_pager9   r&   r'   r5   t   s
    zLogoutView.dispatchc                 O   s   | j |f|ž|ŽS )zLogout may be done via POST.)r@   )r%   r!   r6   r7   r&   r&   r'   Úpost}   s    zLogoutView.postc                 C   s�   | j d k	rt| j ƒ}ntjr(ttjƒ}n| j }| j| jjksJ| j| jjkrŒ| jj | j| jj | j¡¡}t	||  
¡ | j ¡ d�}|sŒ| jj}|S )Nr>   )r]   r   r   ZLOGOUT_REDIRECT_URLrA   r!   r?   rB   r@   r   r(   rC   r2   )r%   r]   rD   r&   r&   r'   r\   �   s(    
ÿþýzLogoutView.get_next_pagec                    s>   t ƒ jf |Ž}t| jƒ}| ||jtdƒdœ| jp4i –¡ |S )Nz
Logged out)rO   rP   Útitle)r4   rQ   r   r!   rR   rS   Ú_rT   rU   r9   r&   r'   rQ   š   s    
ýüzLogoutView.get_context_data)r)   r*   r+   rW   r]   r   rA   rX   rT   r   r   r5   r^   r\   rQ   rY   r&   r&   r9   r'   rZ   k   s   rZ   Nc                 C   s   t |p
tjƒ}tj|d�| ƒS )zR
    Log out the user if they are logged in. Then redirect to the login page.
    )r]   )r   r   Ú	LOGIN_URLrZ   Zas_view)r!   Ú	login_urlr&   r&   r'   Úlogout_then_login¦   s    rc   c                 C   sR   t |p
tjƒ}tt|ƒƒ}|rFt|d dd�}| ||< |jdd�|d< tt|ƒƒS )zM
    Redirect the user to the login page, passing the given 'next' page.
    é   T)Zmutableú/)Úsafe)	r   r   ra   Úlistr   r   Ú	urlencoder   r   )Únextrb   rA   Zresolved_urlZlogin_url_partsZquerystringr&   r&   r'   Úredirect_to_login®   s    rj   c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚPasswordContextMixinNc                    s,   t ƒ jf |Ž}| d| ji| jp"i –¡ |S )Nr_   )r4   rQ   rR   r_   rT   ©r%   r7   rV   r9   r&   r'   rQ   Ç   s     ÿþz%PasswordContextMixin.get_context_data)r)   r*   r+   rT   rQ   rY   r&   r&   r9   r'   rk   Ä   s   rk   c                       s`   e Zd ZdZdZeZdZdZdZ	e
dƒZdZedƒZeZeeƒ‡ fdd„ƒZ‡ fd	d
„Z‡  ZS )ÚPasswordResetViewz&registration/password_reset_email.htmlNz'registration/password_reset_subject.txtZpassword_reset_donez%registration/password_reset_form.htmlzPassword resetc                    s   t ƒ j||ŽS r    ©r4   r5   ©r%   r6   r7   r9   r&   r'   r5   Ü   s    zPasswordResetView.dispatchc              	      sB   | j  ¡ | j| j| j| j| j | j| jdœ}|jf |Ž t	ƒ  
|¡S )N)Z	use_httpsÚtoken_generatorÚ
from_emailÚemail_template_nameÚsubject_template_namer!   Úhtml_email_template_nameÚextra_email_context)r!   rC   rp   rq   rr   rs   rt   ru   Úsaver4   rN   )r%   rM   Úoptsr9   r&   r'   rN   à   s    ø
zPasswordResetView.form_valid)r)   r*   r+   rr   ru   r   rF   rq   rt   rs   r   Úsuccess_urlrX   r`   r_   r   rp   r   r   r5   rN   rY   r&   r&   r9   r'   rm   Ð   s   rm   Z_password_reset_tokenc                   @   s   e Zd ZdZedƒZdS )ÚPasswordResetDoneViewz%registration/password_reset_done.htmlzPassword reset sentN)r)   r*   r+   rX   r`   r_   r&   r&   r&   r'   ry   ò   s   ry   c                       s‚   e Zd ZeZdZdZdZedƒZ	dZ
edƒZeZeeƒ ƒeeƒ‡ fdd„ƒƒZd	d
„ Z‡ fdd„Z‡ fdd„Z‡ fdd„Z‡  ZS )ÚPasswordResetConfirmViewFNzset-passwordZpassword_reset_completez(registration/password_reset_confirm.htmlzEnter new passwordc                    s¾   d|krd|kst ‚d| _|  |d ¡| _| jd k	r°|d }|| jkrz| jj t¡}| j	 
| j|¡r°d| _tƒ j||ŽS n6| j	 
| j|¡r°|| jjt< | jj || j¡}t|ƒS |  |  ¡ ¡S )NÚuidb64ÚtokenFT)ÚAssertionErrorÚ	validlinkrK   r/   Úreset_url_tokenr!   Úsessionr@   ÚINTERNAL_RESET_SESSION_TOKENrp   Zcheck_tokenr4   r5   r2   Úreplacer   Zrender_to_responserQ   )r%   r6   r7   r|   Zsession_tokenZredirect_urlr9   r&   r'   r5     s    

z!PasswordResetConfirmView.dispatchc                 C   sH   zt |ƒ ¡ }tjj|d�}W n$ ttttjt	fk
rB   d }Y nX |S )N)Úpk)
r   ÚdecodeÚ	UserModelZ_default_managerr@   Ú	TypeErrorr3   ÚOverflowErrorZDoesNotExistr   )r%   r{   Úuidr/   r&   r&   r'   rK     s    
z!PasswordResetConfirmView.get_userc                    s   t ƒ  ¡ }| j|d< |S ©Nr/   )r4   rH   r/   rI   r9   r&   r'   rH   '  s    

z(PasswordResetConfirmView.get_form_kwargsc                    s4   |  ¡ }| jjt= | jr(t| j|| jƒ tƒ  |¡S r    )	rv   r!   r€   r�   Úpost_reset_loginrJ   Úpost_reset_login_backendr4   rN   )r%   rM   r/   r9   r&   r'   rN   ,  s
    
z#PasswordResetConfirmView.form_validc                    s8   t ƒ jf |Ž}| jrd|d< n| d tdƒddœ¡ |S )NTr~   zPassword reset unsuccessfulF)rM   r_   r~   )r4   rQ   r~   rR   r`   rl   r9   r&   r'   rQ   3  s    
ýz)PasswordResetConfirmView.get_context_data)r)   r*   r+   r   rF   rŠ   r‹   r   r   rx   rX   r`   r_   r   rp   r   r   r   r5   rK   rH   rN   rQ   rY   r&   r&   r9   r'   rz   ÷   s   	rz   c                       s(   e Zd ZdZedƒZ‡ fdd„Z‡  ZS )ÚPasswordResetCompleteViewz)registration/password_reset_complete.htmlzPassword reset completec                    s    t ƒ jf |Ž}ttjƒ|d< |S )Nrb   )r4   rQ   r   r   ra   rl   r9   r&   r'   rQ   D  s    z*PasswordResetCompleteView.get_context_data)r)   r*   r+   rX   r`   r_   rQ   rY   r&   r&   r9   r'   rŒ   @  s   rŒ   c                       sf   e Zd ZeZedƒZdZedƒZ	e
eƒ ƒe
eƒe
eƒ‡ fdd„ƒƒƒZ‡ fdd„Z‡ fdd	„Z‡  ZS )
ÚPasswordChangeViewZpassword_change_donez&registration/password_change_form.htmlzPassword changec                    s   t ƒ j||ŽS r    rn   ro   r9   r&   r'   r5   P  s    zPasswordChangeView.dispatchc                    s   t ƒ  ¡ }| jj|d< |S r‰   )r4   rH   r!   r/   rI   r9   r&   r'   rH   V  s    
z"PasswordChangeView.get_form_kwargsc                    s"   |  ¡  t| j|jƒ tƒ  |¡S r    )rv   r	   r!   r/   r4   rN   rL   r9   r&   r'   rN   [  s    zPasswordChangeView.form_valid)r)   r*   r+   r   rF   r   rx   rX   r`   r_   r   r   r   r
   r5   rH   rN   rY   r&   r&   r9   r'   r�   J  s   r�   c                       s0   e Zd ZdZedƒZeeƒ‡ fdd„ƒZ‡  Z	S )ÚPasswordChangeDoneViewz&registration/password_change_done.htmlzPassword change successfulc                    s   t ƒ j||ŽS r    rn   ro   r9   r&   r'   r5   g  s    zPasswordChangeDoneView.dispatch)
r)   r*   r+   rX   r`   r_   r   r
   r5   rY   r&   r&   r9   r'   rŽ   c  s   rŽ   )N)AÚurllib.parser   r   Zdjango.confr   Zdjango.contrib.authr   r   r   rJ   r   r[   r	   Zdjango.contrib.auth.decoratorsr
   Zdjango.contrib.auth.formsr   r   r   r   Zdjango.contrib.auth.tokensr   Zdjango.contrib.sites.shortcutsr   Zdjango.core.exceptionsr   Zdjango.httpr   r   Zdjango.shortcutsr   Zdjango.urlsr   Zdjango.utils.decoratorsr   Zdjango.utils.httpr   r   Zdjango.utils.translationr   r`   Zdjango.views.decorators.cacher   Zdjango.views.decorators.csrfr   Zdjango.views.decorators.debugr   Zdjango.views.generic.baser   Zdjango.views.generic.editr   r…   r   r-   rZ   rc   rj   rk   rm   r�   ry   rz   rŒ   r�   rŽ   r&   r&   r&   r'   Ú<module>   s@   C;
I
