o
    &zh                     @   s   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 d d	lmZ d d
lmZmZ dZdZdededdfddZG dd deZG dd deZG dd deZddededefddZdS )    )Optional)messages)HttpRequest)app_settings)get_adapter)AbstractCodeVerificationProcess)"raise_if_reauthentication_required)stash_login)did_user_login)context	ratelimitphone_verificationaccount_phone_verificationrequestphonereturnNc                 C   s   t  || d S N)r   set_phone_verified)r   userr    r   l/var/www/html/kangema/venv/lib/python3.10/site-packages/allauth/account/internal/flows/phone_verification.pyverify_phone_indirectly      r   c                       s^   e Zd Z fddZedefddZddeddfd	d
ZdeddfddZ	dddZ
  ZS )PhoneVerificationProcessc                    s   t  j||tjtjd d S )N)r   statetimeoutmax_attempts)super__init__r   PHONE_VERIFICATION_TIMEOUTPHONE_VERIFICATION_MAX_ATTEMPTS)selfr   r   	__class__r   r   r      s   
z!PhoneVerificationProcess.__init__r   c                 C   s
   | j d S )Nr   r   r!   r   r   r   r   $   s   
zPhoneVerificationProcess.phoneFskip_enumeration_smsNc                 C   s`   t jtjd| jdd t }| }|| jd< | | t 	tjt
jdd| ji |   d S )Nverify_phoneT)actionkeyraise_exceptioncodez,account/messages/phone_verification_sent.txtr   )r   consumer   r   r   r    generate_phone_verification_coder   send_smsadd_messager   INFOpersist)r!   r&   adapterr+   r   r   r   send(   s"   

zPhoneVerificationProcess.sendc                 C   sn   t  }| jr| jdr*|s(| jds| jdr"|| j d S || j d S |j| j| j| jd d S )Naccount_already_existssignup)r   r+   r   )	r   r   r   getsend_account_already_exists_smsr   send_unknown_account_smssend_verification_code_smsr+   )r!   r&   r2   r   r   r   r.   ;   s   
z!PhoneVerificationProcess.send_smsc                 C   s:   | j d }t }|| j| |tjtjdd|i d S )Nr   z#account/messages/phone_verified.txt)	r   r   r   r   r/   r   r   r   SUCCESS)r!   r   r2   r   r   r   finishJ   s   
zPhoneVerificationProcess.finishF)r   N)__name__
__module____qualname__r   propertystrr   boolr3   r.   r;   __classcell__r   r   r"   r   r      s    r   c                       s   e Zd Z fddZdd Zdd Zedefdd	Zed
e	d  fddZ
deded
dfddZdd Zed
efddZed
efddZ  ZS )PhoneVerificationStageProcessc                    s    || _ t j|jj|jd d S N)r   r   )stager   r   loginr   r   )r!   rF   r"   r   r   r   W   s   z&PhoneVerificationStageProcess.__init__c                 C   s   d S r   r   r%   r   r   r   abort[   s   z#PhoneVerificationStageProcess.abortc                 C   s   t | jj| jj d S r   )r	   rF   r   rG   r%   r   r   r   r1   ^   s   z%PhoneVerificationStageProcess.persistr   c                C   sB   |j | j|jj|d t|d}|jj|j d< |jdd |S )Nr   r   )rF   r5   Fr&   )r   updateinitial_staterG   r   rD   r5   r3   )clsrF   r   processr   r   r   initiatea   s
   
z&PhoneVerificationStageProcess.initiater   c                 C   s   |j }|sd S t|}| S r   )r   rD   abort_if_invalid)rM   rF   r   rN   r   r   r   resumei   s
   z$PhoneVerificationStageProcess.resumer4   Nc                 C   sH   || j d< | j|d t }|s|| j|d | jdd |   d S )Nr4   )r   FrJ   )r   record_changer   	set_phoner   r3   r1   )r!   r   r4   r2   r   r   r   	change_toq   s   
z'PhoneVerificationStageProcess.change_toc                 C   s   |    | jdd d S )NTrJ   )record_resendr3   r%   r   r   r   resendz   s   z$PhoneVerificationStageProcess.resendc                 C   s   |  tj S r   )is_resend_quota_reachedr   #PHONE_VERIFICATION_MAX_RESEND_COUNTr%   r   r   r   
can_resend~   s   z(PhoneVerificationStageProcess.can_resendc                 C   s$   |  tj ot| jot| j S r   )is_change_quota_reachedr   #PHONE_VERIFICATION_MAX_CHANGE_COUNTrB   r   r
   r%   r   r   r   
can_change   s   
z(PhoneVerificationStageProcess.can_change)r=   r>   r?   r   rH   r1   classmethodrA   rO   r   rQ   rB   rT   rV   r@   rY   r\   rC   r   r   r"   r   rD   V   s    	rD   c                       sr   e Zd Zdedef fddZdd Zdd Z fd	d
Ze	dede
fddZe	deded  fddZ  ZS )ChangePhoneVerificationProcessr   r   c                    s   || _ t j|j|d d S rE   )r   r   r   r   )r!   r   r   r"   r   r   r      s
   
z'ChangePhoneVerificationProcess.__init__c                 C   s   | j jtd  d S r   )r   sessionpopPHONE_VERIFICATION_SESSION_KEYr%   r   r   r   rH      s   z$ChangePhoneVerificationProcess.abortc                 C   s   | j | jjt< d S r   )r   r   r_   ra   r%   r   r   r   r1      r   z&ChangePhoneVerificationProcess.persistc                    s   t    | jjtd  d S r   )r   r;   r   r_   r`   ra   r%   r"   r   r   r;      s   
z%ChangePhoneVerificationProcess.finishr   c                 C   s6   t jrt| | j|j|d}t||d}|  |S )NrI   r$   )r   REAUTHENTICATION_REQUIREDr   rL   r   r^   r3   )rM   r   r   r   rN   r   r   r   rO      s   z'ChangePhoneVerificationProcess.initiater   c                 C   s(   |j t}|s
d S t||d}| S )Nr$   )r_   r6   ra   r^   rP   )rM   r   r   rN   r   r   r   rQ      s
   z%ChangePhoneVerificationProcess.resume)r=   r>   r?   r   dictr   rH   r1   r;   r]   rA   rO   r   rQ   rC   r   r   r"   r   r^      s    	 r^   Falways_raisec                 C   sB   t  }||}|o|  p| j|jk}|rtjr|r|d|S )z
    Throws a validation error (if allowed by enumeration prevention rules).
    Otherwise, returns True iff another account already exists.
    phone_taken)r   get_user_by_phonepkr   PREVENT_ENUMERATIONvalidation_error)r   r   rd   r2   
other_useralready_existsr   r   r   phone_already_exists   s   

rl   r<   )typingr   django.contribr   django.httpr   allauth.accountr   allauth.account.adapterr   0allauth.account.internal.flows.code_verificationr   /allauth.account.internal.flows.reauthenticationr   !allauth.account.internal.stagekitr	    allauth.account.internal.userkitr
   allauth.corer   r   PHONE_VERIFICATION_STATE_KEYra   rA   r   r   rD   r^   rB   rl   r   r   r   r   <module>   s"    ;<%