o
    h1                     @   sl  d dl mZ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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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edede$de%fddZ&dedeee ee	 f fddZ'dedede%fddZ(dede$fddZ)dee	 fddZ*		d?dede$d e%d!e%de%f
d"d#Z+	d@de$d!e%de%fd$d%Z,dedee fd&d'Z-d(efd)d*Z.dedede%fd+d,Z/		-	dAded.ed/e%d0e%de%f
d1d2Z0ded(ede%fd3d4Z1ded(ede%fd5d6Z2ded(ede%fd7d8Z3dede$d/e%fd9d:Z4ded(ede%fd;d<Z5ded.edee fd=d>Z6d-S )B    )OptionalTuple)messages)AbstractBaseUser)HttpRequestHttpResponse)reverse)app_settingssignals)get_adapter)emit_email_changedsync_email_addresssync_user_email_address)send_unknown_account_mail)EmailAddressLogin)ImmediateHttpResponse)	ratelimit)get_frontend_url)respond_429)build_absolute_urirequestuseremailreturnc                 C   s>   z	t j||}W n t jy   Y dS w |jst| |S dS )NFT)r   objectsget_for_userDoesNotExistverifiedverify_email)r   r   r   email_address r!   h/var/www/html/pos/venv/lib/python3.10/site-packages/allauth/account/internal/flows/email_verification.pyverify_email_indirectly   s   
r#   c                 C   s$   | | }|s	dS t| |}||fS )N)NN)confirmlogin_on_verification)r   verificationr    responser!   r!   r"   verify_email_and_resume%   s
   

r(   r    c                 C   s   |j  }tjj|jdj|j d }|jdds)t| 	| t
jdd|ji dS |jtj d |  |rBtjjt| | j|d tjjt| |d	 tjrhtjj|jdj|j dD ]}|  q[t| || t| 	| t
jd
d|ji dS )z8
    Marks the email address as confirmed on the db
    user_id)pkF)commitz.account/messages/email_confirmation_failed.txtr   )conditional)senderr   r   r    )r.   r   r    z$account/messages/email_confirmed.txtT)r+   r   r   filterr*   excludefirstset_verifiedr   add_messager   ERRORr   set_as_primaryr	   CHANGE_EMAILsaver
   email_addedsendr   email_confirmedremover   SUCCESS)r   r    addedfrom_email_addressinstancer!   r!   r"   r   /   sV   

r   c                 C   s2   t | d|jd}|std|jgd}t| |}|S )zConstructs the email confirmation (activation) url.

    Note that if you have architected your system such that email
    confirmations are sent outside of the request context `request`
    can be `None` here.
    account_confirm_email)key)args)r   rA   r   r   )r   emailconfirmationurlr!   r!   r"   get_email_verification_url^   s
   
rE   c                 C   sf   ddl m}m} || |j}tjstjr'| jj	s'|r'|j
jr'|j
jj|jkr/|r-|  dS | S )a  Simply logging in the user may become a security issue. If you
    do not take proper care (e.g. don't purge used email
    confirmations), a malicious person that got hold of the link
    will be able to login over and over again and the user is
    unable to do anything about it. Even restoring their own mailbox
    security will not help, as the links will still work. For
    password reset this is different, this mechanism works only as
    long as the attacker has access to the mailbox. If they no
    longer has access they cannot issue a password request and
    intercept it. Furthermore, all places where the links are
    listed (log files, but even Google Analytics) all of a sudden
    need to be secured. Purging the email confirmation once
    confirmed changes the behavior -- users will not be able to
    repeatedly confirm (in case they forgot that they already
    clicked the mail).

    All in all, we only login on verification when the user that is in the
    process of signing up is present in the session to avoid all of the above.
    This may not 100% work in case the user closes the browser (and the session
    gets lost), but at least we're secure.
    r   )EmailVerificationStageLoginStageControllerN)allauth.account.stagesrF   rG   enterrA   r	   LOGIN_ON_EMAIL_CONFIRMATION"EMAIL_VERIFICATION_BY_CODE_ENABLEDr   is_authenticatedloginr+   r*   abortexit)r   r    rF   rG   stager!   r!   r"   r%   l   s"   	r%   Fdry_runraise_exceptionc              
   C   s"   t tj| tjd| ||ddS )Nconfirm_emailT)configactionrA   rQ   rR   	limit_get)boolr   consumer	   RATE_LIMITSlower)r   r   rQ   rR   r!   r!   r"   %consume_email_verification_rate_limit   s   r[   c                 C   s(   t | ||d}|stjrtt| |S )aL  
    For email verification by link, it is not an issue if the user runs into rate
    limits. The reason is that the link is session independent. Therefore, if the
    user hits rate limits, we can just silently skip sending additional
    verification emails, as the previous emails that were already sent still
    contain valid links. This is different from email verification by code.  Here,
    the session contains a specific code, meaning, silently skipping new
    verification emails is not an option, and we must hard fail (429) instead. The
    latter was missing, fixed.
    )rR   )r[   r	   rK   r   r   )r   r   rR   rl_okr!   r!   r"   $handle_verification_email_rate_limit   s   
