o
    –¨Êhk  ã                   @   st   d Z ddlZddlZddlmZmZmZ dd„ ZG dd„ deƒZ	G d	d
„ d
e	ƒZ
G dd„ de	ƒZG dd„ de	ƒZdS )z.Implementation of MySQL Authentication Plugin.é    Né   )ÚPY3ÚUNICODE_TYPESÚhexlifyc                 C   sF   t rdd„ t| |ƒD ƒ}n
dd„ t| |ƒD ƒ}tjd |¡g|¢R Ž S )zõEncrypt/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.
    c                 S   s   g | ]\}}||A ‘qS © r   ©Ú.0Úh1Úh2r   r   úG/var/www/html/env/lib/python3.10/site-packages/mysqlx/authentication.pyÚ
<listcomp>1   s    zxor_string.<locals>.<listcomp>c                 S   s    g | ]\}}t |ƒt |ƒA ‘qS r   )Úordr   r   r   r   r   3   s     z{0}B)r   ÚzipÚstructÚpackÚformat)Úhash1Úhash2Ú	hash_sizeÚxoredr   r   r   Ú
xor_string%   s   r   c                   @   s*   e Zd ZdZd	dd„Zdd„ Zdd„ ZdS )
ÚBaseAuthPluginz7Base class for implementing the authentication plugins.Nc                 C   s   || _ || _d S )N)Ú	_usernameÚ	_password)ÚselfÚusernameÚpasswordr   r   r   Ú__init__9   s   
zBaseAuthPlugin.__init__c                 C   ó   t ‚)úUReturns the plugin name.

        Returns:
            str: The plugin name.
        ©ÚNotImplementedError©r   r   r   r   Úname=   ó   zBaseAuthPlugin.namec                 C   r   )úeReturns the authentication name.

        Returns:
            str: The authentication name.
        r    r"   r   r   r   Ú	auth_nameE   r$   zBaseAuthPlugin.auth_name)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r#   r&   r   r   r   r   r   7   s
    
r   c                   @   ó(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚMySQL41AuthPluginzCClass implementing the MySQL Native Password authentication plugin.c                 C   ó   dS )r   zMySQL 4.1 Authentication Pluginr   r"   r   r   r   r#   P   r$   zMySQL41AuthPlugin.namec                 C   r-   )r%   ÚMYSQL41r   r"   r   r   r   r&   X   r$   zMySQL41AuthPlugin.auth_namec                 C   s|   | j r7t| j tƒr| j  d¡n| j }t |¡ ¡ }t |¡ ¡ }t|t || ¡ ¡ dƒ}d d| j	t
|ƒ¡S d d| j	¡S )z®Hashing for MySQL 4.1 authentication.

        Args:
            data (str): The authentication data.

        Returns:
            str: The authentication response.
        úutf-8é   z{0} {1} *{2} Ú z{0} {1} )r   Ú
isinstancer   ÚencodeÚhashlibÚsha1Údigestr   r   r   r   ©r   Údatar   r   r   r   r   r   r   Ú	auth_data`   s   	
ÿÿzMySQL41AuthPlugin.auth_dataN©r'   r(   r)   r*   r#   r&   r9   r   r   r   r   r,   N   ó
    r,   c                   @   r+   )	ÚPlainAuthPluginz9Class implementing the MySQL Plain authentication plugin.c                 C   r-   )r   zPlain Authentication Pluginr   r"   r   r   r   r#   u   r$   zPlainAuthPlugin.namec                 C   r-   )r%   ÚPLAINr   r"   r   r   r   r&   }   r$   zPlainAuthPlugin.auth_namec                 C   s0   t | jtƒrts| j d¡n| j}d | j|¡S )zeReturns the authentication data.

        Returns:
            str: The authentication data.
        r/   ú {0} {1})r2   r   r   r   r3   r   r   )r   r   r   r   r   r9   …   s   
ÿÿþzPlainAuthPlugin.auth_dataNr:   r   r   r   r   r<   s   r;   r<   c                   @   r+   )	ÚSha256MemoryAuthPluginz;Class implementing the SHA256_MEMORY authentication plugin.c                 C   r-   )r   z#SHA256_MEMORY Authentication Pluginr   r"   r   r   r   r#   “   r$   zSha256MemoryAuthPlugin.namec                 C   r-   )r%   ÚSHA256_MEMORYr   r"   r   r   r   r&   ›   r$   z Sha256MemoryAuthPlugin.auth_namec                 C   sf   t | jtƒr| j d¡n| j}t |¡ ¡ }t t |¡ ¡ | ¡ ¡ }t||dƒ}d | j	t
|ƒ¡S )a   Hashing for SHA256_MEMORY authentication.

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

        Args:
            data (str): The authentication data.

        Returns:
            str: The authentication response.
        r/   é    r>   )r2   r   r   r3   r4   Úsha256r6   r   r   r   r   r7   r   r   r   r9   £   s   
ÿÿz Sha256MemoryAuthPlugin.auth_dataNr:   r   r   r   r   r?   ‘   r;   r?   )r*   r4   r   Úcompatr   r   r   r   Úobjectr   r,   r<   r?   r   r   r   r   Ú<module>   s   %