o
    &zhX'                     @   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	m
Z
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mZmZmZmZmZmZmZmZmZmZ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.m/Z/ d dl0m1Z2m+Z3 d dl4m1Z5m+Z6 d dl7m8Z8 dd Z9G dd deZ:G dd deZ;G dd deZ<G dd deZ=G dd deZ>G dd deZ?G d d! d!eZ@G d"d# d#eZAG d$d% d%eZBG d&d' d'eZCG d(d) d)eZDd*S )+    )ValidationError)get_pending_stage)Login)
SignupView)APIResponseAuthenticationResponseConflictResponse)APIViewAuthenticatedAPIViewAuthenticationStageAPIView)ErrorResponse)response)ActivateTOTPInputAddWebAuthnInputAuthenticateInputAuthenticateWebAuthnInputCreateWebAuthnInputDeleteWebAuthnInputGenerateRecoveryCodesInputLoginWebAuthnInputReauthenticateWebAuthnInputSignupWebAuthnInput
TrustInputUpdateWebAuthnInput)DefaultMFAAdapterget_adapter)add)trust_browser)Authenticator)flows)AuthenticateStage
TrustStage)authr   )PasskeySignupStagec              
   C   sF   z	t | j W d S  ty" } zt| d|dW  Y d }~S d }~ww )Ni  )status	exception)r   validate_can_add_authenticatoruserr   r   )requeste r*   U/var/www/html/kangema/venv/lib/python3.10/site-packages/allauth/headless/mfa/views.py_validate_can_add_authenticator+   s   r,   c                   @   s$   e Zd ZeZeZdd Zdd ZdS )AuthenticateViewc                 O      | j   |  S Ninputsaverespond_next_stageselfr(   argskwargsr*   r*   r+   post6      
zAuthenticateView.postc                 C      d| j jjiS Nr'   stageloginr'   r5   r*   r*   r+   get_input_kwargs:      z!AuthenticateView.get_input_kwargsN)	__name__
__module____qualname__r   input_classr    stage_classr8   r@   r*   r*   r*   r+   r-   2   s
    r-   c                   @   s    e Zd ZeZdd Zdd ZdS )ReauthenticateViewc                 O   s   | j   t| jS r/   )r1   r2   r   r(   r4   r*   r*   r+   r8   A   s   

zReauthenticateView.postc                 C      d| j jiS r;   r(   r'   r?   r*   r*   r+   r@   E      z#ReauthenticateView.get_input_kwargsN)rB   rC   rD   r   rE   r8   r@   r*   r*   r*   r+   rG   >   s    rG   c                   @   s   e Zd Zdd ZdS )AuthenticatorsViewc                 O   s   t jj|jd}t||S )N)r'   )r   objectsfilterr'   r   AuthenticatorsResponser5   r(   r6   r7   authenticatorsr*   r*   r+   getJ      zAuthenticatorsView.getN)rB   rC   rD   rQ   r*   r*   r*   r+   rK   I   s    rK   c                   @   sB   e Zd ZdeiZdefddZdd Zdd Zd	d
 Z	dd Z
dS )ManageTOTPViewPOSTreturnc           	      O   sV   |   }|s%t|}|r|S t }tjdd}||j|}t|||S t	||S )NT)
regenerate)
_get_authenticatorr,   r   	totp_authget_totp_secretbuild_totp_urlr'   r   TOTPNotFoundResponseTOTPResponse)	r5   r(   r6   r7   authenticatorerradaptersecrettotp_urlr*   r*   r+   rQ   R   s   zManageTOTPView.getc                 C   s   t jjt jj| jjd S )N)typer'   )r   rL   rM   TypeTOTPr(   r'   firstr?   r*   r*   r+   rW   ^   s
   z!ManageTOTPView._get_authenticatorc                 C   rH   r;   rI   r?   r*   r*   r+   r@   c   rJ   zManageTOTPView.get_input_kwargsc                 O   s   t || jd }t||S )Nr   )
