o
    "4gj                     @   s  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
mZ d dlmZmZ d dlmZ d dl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 m!Z! ddl"m#Z# ddl$m%Z%m&Z& ddl#m'Z' ddl(m)Z)m*Z* ddl+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2 G dd deZ3e#4 Z5G dd dZ6G dd dej7Z8G dd de8Z9G dd dej:Z;G dd dej:Z<d d! Z=G d"d# d#e= Z>G d$d% d%e>Z?G d&d' d'ej:Z@G d(d) d)e@ZAG d*d+ d+e6e@ZBG d,d- d-e6e@ZCG d.d/ d/ej:ZDG d0d1 d1e6ej:ZEG d2d3 d3ej:ZFG d4d5 d5ej:ZGG d6d7 d7ej:ZHG d8d9 d9ej:ZIG d:d; d;eIZJG d<d= d=eIZKd>S )?    )import_module)forms)get_user_modelpassword_validation)PasswordResetTokenGenerator)
exceptions
validators)NoReverseMatchreverse)	mark_safe)gettextgettext_lazypgettext)flows)LOGIN_SESSION_KEY)EmailVerificationStage)context	ratelimit)get_username_max_lengthset_form_field_order   )app_settings)DefaultAccountAdapterget_adapter)AuthenticationMethod)EmailAddressLogin)filter_users_by_emailsetup_user_emailsync_user_email_addressesurl_str_to_user_pk
user_emailuser_pk_to_url_struser_usernamec                          e Zd Z fddZ  ZS )%EmailAwarePasswordResetTokenGeneratorc                    sh   t t| ||}t| t|}t|r|gng }|tjj	|dj
ddd |dt|7 }|S )NuseremailT)flat|)superr%   _make_hash_valuer   r!   setupdater   objectsfiltervalues_listjoinsorted)selfr'   	timestampretr(   emails	__class__ ^/var/www/html/authentication-server/venv/lib/python3.10/site-packages/allauth/account/forms.pyr,   !   s   
z6EmailAwarePasswordResetTokenGenerator._make_hash_value)__name__
__module____qualname__r,   __classcell__r:   r:   r8   r;   r%           r%   c                       r$   )PasswordVerificationMixinc                    sF   t t|  }|d}|d}|r!|r!||kr!| dtd |S )N	password1	password2*You must type the same password each time.)r+   rA   cleanget	add_error_)r4   cleaned_datarB   rC   r8   r:   r;   rE   3   s   

zPasswordVerificationMixin.clean)r<   r=   r>   rE   r?   r:   r:   r8   r;   rA   2   r@   rA   c                       r$   )PasswordFieldc                    sf   | dtj}tj|d|did|d< | dd }|d ur&||d jd< tt| j	|i | d S )Nrender_valueplaceholderlabel)rK   attrswidgetautocomplete)
popr   PASSWORD_INPUT_RENDER_VALUEr   PasswordInputrF   rN   r+   rJ   __init__)r4   argskwargsrK   rP   r8   r:   r;   rT   =   s   
zPasswordField.__init__r<   r=   r>   rT   r?   r:   r:   r8   r;   rJ   <   r@   rJ   c                       s(   e Zd Z fddZ fddZ  ZS )SetPasswordFieldc                    s4   d|d< | dt  t j|i | d | _d S )Nnew-passwordrP   	help_text)
setdefaultr   "password_validators_help_text_htmlr+   rT   r'   r4   rU   rV   r8   r:   r;   rT   L   s   
zSetPasswordField.__init__c                    s"   t  |}t j|| jd}|S )Nr&   )r+   rE   r   clean_passwordr'   r4   valuer8   r:   r;   rE   T   s   zSetPasswordField.clean)r<   r=   r>   rT   rE   r?   r:   r:   r8   r;   rX   K   s    rX   c                       sp   e Zd ZeedddZejedddZdZ	 fdd	Z
d
d Zdd Zdd Z fddZdddZ  ZS )	LoginFormPasswordcurrent-passwordrM   rP   zRemember MeF)rM   requiredNc                    sD  | dd | _tt| j|i | tjtjkr-t	j
tdddd}t	jtd|d}n:tjtjkrJt	jtdd	dd}t	jtd|t d
}ntjtjksRJ t	jtdddd}t	jtdd|d}|| jd< t| g d tjd ur|| jd= ztd}W n
 ty   Y d S w td}td| d| d| jd _d S )NrequestEmail addressr(   rL   rP   rN   EmailrM   rO   Usernameusername)rM   rO   
