a
    ’éi(1  ã                   @   sÖ   d Z dgZddlZddlmZmZmZmZmZ ddl	m
Z
mZ ddlmZmZmZ ddlmZ dd	lmZmZmZ dd
lmZmZmZmZ ddlmZmZ ddlmZ ddl m!Z!m"Z" ddl#m$Z$ G dd„ deƒZdS )z,Implements the MySQL Client/Server protocol.ÚMySQLProtocolé    N)ÚAnyÚDictÚListÚOptionalÚTupleé   )Ú
ClientFlagÚ	ServerCmd)ÚInterfaceErrorÚProgrammingErrorÚget_exception)Úlogger)ÚDEFAULT_CHARSET_IDÚDEFAULT_MAX_ALLOWED_PACKETr   )ÚBinaryProtocolTypeÚDescriptionTypeÚEofPacketTypeÚHandShakeType)Úlc_intÚread_lc_string_listé   )ÚMySQLSocket)ÚMySQLAuthPluginÚget_auth_plugin)Ú"MySQLCachingSHA2PasswordAuthPluginc                   @   s2  e Zd ZdZedeeeeee eee	ee
f  eeef dœdd„ƒZedededdddddf
eeeee eeeee ee ee	eef  eeee	ee
f  eeef dœd	d
„ƒZdeee eeee eeeedf  ee f dœdd„Zdeeedf eee eeeee df  ee f dœdd„ZdS )r   zSImplements MySQL client/server protocol.

    Create and parses MySQL packets.
    NF)Ú	auth_dataÚusernameÚpasswordÚauth_pluginÚauth_plugin_classÚssl_enabledÚplugin_configÚreturnc           
   
   C   s¾   |s|dkrdt |||d�fS |du r*i }z*t||ƒ|||d�}|j| fi |¤Ž}W n8 ttfyŒ }	 ztd|	› �ƒ|	‚W Y d}	~	n
d}	~	0 0 |du r¦td|j› �ƒ‚tt|ƒƒ| }||fS )a  Prepare the first authentication response.

        Args:
            auth_data: Authorization data from initial handshake.
            username: Account's username.
            password: Account's password.
            client_flags: Integer representing client capabilities flags.
            auth_plugin: Authorization plugin name.
            auth_plugin_class: Authorization plugin class (has higher precedence
                               than the authorization plugin name).
            ssl_enabled: Whether SSL is enabled or not.
            plugin_config: Custom configuration to be passed to the auth plugin
                           when invoked. The parameters defined here will override
                           the ones defined in the auth plugin itself.

        Returns:
            auth_response: Authorization plugin response.
            auth_strategy: Authorization plugin instance created based
                           on the provided `auth_plugin` and `auth_plugin_class`
                           parameters.

        Raises:
            InterfaceError: If authentication fails or when got a NULL auth response.
        Ú ó    )r!   NzFailed authentication: z8Got NULL auth response while authenticating with plugin )r   r   Úauth_responseÚ	TypeErrorr   Únamer   Úlen)
r   r   r   r   r    r!   r"   Úauth_strategyr&   Úerr© r,   úL/home/Claro/venv/lib/python3.9/site-packages/mysql/connector/aio/protocol.pyÚauth_plugin_first_response:   s*    "ÿÿ&ÿÿz(MySQLProtocol.auth_plugin_first_responser   )Ú	handshaker   r   ÚdatabaseÚcharsetÚclient_flagsÚmax_allowed_packetr   r    Ú
conn_attrsÚis_change_user_requestr!   r"   r#   c              
   C   s   |  ¡ }g }|