totp_flowsactivate_totpr1   r   r\   r5   r(   r6   r7   r]   r*   r*   r+   r8   f   s   zManageTOTPView.postc                 O   s"   |   }|rt||}t|S r/   )rW   rf   deactivate_totpr   AuthenticatorDeletedResponserh   r*   r*   r+   deletej   s   
zManageTOTPView.deleteN)rB   rC   rD   r   rE   r   rQ   rW   r@   r8   rk   r*   r*   r*   r+   rS   O   s    rS   c                   @   s(   e Zd ZeZdd Zdd Zdd ZdS )ManageRecoveryCodesViewc                 O   s$   t |}|st|S t||S r/   )recovery_codes_flowsview_recovery_codesr   RecoveryCodesNotFoundResponseRecoveryCodesResponserh   r*   r*   r+   rQ   t   s   

zManageRecoveryCodesView.getc                 O   s   t |}t||S r/   )rm   generate_recovery_codesr   rp   rh   r*   r*   r+   r8   z   s   
zManageRecoveryCodesView.postc                 C   rH   r;   rI   r?   r*   r*   r+   r@   ~   rJ   z(ManageRecoveryCodesView.get_input_kwargsN)rB   rC   rD   r   rE   rQ   r8   r@   r*   r*   r*   r+   rl   q   s
    rl   c                       sP   e Zd ZeeedZ fddZdd Zdd Z	dd	 Z
d
d Zdd Z  ZS )ManageWebAuthnView)rT   PUTDELETEc                    s4   |j dv rt|}|r|S t j|g|R i |S )N)GETrT   )methodr,   superhandle)r5   r(   r6   r7   r^   	__class__r*   r+   rx      s
   
zManageWebAuthnView.handlec                 O   s&   d|j v }t||j|}t||S )Npasswordless)ru   webauthn_flowsbegin_registrationr'   r   AddWebAuthnResponse)r5   r(   r6   r7   r{   creation_optionsr*   r*   r+   rQ      s
   
zManageWebAuthnView.getc                 C   rH   r;   rI   r?   r*   r*   r+   r@      rJ   z#ManageWebAuthnView.get_input_kwargsc                 O   s@   t j|| jjd | jjd d\}}t|}tj||d|idS )Nname
credential)r   r   recovery_codes_generated)meta)r|   add_authenticatorr1   cleaned_databoolr   AuthenticatorResponse)r5   r(   r6   r7   r"   rc_authdid_generate_recovery_codesr*   r*   r+   r8      s   


zManageWebAuthnView.postc                 O   s.   | j jd }t||| j jd  t||S )Nidr   )r1   r   r|   rename_authenticatorr   r   rh   r*   r*   r+   put   s
   zManageWebAuthnView.putc                 O   s"   | j jd }t|| t|S )NrP   )r1   r   r|   remove_authenticatorsr   AuthenticatorsDeletedResponserO   r*   r*   r+   rk         
zManageWebAuthnView.delete)rB   rC   rD   r   r   r   rE   rx   rQ   r@   r8   r   rk   __classcell__r*   r*   ry   r+   rr      s    rr   c                   @   s,   e Zd ZdeiZdd Zdd Zdd ZdS )	ReauthenticateWebAuthnViewrT   c                 O   s   t |j}t||S r/   )webauthn_authbegin_authenticationr'   r   WebAuthnRequestOptionsResponser5   r(   r6   r7   request_optionsr*   r*   r+   rQ      s   zReauthenticateWebAuthnView.getc                 C   rH   r;   rI   r?   r*   r*   r+   r@      rJ   z+ReauthenticateWebAuthnView.get_input_kwargsc                 O   s"   | j jd }t|| t| jS )Nr   )r1   r   r|   reauthenticater   r(   rh   r*   r*   r+   r8      r   zReauthenticateWebAuthnView.postN)rB   rC   rD   r   rE   rQ   r@   r8   r*   r*   r*   r+   r      s    r   c                   @   s0   e Zd ZdeiZeZdd Zdd Zdd Z	dS )	AuthenticateWebAuthnViewrT   c                 O   s   t | jjj}t||S r/   )r   r   r=   r>   r'   r   r   r   r*   r*   r+   rQ      rR   zAuthenticateWebAuthnView.getc                 C   r:   r;   r<   r?   r*   r*   r+   r@      rA   z)AuthenticateWebAuthnView.get_input_kwargsc                 O   r.   r/   r0   r4   r*   r*   r+   r8      r9   zAuthenticateWebAuthnView.postN)
rB   rC   rD   r   rE   r    rF   rQ   r@   r8   r*   r*   r*   r+   r      s    r   c                   @   s$   e Zd ZdeiZdd Zdd ZdS )LoginWebAuthnViewrT   c                 O   s   t  }t||S r/   )r   r   r   r   r   r*   r*   r+   rQ      s   zLoginWebAuthnView.getc                 O   s4   | j jd }d }t|j|d}t||| t|S )Nr   )r'   redirect_url)r1   r   r   r'   r|   perform_passwordless_loginr   )r5   r(   r6   r7   r]   r   r>   r*   r*   r+   r8      s
   zLoginWebAuthnView.postN)rB   rC   rD   r   rE   rQ   r8   r*   r*   r*   r+   r      s
    r   c                       sJ   e Zd ZeedZdZdd Zdd Zdd Z	 fd	d