max_lengthzUsername or emailzfield labelr   login)ro   passwordrememberrq   account_reset_passwordzForgot your password?z	<a href="z">z</a>rp   )rQ   rf   r+   ra   rT   r   AUTHENTICATION_METHODr   EMAILr   
EmailInputrH   
EmailFieldUSERNAME	TextInput	CharFieldr   USERNAME_EMAILr   fieldsr   SESSION_REMEMBERr
   r	   r   rZ   )r4   rU   rV   login_widgetlogin_field	reset_url
forgot_txtr8   r:   r;   rT   `   sP   


zLoginForm.__init__c                 C   sf   i }| j d }tjtjkr||d< ntjtjkr||d< n| |r&||d< ||d< | j d |d< |S )z_
        Provides the credentials required to authenticate the user for
        login.
        ro   r(   rm   rp   )rI   r   rs   r   rt   rw   _is_login_email)r4   credentialsro   r:   r:   r;   user_credentials   s   



zLoginForm.user_credentialsc                 C   s   | j d }| S )Nro   )rI   strip)r4   ro   r:   r:   r;   clean_login   s   
zLoginForm.clean_loginc                 C   s0   z
t | d}W |S  tjy   d}Y |S w )NTF)r   validate_emailr   ValidationError)r4   ro   r6   r:   r:   r;   r      s   
zLoginForm._is_login_emailc                    s   t t|   | jrd S |  }t| j}|j| jfi |}|r@t||	dd}t
jtj|r7|d|| _|| _| jS tj}|tjjkr\| jd }| |rXtjj}ntjj}|d|j )Nr(   r'   r(   too_many_login_attemptsro   z%s_password_mismatch)r+   ra   rE   _errorsr   r   rf   authenticater   rF   r   ro   is_login_rate_limitedr   validation_error_loginr'   r   rs   r   rz   rI   r   rt   rw   r`   )r4   r   adapterr'   ro   auth_methodr8   r:   r;   rE      s(   





zLoginForm.cleanc                 C   sb   |   }| j}||_tj|||}tj}|d u r| jd }|r)|j	
tj |S |j	
d |S )Nrq   r   )r   r   redirect_urlr   ro   perform_password_loginr   r|   rI   session
set_expirySESSION_COOKIE_AGE)r4   rf   r   r   ro   r6   rq   r:   r:   r;   ro      s   
zLoginForm.loginN)r<   r=   r>   rJ   rH   rp   r   BooleanFieldrq   r'   rT   r   r   r   rE   ro   r?   r:   r:   r8   r;   ra   Z   s    -ra   c                   @   s   e Zd Zdd ZdS )_DummyCustomSignupFormc                 C   s   dS )zL
        Invoked at signup time to complete the signup of the user.
        Nr:   r4   rf   r'   r:   r:   r;   signup   s   z_DummyCustomSignupForm.signupN)r<   r=   r>   r   r:   r:   r:   r;   r      s    r   c               
   C   s   t jstS zt jdd\} }W n ty   tdt j w zt| }W n ty; } z	td| |f d}~ww zt	||}W n t
yS   td| |f w t|ds^td|S )	a  
    Currently, we inherit from the custom form, if any. This is all
    not very elegant, though it serves a purpose:

    - There are two signup forms: one for local accounts, and one for
      social accounts
    - Both share a common base (BaseSignupForm)

    - Given the above, how to put in a custom signup form? Which form
      would your custom form derive from, the local or the social one?
    .r   z!%s does not point to a form classz#Error importing form class %s: "%s"Nz(Module "%s" does not define a "%s" classr   zLThe custom signup form must offer a `def signup(self, request, user)` method)r   SIGNUP_FORM_CLASSr   rsplit
ValueErrorr   ImproperlyConfiguredr   ImportErrorgetattrAttributeErrorhasattr)	fc_modulefc_classnamemodefc_classr:   r:   r;   _base_signup_form_class   s:   


r   c                       s   e Zd ZejedejejedddddZ	ej
ejdeddddd	Z fd
dZdd Zdd Zdd Zdd Z fddZdd Zdd Zdd Z  ZS )BaseSignupFormrl   rm   rh   ri   )rM   
min_lengthrO   r(   rg   typerL   rP   rO   c                    s,  | dtj}| dtj| _d| _tt| j|i | | j	d }t
 |_|jt|j t|j|jjd< g d}tjrUtjtdtjdtd	d
