a
    lJh(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 rt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,   N/var/www/shaz/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<B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 rt|
|	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   )selfrC   rD   rE   r1   rF   rowseofvaluesipacketr,   r,   r-   read_binary_result   s&    

z MySQLProtocol.read_binary_result)rC   versionrE   rF   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 )	zRead 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   rG   r8   rH      r   )rJ   
startswithr=   r   r@   rK   bytesr   )rL   rC   rS   rE   rF   _rM   rN   ZrowdatarP   rQ   Zdatasr,   r,   r-   read_text_result  s6    



zMySQLProtocol.read_text_result)NFN)r   rB   N)r   N)__name__
__module____qualname____doc__staticmethodrV   strr   boolr   r   r   r   r.   r   r   r   intrA   r   r   r   r   r   rR   rX   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   pluginsr   r   Zplugins.caching_sha2_passwordr   r,   r,   r,   r-   <module>   s   