o
    –¨Êht$  ã                   @   s˜   d Z ddlmZmZ ddlZddlmZ ddlmZm	Z	m
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G dd„ deƒZdd„ ZdS )z5Implementing support for MySQL Authentication Pluginsé    )Úsha1Úsha256Né   )Úerrors)ÚPY2ÚisstrÚUNICODE_TYPESc                   @   s6   e Zd ZdZdZdZ		ddd„Zdd„ Zd	d
„ ZdS )ÚBaseAuthPluginaÉ  Base class for authentication plugins


    Classes inheriting from BaseAuthPlugin should implement the method
    prepare_password(). When instantiating, auth_data argument is
    required. The username, password and database are optional. The
    ssl_enabled argument can be used to tell the plugin whether SSL is
    active or not.

    The method auth_response() method is used to retrieve the password
    which was prepared by prepare_password().
    FÚ Nc                 C   s"   || _ || _|| _|| _|| _dS )ÚInitializationN)Ú
_auth_dataÚ	_usernameÚ	_passwordÚ	_databaseÚ_ssl_enabled)ÚselfÚ	auth_dataÚusernameÚpasswordÚdatabaseÚssl_enabled© r   úP/var/www/html/env/lib/python3.10/site-packages/mysql/connector/authentication.pyÚ__init__7   s
   
zBaseAuthPlugin.__init__c                 C   s   t ‚)zåPrepares and returns password to be send to MySQL

        This method needs to be implemented by classes inheriting from
        this class. It is used by the auth_response() method.

        Raises NotImplementedError.
        )ÚNotImplementedError©r   r   r   r   Úprepare_password@   s   zBaseAuthPlugin.prepare_passwordc                 C   s(   | j r| jst dj| jd�¡‚|  ¡ S )z°Returns the prepared password to send to MySQL

        Raises InterfaceError on errors. For example, when SSL is required
        by not enabled.

        Returns str
        ú{name} requires SSL©Úname)Úrequires_sslr   r   ÚInterfaceErrorÚformatÚplugin_namer   r   r   r   r   Úauth_responseJ   s
   ÿzBaseAuthPlugin.auth_response)NNNF)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    r#   r   r   r$   r   r   r   r   r	   &   s    
ÿ	
r	   c                   @   ó    e Zd ZdZdZdZdd„ ZdS )ÚMySQLNativePasswordAuthPluginzBClass implementing the MySQL Native Password authentication pluginFÚmysql_native_passwordc           	   
   C   s  | j st d¡‚| jsdS | j}t| jƒr| j d¡}n| j}tr9t|ƒ}zt| j ƒ}W n ty8   t d¡‚w |}| j }d}z7t	|ƒ 
¡ }t	|ƒ 
¡ }t	|| ƒ 
¡ }trbdd„ t||ƒD ƒ}n
dd„ t||ƒD ƒ}tjd	g|¢R Ž }W |S  ty‹ } zt d
 |¡¡‚d}~ww )z;Prepares and returns password as native MySQL 4.1+ passwordú"Missing authentication data (seed)ó    úutf-8úAuthentication data incorrectNc                 S   ó    g | ]\}}t |ƒt |ƒA ‘qS r   ©Úord©Ú.0Úh1Úh3r   r   r   Ú
<listcomp>|   ó     zBMySQLNativePasswordAuthPlugin.prepare_password.<locals>.<listcomp>c                 S   ó   g | ]\}}||A ‘qS r   r   r3   r   r   r   r7   ~   ó    Ú20BzFailed scrambling password; {0})r   r   r!   r   r   Úencoder   ÚbufferÚ	TypeErrorr   ÚdigestÚzipÚstructÚpackÚ	Exceptionr"   )	r   r   r   Úhash4Úhash1Úhash2Úhash3ÚxoredÚexcr   r   r   r   ^   sB   


ÿüÿ€ÿz.MySQLNativePasswordAuthPlugin.prepare_passwordN©r%   r&   r'   r(   r    r#   r   r   r   r   r   r*   X   ó
    r*   c                   @   r)   )ÚMySQLClearPasswordAuthPluginzAClass implementing the MySQL Clear Password authentication pluginTÚmysql_clear_passwordc                 C   óL   | j sdS | j }trt|tƒr| d¡}|d S t|tƒr"| d¡}|d S ©ú!Returns password as as clear textó    Úutf8©r   r   Ú
