U
    >i                     @   s  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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 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*m+Z+m,Z, d dl-m.Z.m/Z/ d dl0m1Z1 ee$ddeeddG dd deZ2e23 Z4eeddG dd deZ5e53 Z6eeddG dd deeZ7e73 Z8G dd deeZ9e93 Z:eeddG dd de"Z;e;3 Z<eeddG d d! d!eeZ=e=3 Z>eee1j?d"d#ddG d$d% d%eZ@e@3 ZAd&S )'    )messages)login_required)HttpResponseRedirectJsonResponse)reversereverse_lazy)method_decorator)
DeleteViewFormView
UpdateView)ListView)app_settings)get_adapter)reauthentication_required)login_stage_required)NextRedirectMixinRedirectAuthenticatedUserMixin)Login)BaseReauthenticateView)redirect_if_add_not_allowed)Authenticator)AddWebAuthnFormEditWebAuthnFormLoginWebAuthnFormReauthenticateWebAuthnFormSignupWebAuthnForm)authflows)PasskeySignupStagedispatch)namec                       sJ   e Zd ZeZdej Z fddZ fddZ	dd Z
 fdd	Z  ZS )
AddWebAuthnViewzmfa/webauthn/add_form.c                    s*   t   }t| jjd}d|i|d< |S )NFcreation_optionsjs_data)superget_context_datar   begin_registrationrequestuserselfkwargsretr"   	__class__ >/tmp/pip-unpacked-wheel-upujnpc2/allauth/mfa/webauthn/views.pyr%   #   s    
z AddWebAuthnView.get_context_datac                    s   t   }| jj|d< |S Nr(   r$   get_form_kwargsr'   r(   r*   r,   r-   r/   r0   r3   )   s    
zAddWebAuthnView.get_form_kwargsc                 C   s   | j rtdS tdS )NZmfa_view_recovery_codesZ	mfa_index)did_generate_recovery_codesr   r*   r/   r/   r0   get_success_url.   s    zAddWebAuthnView.get_success_urlc                    s8   t j| j|jd |jd d\}}t|| _t |S )Nr    
credential)r    r8   )r   Zadd_authenticatorr'   cleaned_databoolr5   r$   
form_valid)r*   formr   Zrc_authr-   r/   r0   r;   3   s    

zAddWebAuthnView.form_valid)__name__
__module____qualname__r   
form_classaccount_settingsTEMPLATE_EXTENSIONtemplate_namer%   r3   r7   r;   __classcell__r/   r/   r-   r0   r!      s   
r!   c                   @   s"   e Zd Zdej ZdZdd ZdS )ListWebAuthnViewz mfa/webauthn/authenticator_list.authenticatorsc                 C   s   t jj| jjt jjdS N)r(   typer   Zobjectsfilterr'   r(   TypeZWEBAUTHNr6   r/   r/   r0   get_querysetG   s     zListWebAuthnView.get_querysetN)r=   r>   r?   rA   rB   rC   Zcontext_object_namerL   r/   r/   r/   r0   rE   @   s   rE   c                   @   s8   e Zd ZU eed< dej ZedZ	dd Z
dd ZdS )	RemoveWebAuthnViewobjectz*mfa/webauthn/authenticator_confirm_delete.mfa_list_webauthnc                 C   s   t jj| jjt jjdS rG   rI   r6   r/   r/   r0   rL   Y   s     zRemoveWebAuthnView.get_querysetc                 C   s"   |   }t| j| t|  S )N)Z
get_objectr   Zremove_authenticatorr'   r   r7   r*   r<   authenticatorr/   r/   r0   r;   ^   s    zRemoveWebAuthnView.form_validN)r=   r>   r?   r   __annotations__rA   rB   rC   r   success_urlrL   r;   r/   r/   r/   r0   rM   P   s   
rM   c                   @   s(   e Zd ZeZdd Zdd Zdd ZdS )LoginWebAuthnViewc                 O   s4   t  |r(tjd d}d|i}t|S ttdS )N)r(   request_optionsaccount_login)get_account_adapterZis_ajaxr   begin_authenticationr   r   r   )r*   r'   argsr+   rU   datar/   r/   r0   getj   s
    zLoginWebAuthnView.getc                 C   s6   |j dg D ]}t j| jtj|d qttdS Nr8   )messagerV   	errorsr[   rW   Zadd_messager'   r   ERRORr   r   r*   r<   r]   r/   r/   r0   form_invalidq   s      zLoginWebAuthnView.form_invalidc                 C   s,   |j d }d }t|j|d}t| j||S )Nr8   )r(   redirect_url)r9   r   r(   r   Zperform_passwordless_loginr'   )r*   r<   rQ   rc   loginr/   r/   r0   r;   x   s    