r]   c                 C   s,   t jj| jddd }|st| }|S )Nr)   z-primaryz	-verified)r   r   r/   r+   order_byr1   r   )r   addressr!   r!   r"   get_address_for_user   s   r`   rM   c                 C   sR   | j sJ | jr$z
tj| j | jW S  tjy#   t| j | j Y S w t| j S N)r   r   r   r   r   r   r   r`   )rM   r!   r!   r"   get_address_for_login   s   

rb   c                 C   s   t |}|sdS t| |S )z/
    Used in the email-required-decorator.
    F)r`   "send_verification_email_to_address)r   r   r_   r!   r!   r"    send_verification_email_for_user   s   
rd   Nr_   signupskip_enumeration_mailsc                 C   s   t jr|sddlm} |j| |jr|jnd|jd}|jS t	| |j}|s'dS |js@|r-n|r7t
 |j nt| |j d}nt jrOt
 j| ||d |}n|j| |d}t| |j|d |rktjj|j| ||d dS )	zB
    Resend email verification for an existing email address.
    r   )EmailVerificationProcessN)r   r   r   Fre   )r.   r   confirmationre   T)r	   rK   9allauth.account.internal.flows.email_verification_by_coderg   initiater*   r   r   did_sendr]   r    send_account_already_exists_mailr   send_confirmation_mailsend_confirmation#add_email_verification_sent_messager
   email_confirmation_sentr9   	__class__)r   r_   re   processrf   rg   r9   ri   r!   r!   r"   rc      sF   
rc   c                 C   s"   |j s
t| |}|S t| |}|S )a$  
    At this point, it has already been confirmed that email verification
    is needed.

    Email verification mails are sent:
    a) Explicitly: when a user signs up
    b) Implicitly: when a user attempts to log in using an unverified
    email while EMAIL_VERIFICATION is mandatory.
    )r   %send_verification_email_at_fake_login%send_verification_email_at_real_login)r   rM   sentr!   r!   r"    send_verification_email_at_login  s
   


rw   c                 C   sN   |j sJ t|}|sdS |jrdS t | ||j}|sdS t| ||jdS )NFrh   )r   rb   r   r   should_send_confirmation_mailre   rc   )r   rM   r_   r9   r!   r!   r"   ru   *  s   
ru   c                 C   s0   |j rJ |js
dS td|jd}t| |ddS )z!
    Enumeration prevention.
    FN)r   r   Trh   )r   r   r   rc   )r   rM   r_   r!   r!   r"   rt   7  s
   
rt   c              	   C   s"   t  | tjd|| |d d S )Nz,account/messages/email_confirmation_sent.txt)r   rM   re   )r   r3   r   INFO)r   r   re   r!   r!   r"   rp   D  s   rp   c                 C   sp   |j rtjr|jtjjkrdS ztj|j	|j }|j
s't| |j dds*W dS W dS W dS  tjy7   Y dS w )z
    Returns whether or not the email verification is *hard* rate limited.
    Hard, meaning, it would be blocking login (verification by code, not link).
    FT)rQ   )r   r	   rK   email_verificationEmailVerificationMethod	MANDATORYr   r   r   r   r   r[   r   )r   rM   r    r!   r!   r"   is_verification_rate_limitedM  s*   r}   c                 C   s    |j st | |}|r|S d S ra   )r   r   rS   )r   r_   	confirmedr!   r!   r"   mark_email_address_as_verifiedd  s
   r   )FF)F)FNF)7typingr   r   django.contribr   django.contrib.auth.base_userr   django.httpr   r   django.urlsr   allauth.accountr	   r
   allauth.account.adapterr   +allauth.account.internal.flows.manage_emailr   r   r   %allauth.account.internal.flows.signupr   allauth.account.modelsr   r   allauth.core.exceptionsr   allauth.core.internalr   allauth.core.internal.httpkitr   allauth.core.ratelimitr   allauth.utilsr   strrW   r#   r(   r   rE   r%   r[   r]   r`   rb   rd   rc   rw   ru   rt   rp   r}   r   r!   r!   r!   r"   <module>   s    


/-



6	