3
ใde  ใ               @   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 )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	   ๚L/var/www/agendate/envp3/lib/python3.6/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               @   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 )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   ฺ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 )zUReturns the plugin name.

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

        Returns:
            str: The authentication name.
        ZMYSQL41r	   )r   r	   r	   r   r   Y   s    zMySQL41AuthPlugin.auth_name)ฺdatar   c             C   s   | j rrt| j tr| j jdn| j }tj|j }tj|j }t|tj|| j d}d| j dt	| dS d| j dS )zฐHashing for MySQL 4.1 authentication.

        Args:
            data (bytes): The authentication data.

        Returns:
            str: The authentication response.
        z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   ฺbytesr-   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 )zUReturns the plugin name.

        Returns:
            str: The plugin name.
        zPlain Authentication Pluginr	   )r   r	   r	   r   r   z   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   s   d| j  d| j S )zeReturns the authentication data.

        Returns:
            str: The authentication data.
        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   r/   w   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 )ฺSha256MemoryAuthPluginz;Class implementing the SHA256_MEMORY authentication plugin.)r   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_name)r%   r   c             C   sj   t | jtr| jjdn| j}tj|j }tjtj|j | j }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.
        zutf-8้    r'   )
r(   r   r#   r)   r*   ฺsha256r,   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	   r   r0      s   r0   )r"   r*   r   ฺtypingr   Zhelpersr   r.   ฺintr   r   r$   r/   r0   r	   r	   r	   r   ฺ<module>   s   )