o
    "4g%                     @   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 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l0m-Z1m(Z2 d dl3m4Z4 dd Z5G dd deZ6G dd deZ7G dd deZ8G dd de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?d'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UpdateWebAuthnInput)DefaultMFAAdapterget_adapter)add)Authenticator)flows)AuthenticateStage)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'   c/var/www/html/authentication-server/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(   post7      
zAuthenticateView.postc                 C      d| j jjiS Nr$   stageloginr$   r2   r'   r'   r(   get_input_kwargs;      z!AuthenticateView.get_input_kwargsN)	__name__
__module____qualname__r   input_classr   stage_classr5   r=   r'   r'   r'   r(   r*   3   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,   )r.   r/   r   r%   r1   r'   r'   r(   r5   B   s   

zReauthenticateView.postc                 C      d| j jiS r8   r%   r$   r<   r'   r'   r(   r=   F      z#ReauthenticateView.get_input_kwargsN)r?   r@   rA   r   rB   r5   r=   r'   r'   r'   r(   rD   ?   s    rD   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r2   r%   r3   r4   authenticatorsr'   r'   r(   getK      zAuthenticatorsView.getN)r?   r@   rA   rN   r'   r'   r'   r(   rH   J   s    rH   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)	r2   r%   r3   r4   authenticatorerradaptersecrettotp_urlr'   r'   r(   rN   S   s   zManageTOTPView.getc                 C   s   t jjt jj| jjd S )N)typer$   )r   rI   rJ   TypeTOTPr%   r$   firstr<   r'   r'   r(   rT   _   s
   z!ManageTOTPView._get_authenticatorc                 C   rE   r8   rF   r<   r'   r'   r(   r=   d   rG   zManageTOTPView.get_input_kwargsc                 O   s   t || jd }t||S )Nr   )
totp_flowsactivate_totpr.   r   rY   r2   r%   r3   r4   rZ   r'   r'   r(   r5   g   s   zManageTOTPView.postc                 O   s"   |   }|rt||}t|S r,   )rT   rc   deactivate_totpr   AuthenticatorDeletedResponsere   r'   r'   r(   deletek   s   
zManageTOTPView.deleteN)r?   r@   rA   r   rB   r   rN   rT   r=   r5   rh   r'   r'   r'   r(   rP   P   s    rP   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RecoveryCodesResponsere   r'   r'   r(   rN   u   s   

zManageRecoveryCodesView.getc                 O   s   t |}t||S r,   )rj   generate_recovery_codesr   rm   re   r'   r'   r(   r5   {   s   
zManageRecoveryCodesView.postc                 C   rE   r8   rF   r<   r'   r'   r(   r=      rG   z(ManageRecoveryCodesView.get_input_kwargsN)r?   r@   rA   r   rB   rN   r5   r=   r'   r'   r'   r(   ri   r   s
    ri   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)rQ   PUTDELETEc                    s4   |j dv rt|}|r|S t j|g|R i |S )N)GETrQ   )methodr)   superhandle)r2   r%   r3   r4   r[   	__class__r'   r(   ru      s
   
zManageWebAuthnView.handlec                 O   s&   d|j v }t||j|}t||S )Npasswordless)rr   webauthn_flowsbegin_registrationr$   r   AddWebAuthnResponse)r2   r%   r3   r4   rx   creation_optionsr'   r'   r(   rN      s
   
zManageWebAuthnView.getc                 C   rE   r8   rF   r<   r'   r'   r(   r=      rG   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)ry   add_authenticatorr.   cleaned_databoolr   AuthenticatorResponse)r2   r%   r3   r4   r   rc_authdid_generate_recovery_codesr'   r'   r(   r5      s   


zManageWebAuthnView.postc                 O   s.   | j jd }t||| j jd  t||S )Nidr}   )r.   r   ry   rename_authenticatorr   r   re   r'   r'   r(   put   s
   zManageWebAuthnView.putc                 O   s"   | j jd }t|| t|S )NrM   )r.   r   ry   remove_authenticatorsr   AuthenticatorsDeletedResponserL   r'   r'   r(   rh         
zManageWebAuthnView.delete)r?   r@   rA   r   r   r   rB   ru   rN   r=   r5   r   rh   __classcell__r'   r'   rv   r(   ro      s    ro   c                   @   s,   e Zd ZdeiZdd Zdd Zdd ZdS )	ReauthenticateWebAuthnViewrQ   c                 O   s   t |j}t||S r,   )webauthn_authbegin_authenticationr$   r   WebAuthnRequestOptionsResponser2   r%   r3   r4   request_optionsr'   r'   r(   rN      s   zReauthenticateWebAuthnView.getc                 C   rE   r8   rF   r<   r'   r'   r(   r=      rG   z+ReauthenticateWebAuthnView.get_input_kwargsc                 O   s"   | j jd }t|| t| jS )Nr~   )r.   r   ry   reauthenticater   r%   re   r'   r'   r(   r5      r   zReauthenticateWebAuthnView.postN)r?   r@   rA   r   rB   rN   r=   r5   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 )	AuthenticateWebAuthnViewrQ   c                 O   s   t | jjj}t||S r,   )r   r   r:   r;   r$   r   r   r   r'   r'   r(   rN      rO   zAuthenticateWebAuthnView.getc                 C   r7   r8   r9   r<   r'   r'   r(   r=      r>   z)AuthenticateWebAuthnView.get_input_kwargsc                 O   r+   r,   r-   r1   r'   r'   r(   r5      r6   zAuthenticateWebAuthnView.postN)
r?   r@   rA   r   rB   r   rC   rN   r=   r5   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 )LoginWebAuthnViewrQ   c                 O   s   t  }t||S r,   )r   r   r   r   r   r'   r'   r(   rN      s   zLoginWebAuthnView.getc                 O   s4   | j jd }d }t|j|d}t||| t|S )Nr~   )r$   redirect_url)r.   r   r   r$   ry   perform_passwordless_loginr   )r2   r%   r3   r4   rZ   r   r;   r'   r'   r(   r5      s
   zLoginWebAuthnView.postN)r?   r@   rA   r   rB   rN   r5   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)rQ   rp   Tc                 O   s4   |   }|r|S tj|| jjjddd}t||S )NT)rx   signup)_require_stagery   rz   r:   r;   r$   r   r{   )r2   r%   r3   r4   respr|   r'   r'   r(   rN      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 )Nrp   r$   )rt   r=   r   r:   r%   rs   r;   r$   )r2   retrv   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   ry   signup_authenticatorr:   r;   r$   r.   r   exitr   )r2   r%   r3   r4   r   r'   r'   r(   r     s   


zSignupWebAuthnView.put)r?   r@   rA   r   r   rB   
by_passkeyrN   r   r   r=   r   r   r'   r'   rv   r(   r      s    	r   N)@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   allauth.mfa.adapterr   r   allauth.mfa.internal.flowsr   allauth.mfa.modelsr   #allauth.mfa.recovery_codes.internalr   rj   allauth.mfa.stagesr   allauth.mfa.totp.internalr   rU   rc   allauth.mfa.webauthn.internalr   ry   allauth.mfa.webauthn.stagesr    r)   r*   rD   rH   rP   ri   ro   r   r   r   r   r'   r'   r'   r(   <module>   s8    4"2