
    *h9                        S r SSKJr  SSKJrJrJrJr  SSKJ	r	J
r
Jr  SSKJr  SSKJrJr  SSKJrJrJrJrJrJrJrJr  SS	KJr  \(       a  SS
KJr   " S S5      rg)z5Implementing support for MySQL Authentication Plugins    )annotations)TYPE_CHECKINGAnyDictOptional   )InterfaceErrorNotSupportedErrorget_exception)logger)MySQLAuthPluginget_auth_plugin)AUTH_SWITCH_STATUSDEFAULT_CHARSET_IDDEFAULT_MAX_ALLOWED_PACKET
ERR_STATUSEXCHANGE_FURTHER_STATUS
MFA_STATUS	OK_STATUSMySQLProtocol)HandShakeType)MySQLSocketc                  @   \ rS rSrSrSS jr\SS j5       r\SS j5       rSS jr	\
S\4             SS jjr   S         SS
 jjr      SS jr      SS jrSSSSS	\
S\S	S	S	SS	S	4                                 SS jjrSrg	)MySQLAuthenticator5   z$Implements the authentication phase.c                X    SU l         0 U l        0 U l        SU l        SU l        SU l        g)zConstructor. FN)	_username
_passwords_plugin_config_ssl_enabled_auth_strategy_auth_plugin_classselfs    V/var/www/auris/envauris/lib/python3.13/site-packages/mysql/connector/authentication.py__init__MySQLAuthenticator.__init__8   s0     *,.0"'9=15    c                    U R                   $ )z&Signals whether or not SSL is enabled.)r!   r$   s    r&   ssl_enabledMySQLAuthenticator.ssl_enabledA   s        r)   c                    U R                   $ )a  Custom arguments that are being provided to the authentication plugin when called.

The parameters defined here will override the ones defined in the
auth plugin itself.

The plugin config is a read-only property - the plugin configuration
provided when invoking `authenticate()` is recorded and can be queried
by accessing this property.

