U
    >i                     @   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mZ d dlmZ d dlmZmZ G d	d
 d
ejZG dd deZG dd deZG dd dejZG dd deZG dd deZG dd dejZdS )    )forms)gettext_lazy)context)app_settings)get_adapter)check_rate_limitpost_authentication)Authenticator)authflowsc                       sL   e Zd ZejddZejdejdZ fddZ	dd Z
 fd	d
Z  ZS )_BaseAddWebAuthnFormFrequiredTr   Zwidgetc                    sF   | d| _|di }|dt | jtjj t j	|| d S )Nuserinitialname)
popr   
setdefaultr   generate_authenticator_namer	   TypeWEBAUTHNsuper__init__selfargskwargsr   	__class__ >/tmp/pip-unpacked-wheel-upujnpc2/allauth/mfa/webauthn/forms.pyr      s     z_BaseAddWebAuthnForm.__init__c                 C   s&   | j d }|s"t | jtjj}|S )a+  
        We don't want to make `name` a required field, as the WebAuthn
        ceremony happens before posting the resulting credential, and we don't
        want to reject a valid credential because of a missing name -- it might
        be resident already. So, gracefully plug in a name.
        r   )cleaned_datar   r   r   r	   r   r   )r   r   r    r    r!   
clean_name   s    
 z_BaseAddWebAuthnForm.clean_namec                    s0   t   }|d}|r,t| t| |S N
credential)r   cleangetr
   Zparse_registration_responseZcomplete_registration)r   r"   r%   r   r    r!   r&   )   s    



z_BaseAddWebAuthnForm.clean)__name__
__module____qualname__r   	CharFieldr   	JSONFieldHiddenInputr%   r   r#   r&   __classcell__r    r    r   r!   r      s
   r   c                   @   s*   e Zd Zejr&ejeddeddZdS )AddWebAuthnFormZPasswordlessFzEnabling passwordless operation allows you to sign in using just this key, but imposes additional requirements such as biometrics or PIN protection.)labelr   	help_textN)	r(   r)   r*   r   ZPASSKEY_LOGIN_ENABLEDr   ZBooleanField_passwordlessr    r    r    r!   r/   5   s   r/   c                   @   s   e Zd ZdS )SignupWebAuthnFormN)r(   r)   r*   r    r    r    r!   r4   @   s   r4   c                       sD   e Zd ZejdejdZdZdZ fddZ	dd Z
dd	 Z  ZS )
AuthenticateWebAuthnFormTr   Fc                    s   | d| _t j|| d S Nr   )r   r   r   r   r   r   r   r   r    r!   r   I   s    z!AuthenticateWebAuthnForm.__init__c                 C   sJ   | j d }t| | j}|d kr,t|}t|}t||}|  |S r$   )r"   r
   Zparse_authentication_responser   Zextract_user_from_responser   Zcomplete_authentication)r   r%   r   Zclear_rlauthenticatorr    r    r!   clean_credentialM   s    


z)AuthenticateWebAuthnForm.clean_credentialc                 C   s$   | j d }ttj|| j| jd d S )Nr%   )reauthenticatedr3   )r"   r   r   requestr:   r3   )r   r8   r    r    r!   save[   s    
zAuthenticateWebAuthnForm.save)r(   r)   r*   r   r,   r-   r%   r:   r3   r   r9   r<   r.   r    r    r   r!   r5   D   s   r5   c                       s$   e Zd ZdZdZ fddZ  ZS )LoginWebAuthnFormFTc                    s   t  j|dd i| d S r6   )r   r   r7   r   r    r!   r   i   s    zLoginWebAuthnForm.__init__)r(   r)   r*   r:   r3   r   r.   r    r    r   r!   r=   e   s   r=   c                   @   s   e Zd ZdZdZdS )ReauthenticateWebAuthnFormTFN)r(   r)   r*   r:   r3   r    r    r    r!   r>   m   s   r>   c                       s6   e Zd ZejddZ fddZedddZ  Z	S )EditWebAuthnFormTr   c                    s>   | d| _|di }|d| j j t j|| d S )Ninstancer   r   )r   r@   r   wrapr   r   r   r   r   r    r!   r   u   s    zEditWebAuthnForm.__init__)returnc                 C   s   t tj| j| jd  | jS )Nr   )r   Zrename_authenticatorr   r;   r@   r"   )r   r    r    r!   r<   {   s      zEditWebAuthnForm.save)
r(   r)   r*   r   r+   r   r   r	   r<   r.   r    r    r   r!   r?   r   s   r?   N)Zdjangor   Zdjango.utils.translationr   r2   Zallauth.corer   Zallauth.mfar   Zallauth.mfa.adapterr   Zallauth.mfa.base.internal.flowsr   r   Zallauth.mfa.modelsr	   Zallauth.mfa.webauthn.internalr
   r   ZFormr   r/   r4   r5   r=   r>   r?   r    r    r    r!   <module>   s   )!