
    Aah$                     b   d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	m
Z
mZ ddlmZ ddlmZ erdd	lmZ 	 dd
lmZ ddlmZmZ ddlmZ ddlmZ n# e$ r  ej        d          dw xY w	 ddlmZmZ n# e$ r  ej        d          dw xY wddlmZ dZ dZ!dZ"dZ#dZ$ G d de          Z%dS )zOCI Authentication Plugin.    N)	b64encode)Path)TYPE_CHECKINGAnyDictOptional   )errors)logger)MySQLSocket)UnsupportedAlgorithm)hashesserialization)padding)PRIVATE_KEY_TYPESz'Package 'cryptography' is not installed)config
exceptionszGPackage 'oci' (Oracle Cloud Infrastructure Python SDK) is not installed   )MySQLAuthPluginMySQLOCIAuthPlugini (  z0Ephemeral security token is too large (10KB max)zGEphemeral security token file ('security_token_file') could not be readzKOCI configuration file does not contain a 'fingerprint' or 'key_file' entryc                   (   e Zd ZU dZdZeed<   dZeed<   e	j
        Zeed<   ededeeef         d	efd
            Zeded	efd            Zd	eeef         fdZed	efd            Zed	efd            Zdeded	ee         fdZdddeded	efdZdS )r   z2Implement the MySQL OCI IAM authentication plugin.NcontextDEFAULToci_config_profileoci_config_file	signature
oci_configreturnc                    t          |           }|d         |                                d}|                    d          r	 t          |d                   }|                                j        t          k    rt          j        t                    |
                    d          |d<   n3# t          t          f$ r}t          j        t                    |d}~ww xY wt          j        |d	          S )
a=  Prepare client's authentication response

        Prepares client's authentication response in JSON format
        Args:
            signature (bytes):  server's nonce to be signed by client.
            oci_config (dict): OCI configuration object.

        Returns:
            str: JSON string with the following format:
                 {"fingerprint": str, "signature": str, "token": base64.base64.base64}

        Raises:
            ProgrammingError: If the ephemeral security token file can't be open or the
                              token is too large.
        fingerprint)r    r   security_token_filezutf-8)encodingtokenN),:)
separators)r   decodegetr   statst_sizeOCI_SECURITY_TOKEN_MAX_SIZEr
   ProgrammingErrorOCI_SECURITY_TOKEN_TOO_LARGE	read_textOSErrorUnicodeError%OCI_SECURITY_TOKEN_FILE_NOT_AVAILABLEjsondumps)r   r   signature_64auth_responser!   errs         c/var/www/lms/venv/lib/python3.11/site-packages/mysql/connector/plugins/authentication_oci_client.py_prepare_auth_responsez)MySQLOCIAuthPlugin._prepare_auth_responseQ   s   " !++%m4%,,..
 
 >>/00 
		&*:6K+L&M&M#&++--58SSS 12NOOO)<)F)FPW)F)X)Xg&&\*   -9  z-J????s   A)B- -C>CCkey_pathc                 b   	 t          t          j                            |           d          5 }t	          j        |                                d          }ddd           n# 1 swxY w Y   n?# t          t          t          t          f$ r}t          j        d|  d|           d}~ww xY w|S )z+Get the private_key form the given locationrbN)passwordz2An error occurred while reading the API_KEY from "z": )openospath
expanduserr   load_pem_private_keyread	TypeErrorr/   
ValueErrorr   r
   r,   )r9   key_fileprivate_keyr6   s       r7   _get_private_keyz#MySQLOCIAuthPlugin._get_private_keyy   s   
	bg((22D99 X+@MMOO!                
 7J0DE 	 	 	)'' '!$' '  	 s:   -A0 )A$A0 $A((A0 +A(,A0 0B,B''B,c                 |   g }d d d}i }	 t          j        | j        pt           j        | j        pd          }|                                D ]`\  }}	 ||         r* |||                   s|                    d| d           9# t          $ r |                    d|            Y ]w xY wnk# t          j	        t          j
        t          j        t          j        t          j        f$ r,}|                    t          |                     Y d}~nd}~ww xY w|rt          j        d	| j         d
