U
    4Je                     @   s   d Z ddlZddlZddlmZ ddlmZ eeeed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f| 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	   9/tmp/pip-unpacked-wheel-7_167w8m/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                   @   sF   e Zd ZdZdee ee dddZedddZed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   s   t dS )UReturns the plugin name.

        Returns:
            str: The plugin name.
        NNotImplementedErrorr   r	   r	   r   name=   s    zBaseAuthPlugin.namec                 C   s   t dS )eReturns the authentication name.

        Returns:
            str: The authentication name.
        Nr   r!   r	   r	   r   	auth_nameE   s    zBaseAuthPlugin.auth_name)NN)	__name__
__module____qualname____doc__r   strr   r"   r$   r	   r	   r	   r   r   6   s   r   c                   @   s<   e Zd ZdZedddZedddZeeddd	Zd
S )MySQL41AuthPluginzCClass implementing the MySQL Native Password authentication plugin.r   c                 C   s   dS )r   zMySQL 4.1 Authentication Pluginr	   r!   r	   r	   r   r"   Q   s    zMySQL41AuthPlugin.namec                 C   s   dS )r#   ZMYSQL41r	   r!   r	   r	   r   r$   Y   s    zMySQL41AuthPlugin.auth_namedatar   c                 C   s   | j rrt| 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$   bytesr6   r	   r	   r	   r   r*   N   s   r*   c                   @   s:   e Zd ZdZedddZedddZedddZd	S )
PlainAuthPluginz9Class implementing the MySQL Plain authentication plugin.r   c                 C   s   dS )r   zPlain Authentication Pluginr	   r!   r	   r	   r   r"   z   s    zPlainAuthPlugin.namec                 C   s   dS )r#   ZPLAINr	   r!   r	   r	   r   r$      s    zPlainAuthPlugin.auth_namec                 C   s   d| j  d| j S )zeReturns the authentication data.

        Returns:
            str: The authentication data.
        r/   r   r!   r	   r	   r   r6      s    zPlainAuthPlugin.auth_dataN)r%   r&   r'   r(   r)   r"   r$   r6   r	   r	   r	   r   r9   w   s   r9   c                   @   s<   e Zd ZdZedddZedddZeeddd	Zd
S )Sha256MemoryAuthPluginz;Class implementing the SHA256_MEMORY authentication plugin.r   c                 C   s   dS )r   z#SHA256_MEMORY Authentication Pluginr	   r!   r	   r	   r   r"      s    zSha256MemoryAuthPlugin.namec                 C   s   dS )r#   ZSHA256_MEMORYr	   r!   r	   r	   r   r$      s    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.
        r-       r/   )
r0   r   r)   r1   r2   sha256r4   r   r   r   r5   r	   r	   r   r6      s    
z Sha256MemoryAuthPlugin.auth_dataNr7   r	   r	   r	   r   r:      s   r:   )r(   r2   r   typingr   Zhelpersr   r8   intr   r   r*   r9   r:   r	   r	   r	   r   <module>   s   )