o
    bi+                     @   sN   d Z ddlmZ ddlmZ ddlmZ dddZdd	 Zd
d Z	dd Z
dS )zUtilites for mutual TLS.    )getenv)
exceptions)_mtls_helperTc                 C   sN   | rt t jdurdS t t jdurdS td}|r%t |dur%dS dS )a2  Check if default client SSL credentials exists on the device.

    Args:
       include_context_aware (bool): include_context_aware indicates if context_aware
       path location will be checked or should it be skipped.

    Returns:
        bool: indicating if the default client cert source exists.
    NTZGOOGLE_API_CERTIFICATE_CONFIGF)r   Z_check_config_pathZCONTEXT_AWARE_METADATA_PATHZ&CERTIFICATE_CONFIGURATION_DEFAULT_PATHr   )include_context_awareZcert_config_path r   T/home/kim/smarthome/.venv/lib/python3.10/site-packages/google/auth/transport/mtls.pyhas_default_client_cert_source   s$   
r   c                  C   s    t dds
tddd } | S )a  Get a callback which returns the default client SSL credentials.

    Returns:
        Callable[[], [bytes, bytes]]: A callback which returns the default
            client certificate bytes and private key bytes, both in PEM format.

    Raises:
        google.auth.exceptions.DefaultClientCertSourceError: If the default
            client SSL credentials don't exist or are malformed.
    Tr   z(Default client cert source doesn't existc               
   S   sH   zt  \} }}W ||fS  tttfy# } zt|}||d }~ww )N)r   Zget_client_cert_and_keyOSErrorRuntimeError
ValueErrorr   MutualTLSChannelError)_
cert_bytes	key_bytes
caught_excnew_excr   r   r   callbackG   s   
z,default_client_cert_source.<locals>.callbackr   r   r   )r   r   r   r   default_client_cert_source7   s   
	r   c                    s&   t dds
td fdd}|S )at  Get a callback which returns the default encrpyted client SSL credentials.

    Args:
        cert_path (str): The cert file path. The default client certificate will
            be written to this file when the returned callback is called.
        key_path (str): The key file path. The default encrypted client key will
            be written to this file when the returned callback is called.

    Returns:
        Callable[[], [str, str, bytes]]: A callback which generates the default
            client certificate, encrpyted private key and passphrase. It writes
            the certificate and private key into the cert_path and key_path, and
            returns the cert_path, key_path and passphrase bytes.

    Raises:
        google.auth.exceptions.DefaultClientCertSourceError: If any problem
            occurs when loading or saving the client certificate and key.
    Tr	   z2Default client encrypted cert source doesn't existc               
      s   z@t jdd\} }}}t d}|| W d    n1 s w   Y  td}|| W d    n1 s:w   Y  W n tjtfyW } zt|}||d }~ww  |fS )NT)Zgenerate_encrypted_keywb)r   Zget_client_ssl_credentialsopenwriter   ZClientCertErrorr
   r   )r   r   r   Zpassphrase_bytes	cert_filekey_filer   r   	cert_pathkey_pathr   r   r   k   s(   


z6default_client_encrypted_cert_source.<locals>.callbackr   )r   r   r   r   r   r   $default_client_encrypted_cert_sourceS   s   
r   c                   C   s   t  S )ad  Returns boolean for whether the client certificate should be used for mTLS.

    This is a wrapper around _mtls_helper.check_use_client_cert().
    If GOOGLE_API_USE_CLIENT_CERTIFICATE is set to true or false, a corresponding
    bool value will be returned
    If GOOGLE_API_USE_CLIENT_CERTIFICATE is unset, the value will be inferred by
    reading a file pointed at by GOOGLE_API_CERTIFICATE_CONFIG, and verifying it
    contains a "workload" section. If so, the function will return True,
    otherwise False.

    Returns:
       bool: indicating whether the client certificate should be used for mTLS.
    )r   Zcheck_use_client_certr   r   r   r   should_use_client_cert   s   r   N)T)__doc__osr   Zgoogle.authr   Zgoogle.auth.transportr   r   r   r   r   r   r   r   r   <module>   s   
 -