
    3i                     ~    d Z ddl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 erdd
lmZ dZ G d de      Zy)z,Caching SHA2 Password Authentication Plugin.    N)sha256)TYPE_CHECKINGAnyOptional   )InterfaceError)logger   )MySQLAuthPlugin)MySQLSocket"MySQLCachingSHA2PasswordAuthPluginc                       e Zd ZU dZdZeed<   dedefdZe	de
fd       Ze	defd       Zded	edee   fd
Zddded	edefdZddded	edefdZy)r   zClass implementing the MySQL caching_sha2_password authentication plugin

    Note that encrypting using RSA is not supported since the Python
    Standard Library does not provide this OpenSSL functionality.
       perform_full_authentication	auth_datareturnc                    |st        d      | j                  syt        | j                  j                               j	                         }t               }|j                  t        |      j	                                |j                  |       |j	                         }t        ||      D cg c]
  \  }}||z   }}}t        j                  dg| }|S c c}}w )zReturn a scramble of the password using a Nonce sent by the
        server.

        The scramble is of the form:
        XOR(SHA2(password), SHA2(SHA2(SHA2(password)), Nonce))
        z"Missing authentication data (seed)    32B)	r   	_passwordr   encodedigestupdatezipstructpack)	selfr   hash1hash2hash2_digesth1h2xoredhash3s	            RD:\jyotish\venv\Lib\site-packages\mysql/connector/plugins/caching_sha2_password.py	_scramblez,MySQLCachingSHA2PasswordAuthPlugin._scramble7   s      !EFF~~t~~,,./668VE]))+,Y||~),UL)AB)AXb"b)ABE*E* Cs   .Cc                      y)zPlugin official name.caching_sha2_password r   s    r%   namez'MySQLCachingSHA2PasswordAuthPlugin.nameM   s     'r   c                      y)z'Signals whether or not SSL is required.Fr)   r*   s    r%   requires_sslz/MySQLCachingSHA2PasswordAuthPlugin.requires_sslR   s     r   kwargsc                     |syt        |      dkD  r| j                  |      S |d   | j                  k(  r| j                  j	                         dz   S y)a  Make the client's authorization response.

        Args:
            auth_data: Authorization data.
            kwargs: Custom configuration to be passed to the auth plugin
                    when invoked. The parameters defined here will override the ones
                    defined in the auth plugin itself.

        Returns:
            packet: Client's authorization response.
        Nr
   r       )lenr&   r   r   r   )r   r   r.   s      r%   auth_responsez0MySQLCachingSHA2PasswordAuthPlugin.auth_responseW   sR     y>A>>),,Q<4;;;>>((*W44r   sockr   c                      | j                   |fi |}|r|j                  |       t        |j                               S )aE  Handles server's `auth more data` response.

        Args:
            sock: Pointer to the socket connection.
            auth_data: Authentication method data (from a packet representing
                       an `auth more data` response).
            kwargs: Custom configuration to be passed to the auth plugin
                    when invoked. The parameters defined here will override the ones
                    defined in the auth plugin itself.

        Returns:
            packet: Last server's response after back-and-forth
                    communication.
        )r2   sendbytesrecv)r   r3   r   r.   responses        r%   auth_more_responsez5MySQLCachingSHA2PasswordAuthPlugin.auth_more_responsem   s;    " &4%%i:6:IIhTYY[!!r   c                     | j                   |fi |}|t        d      t        j                  d|t	        |             |j                  |       t        |j                               }t        j                  d|       |S )aS  Handles server's `auth switch request` response.

        Args:
            sock: Pointer to the socket connection.
            auth_data: Plugin provided data (extracted from a packet
                       representing an `auth switch request` response).
            kwargs: Custom configuration to be passed to the auth plugin
                    when invoked. The parameters defined here will override the ones
                    defined in the auth plugin itself.

        Returns:
            packet: Last server's response after back-and-forth
                    communication.
        zGot a NULL auth responsez# request: %s size: %sz# server response packet: %s)r2   r   r	   debugr1   r5   r6   r7   )r   r3   r   r.   r8   pkts         r%   auth_switch_responsez7MySQLCachingSHA2PasswordAuthPlugin.auth_switch_response   sr    " &4%%i:6: !;<<-xXG		(DIIK 3S9
r   N)__name__
__module____qualname____doc__r   int__annotations__r6   r&   propertystrr+   boolr-   r   r   r2   r9   r=   r)   r   r%   r   r   .   s     ()(5 U , 'c ' ' d  u   ,"!".3"?B"	".!.3?B	r   )rA   r   hashlibr   typingr   r   r   errorsr   r	    r   networkr   AUTHENTICATION_PLUGIN_CLASSr   r)   r   r%   <module>rM      s9   : 3   / / #  %B q qr   