o
    b7                     @   sl   d Z ddlZddlZddl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   )hexlifyc                 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   T/var/www/secure340b-portal/env/lib/python3.10/site-packages/mysqlx/authentication.py
<listcomp>0   s    zxor_string.<locals>.<listcomp>z{0}B)zipstructpackformat)hash1hash2Z	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__6   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_nameB   r   zBaseAuthPlugin.auth_name)NN)__name__
__module____qualname____doc__r   r   r!   r   r   r   r   r   4   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   M   r   zMySQL41AuthPlugin.namec                 C   r(   )r    ZMYSQL41r   r   r   r   r   r!   U   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 )zHashing 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   
isinstancestr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!   r4   r   r   r   r   r'   K   
    r'   c                   @   r&   )	PlainAuthPluginz9Class implementing the MySQL Plain authentication plugin.c                 C   r(   )r   zPlain Authentication Pluginr   r   r   r   r   r   r   r   zPlainAuthPlugin.namec                 C   r(   )r    ZPLAINr   r   r   r   r   r!   z   r   zPlainAuthPlugin.auth_namec                 C   s   d | j| jS )zeReturns the authentication data.

        Returns:
            str: The authentication data.
         {0} {1})r   r   r   r   r   r   r   r4      s   zPlainAuthPlugin.auth_dataNr5   r   r   r   r   r7   p   r6   r7   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    Z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)       r8   )r,   r   r-   r.   r/   sha256r1   r   r   r   r   r2   r   r   r   r4      s   
z Sha256MemoryAuthPlugin.auth_dataNr5   r   r   r   r   r9      r6   r9   )r%   r/   r   helpersr   r   objectr   r'   r7   r9   r   r   r   r   <module>   s   %