Returns:
    dict: The latest plugin configuration provided when invoking
          `authenticate()`.
)r    r$   s    r&   plugin_config MySQLAuthenticator.plugin_configF   s     """r)   c                :    U R                   R                  U5        g)z,Update the 'plugin_config' instance variableN)r    update)r%   configs     r&   update_plugin_config'MySQLAuthenticator.update_plugin_configW   s    ""6*r)   r   c                   Uc  0 n[         R                  " UUUS9nUR                  U5        [        R                  " S5        UR                  UR                  S5      UR                  S5      UR                  S5      UR                  SS5      UR                  SS5      UR                  S	5      UR                  S
5      S9n[        R                  " S5        UR                  X5        [        R                  " S5        SU l        U$ )a{  Sets up an SSL communication channel.

Args:
    sock: Pointer to the socket connection.
    host: Server host name.
    ssl_options: SSL and TLS connection options (see
                 `network.MySQLSocket.build_ssl_context`).
    charset: Client charset (see [1]), only the lower 8-bits.
    client_flags: Integer representing client capabilities flags.
    max_allowed_packet: Maximum packet size.

Returns:
    ssl_request_payload: Payload used to carry out SSL authentication.

References:
    [1]: https://dev.mysql.com/doc/dev/mysql-server/latest/                page_protocol_basic_character_set.html#a_protocol_character_set
)charsetclient_flagsmax_allowed_packetzBuilding SSL contextcacertkeyverify_certFverify_identitytls_versionstls_ciphersuites)ssl_cassl_certssl_keyssl_verify_certssl_verify_identityr>   tls_cipher_suiteszSwitching to SSLzSSL has been enabledT)	r   make_auth_sslsendr   debugbuild_ssl_contextgetswitch_to_sslr!   )	r%   sockhostssl_optionsr6   r7   r8   ssl_request_payloadssl_contexts	            r&   	setup_sslMySQLAuthenticator.setup_ssl[   s    6 K ,99%1

 			%&+,,,??4( __V,OOE*'OOM5A +0A5 I$8)oo.@A - 
 	'(;-+, ""r)   Nc                    Uc  U R                   nUc  U R                  n[        R                  " SU5        [	        XS9" UU R
                  R                  US5      U R                  S9U l        g)a^  Switches the authorization plugin.

Args:
    new_strategy_name: New authorization plugin name to switch to.
    strategy_class: New authorization plugin class to switch to
                    (has higher precedence than the authorization plugin name).
    username: Username to be used - if not defined, the username
              provided when `authentication()` was invoked is used.
    password_factor: Up to three levels of authentication (MFA) are allowed,
                     hence you can choose the password corresponding to the 1st,
                     2nd, or 3rd factor - 1st is the default.
NzSwitching to strategy %s)plugin_nameauth_plugin_classr   )r+   )	r   r#   r   rH   r   r   rJ   r+   r"   )r%   new_strategy_namestrategy_classusernamepassword_factors        r&   _switch_auth_strategy(MySQLAuthenticator._switch_auth_strategy   sk    & ~~H!!44N/1BC-)
 OO4((
r)   c                   SnUS   [         :X  Ga/  X0R                  ;  a  [        S5      e[        R                  " U5      u  pEU R                  XCS9  [        R                  " SX0R                  R                  5        U R                  R                  " X40 U R                  D6nUS   [        :X  a=  [        R                  " U5      nU R                  R                  " X40 U R                  D6nUS   [        :X  a  [        R                  " S5        U$ US   [         :X  a  [#        U5      eUS-  nUS   [         :X  a  GM/  [        R$                  " S5        g	)
a  Handles MFA (Multi-Factor Authentication) response.

Up to three levels of authentication (MFA) are allowed.

Args:
    sock: Pointer to the socket connection.
    pkt: MFA response.

Returns:
    ok_packet: If last server's response is an OK packet.
    None: If last server's response isn't an OK packet and no ERROR was raised.

Raises:
    InterfaceError: If got an invalid N factor.
    errors.ErrorTypes: If got an ERROR response.
      z5Failed Multi Factor Authentication (invalid N factor))rY   zMFA %i factor %szMFA completed succesfullyr   z"MFA terminated with a no ok packetN)r   r   r	   r   parse_auth_next_factorrZ   r   rH   r"   nameauth_switch_responser    r   parse_auth_more_dataauth_more_responser   r   r   warning)r%   rL   pktn_factorrV   	auth_datas         r&   _mfa_n_factor MySQLAuthenticator._mfa_n_factor   s@   * !f
".$K  ,9+O+OPS+T(&&'8&SLL+X7J7J7O7OP%%::#'#6#6C 1v00)>>sC	))<<'+':': 1v"89
1v##C((MH7 !f
": 	;<r)   c                Z   US   [         :X  a  [        U5      S:X  a  [        S5      eUS   [         :X  af  [        R                  " S5        [
        R                  " U5      u  p4U R                  U5        U R                  R                  " X40 U R                  D6nUS   [        :X  aS  [        R                  " S5        [
        R                  " U5      nU R                  R                  " X40 U R                  D6nUS   [        :X  a-  [        R                  " SU R                  R                  5        U$ US   [         :X  aR  [        R                  " S5        [        R                  " SU R                  R                  5        U R#                  X5      $ US   [$        :X  a  ['        U5      eg	)
a  Handles server's response.

Args:
    sock: Pointer to the socket connection.
    pkt: Server's response after completing the `HandShakeResponse`.

Returns:
    ok_packet: If last server's response is an OK packet.
    None: If last server's response isn't an OK packet and no ERROR was raised.

Raises:
    errors.ErrorTypes: If got an ERROR response.
    NotSupportedError: If got Authentication with old (insecure) passwords.
r^      zAuthentication with old (insecure) passwords is not supported. For more information, lookup Password Hashing in the latest MySQL manualz+Server's response is an auth switch requestzExchanging further packetsz%s completed succesfullyz$Starting multi-factor authenticationzMFA 1 factor %sN)r   lenr
   r   rH   r   parse_auth_switch_requestrZ   r"   ra   r    r   rb   rc   r   r`   r   rh   r   r   )r%   rL   re   rV   rg   s        r&   _handle_server_response*MySQLAuthenticator._handle_server_response   st   & q6''CHM#>  q6''LLFG+8+R+RSV+W(&&'89%%::#'#6#6C q6,,LL56%::3?I%%88#'#6#6C q6YLL3T5H5H5M5MNJq6ZLL?@LL*D,?,?,D,DE%%d00q6Z$$r)   r   Fc                h   X0l         XEUS.U l        Xl        [        R                  " UUUUUU	U
UUUUU R
                  U R                  S9u  nU l        U(       a  SSU4OSSU4nUR                  " U/UQ76   [        UR                  U5      5      nU R                  UU5      nUc  [        S5      SeU$ )a  Performs the authentication phase.

During re-authentication you must set `is_change_user_request` to True.

Args:
    sock: Pointer to the socket connection.
    handshake: Initial handshake.
    username: Account's username.
    password1: Account's password factor 1.
    password2: Account's password factor 2.
    password3: Account's password factor 3.
    database: Initial database name for the connection.
    charset: Client charset (see [1]), 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.
    read_timeout: Timeout in seconds upto which the connector should wait for
                  the server to reply back before raising an ReadTimeoutError.
    write_timeout: Timeout in seconds upto which the connector should spend to
                   send data to the server before raising an WriteTimeoutError.
Returns:
    ok_packet: OK packet.

Raises:
    InterfaceError: If OK packet is NULL.
    ReadTimeoutError: If the time taken for the server to reply back exceeds
                      'read_timeout' (if set).
    WriteTimeoutError: If the time taken to send data packets to the server
                       exceeds 'write_timeout' (if set).

References:
    [1]: https://dev.mysql.com/doc/dev/mysql-server/latest/                page_protocol_basic_character_set.html#a_protocol_character_set
)r   r]      )	handshakerX   passworddatabaser6   r7   r8   auth_pluginrU   
conn_attrsis_change_user_requestr+   r.   r   NzGot a NULL ok_pkt)r   r   r#   r   	make_authr+   r.   r"   rG   bytesrecvrn   r	   )r%   rL   rr   rX   	password1	password2	password3rt   r6   r7   r8   ru   rU   rv   rw   read_timeoutwrite_timeoutresponse_payload	send_argsre   ok_pkts                        r&   authenticateMySQLAuthenticator.authenticate#  s    t "')D"3 1>0G0G%1#/!#9((,,1
-$-& & =!m, 	
 			"/Y/ DIIl+,--dC8> !454?r)   )r#   r"   r   r    r!   r   )returnNone)r   bool)r   Dict[str, Any])r2   r   r   r   )rL   r   rM   strrN   zOptional[Dict[str, Any]]r6   intr7   r   r8   r   r   ry   )NNr   )
rV   r   rW   Optional[str]rX   r   rY   r   r   r   )rL   r   re   ry   r   zOptional[bytes])"rL   r   rr   r   rX   r   r{   r   r|   r   r}   r   rt   r   r6   r   r7   r   r8   r   ru   r   rU   r   rv   zOptional[Dict[str, str]]rw   r   r~   Optional[int]r   r   r   ry   )__name__
__module____qualname____firstlineno____doc__r'   propertyr+   r.   r3   r   r   rQ   rZ   rh   rn   r   __static_attributes__ r)   r&   r   r   5   s   .6 ! ! # # + *"<7#7# 7# .	7#
 7# 7#  7# 
7#x )-"&  
 
 & 
  	 

  
 
 
D44 4 
	4l55 5 
	5v "&)"<%)+//3',&*'+#^^ !^ 	^
 ^ ^ ^  ^ ^ ^  ^ #^ )^ -^ !%^  $!^" %#^$ 
%^ ^r)   r   N)r   
__future__r   typingr   r   r   r   errorsr	   r
   r   r   pluginsr   r   protocolr   r   r   r   r   r   r   r   typesr   networkr   r   r   r)   r&   <module>r      sE   : < " 5 5 D D  5	 	 	 !$L Lr)   