
    G)f0                         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e      Zy)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)	int1storeread_lc_string_list   )MySQLSocket)MySQLAuthPluginget_auth_plugin)"MySQLCachingSHA2PasswordAuthPluginc                       e Zd ZdZe	 	 	 ddededededed	ee   d
e	dee
eef      deeef   fd       Zedededdddddf
dedededee   dedededee   d	ee   dee
eef      de	d
e	dee
eef      deeef   fd       Z	 	 ddedee   dededeeeedf      ee   f   f
dZ	 ddedeedf   dedeeeee   df      ee   f   fdZy) r   zSImplements MySQL client/server protocol.

    Create and parses MySQL packets.
    NF	auth_datausernamepasswordclient_flagsauth_pluginauth_plugin_classssl_enabledplugin_configreturnc                 f   |sdt        |||      fS |i }	  t        ||      |||      } |j                  | fi |}	|	t	        d|j
                         |t        j                  z  rt        t        |	            |	z   n|	dz   }	|	|fS # t        t        f$ r}
t	        d|
       |
d}
~
ww xY w)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
   SECURE_CONNECTIONr   len)r   r   r    r!   r"   r#   r$   r%   auth_strategyr)   errs              Y/var/www/html/flask-app/venv/lib/python3.12/site-packages/mysql/connector/aio/protocol.pyauth_plugin_first_responsez(MySQLProtocol.auth_plugin_first_response:   s   F >(    M	KKOK9JK(M 8M77	S]SM   ',,-/  j::: c-()M9( 	 m++ >* 	K #:3%!@AsJ	Ks   (B B0B++B0r   	handshakedatabasecharsetmax_allowed_packet
conn_attrsis_change_user_requestc           
      ^   |j                         }g }|
rt        j                  d       t        j                  d       | t        d      d| j	                  d      t        d      d	 |xs | d   }t        j                  d
|       |
rB|j                  t        j                  dt        |       dt        j                  |             n9d}|j                  t        j                  d| t        |       d||||             t        j                  | d   |||||||      \  }}|j                  |       |j                  t        j                  ||             |
r%|j                  t        j                  d|             |t         j"                  z  r"|j                  |j                         dz          |t         j$                  z  r&|	$|j                  t        j'                  |	             dj)                  |      |fS # t
        t        f$ r}t        d| d	      dd}~ww xY w)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sxxxxxxxxxxxxxxxxxxxxxxxz<IIH)r   r   r    r!   r"   r#   r$   r%   z<Hr(       )encoder   debugr   getr*   KeyErrorappendstructpackr-   r   CHANGE_USERr   r1   connect_with_dbr
   PLUGIN_AUTHCONNECT_ARGSmake_conn_attrsjoin)r2   r   r    r3   r4   r!   r5   r"   r#   r6   r7   r$   r%   
b_usernameresponse_payloadr/   fillerr)   r.   s                      r0   	make_authzMySQLProtocol.make_auth|   s/   f __&
!LL6734"#9:D==%-"#IJPTT	%A=)AK 	:KH!##Z),)) F##6(3z?"326 & (5'O'O,%#/#' (P 	(
$} 	. 	 = =lH UV "##FKKg$>? *000##K$6$6$87$BC :222
8N##M$A$A*$MNxx()=88o 8$ 	"?uAF	s   +	H H,H''H,sockcolumnscount.c                 6  K   g }d}d}d}	 |s||k(  r	 ||fS |j                          d{   }	|	d   dk(  r| j                  |	      }d}n |	d   dk(  rd}| j                  ||	dd |      }|||j                  |       n||t	        |	      |dz  }7 kw)zxRead MySQL binary protocol result.

        Reads all or given number of binary resultset rows from the socket.
        Nr            r   )read	parse_eof_parse_binary_valuesrA   r   )
selfrN   rO   rP   r4   rowseofvaluesipackets
             r0   read_binary_resultz MySQLProtocol.read_binary_result   s      a5j c{  99;&FayCnnV,a227F12JP{v1F##F++FA  's   )BBA,Bversionc                   K   |}g }d}d}d}	 |s||k(  r	 ||fS |j                          d{   }	|	j                  d      r|	dd g}
|j                          d{   }	|	j                  d      r>|
j                  |	dd        |j                          d{   }	|	j                  d      r>|
j                  |	dd        t        t	        d      j                  |
            }n4|	d   dk(  r|	d   dk  r| j                  |	      }d}nd}t        |	dd       }|||j                  |       n||t        |	      |dz  }:7 7 7 w)	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   rR   r<   rS      r   )rU   
startswithrA   r   	bytearrayrI   rV   r   )rX   rN   r_   rP   _rY   rZ   rowdatar\   r]   datass              r0   read_text_resultzMySQLProtocol.read_text_result  sg     a5j* Sy)  99;&F  1#yy{*''8LL,#'99;.F ''8 VABZ(-in.A.A%.HIc!fQi!mnnV,-fQRj9{w2G$#F++FA-  ' + /s:   +EE.EE<EEE0BEEE)NFN)r   zutf-8)r   )__name__
__module____qualname____doc__staticmethodbytesstrintr   boolr   r   r   r   r1   r   r   r   rM   r   r   r   r   r   r^   rg    r<   r0   r   r   4   sM   
  ,0!26?,?,?, ?, 	?,
 ?, $C=?, ?,  S#X/?, 
uo%	&?, ?,B 
 #')"<%)+//3',!26x9 x9x9 x9 3-	x9
 x9 x9  x9 c]x9 $C=x9 T#s(^,x9 !%x9 x9  S#X/x9 
uo%	&x9 x9~  o& 	
  
tE,c123Xm5LM	ND IJ''*/S/'BE'	tE(5/3./0(=2IJ	K'r<   )%rk   __all__rB   typingr   r   r   r   r   	constantsr
   r   errorsr   r   r   r   protocolr   r   r   _MySQLProtocoltypesr   r   r   r   utilsr   r   networkr   pluginsr   r   plugins.caching_sha2_passwordr   rq   r<   r0   <module>r}      sT   : 3
  3 3 - D D  
 V U 2   5 MLN Lr<   