o
    h                     @   s   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
dZ	G dd d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)Optional   )hexlifyhash1hash2	hash_sizereturnc                 C   s,   dd t | |D }tj| dg|R  S )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.
    c                 S   s   g | ]\}}||A qS  r	   ).0h1h2r	   r	   P/var/www/html/scripts/venv/lib/python3.10/site-packages/mysqlx/authentication.py
<listcomp>2   s    zxor_string.<locals>.<listcomp>B)zipstructpack)r   r   r   xoredr	   r	   r   
xor_string'   s   r   c                   @   sH   e Zd ZdZddee dee fddZdefdd	Zdefd
dZdS )BaseAuthPluginz7Base class for implementing the authentication plugins.Nusernamepasswordc                 C   s   || _ || _d S )N	_username	_password)selfr   r   r	   r	   r   __init__9   s   
zBaseAuthPlugin.__init__r   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   strr   r"   r%   r	   r	   r	   r   r   6   s
    r   c                   @   >   e Zd ZdZdefddZdefddZdedefdd	Zd
S )MySQL41AuthPluginzCClass implementing the MySQL Native Password authentication plugin.r   c                 C      dS )r   zMySQL 4.1 Authentication Pluginr	   r!   r	   r	   r   r"   Q   r#   zMySQL41AuthPlugin.namec                 C   r-   )r$   MYSQL41r	   r!   r	   r	   r   r%   Y   r#   zMySQL41AuthPlugin.auth_namedatac                 C   s   | j r9t| j tr| j dn| j }t| }t| }t|t||  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   r/   r   r   r   r   r	   r	   r   	auth_dataa   s   	
zMySQL41AuthPlugin.auth_dataN	r&   r'   r(   r)   r*   r"   r%   bytesr9   r	   r	   r	   r   r,   N   
    r,   c                   @   s:   e Zd ZdZdefddZdefddZdefddZd	S )
PlainAuthPluginz9Class implementing the MySQL Plain authentication plugin.r   c                 C   r-   )r   zPlain Authentication Pluginr	   r!   r	   r	   r   r"   z   r#   zPlainAuthPlugin.namec                 C   r-   )r$   PLAINr	   r!   r	   r	   r   r%      r#   zPlainAuthPlugin.auth_namec                 C   s   d| j  d| j S )zeReturns the authentication data.

        Returns:
            str: The authentication data.
        r2   r   r!   r	   r	   r   r9      s   zPlainAuthPlugin.auth_dataN)r&   r'   r(   r)   r*   r"   r%   r9   r	   r	   r	   r   r=   w   s
    r=   c                   @   r+   )Sha256MemoryAuthPluginz;Class implementing the SHA256_MEMORY authentication plugin.r   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_namer/   c                 C   sj   t | jtr| jdn| j}t| }tt| |  }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.
        r0       r2   )
r3   r   r*   r4   r5   sha256r7   r   r   r   r8   r	   r	   r   r9      s   
z Sha256MemoryAuthPlugin.auth_dataNr:   r	   r	   r	   r   r?      r<   r?   )r)   r5   r   typingr   helpersr   r;   intr   r   r,   r=   r?   r	   r	   r	   r   <module>   s   )