Z
dd Z  ZS )SignupWebAuthnView)rT   rs   Tc                 O   s4   |   }|r|S tj|| jjjddd}t||S )NT)r{   signup)_require_stager|   r}   r=   r>   r'   r   r~   )r5   r(   r6   r7   respr   r*   r*   r+   rQ      s   zSignupWebAuthnView.getc                 C   s"   t | dr| jS t| j| _| jS )Nr=   )hasattrr=   r   r(   r?   r*   r*   r+   _prep_stage   s   
zSignupWebAuthnView._prep_stagec                 C   s*   |    | jr| jjtjkrt| jS d S r/   )r   r=   keyr#   r   r(   r?   r*   r*   r+   r      s   
z!SignupWebAuthnView._require_stagec                    s6   t   }|   | jr| jjdkr| jjj|d< |S )Nrs   r'   )rw   r@   r   r=   r(   rv   r>   r'   )r5   retry   r*   r+   r@     s
   
z#SignupWebAuthnView.get_input_kwargsc                 O   sJ   |   }|r|S tj|| jjj| jjd | jjd d | j  t	|S )Nr   r   )r'   r   r   )
r   r|   signup_authenticatorr=   r>   r'   r1   r   exitr   )r5   r(   r6   r7   r   r*   r*   r+   r     s   


zSignupWebAuthnView.put)rB   rC   rD   r   r   rE   
by_passkeyrQ   r   r   r@   r   r   r*   r*   ry   r+   r      s    	r   c                   @   s   e Zd ZeZeZdd ZdS )	TrustViewc                 O   s.   | j jd }|  }|rt|| jjj| |S )Ntrust)r1   r   r3   r   r=   r>   r'   )r5   r(   r6   r7   r   r   r*   r*   r+   r8     s
   zTrustView.postN)rB   rC   rD   r   rE   r!   rF   r8   r*   r*   r*   r+   r     s    r   N)Edjango.core.exceptionsr   !allauth.account.internal.stagekitr   allauth.account.modelsr   allauth.headless.account.viewsr   allauth.headless.base.responser   r   r   allauth.headless.base.viewsr	   r
   r   *allauth.headless.internal.restkit.responser   allauth.headless.mfar   allauth.headless.mfa.inputsr   r   r   r   r   r   r   r   r   r   r   r   allauth.mfa.adapterr   r   allauth.mfa.internal.flowsr    allauth.mfa.internal.flows.trustr   allauth.mfa.modelsr   #allauth.mfa.recovery_codes.internalr   rm   allauth.mfa.stagesr    r!   allauth.mfa.totp.internalr"   rX   rf   allauth.mfa.webauthn.internalr   r|   allauth.mfa.webauthn.stagesr#   r,   r-   rG   rK   rS   rl   rr   r   r   r   r   r   r*   r*   r*   r+   <module>   s<    8"21