zLoginWebAuthnView.form_validN)r=   r>   r?   r   r@   r[   rb   r;   r/   r/   r/   r0   rT   g   s   rT   c                       sJ   e Zd ZeZdej Z fddZdd Z	 fddZ
 fdd	Z  ZS )
ReauthenticateWebAuthnViewzmfa/webauthn/reauthenticate.c                    s   t   }| jj|d< |S r1   r2   r4   r-   r/   r0   r3      s    
z*ReauthenticateWebAuthnView.get_form_kwargsc                 C   s6   |j dg D ]}t j| jtj|d qttdS r\   r^   ra   r/   r/   r0   rb      s      z'ReauthenticateWebAuthnView.form_invalidc                    s$   |j d }t| j| t |S )Nr8   )r9   r   Zreauthenticater'   r$   r;   rP   r-   r/   r0   r;      s    
z%ReauthenticateWebAuthnView.form_validc                    s(   t   }t| jj}d|i|d< |S )NrU   r#   )r$   r%   r   rX   r'   r(   )r*   r+   r,   rU   r-   r/   r0   r%      s    
z+ReauthenticateWebAuthnView.get_context_data)r=   r>   r?   r   r@   rA   rB   rC   r3   rb   r;   r%   rD   r/   r/   r-   r0   re      s   
re   c                   @   s*   e Zd ZeZdej ZedZ	dd Z
dS )EditWebAuthnViewzmfa/webauthn/edit_form.rO   c                 C   s   t jj| jjt jjdS rG   rI   r6   r/   r/   r0   rL      s     zEditWebAuthnView.get_querysetN)r=   r>   r?   r   r@   rA   rB   rC   r   rS   rL   r/   r/   r/   r0   rf      s   
rf   Zaccount_signup)ZstageZredirect_urlnamec                       s>   e Zd ZeZdej Z fddZ fddZ	dd Z
  ZS )SignupWebAuthnViewzmfa/webauthn/signup_form.c                    s.   t   }t| jjjjd}d|i|d< |S )NTr"   r#   )r$   r%   r   r&   r'   _login_stagerd   r(   r)   r-   r/   r0   r%      s    

 z#SignupWebAuthnView.get_context_datac                    s   t   }| jjjj|d< |S r1   )r$   r3   r'   rh   rd   r(   r4   r-   r/   r0   r3      s    
z"SignupWebAuthnView.get_form_kwargsc                 C   s4   t j| j| jjjj|jd |jd d | jj S )Nr    r8   )r(   r    r8   )r   Zsignup_authenticatorr'   rh   rd   r(   r9   exit)r*   r<   r/   r/   r0   r;      s    
zSignupWebAuthnView.form_valid)r=   r>   r?   r   r@   rA   rB   rC   r%   r3   r;   rD   r/   r/   r-   r0   rg      s
   
rg   N)BZdjango.contribr   Zdjango.contrib.auth.decoratorsr   Zdjango.httpr   r   Zdjango.urlsr   r   Zdjango.utils.decoratorsr   Zdjango.views.generic.editr	   r
   r   Zdjango.views.generic.listr   Zallauth.accountr   rA   Zallauth.account.adapterr   rW   Zallauth.account.decoratorsr   Z#allauth.account.internal.decoratorsr   Zallauth.account.mixinsr   r   Zallauth.account.modelsr   Zallauth.account.viewsr   Zallauth.mfa.internal.flows.addr   Zallauth.mfa.modelsr   Zallauth.mfa.webauthn.formsr   r   r   r   r   Zallauth.mfa.webauthn.internalr   r   Zallauth.mfa.webauthn.stagesr   r!   Zas_viewZadd_webauthnrE   Zlist_webauthnrM   Zremove_webauthnrT   Zlogin_webauthnre   Zreauthenticate_webauthnrf   Zedit_webauthnkeyrg   Zsignup_webauthnr/   r/   r/   r0   <module>   sZ   





 