3
S^k                 @   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   sD   t rdd t| |D }ndd t| |D }tjdj|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   G/tmp/pip-build-8app2_gc/mysql-connector-python/mysqlx/authentication.py
<listcomp>1   s    zxor_string.<locals>.<listcomp>c             S   s    g | ]\}}t |t |A qS r   )ord)r   r   r	   r   r   r
   r   3   s    z{0}B)r   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__9   s    zBaseAuthPlugin.__init__c             C   s   t dS )zUReturns the plugin name.

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

        Returns:
            str: The authentication name.
        N)r   )r   r   r   r
   	auth_nameE   s    zBaseAuthPlugin.auth_name)NN)__name__
__module____qualname____doc__r   r   r   r   r   r   r
   r   7   s   
r   c               @   s(   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   s   dS )zUReturns the plugin name.

        Returns:
            str: The plugin name.
        zMySQL 4.1 Authentication Pluginr   )r   r   r   r
   r   P   s    zMySQL41AuthPlugin.namec             C   s   dS )zeReturns the authentication name.

        Returns:
            str: The authentication name.
        ZMYSQL41r   )r   r   r   r
   r   X   s    zMySQL41AuthPlugin.auth_namec             C   s|   | j rnt| j tr| j jdn| j }tj|j }tj|j }t|tj|| j d}djd| j	t
|S djd| j	S )zHashing for MySQL 4.1 authentication.

        Args:
            data (str): The authentication data.

        Returns:
            str: The authentication response.
        z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   r,   r   r   r   r
   r#   N   s   r#   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	PlainAuthPluginz9Class implementing the MySQL Plain authentication plugin.c             C   s   dS )zUReturns the plugin name.

        Returns:
            str: The plugin name.
        zPlain Authentication Pluginr   )r   r   r   r
   r   u   s    zPlainAuthPlugin.namec             C   s   dS )zeReturns the authentication name.

        Returns:
            str: The authentication name.
        ZPLAINr   )r   r   r   r
   r   }   s    zPlainAuthPlugin.auth_namec             C   s2   t | jtrt r| jjdn| j}dj| j|S )zeReturns the authentication data.

        Returns:
            str: The authentication data.
        zutf-8z {0} {1})r&   r   r   r   r'   r   r   )r   r   r   r   r
   r,      s    zPlainAuthPlugin.auth_dataN)r   r    r!   r"   r   r   r,   r   r   r   r
   r-   s   s   r-   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	Sha256MemoryAuthPluginz;Class implementing the SHA256_MEMORY authentication plugin.c             C   s   dS )zUReturns the plugin name.

        Returns:
            str: The plugin name.
        z#SHA256_MEMORY Authentication Pluginr   )r   r   r   r
   r      s    zSha256MemoryAuthPlugin.namec             C   s   dS )zeReturns the authentication name.

        Returns:
            str: The authentication name.
        ZSHA256_MEMORYr   )r   r   r   r
   r      s    z Sha256MemoryAuthPlugin.auth_namec             C   sf   t | jtr| jjdn| j}tj|j }tjtj|j | j }t||d}dj| 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.
        zutf-8    z {0} {1})r&   r   r   r'   r(   sha256r*   r   r   r   r   )r   r+   r   r   r   r   r   r   r
   r,      s
    z Sha256MemoryAuthPlugin.auth_dataN)r   r    r!   r"   r   r   r,   r   r   r   r
   r.      s   r.   )r"   r(   r   compatr   r   r   r   objectr   r#   r-   r.   r   r   r   r
   <module>   s   %