o
    ëÉ¶dR+  ã                   @   s   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ZddlZddlZddlZddlZG dd	„ d	eƒZG d
d„ dejjƒZdS )é   )Ú
auth_utils)ÚSecurityTokenContaineré    )ÚEncodingÚPublicFormat)ÚSHA1)ÚrequestsNc                   @   s<   e Zd Zg d¢Zdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚX509FederationClient)Úfederation_endpointÚ
tenancy_idÚsession_key_supplierÚleaf_certificate_retrieverc                 K   s^  |  ¡ }g }| jD ]}||vr| |¡ q	|| s| |¡ q	|r+td d |¡¡ƒ‚|d | _|d | _|d | _|d | _	d| _
d|v rQ|d durQ|d | _
t d	 tt| ƒ¡¡| _| j t ¡ ¡ | d
¡rvd| j_| j tj¡ nd| j_d|v rˆ|d rˆ|d | _ng | _| dd¡| _t ¡ | _| dd¡}|r£|| _ntjj| _t  ¡ | _!dS )a  
        A client which can be used to retrieve a token from Auth Service. It needs the following supplied to it:

            - The endpoint for Auth Service
            - Our tenancy OCID
            - A session key supplier so that we can send its public key as part of the token request. The private key
            in the session key supplier should be used to sign all requests made with the token
            - The certificate (via leaf_certificate_retriever) which will be used to sign the requests to Auth Service.

        Optionally, intermediate certificates (if present) can be supplied as part of the request to Auth Service.

        The client has knowledge of its last requested token and can re-request the token if it is expired (otherwise
        it will vend the last requested token if it is not expired).

        :param str federation_endpoint:
            The Auth Service endpoint from which to retrieve the token.

        :param str tenancy_id:
            The OCID of the tenancy whose resources will be interacted with by users of the token.

        :param SessionKeySupplier session_key_supplier:
            A SessionKeySupplier that can vend a public and private key. The public key will be sent as part of the token
            request and the private key should be used to sign all requests made with the token vended by this client.

        :param CertificateRetriever leaf_certificate_retriever:
            The certificate which will be used to sign requests to Auth Service.

        :param list[CertificateRetriever] intermediate_certificate_retrievers: (optional)
            A list of retrievers which can be used to fetch intermediate certificates which can be sent as part of the Auth Service request. This is an optional parameter

        :param cert_bundle_verify: (optional)
            If we need a specific cert bundle in order to perform verification against the federation endpoint, this parameter is the path to that bundle. Alternatively,
            False can be passed to disable verification.
        :type cert_bundle_verify: str or Boolean

        :param obj retry_strategy: (optional)
            A retry strategy to apply to calls made by this client. This should be one of the strategies available in
            the :py:mod:`~oci.retry` module. A convenience :py:data:`~oci.retry.DEFAULT_RETRY_STRATEGY` is also available and
            will be used if no explicit retry strategy is specified.

            The specifics of the default retry strategy are described `here <https://docs.oracle.com/en-us/iaas/tools/python/latest/sdk_behaviors/retries.html>`__.

            To have this operation explicitly not perform any retries, pass an instance of :py:class:`~oci.retry.NoneRetryStrategy`.

        :param bool log_requests: (optional)
        log_request if set to True, will log the request url and response data when retrieving
        the token from the federation endpoint.
        z6The following required arguments were not provided: {}z, r
   r   r   r   NÚpurposez{}.{}Zlog_requestsFTÚ#intermediate_certificate_retrieversÚcert_bundle_verifyÚretry_strategy)"ÚkeysÚREQUIRED_INIT_KWARGSÚappendÚ	TypeErrorÚformatÚjoinr
   r   r   r   r   ÚloggingÚ	getLoggerÚ__name__ÚidÚloggerÚ
addHandlerÚNullHandlerÚgetÚdisabledÚsetLevelÚDEBUGr   r   Ú	threadingÚLockÚ_refresh_lockr   ÚociÚretryZDEFAULT_RETRY_STRATEGYr   ZSessionÚrequests_session)ÚselfÚkwargsZ
kwarg_keysZmissing_keysÚrequiredr   © r,   ú>usr/lib/python3.10/site-packages/oci/auth/federation_client.pyÚ__init__   sB   2

€







zX509FederationClient.__init__c                 C   s   |   ¡ S )N)Ú_refresh_security_token_inner©r)   r,   r,   r-   Úrefresh_security_token{   s   z+X509FederationClient.refresh_security_tokenc                 C   s$   t | dƒr| j ¡ r| jjS |  ¡ S )NÚsecurity_token)Úhasattrr2   Zvalid_with_jitterr/   r0   r,   r,   r-   Úget_security_token~   s   

