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
mZ d dlmZ d dlmZ edd	d
ZedddZee dddZee dddZdS )    )Optional)messages)get_adapter)"raise_if_reauthentication_required)app_settingssignals)Authenticator)RecoveryCodes)returnc                 C   s   t jj| djt jjd S )Nuser)type)r   objectsfilterexcludeTypeRECOVERY_CODESexistsr    r   M/tmp/pip-unpacked-wheel-upujnpc2/allauth/mfa/recovery_codes/internal/flows.pycan_generate_recovery_codes   s    r   c                 C   st   t |  tjj| jtjjd  t	| j}|j
}t| }|| tjd tjjt| | j|d |d| j |S )Nr   r   )mfa/messages/recovery_codes_generated.txtZsenderrequestr   authenticatorz"mfa/email/recovery_codes_generated)r   r   r   r   r   r   r   deleter	   activateinstanceget_account_adapteradd_messager   SUCCESSr   Zauthenticator_resetsendZsend_notification_mail)r   Zrc_authr   adapterr   r   r   generate_recovery_codes   s*     
  r$   c                 C   s.   t jj| jt jjd }|s"d S t|  |S )Nr   )r   r   r   r   r   r   firstr   )r   r   r   r   r   view_recovery_codes+   s    
r&   c                 C   sv   t jjtjkrdS t jj| jt jjd }|r4dS t	
| j}tjjt | | j|jd t| }|| tjd |jS )zAutomatically (implicitly) setup recovery codes when another
    authenticator is setup for. As this is part of setting up another (primary)
    authenticator, we do not send a notification email in this case.
    Nr   r   r   )r   r   r   r   SUPPORTED_TYPESr   r   r   r   r	   r   r   Zauthenticator_addedr"   r   r   r    r   r!   )r   Zhas_rcrcr#   r   r   r   auto_generate_recovery_codes6   s,     
  r)   N)typingr   Zdjango.contribr   Zallauth.account.adapterr   r   Z/allauth.account.internal.flows.reauthenticationr   Zallauth.mfar   r   Zallauth.mfa.modelsr   Z(allauth.mfa.recovery_codes.internal.authr	   boolr   r$   r&   r)   r   r   r   r   <module>   s   