dd| j	d< |rftd| j	d _d| j	d _ntd| j	d _d| j	d _d| j	d j_| jrg d}| js| j	d= t| t| dd p| d S )Nemail_requiredusername_requiredFrm   	maxlength)r(   email2rm   rB   rC   zEmail (again)r(   zEmail address confirmationr   rL   ri   rk   r   rj   TzEmail (optional))rm   r(   r   rB   rC   field_order)rQ   r   EMAIL_REQUIREDUSERNAME_REQUIREDr   account_already_existsr+   r   rT   r{   r   rn   r   appendMaxLengthValidatorstrrO   rN   SIGNUP_EMAIL_ENTER_TWICEr   rv   rH   rx   r   rM   re   is_requiredr   r   )r4   rU   rV   r   username_fielddefault_field_orderr8   r:   r;   rT     sF   

	zBaseSignupForm.__init__c                 C   s   | j d }t |}|S )Nrm   )rI   r   clean_usernamer_   r:   r:   r;   r   J  s   
zBaseSignupForm.clean_usernamec                 C   s2   | j d  }t |}|rtjr| |}|S )Nr(   )rI   lowerr   clean_emailr   UNIQUE_EMAILvalidate_unique_emailr_   r:   r:   r;   r   S  s
   

zBaseSignupForm.clean_emailc                 C   s   | j d  }|S )Nr   )rI   r   r_   r:   r:   r;   clean_email2Z  s   zBaseSignupForm.clean_email2c                 C   sJ   t  }tj|}|du rn|du r|d|d u sJ d| _||S )NTFemail_taken)r   r   manage_emailassess_unique_emailr   r   r   )r4   r`   r   
assessmentr:   r:   r;   r   ^  s   

z$BaseSignupForm.validate_unique_emailc                    sL   t t|  }tjr$|d}|d}|r$|r$||kr$| dtd |S )Nr(   r   z'You must type the same email each time.)r+   r   rE   r   r   rF   rG   rH   )r4   rI   r(   r   r8   r:   r;   rE   l  s   

zBaseSignupForm.cleanc                 C   s   |  || d S r   )r   r   r:   r:   r;   custom_signupu  s   zBaseSignupForm.custom_signupc                 C   sL   | j r| jd }tj||}d}tj|jt< ||fS | 	|}d}||fS )zTry and save the user. This can fail in case of a conflict on the
        email address, in that case we will send an "account already exists"
        email and return a standard "email verification sent" response.
        r(   N)
r   rI   r   r   prevent_enumerationr   keyr   r   save)r4   rf   r(   respr'   r:   r:   r;   try_savex  s   

zBaseSignupForm.try_savec                 C   sj   | j d}| jrt|t }||}||||  | || t|||r0t	|dg |S g  |S )Nr(   )r(   )
rI   rF   r   r   r   new_user	save_userr   r   r   )r4   rf   r(   r   r'   r:   r:   r;   r     s   
zBaseSignupForm.save)r<   r=   r>   r   ry   rH   r   USERNAME_MIN_LENGTHrx   rm   rv   r(   rT   r   r   r   r   rE   r   r   r   r?   r:   r:   r8   r;   r     s2    
6		r   c                       s4   e Zd Z fddZ fddZ fddZ  ZS )
SignupFormc                    s   | dd| _tt| j|i | | js1ttddt d| j	d< t
jr1ttddd| j	d	< t| d
r<t| | j t
j}|rVtjddtjdddddd| j	|< d S d S )N
by_passkeyFrb   rY   )rM   rP   rZ   rB   Password (again)rd   rC   r    z$position: absolute; right: -99999px;z-1nope)styletabindexrP   ri   )re   rM   rO   )rQ   r   r+   r   rT   rJ   rH   r   r\   r{   r   SIGNUP_PASSWORD_ENTER_TWICEr   r   r   SIGNUP_FORM_HONEYPOT_FIELDr   ry   rx   )r4   rU   rV   honeypot_field_namer8   r:   r;   rT     s6   
zSignupForm.__init__c                    s>   t j}|r| j| rd}t }||d}||fS t |S )z
        override of parent class method that adds additional catching
        of a potential bot filling out the honeypot field and returns a
        'fake' email verification response if honeypot was filled out
        N)r   r   rI   r   respond_email_verification_sentr+   r   )r4   rf   r   r'   r   r   r8   r:   r;   r     s   
zSignupForm.try_savec              
      s   t    t }| }t|| jd t|| jd | jd}|rIz
t j||d W n t	j
yH } z| d| W Y d }~nd }~ww tjrhd| jv rhd| jv rh| jd | jd krh| dtd | jS )Nrm   r(   rB   r&   rC   rD   )r+   rE   r   r#   rI   rF   r!   r   r^   r   r   rG   r   r   rH   )r4   User
dummy_userrp   r   r8   r:   r;   rE     s.   


zSignupForm.clean)r<   r=   r>   rT   r   rE   r?   r:   r:   r8   r;   r     s    r   c                       s   e Zd Zd fdd	Z  ZS )UserFormNc                    s    || _ tt| j|i | d S r   )r'   r+   r   rT   )r4   r'   rU   rV   r8   r:   r;   rT     s   zUserForm.__init__r   rW   r:   r:   r8   r;   r     s    r   c                   @   sB   e Zd ZejeddejdeddddZdd	 Zd
d Z	dS )AddEmailFormrj   Tr(   rg   r   ri   rM   re   rO   c                    s   ddl m}  jd  }t }||}t|} fdd|D } fdd|D }|r2|d|r?tj	r?tj
s?|dtj jsM|d	tj|jj jj| jd
 |S )Nr   )signalsr(   c                    s   g | ]}|j  jj kr|qS r:   pkr'   .0ur4   r:   r;   
<listcomp>      z,AddEmailForm.clean_email.<locals>.<listcomp>c                    s   g | ]}|j  jj kr|qS r:   r   r   r   r:   r;   r     r   duplicate_emailr   max_email_addresses)senderr(   r'   )allauth.accountr   rI   r   r   r   r   r   r   r   PREVENT_ENUMERATIONr   r/   can_add_emailr'   MAX_EMAIL_ADDRESSES
_add_emailsendr9   )r4   r   r`   r   userson_this_accounton_diff_accountr:   r   r;   r     s4   


