
    iy/                        d Z ddlmZ dgZddlZddlmZmZmZm	Z	 ddl
mZmZmZ ddlmZmZmZmZmZmZmZ dd	lmZ d
dlmZ d
dlmZmZ d
dlmZ erd
dlmZ  G d d          ZdS )z6Implementing support for MySQL Authentication Plugins.    )annotationsMySQLAuthenticatorN)TYPE_CHECKINGAnyDictOptional   )InterfaceErrorNotSupportedErrorget_exception)AUTH_SWITCH_STATUSDEFAULT_CHARSET_IDDEFAULT_MAX_ALLOWED_PACKET
ERR_STATUSEXCHANGE_FURTHER_STATUS
MFA_STATUS	OK_STATUS)HandShakeType   )logger)MySQLAuthPluginget_auth_plugin)MySQLProtocol)MySQLSocketc                      e Zd ZdZd.dZed/d            Zed0d            Z	 	 	 d1d2dZd3dZ	d3dZ
ddddd	eded	d	d	dfd4d-Zd	S )5r   z$Implements the authentication phase.returnNonec                Z    d| _         i | _        i | _        d| _        d| _        d| _        dS )zConstructor. FN)	_username
_passwords_plugin_config_ssl_enabled_auth_strategy_auth_plugin_classselfs    \/var/www/cyber-strat/venv/lib/python3.11/site-packages/mysql/connector/aio/authentication.py__init__zMySQLAuthenticator.__init__=   s6     *,.0"'9=15    boolc                    | j         S )z&Signals whether or not SSL is enabled.)r#   r&   s    r(   ssl_enabledzMySQLAuthenticator.ssl_enabledF   s       r*   Dict[str, Any]c                    | j         S )a  Custom arguments that are being provided to the authentication plugin.

        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_configz MySQLAuthenticator.plugin_configK   s     ""r*   Nr   new_strategy_namestrstrategy_classOptional[str]usernamepassword_factorintc                    || j         }|| j        }t          j        d|            t	          ||          || j                            |d          | j                  | _        dS )a  Switch 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   debugr   r!   getr-   r$   )r'   r1   r3   r5   r6   s        r(   _switch_auth_strategyz(MySQLAuthenticator._switch_auth_strategy\   s    & ~H!!4N/1BCCC
o)^
 
 
 O44(
 
 
r*   sockr   pktbytesOptional[bytes]c                  K   d}|d         t           k    r'|| j        vrt          d          t          j        |          \  }}|                     ||           t          j        d|| j        j	                    | j        j
        ||fi | j         d{V }|d         t          k    r3t          j        |          } | j        j        ||fi | j         d{V }|d         t          k    rt          j        d           |S |d         t           k    rt#          |          |dz  }|d         t           k    't          j        d	           dS )
a  Handle 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.
        r	      z5Failed Multi Factor Authentication (invalid N factor))r6   zMFA %i factor %sNzMFA completed succesfullyr   z"MFA terminated with a no ok packet)r   r!   r
   r   parse_auth_next_factorr=   r   r;   r$   nameauth_switch_responser"   r   parse_auth_more_dataauth_more_responser   r   r   warning)r'   r>   r?   n_factorr1   	auth_datas         r(   _mfa_n_factorz MySQLAuthenticator._mfa_n_factor~   s     * !f
""t..$K   ,9+OPS+T+T(y&&'8(&SSSL+Xt7J7OPPP@+@i #'#6       C 1v000)>sCC	BD/B) '+':        1v""8999
1v###C(((MH7 !f
"": 	;<<<tr*   c                d  K   |d         t           k    r"t          |          dk    rt          d          |d         t           k    r_t          j        d           t          j        |          \  }}|                     |            | j        j	        ||fi | j
         d{V }|d         t          k    rGt          j        d           t          j        |          } | j        j        ||fi | j
         d{V }|d         t          k    r!t          j        d| j        j                   |S |d         t           k    rOt          j        d           t          j        d	| j        j                   |                     ||           d{V S |d         t$          k    rt'          |          dS )
a  Handle 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.
        rC      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 requestNzExchanging further packetsz%s completed succesfullyz$Starting multi-factor authenticationzMFA 1 factor %s)r   lenr   r   r;   r   parse_auth_switch_requestr=   r$   rF   r"   r   rG   rH   r   rE   r   rL   r   r   )r'   r>   r?   r1   rK   s        r(   _handle_server_responsez*MySQLAuthenticator._handle_server_response   s     & q6'''CHHMM#>   q6'''LFGGG+8+RSV+W+W(y&&'8999@+@i #'#6       C q6,,,L5666%:3??I>+>i #'#6       C q6YL3T5H5MNNNJq6ZL?@@@L*D,?,DEEE++D#666666666q6Z$$$tr*   r   r   F	handshaker   	password1	password2	password3databasecharsetclient_flagsmax_allowed_packetauth_pluginr:   
conn_attrsOptional[Dict[str, str]]is_change_user_requestr0   r   c                  K   || _         |||d| _        t          j        |          | _        || _        t          j        ||||||	|
||||| j        | j	                  \  }| _
        |rdnd} |j        |g|R   d{V  t          |                                 d{V           }|                     ||           d{V }|t          d          d|S )a  Perform 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.
            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:
            ok_packet: OK packet.

        Raises:
            InterfaceError: If OK packet is NULL.

        References:
            [1]: https://dev.mysql.com/doc/dev/mysql-server/latest/                page_protocol_basic_character_set.html#a_protocol_character_set
        )r   r	      )rR   r5   passwordrV   rW   rX   rY   rZ   r:   r[   r]   r-   r0   )r   r   )NNNzGot a NULL ok_pkt)r    r!   copydeepcopyr"   r%   r   	make_authr-   r0   r$   writer@   readrQ   r
   )r'   r>   rR   r5   rS   rT   rU   rV   rW   rX   rY   rZ   r:   r[   r]   r0   response_payload	send_argsr?   ok_pkts                       r(   authenticatezMySQLAuthenticator.authenticate   s2     j "'I)DD"mM::"3 1>0G%1#/!#9(,1
 1
 1
-$-" 5FFF,	dj)6I6666666666 $))++%%%%%%&&33D#>>>>>>>>> !4554?r*   )r   r   )r   r+   )r   r.   )NNr   )
r1   r2   r3   r4   r5   r4   r6   r7   r   r   )r>   r   r?   r@   r   rA   ) r>   r   rR   r   r5   r2   rS   r2   rT   r2   rU   r2   rV   r4   rW   r7   rX   r7   rY   r7   rZ   r4   r:   r4   r[   r\   r]   r+   r0   r   r   r@   )__name__
__module____qualname____doc__r)   propertyr-   r0   r=   rL   rQ   r   r   ri    r*   r(   r   r   :   s       ..6 6 6 6 ! ! ! X! # # # X#& )-"&  
  
  
  
  
D4 4 4 4l5 5 5 5v "&)"<%)+//3',V V V V V V Vr*   ) rm   
__future__r   __all__ra   typingr   r   r   r   errorsr
   r   r   protocolr   r   r   r   r   r   r   typesr   r   pluginsr   r   r   networkr   r   ro   r*   r(   <module>rx      sr  : = < " " " " " "
   5 5 5 5 5 5 5 5 5 5 5 5 E E E E E E E E E E                  " ! ! ! ! !       5 5 5 5 5 5 5 5 # # # # # # %$$$$$$G G G G G G G G G Gr*   