|           |S )z=Get a valid OCI config from the given configuration file pathc                 (    t          |           dk    S )N    )lenxs    r7   <lambda>z:MySQLOCIAuthPlugin._get_valid_oci_config.<locals>.<lambda>   s    c!ffrk     c                 z    t           j                            t           j                            |                     S )N)r>   r?   existsr@   rL   s    r7   rN   z:MySQLOCIAuthPlugin._get_valid_oci_config.<locals>.<lambda>   s$    27>>"'2D2DQ2G2G#H#H rO   )r    rE   r   zParameter "z" is invalidzDoes not contain parameter NzInvalid oci-config-file: z. Errors found: )r   	from_filer   DEFAULT_LOCATIONr   itemsappendKeyErrorr   ConfigFileNotFoundInvalidConfigInvalidKeyFilePathInvalidPrivateKeyProfileNotFoundstrr
   r,   )self
error_listreq_keysr   req_key	req_valuer6   s          r7   _get_valid_oci_configz(MySQLOCIAuthPlugin._get_valid_oci_config   s   
11HH
 

 &(
	()$?(?'49 J '/nn&6&6 O O"O!'* O99Z=P3Q3Q O"))*M*M*M*MNNN O O O%%&MG&M&MNNNNNOO )$)(&
 	( 	( 	( c#hh''''''''	(  	).D,@ . .!+. .  
 s<   AB0 2BB0 "B,)B0 +B,,B0 0<D,"DDc                     dS )zPlugin official name.authentication_oci_client r]   s    r7   namezMySQLOCIAuthPlugin.name   s
     +*rO   c                     dS )z'Signals whether or not SSL is required.Fre   rf   s    r7   requires_sslzMySQLOCIAuthPlugin.requires_ssl   s	     urO   	auth_datakwargsc                    t          j        d|t          |                     |                                 }|                     |d                   }|                    |t          j                    t          j	                              }| 
                    ||          }t          j        d|           |                                S )z-Prepare authentication string for the server.zserver nonce: %s, len %drE   zauthentication response: %s)r   debugrK   rb   rG   signr   PKCS1v15r   SHA256r8   encode)r]   rj   rk   r   rF   r   r5   s          r7   r5   z MySQLOCIAuthPlugin.auth_response   s    /C	NNKKK//11
++Jz,BCC$$Y0@0B0BFMOOTT	33IzJJ2MBBB##%%%rO   sockr   c                    |                     dd          | _        |                     dt          j                  | _        t          j        d| j                    | j        |fi |}|t          j	        d          t          j        d|t          |                     |                    |           |                                }t          j        d|           t          |          S )	aS  Handles server's `auth switch request` response.

        Args:
            sock: Pointer to the socket connection.
            auth_data: Plugin provided data (extracted from a packet
                       representing an `auth switch request` response).
            kwargs: 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:
            packet: Last server's response after back-and-forth
                    communication.
        r   r   r   z!# oci configuration file path: %sNzGot a NULL auth responsez# request: %s size: %sz# server response packet: %s)r(   r   r   rS   r   r   rm   r5   r
   InterfaceErrorrK   sendrecvbytes)r]   rr   rj   rk   responsepackets         r7   auth_switch_responsez'MySQLOCIAuthPlugin.auth_switch_response   s    "  &zz*;YGG"(** &"9#
 #
 	8$:NOOO%4%i::6::'(BCCC-xXGGG		(3V<<<V}}rO   )__name__
__module____qualname____doc__r   r   __annotations__r   r\   r   rS   r   staticmethodrw   r   r8   r   rG   rb   propertyrg   boolri   r   r5   rz   re   rO   r7   r   r   J   s        <<GS''''!2OS222%@% %@T#s(^ %@PS %@ %@ %@ \%@N 3 +<    \ &tCH~ & & & &P +c + + + X+ d    X&u & & & & & &!!!.3!?B!	! ! ! ! ! !rO   )&r~   r2   r>   base64r   pathlibr   typingr   r   r   r    r
   r   networkr   cryptography.exceptionsr   cryptography.hazmat.primitivesr   r   )cryptography.hazmat.primitives.asymmetricr   /cryptography.hazmat.primitives.asymmetric.typesr   ImportErrorr,   ocir   r   r   AUTHENTICATION_PLUGIN_CLASSr+   r-   r1   OCI_PROFILE_MISSING_PROPERTIESr   re   rO   r7   <module>r      s  > !    				             5 5 5 5 5 5 5 5 5 5 5 5             &%%%%%%W<<<<<<DDDDDDDDAAAAAAQQQQQQQ W W W
!&
!"K
L
LRVVW&&&&&&&&&   
!&
!Q 
      2 ' Q M & R 
` ` ` ` ` ` ` ` ` `s   A A*.A7 7B