zAddEmailForm.clean_emailc                 C   sb   t jrt| j| jd d}|| |S t jr#tj|| j| jd S tjj	|| j| jd ddS )Nr(   r   T)confirm)
r   "EMAIL_VERIFICATION_BY_CODE_ENABLEDr   r'   rI   send_confirmationCHANGE_EMAILr/   add_new_email	add_email)r4   rf   email_addressr:   r:   r;   r     s   
zAddEmailForm.saveN)
r<   r=   r>   r   rv   rH   rx   r(   r   r   r:   r:   r:   r;   r     s    !r   c                       sX   e Zd ZeedddZeeddZeeddZ fddZ	d	d
 Z
dd Z  ZS )ChangePasswordFormzCurrent Passwordrc   rd   New PasswordrM   New Password (again)c                    (   t t| j|i | | j| jd _d S NrB   )r+   r   rT   r'   r{   r]   r8   r:   r;   rT   /     zChangePasswordForm.__init__c                 C   s*   | j | jdst d| jd S )Noldpasswordenter_current_password)r'   check_passwordrI   rF   r   r   r   r:   r:   r;   clean_oldpassword3  s   
z$ChangePasswordForm.clean_oldpasswordc                 C      t j| j| jd  d S r  r   password_changechange_passwordr'   rI   r   r:   r:   r;   r   8     zChangePasswordForm.save)r<   r=   r>   rJ   rH   r  rX   rB   rC   rT   r  r   r?   r:   r:   r8   r;   r   (  s    r   c                       @   e Zd ZeeddZeeddZ fddZdd Z	  Z
S )SetPasswordFormrb   r   r   c                    r  r  )r+   r  rT   r'   r{   r]   r8   r:   r;   rT   @  r  zSetPasswordForm.__init__c                 C   r	  r  r
  r   r:   r:   r;   r   D  r  zSetPasswordForm.saver<   r=   r>   rX   rH   rB   rJ   rC   rT   r   r?   r:   r:   r8   r;   r  <  s
    r  c                   @   sJ   e Zd ZejeddejdedddddZdd	 Zd
e	fddZ
dS )ResetPasswordFormrj   Tr(   rg   r   ri   r   c                 C   sL   | j d  }t |}t|ddd| _| js!tjs!t d| j d S )Nr(   T	is_activeprefer_verifiedunknown_email)	rI   r   r   r   r   r   r   r   r   )r4   r(   r:   r:   r;   r   U  s   
zResetPasswordForm.clean_emailreturnc                 K   s   | j d }| jstj|| |S t }|dt}| jD ]2}||}t	|}| d| }	|
|	}
||
|||d}tjtjkrHt||d< |||| q|S )Nr(   token_generator-)r'   password_reset_urluidr   rf   rm   )rI   r   r   r   send_unknown_account_mailr   rF   default_token_generator
make_tokenr"   get_reset_password_from_key_urlr   rs   r   rt   r#   send_password_reset_mail)r4   rf   rV   r(   r   r  r'   temp_keyr  r   urlr   r:   r:   r;   r   ]  s*   



