
    g                         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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  G d de      Z de fdZy)    )BytesIO)Dict)quote)	urlencode)gettextgettext_lazy)app_settings)get_adapter)user_display
user_emailuser_pk_to_url_struser_username)context)BaseAdapter)Authenticator)import_attributec                   8   e Zd ZdZ ed       ed       ed       ed       ed      dZ	 defd	Zdefd
ZdefdZ	dedefdZ
dedefdZdefdZdedefdZdedefdZdedefdZd ZddefdZdej(                  defdZdeeef   fdZdefdZy)DefaultMFAAdaptera4  The adapter class allows you to override various functionality of the
    ``allauth.mfa`` app.  To do so, point ``settings.MFA_ADAPTER`` to your own
    class that derives from ``DefaultMFAAdapter`` and override the behavior by
    altering the implementation of the methods according to your own needs.
    zUYou cannot add an email address to an account protected by two-factor authentication.z0You cannot deactivate two-factor authentication.zTYou cannot generate recovery codes without having two-factor authentication enabled.zIncorrect code.zYYou cannot activate two-factor authentication until you have verified your email address.)add_email_blockedcannot_delete_authenticatorcannot_generate_recovery_codesincorrect_codeunverified_emailreturnc                 $    | j                  |      S )zZReturns the label used for representing the given user in a TOTP QR
        code.
        )_get_user_identifierselfusers     P/var/www/django_project/virt/lib/python3.12/site-packages/allauth/mfa/adapter.pyget_totp_labelz DefaultMFAAdapter.get_totp_label/   s     ((..    c                 P    t        |      }|st        |      }|st        |      }|S )z`Human-palatable identifier for a user account. It is intended only
        for display.
        )r   r   str)r   r   labels      r    r   z&DefaultMFAAdapter._get_user_identifier5   s,     4 !$'EIEr"   c                 J    t         j                  }|s| j                         }|S )zYReturns the TOTP issuer name that will be contained in the TOTP QR
        code.
        )r	   TOTP_ISSUER_get_site_name)r   issuers     r    get_totp_issuerz!DefaultMFAAdapter.get_totp_issuer@   s%     ))((*Fr"   secretc                    | j                  |      }| j                         }||d}t        j                  dk7  rt        j                  |d<   t        j                  dk7  rt        j                  |d<   dt        |       dt        |       S )N)r+   r)      digits   periodzotpauth://totp/?)r!   r*   r	   TOTP_DIGITSTOTP_PERIODr   r   )r   r   r+   r%   r)   paramss         r    build_totp_urlz DefaultMFAAdapter.build_totp_urlI   s    ##D)%%' 	
 ##q(+77F8##r)+77F8 ua	&0A/BCCr"   urlc                     dd l }ddlm} |j                  ||      }t	               }|j                  |       |j                         j                  d      S )Nr   )SvgPathImage)image_factoryutf8)qrcodeqrcode.image.svgr8   maker   savegetvaluedecode)r   r6   r;   r8   imgbufs         r    build_totp_svgz DefaultMFAAdapter.build_totp_svgX   sC    1kk#\k:i||~$$V,,r"   c                     t         j                  r9ddlm} |j                  j                  t        j                        j                  S t        j                  j                         S )Nr   )Site)
allauth_settingsSITES_ENABLEDdjango.contrib.sites.modelsrE   objectsget_currentr   requestnameget_host)r   rE   s     r    r(   z DefaultMFAAdapter._get_site_namea   s@    ))8<<++GOO<AAA??++--r"   textc                     |S )zSecrets such as the TOTP key are stored in the database.  This
        hook can be used to encrypt those so that they are not stored in the
        clear in the database.
         )r   rN   s     r    encryptzDefaultMFAAdapter.encrypti   s	    
 r"   encrypted_textc                 
    |}|S )zCounter part of ``encrypt()``.rP   )r   rR   rN   s      r    decryptzDefaultMFAAdapter.decryptp   s    r"   authenticatorc                      y)NTrP   )r   rU   s     r    can_delete_authenticatorz*DefaultMFAAdapter.can_delete_authenticatoru   s    r"   c                 6     t               j                  |i |S N)get_account_adaptersend_notification_mail)r   argskwargss      r    r[   z(DefaultMFAAdapter.send_notification_mailx   s    ;"$;;TLVLLr"   Nc                     |j                   ryt        j                  j                  |      }||j                  |      }|j	                         S )zM
        Returns ``True`` if (and only if) the user has 2FA enabled.
        F)r   )type__in)is_anonymousr   rI   filterexists)r   r   typesqss       r    is_mfa_enabledz DefaultMFAAdapter.is_mfa_enabled{   sK     ""))t)4E*Byy{r"   typec                     t         j                  j                  ||      j                         }|dk(  rt	        d      S |dk(  rt	        d      S t	        d      j                  |dz         S )zi
        Generate a human friendly name for the key. Used to prefill the "Add
        key" form.
        )r   rf   r   z
Master key   z
Backup keyzKey nr. {number})number)r   rI   ra   countr   format)r   r   rf   ns       r    generate_authenticator_namez-DefaultMFAAdapter.generate_authenticator_name   sj    
 !!((d(>DDF6<((!V<(()*11Q1??r"   c                     | j                         }t        j                  j                         j	                  d      d   |dS )N:r   )idrL   )r(   r   rK   rM   	partition)r   rL   s     r    #get_public_key_credential_rp_entityz5DefaultMFAAdapter.get_public_key_credential_rp_entity   s=    ""$//**,66s;A>
 	
r"   c                 n    t        |      j                  d      t        |      | j                  |      dS )Nr:   )rp   display_namerL   )r   encoder   r   r   s     r    %get_public_key_credential_user_entityz7DefaultMFAAdapter.get_public_key_credential_user_entity   s5    $T*11&9(.--d3
 	
r"   rY   )__name__
__module____qualname____doc___error_messagesr$   r!   r   r*   r5   rC   r(   rQ   rT   r   boolrW   r[   re   Typerm   r   rr   dictrv   rP   r"   r    r   r      s1    c
 ()>(
 +,b+
 -.g
N F/c /	C 	 D3 D3 D-# -# -. .C C c c 
m  M	$ 	
@m6H6H 
@S 
@
T#s(^ 

T 
r"   r   r   c                  >     t        t        j                               S rY   )r   r	   ADAPTERrP   r"   r    r
   r
      s    1L00133r"   N)!ior   typingr   urllib.parser   django.utils.httpr   django.utils.translationr   r   r{   allauthr	   rF   allauth.account.adapterr
   rZ   allauth.account.utilsr   r   r   r   allauth.corer   allauth.core.internal.adapterr   allauth.mfaallauth.mfa.modelsr   allauth.utilsr   r   rP   r"   r    <module>r      sQ       ' ? 4 F  ! 5 $ , *G
 G
T4& 4r"   