rt d¡ t d¡ | du r6tdƒd‚|  d¡du rNtdƒd‚z|pZ| d }W n: ttfy˜ } ztd|› d	�ƒd‚W Y d}~n
d}~0 0 t d
|¡ |
rÎ| t 	dt
|ƒ› d�tj|¡¡ n,d}| t 	d|› t
|ƒ› d�||||¡¡ tj| d ||||||d�\}}| |¡ | t ||¡¡ |
�rN| t 	d|¡¡ |tj@ �rl| |  ¡ d ¡ |tj@ �r’|	du�r’| t |	¡¡ d |¡|fS )a²  Make a MySQL Authentication packet.

        Args:
            handshake: Initial handshake.
            username: Account's username.
            password: Account's password.
            database: Initial database name for the connection
            charset: Client charset (see [2]), only the lower 8-bits.
            client_flags: Integer representing client capabilities flags.
            max_allowed_packet: Maximum packet size.
            auth_plugin: Authorization plugin name.
            auth_plugin_class: Authorization plugin class (has higher precedence
                               than the authorization plugin name).
            conn_attrs: Connection attributes.
            is_change_user_request: Whether is a `change user request` operation or not.
            ssl_enabled: Whether SSL is enabled or not.
            plugin_config: Custom configuration to be passed to the auth plugin
                           when invoked. The parameters defined here will override
                           the one defined in the auth plugin itself.

        Returns:
            handshake_response: Handshake response as per [1].
            auth_strategy: Authorization plugin instance created based
                           on the provided `auth_plugin` and `auth_plugin_class`.

        Raises:
            ProgrammingError: Handshake misses authentication info.

        References:
            [1]: https://dev.mysql.com/doc/dev/mysql-server/latest/                page_protocol_connection_phase_packets_protocol_handshake_response.html

            [2]: https://dev.mysql.com/doc/dev/mysql-server/latest/                page_protocol_basic_character_set.html#a_protocol_character_set
        zGot a `change user` requestzStarting authorization phaseNzGot a NULL handshaker   z$Handshake misses authentication infor   z-Handshake misses authentication plugin info (ú)z#The provided initial strategy is %sz<BZsxZxxxxxxxxxxxxxxxxxxxxxxxz<IIB)r   r   r   r   r    r!   r"   z<Hr%   ó    )Úencoder   Údebugr   Úgetr'   ÚKeyErrorÚappendÚstructÚpackr)   r
   ZCHANGE_USERr   r.   Zconnect_with_dbr	   ZPLUGIN_AUTHZCONNECT_ARGSZmake_conn_attrsÚjoin)r/   r   r   r0   r1   r2   r3   r   r    r4   r5   r!   r"   Z
b_usernameZresponse_payloadr+   Zfillerr&   r*   r,   r,   r-   Ú	make_authw   sl    3




ÿþýÿûÿù
	
zMySQLProtocol.make_authr   úutf-8.)ÚsockÚcolumnsÚcountr1   Úread_timeoutr#   c                 Ã   s¶   g }d}d}d}	|s®|	|krq®|  |¡I dH }
|
d dkrJ|  |
¡}d}n&|
d dkrpd}|  ||
dd… |¡}|du rŒ|durŒ| |¡ n|du r¤|du r¤t|
ƒ‚|	d7 }	q||fS )zxRead MySQL binary protocol result.

        Reads all or given number of binary resultset rows from the socket.
        Nr   é   éþ   é   r   )ÚreadÚ	parse_eofZ_parse_binary_valuesr<   r   )ÚselfrB   rC   rD   r1   rE   ÚrowsÚeofÚvaluesÚiÚpacketr,   r,   r-   Úread_binary_resultò   s&    

z MySQLProtocol.read_binary_result)rB   ÚversionrD   rE   r#   c                 Ã   s:  |}g }d}d}d}	|s |	|kr$�q2|  |¡I dH }
|
 d¡r¬|
dd… g}|  |¡I dH }
|
 d¡rŠ| |
dd… ¡ |  |¡I dH }
q\| |
dd… ¡ td |¡ƒ}n@|
d dkrÔ|
d dk rÔ|  |
¡}d}nd}tt|
dd… ƒƒ}|du �r|du�r| |¡ n|du �r(|du �r(t|
ƒ‚|	d7 }	q||fS )	z·Read MySQL text result.

        Reads all or given number of rows from the socket.

        Returns a tuple with 2 elements: a list with all rows and
        the EOF packet.
        Nr   s   ÿÿÿrF   r7   rG   é   r   )rI   Ú
startswithr<   r   r?   rJ   Úbytesr   )rK   rB   rR   rD   rE   Ú_rL   rM   ZrowdatarO   rP   Zdatasr,   r,   r-   Úread_text_result  s6    



zMySQLProtocol.read_text_result)NFN)r   rA   N)r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstaticmethodrU   Ústrr   Úboolr   r   r   r   r.   r   r   r   Úintr@   r   r   r   r   r   rQ   rW   r,   r,   r,   r-   r   4   sŒ      ù
ø<ó
ò~   úÿÿù)  û
ÿÿú)%r[   Ú__all__r=   Útypingr   r   r   r   r   Ú	constantsr	   r
   Úerrorsr   r   r   r   Úprotocolr   r   r   Z_MySQLProtocolÚtypesr   r   r   r   Úutilsr   r   Únetworkr   Zpluginsr   r   Zplugins.caching_sha2_passwordr   r,   r,   r,   r-   Ú<module>   s   