z'X509FederationClient.get_security_tokenc                 C   s˜   | j  ¡  z@| j ¡  | j ¡  | jd u r+t | j ¡ ¡}|| j	kr+t
d | j	|¡ƒ‚| jD ]}| ¡  q.| j | j¡ | jjW | j  ¡  S | j  ¡  w )Nz\Unexpected update of tenancy OCID in the leaf certificate. Previous tenancy: {}, Updated: {})r%   Úacquirer   Zrefreshr   r   r   Zget_tenancy_id_from_certificateÚget_certificate_as_certificater   ÚRuntimeErrorr   r   r   Zmake_retrying_callÚ%_get_security_token_from_auth_servicer2   Úrelease)r)   Zupdated_tenancy_idÚ	retrieverr,   r,   r-   r/   …   s   






z2X509FederationClient._refresh_security_token_innerc           
   
   C   sÆ  t  | j ¡ ¡t  | j ¡ d  tjt	j
¡¡dœ}| jd ur#| j|d< | jr<g }| jD ]}| t  | ¡ ¡¡ q+||d< | j ¡ }d dd„ t| tƒ ¡ƒD ƒ¡}t| j|| jƒ}| j d| j ¡ | jj| j||| jd	d
}| j d tj|j|jt |j! "¡ ƒ|j#dœdd¡¡ d }z| $¡ }W n" t%y´   d | j|j&¡}	|j'r¨t(|	ƒ‚t)j* +|j|j#|j!|	¡‚w |j'sÉt)j* +|j| ,d¡|j!| ,d¡¡‚d|v rÚt-| j| $¡ d ƒ| _.d S t(d | j|¡ƒ‚)NÚpublic)ÚcertificateZ	publicKeyr   ZintermediateCertificatesú:c                 s   s    | ]}d   |¡V  qdS )z{:02X}N)r   )Ú.0Úchr,   r,   r-   Ú	<genexpr>©   s   € zMX509FederationClient._get_security_token_from_auth_service.<locals>.<genexpr>zRequesting token from : %s )é
   é<   )ÚjsonZauthZverifyÚtimeoutz"Receiving token response......
{}
)Ústatus_codeÚurlÚheaderÚreasoné   )Úindentz3Unable to parse response from auth service ({}): {}ÚcodeÚmessageÚtokenz;Could not find token in response from auth service ({}): {})/r   Zsanitize_certificate_stringr   Zget_certificate_rawr   Zget_key_pairZpublic_bytesr   ZPEMr   ZSubjectPublicKeyInfor   r   r   r6   r   Ú	bytearrayÚfingerprintr   ÚAuthTokenRequestSignerr   r   Údebugr
   r(   Úpostr   r   ÚpprintÚpformatrE   rF   ÚdictÚheadersÚitemsrH   rC   Ú
ValueErrorÚtextÚokr7   r&   Ú
exceptionsZServiceErrorr   r   r2   )
r)   Zrequest_payloadZretrieved_certsr:   r<   rO   ÚsignerÚresponseZparsed_responseZ
error_textr,   r,   r-   r8   ˜   sZ   þ



 ÿ
þüöüz:X509FederationClient._get_security_token_from_auth_serviceN)	r   Ú
__module__Ú__qualname__r   r.   r1   r4   r/   r8   r,   r,   r,   r-   r	      s    `r	   c                   @   s   e Zd ZdZdd„ ZdS )rP   z~
    A signer intended for X509FederationClient's use to request a token from Auth Service. Not intended for general use.
    c                 C   s@   d  ||¡| _|| _ddg}g d¢}|  | j| j ¡ ||¡ d S )Nz{}/fed-x509/{}Údatez(request-target))zcontent-lengthzcontent-typezx-content-sha256)r   Zapi_keyÚ!private_key_certificate_retrieverZcreate_signersZget_private_key)r)   r   rO   ra   Zgeneric_headersZbody_headersr,   r,   r-   r.   Ù   s
   zAuthTokenRequestSigner.__init__N)r   r^   r_   Ú__doc__r.   r,   r,   r,   r-   rP   Ô   s    rP   )Ú r   Zsecurity_token_containerr   Z,cryptography.hazmat.primitives.serializationr   r   Z%cryptography.hazmat.primitives.hashesr   Zoci._vendorr   Z	oci.retryr&   Z
oci.signerr#   r   rS   Úobjectr	   r\   ZAbstractBaseSignerrP   r,   r,   r,   r-   Ú<module>   s    B