zResetPasswordForm.saveN)r<   r=   r>   r   rv   rH   rx   r(   r   r   r   r:   r:   r:   r;   r  H  s    r  c                       r  )ResetPasswordKeyFormr   r   r  c                    sD   | dd | _| dd | _tt| j|i | | j| jd _d S )Nr'   r   rB   )rQ   r'   r   r+   r"  rT   r{   r]   r8   r:   r;   rT     s   zResetPasswordKeyForm.__init__c                 C   r	  r  )r   password_resetreset_passwordr'   rI   r   r:   r:   r;   r     r  zResetPasswordKeyForm.saver  r:   r:   r8   r;   r"  }  s
    r"  c                       s<   e Zd Ze Ze ZdZeZ	dd Z
 fddZ  ZS )UserTokenFormNc              	   C   s:   t  }zt|}|jj|dW S  t|jfy   Y d S w )N)r   )r   r    r/   rF   r   DoesNotExist)r4   uidb36r   r   r:   r:   r;   	_get_user  s   zUserTokenForm._get_userc                    sn   t t|  }|dd }|dd }t }|s|d| || _| jd u s0| j	| j|s5|d|S )Nr'  r   invalid_password_reset)
r+   r%  rE   rF   r   r   r(  
reset_userr  check_token)r4   rI   r'  r   r   r8   r:   r;   rE     s   

zUserTokenForm.clean)r<   r=   r>   r   ry   r'  r   r*  r  r  r(  rE   r?   r:   r:   r8   r;   r%    s    r%  c                       s4   e Zd ZeedddZ fddZdd Z  ZS )ReauthenticateFormrb   rc   rd   c                    "   | d| _t j|i | d S )Nr'   )rQ   r'   r+   rT   r]   r8   r:   r;   rT        zReauthenticateForm.__init__c                 C   s,   | j d}t | j|st d|S )Nrp   incorrect_password)rI   rF   r   reauthenticater'   r   )r4   rp   r:   r:   r;   r^     s   z!ReauthenticateForm.clean_password)	r<   r=   r>   rJ   rH   rp   rT   r^   r?   r:   r:   r8   r;   r,    s    r,  c                   @   s2   e Zd ZejejedddddZdd ZdS )	RequestLoginCodeFormrg   r(   rh   ri   r   c                 C   sV   t  }| jd }tjst|ddd}|s|dtjtj	d|
 ds)|d|S )Nr(   Tr  r  request_login_code)actionr   r   )r   rI   r   r   r   r   r   consumer   rf   r   )r4   r   r(   r   r:   r:   r;   r     s   


z RequestLoginCodeForm.clean_emailN)	r<   r=   r>   r   rv   ru   rH   r(   r   r:   r:   r:   r;   r1    s    	r1  c                       sH   e Zd ZejedejedddddZ fddZdd	 Z	  Z
S )
BaseConfirmCodeFormCodezone-time-coderh   ri   rk   c                    r-  )Ncode)rQ   r7  r+   rT   r]   r8   r:   r;   rT     r.  zBaseConfirmCodeForm.__init__c                 C   s.   | j d}tjj|| jdst d|S )Nr7  )actualexpectedincorrect_code)rI   rF   r   login_by_codecompare_coder7  r   r   )r4   r7  r:   r:   r;   
clean_code  s   zBaseConfirmCodeForm.clean_code)r<   r=   r>   r   ry   rH   rx   r7  rT   r=  r?   r:   r:   r8   r;   r5    s    r5  c                   @      e Zd ZdS )ConfirmLoginCodeFormNr<   r=   r>   r:   r:   r:   r;   r?        r?  c                   @   r>  ) ConfirmEmailVerificationCodeFormNr@  r:   r:   r:   r;   rB    rA  rB  N)L	importlibr   djangor   django.contrib.authr   r   django.contrib.auth.tokensr   django.corer   r   django.urlsr	   r
   django.utils.safestringr   django.utils.translationr   r   rH   r   allauth.account.internalr   !allauth.account.internal.stagekitr   allauth.account.stagesr   allauth.corer   r   allauth.utilsr   r   r   r   r   r   r   r   modelsr   r   utilsr   r   r   r    r!   r"   r#   r%   PASSWORD_RESET_TOKEN_GENERATORr  rA   ry   rJ   rX   Formra   r   r   r   r   r   r   r   r  r  r"  r%  r,  r1  r5  r?  rB  r:   r:   r:   r;   <module>   sR    $
x( Q:5!