isinstanceÚunicoder<   Ústr©r   r   r   r   r   r   �   ó   


ý
z-MySQLClearPasswordAuthPlugin.prepare_passwordNrJ   r   r   r   r   rL   ‡   rK   rL   c                   @   r)   )ÚMySQLSHA256PasswordAuthPluginzÆClass implementing the MySQL SHA256 authentication plugin

    Note that encrypting using RSA is not supported since the Python
    Standard Library does not provide this OpenSSL functionality.
    TÚsha256_passwordc                 C   rN   rO   rS   rW   r   r   r   r   ¦   rX   z.MySQLSHA256PasswordAuthPlugin.prepare_passwordNrJ   r   r   r   r   rY   œ   s
    rY   c                   @   s8   e Zd ZdZdZdZdZdZdd„ Zdd	„ Z	d
d„ Z
dS )Ú"MySQLCachingSHA2PasswordAuthPluginzÕClass implementing the MySQL caching_sha2_password authentication plugin

    Note that encrypting using RSA is not supported since the Python
    Standard Library does not provide this OpenSSL functionality.
    FÚcaching_sha2_passwordé   é   c                 C   sð   | j st d¡‚| jsdS t| jtƒr| j d¡n| j}tr6t|ƒ}zt| j ƒ}W n t	y5   t d¡‚w |}| j }t
|ƒ ¡ }t
ƒ }| t
|ƒ ¡ ¡ | |¡ | ¡ }trcdd„ t||ƒD ƒ}n
dd„ t||ƒD ƒ}tjdg|¢R Ž }|S )	z» Returns a scramble of the password using a Nonce sent by the
        server.

        The scramble is of the form:
        XOR(SHA2(password), SHA2(SHA2(SHA2(password)), Nonce))
        r,   r-   r.   r/   c                 S   r0   r   r1   ©r4   r5   Úh2r   r   r   r7   à   r8   z@MySQLCachingSHA2PasswordAuthPlugin._scramble.<locals>.<listcomp>c                 S   r9   r   r   r_   r   r   r   r7   â   r:   Ú32B)r   r   r!   r   rT   r   r<   r   r=   r>   r   r?   Úupdater@   rA   rB   )r   r   r   rE   rF   rH   rG   r   r   r   Ú	_scrambleÀ   s6   

ÿÿ
ÿ
z,MySQLCachingSHA2PasswordAuthPlugin._scramblec                 C   s2   t | jƒdkr|  ¡ S | jd | jkr|  ¡ S d S )Nr   r   )Úlenr   rc   Úperform_full_authenticationÚ_full_authenticationr   r   r   r   r   ç   s
   z3MySQLCachingSHA2PasswordAuthPlugin.prepare_passwordc                 C   sf   | j st dj| jd�¡‚| jsdS | j}tr%t|tƒr!| 	d¡}|d S t|t
ƒr/| 	d¡}|d S )rP   r   r   rQ   rR   )r   r   r!   r"   r#   r   r   rT   rU   r<   rV   rW   r   r   r   rf   î   s   ÿ


ý
z7MySQLCachingSHA2PasswordAuthPlugin._full_authenticationN)r%   r&   r'   r(   r    r#   re   Úfast_auth_successrc   r   rf   r   r   r   r   r[   µ   s    'r[   c                 C   s0   t  ¡ D ]}|j| kr|  S qt d | ¡¡‚)a.  Return authentication class based on plugin name

    This function returns the class for the authentication plugin plugin_name.
    The returned class is a subclass of BaseAuthPlugin.

    Raises errors.NotSupportedError when plugin_name is not supported.

    Returns subclass of BaseAuthPlugin.
    z,Authentication plugin '{0}' is not supported)r	   Ú__subclasses__r#   r   ÚNotSupportedErrorr"   )r#   Ú	authclassr   r   r   Úget_auth_plugin  s   

ÿÿrk   )r(   Úhashlibr   r   rA   r
   r   Úcatch23r   r   r   Úobjectr	   r*   rL   rY   r[   rk   r   r   r   r   Ú<module>   s   2/L