Ë
    aQbj�$  ã                   ó^  — 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 	 ddlmZmZ ddlmZ dZ dZ!dZ"dZ#dZ$ G d„ de«      Z%y# e$ r  ej6                  d«      d‚w xY w# e$ r  ej6                  d«      d‚w xY w)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y)r   z2Implement the MySQL OCI IAM authentication plugin.NÚcontextÚDEFAULTÚoci_config_profileÚoci_config_fileÚ	signatureÚ
oci_configÚreturnc                 ó²  — t        | «      }|d   |j                  «       dœ}|j                  d«      r^	 t        |d   «      }|j	                  «       j
                  t        kD  rt        j                  t        «      ‚|j                  d¬«      |d<   t        j                  |d¬	«      S # t        t        f$ r}t        j                  t        «      |‚d}~ww xY w)
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         úd/var/www/html/venv/lib/python3.12/site-packages/mysql/connector/plugins/authentication_oci_client.pyÚ_prepare_auth_responsez)MySQLOCIAuthPlugin._prepare_auth_responseQ   sÔ   € ô" ! Ó+ˆà% mÑ4Ø%×,Ñ,Ó.ñ
ˆð �>‰>Ð/Ô0ð	Ü&*¨:Ð6KÑ+LÓ&MÐ#à&×+Ñ+Ó-×5Ñ5Ô8SÒSÜ ×1Ñ1Ô2NÓOÐOØ)<×)FÑ)FÐPWÐ)FÓ)X�˜gÑ&ô
 �z‰z˜-°JÔ?Ð?øô	 œ\Ð*ò Ü×-Ñ-Ü9óàðûðús   ´AB( Â(CÂ7CÃCÚkey_pathc                 óH  — 	 t        t        j                  j                  | «      d«      5 }t	        j
                  |j                  «       d¬«      }ddd«       |S # 1 sw Y   S xY w# t        t        t        t        f$ r }t        j                  d| › d|› �«      ‚d}~ww xY w)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¡   € ð
	Ü”b—g‘g×(Ñ(¨Ó2°DÓ9ð ¸XÜ+×@Ñ@Ø—M‘M“OØ!ô�÷ð Ð÷ð Ðûô œ7¤JÔ0DÐEò 	Ü×)Ñ)ðØ�:˜S  ð'óð ûð	ús4   ‚)A( «&AÁA( ÁA%Á A( Á%A( Á(B!ÂBÂB!c                 óž  — g }d„ d„ dœ}i }	 t        j                  | j                  xs t         j                  | j                  xs d«      }|j                  «       D ]+  \  }}	 ||   r  |||   «      s|j                  d|› d�«       Œ- 	 |r%t        j                   d	| j                  › d
|› �«      ‚|S # t        $ r |j                  d|› �«       Y Œww xY w# t        j                  t        j                  t        j                  t        j                  t        j                  f$ r$}|j                  t        |«      «       Y d}~Œ»d}~ww xY w)z=Get a valid OCI config from the given configuration file pathc                 ó   — t        | «      dkD  S )Né    )Úlen©Úxs    r7   ú<lambda>z:MySQLOCIAuthPlugin._get_valid_oci_config.<locals>.<lambda>Ž   s   € ¤c¨!£f¨r¡k€ ó    c                 óz   — t         j                  j                  t         j                  j                  | «      «      S )N)r>   r?   Úexistsr@   rL   s    r7   rN   z:MySQLOCIAuthPlugin._get_valid_oci_config.<locals>.<lambda>�   s"   € ¤2§7¡7§>¡>´"·'±'×2DÑ2DÀQÓ2GÓ#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Š   sa  € àˆ
á1ÙHñ
ˆð
 &(ˆ
ð	(ä×)Ñ)Ø×$Ñ$Ò?¬×(?Ñ(?Ø×'Ñ'Ò4¨9óˆJð '/§n¡nÓ&6ò OÑ"�˜ðOà! 'Ò*±9¸ZÈÑ=PÔ3QØ"×)Ñ)¨K¸°yÀÐ*MÔNøñ	Oñ" Ü×)Ñ)Ø+¨D×,@Ñ,@Ð+Að B!Ø!+ ð.óð ð
 Ðøô%  ò OØ×%Ñ%Ð(CÀGÀ9Ð&MÖNðOûô ×)Ñ)Ü×$Ñ$Ü×)Ñ)Ü×(Ñ(Ü×&Ñ&ð
ò 	(ð ×Ñœc #›h×'Ñ'ûð	(ús=   �AC Á$%B5Â	C Â5CÃC ÃCÃC ÃAEÄ(EÅEc                  ó   — y)zPlugin official name.Úauthentication_oci_client© ©r]   s    r7   ÚnamezMySQLOCIAuthPlugin.name²   s   € ð +rO   c                  ó   — y)z'Signals whether or not SSL is required.Fre   rf   s    r7   Úrequires_sslzMySQLOCIAuthPlugin.requires_ssl·   s   € ð rO   Ú	auth_dataÚkwargsc                 óh  — t        j                  d|t        |«      «       | j                  «       }| j	                  |d   «      }|j                  |t        j                  «       t        j                  «       «      }| j                  ||«      }t        j                  d|«       |j                  «       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À	»NÔKà×/Ñ/Ó1ˆ
à×+Ñ+¨J°zÑ,BÓCˆØ×$Ñ$ Y´×0@Ñ0@Ó0BÄFÇMÁMÃOÓTˆ	à×3Ñ3°I¸zÓJˆÜ�‰Ð2°MÔBØ×#Ñ#Ó%Ð%rO   Úsockr   c                 óÒ  — |j                  dd«      | _        |j                  dt        j                  «      | _        t        j                  d| j                  «        | j                  |fi |¤Ž}|€t        j                  d«      ‚t        j                  d|t        |«      «       |j                  |«       |j                  «       }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: %sz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¿   € ð"  &Ÿz™zÐ*;¸YÓGˆÔØ"(§*¡*Ø ¤&×"9Ñ"9ó#
ˆÔô 	�‰Ð8¸$×:NÑ:NÔOà%�4×%Ñ% iÑ:°6Ñ:ˆØÐÜ×'Ñ'Ð(BÓCÐCä�‰Ð-¨x¼¸X»ÔGØ�	‰	�(Ôà—‘“ˆÜ�‰Ð3°VÔ<ä�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  … Ù<à€GˆSÓØ'Ð˜Ó'Ø!×2Ñ2€O�SÓ2àð%@¨%ð %@¸TÀ#ÀsÀ(¹^ð %@ÐPSò %@ó ð%@ðN ð 3ð Ð+<ò ó ðð & t¨C°¨H¡~ó &ðP ð+�cò +ó ð+ð ð˜dò ó ðð& 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å Ý áÝ%ðWÝ<ßDÝAÝQðß&õ à2Ð Ø'Ð ØQÐ àMð &ð Rð ô
`˜õ `øð/ ò WØ
!ˆ&×
!Ñ
!Ð"KÓ
LÐRVÐVðWûð
 ò Ø
!ˆ&×
!Ñ
!ØQóàððús   ¸A7 ÁB Á7BÂB,