
    BR<i                         d Z ddlZddlZddlmZ ddlmZ dededed	efd
Z	 G d d      Z
 G d de
      Z G d de
      Z G d de
      Zy)z.Implementation of MySQL Authentication Plugin.    N)Optional   )hexlifyhash1hash2	hash_sizereturnc                     t        | |      D cg c]
  \  }}||z   }}}t        j                  | dg| S c c}}w )zEncrypt/Decrypt function used for password encryption in
    authentication, using a simple XOR.

    Args:
        hash1 (str): The first hash.
        hash2 (str): The second hash.

    Returns:
        str: A string with the xor applied.
    B)zipstructpack)r   r   r   h1h2xoreds         V/var/www/html/delta-backend/venv/lib/python3.12/site-packages/mysqlx/authentication.py
xor_stringr   '   sE     &)%67"bR"W7E7;;)A/// 8s   ;c                   F    e Zd ZdZd	dee   dee   fdZdefdZdefdZy)
BaseAuthPluginz7Base class for implementing the authentication plugins.Nusernamepasswordc                      || _         || _        y )N	_username	_password)selfr   r   s      r   __init__zBaseAuthPlugin.__init__9   s    (0(0    r	   c                     t         )UReturns the plugin name.

        Returns:
            str: The plugin name.
        NotImplementedErrorr   s    r   namezBaseAuthPlugin.name=   
     "!r   c                     t         )eReturns the authentication name.

        Returns:
            str: The authentication name.
        r!   r#   s    r   	auth_namezBaseAuthPlugin.auth_nameE   r%   r   )NN)	__name__
__module____qualname____doc__r   strr   r$   r(    r   r   r   r   6   s8    A1# 1# 1"c ""3 "r   r   c                   8    e Zd ZdZdefdZdefdZdedefdZy)MySQL41AuthPluginzCClass implementing the MySQL Native Password authentication plugin.r	   c                      y)r    zMySQL 4.1 Authentication Pluginr.   r#   s    r   r$   zMySQL41AuthPlugin.nameQ   s     1r   c                      y)r'   MYSQL41r.   r#   s    r   r(   zMySQL41AuthPlugin.auth_nameY   s     r   datac                    | j                   rt        | j                   t              r| j                   j                  d      n| j                   }t	        j
                  |      j                         }t	        j
                  |      j                         }t        |t	        j
                  ||z         j                         d      }d| j                   dt        |       dS d| j                   dS )zHashing for MySQL 4.1 authentication.

        Args:
            data (bytes): The authentication data.

        Returns:
            str: The authentication response.
        utf-8    z *)
r   
isinstancer-   encodehashlibsha1digestr   r   r   r   r4   r   r   r   r   s         r   	auth_datazMySQL41AuthPlugin.auth_dataa   s     >> dnnc2 %%g.^^ 
 LL*113ELL'..0Eugll4%<&@&G&G&I2NE's75>*:"==DNN#2&&r   N	r)   r*   r+   r,   r-   r$   r(   bytesr?   r.   r   r   r0   r0   N   s0    M1c 13 'e ' 'r   r0   c                   4    e Zd ZdZdefdZdefdZdefdZy)PlainAuthPluginz9Class implementing the MySQL Plain authentication plugin.r	   c                      y)r    zPlain Authentication Pluginr.   r#   s    r   r$   zPlainAuthPlugin.namez   s     -r   c                      y)r'   PLAINr.   r#   s    r   r(   zPlainAuthPlugin.auth_name   s     r   c                 :    d| j                    d| j                   S )zeReturns the authentication data.

        Returns:
            str: The authentication data.
        r8   r   r#   s    r   r?   zPlainAuthPlugin.auth_data   s!     DNN#2dnn%566r   N)r)   r*   r+   r,   r-   r$   r(   r?   r.   r   r   rC   rC   w   s)    C-c -3 73 7r   rC   c                   8    e Zd ZdZdefdZdefdZdedefdZy)Sha256MemoryAuthPluginz;Class implementing the SHA256_MEMORY authentication plugin.r	   c                      y)r    z#SHA256_MEMORY Authentication Pluginr.   r#   s    r   r$   zSha256MemoryAuthPlugin.name   s     5r   c                      y)r'   SHA256_MEMORYr.   r#   s    r   r(   z Sha256MemoryAuthPlugin.auth_name   s     r   r4   c                    t        | j                  t              r| j                  j                  d      n| j                  }t	        j
                  |      j                         }t	        j
                  t	        j
                  |      j                         |z         j                         }t        ||d      }d| j                   dt        |       S )a"  Hashing for SHA256_MEMORY authentication.

        The scramble is of the form:
            SHA256(SHA256(SHA256(PASSWORD)),NONCE) XOR SHA256(PASSWORD)

        Args:
            data (bytes): The authentication data.

        Returns:
            str: The authentication response.
        r6       r8   )
r9   r   r-   r:   r;   sha256r=   r   r   r   r>   s         r   r?   z Sha256MemoryAuthPlugin.auth_data   s     $..#. NN!!'* 	
 x(//1w~~e4;;=DELLN5%,DNN#2gen%566r   Nr@   r.   r   r   rI   rI      s0    E5c 53 7e 7 7r   rI   )r,   r;   r   typingr   helpersr   rA   intr   r   r0   rC   rI   r.   r   r   <module>rS      sf   : 5    0e 0E 0c 0e 0" "0&' &'R7n 78'7^ '7r   