o
    ˷e-                     @   s   d Z zddlmZ W n ey   ddlmZ Y nw ddlZddlZddlZddlZddl	m
Z
 ddl	mZ ddl	mZ dZG dd	 d	ejZdS )
a  Pluggable Credentials.
Pluggable Credentials are initialized using external_account arguments which
are typically loaded from third-party executables. Unlike other
credentials that can be initialized with a list of explicit arguments, secrets
or credentials, external account clients use the environment and hints/guidelines
provided by the external_account JSON file to retrieve credentials and exchange
them for Google access tokens.

Example credential_source for pluggable credential:
{
    "executable": {
        "command": "/path/to/get/credentials.sh --arg1=value1 --arg2=value2",
        "timeout_millis": 5000,
        "output_file": "/path/to/generated/cached/credentials"
    }
}
    )MappingN)_helpers)
exceptions)external_account   c                       s\   e Zd ZdZ fddZeejdd Z	e
 fddZe
 fdd	Zd
d Z  ZS )Credentialsz6External account credentials sourced from executables.c                    s   t t| j|||||d| t|tsd| _td|d| _| js)td| jd| _| jd| _	| jd| _
| jsEtd	| j	sMd
| _	dS | j	dk sW| j	dkr[tddS )a   Instantiates an external account credentials object from a executables.

        Args:
            audience (str): The STS audience field.
            subject_token_type (str): The subject token type.
            token_url (str): The STS endpoint URL.
            credential_source (Mapping): The credential source dictionary used to
                provide instructions on how to retrieve external credential to be
                exchanged for Google access tokens.

                Example credential_source for pluggable credential:

                    {
                        "executable": {
                            "command": "/path/to/get/credentials.sh --arg1=value1 --arg2=value2",
                            "timeout_millis": 5000,
                            "output_file": "/path/to/generated/cached/credentials"
                        }
                    }
            args (List): Optional positional arguments passed into the underlying :meth:`~external_account.Credentials.__init__` method.
            kwargs (Mapping): Optional keyword arguments passed into the underlying :meth:`~external_account.Credentials.__init__` method.

        Raises:
            google.auth.exceptions.RefreshError: If an error is encountered during
                access token retrieval logic.
            ValueError: For invalid parameters.

        .. note:: Typically one of the helper constructors
            :meth:`from_file` or
            :meth:`from_info` are used instead of calling the constructor directly.
        )audiencesubject_token_type	token_urlcredential_sourceNz?Missing credential_source. The credential_source is not a dict.
executablez<Missing credential_source. An 'executable' must be provided.commandtimeout_millisoutput_filez;Missing command field. Executable command must be provided.i0u  i  i z*Timeout must be between 5 and 120 seconds.)superr   __init__
isinstancer   _credential_source_executable
ValueErrorget%_credential_source_executable_command,_credential_source_executable_timeout_millis)_credential_source_executable_output_file)selfr   r	   r
   r   argskwargs	__class__ L/var/www/ideatree/venv/lib/python3.10/site-packages/google/auth/pluggable.pyr   6   sL   
)



zCredentials.__init__c           	      C   s  t jd}|dkrtd| jd urSzt| j}t|}W d    n1 s)w   Y  W n	 ty8   Y nw z| 	|}W |S  tyI     t
jyR   Y nw t s\t
dt j }| j|d< | j|d< d|d< | jd ury| j|d	< | jd ur| j|d
< z$tj| j | jd tjtj|d}|jdkrt
d|j|jW n ty    w z|jd}t|}| 	|}W |S  ty    w )N)GOOGLE_EXTERNAL_ACCOUNT_ALLOW_EXECUTABLES1zhExecutables need to be explicitly allowed (set GOOGLE_EXTERNAL_ACCOUNT_ALLOW_EXECUTABLES to '1') to run.z0Pluggable auth is only supported for python 3.6+ GOOGLE_EXTERNAL_ACCOUNT_AUDIENCE"GOOGLE_EXTERNAL_ACCOUNT_TOKEN_TYPE0#GOOGLE_EXTERNAL_ACCOUNT_INTERACTIVE*GOOGLE_EXTERNAL_ACCOUNT_IMPERSONATED_EMAIL#GOOGLE_EXTERNAL_ACCOUNT_OUTPUT_FILEi  )timeoutstdoutstderrenvr   z9Executable exited with non-zero return code {}. Error: {}zutf-8)osenvironr   r   r   openjsonload	Exception_parse_subject_tokenr   RefreshErrorr   is_python_3copy	_audience_subject_token_type"_service_account_impersonation_urlservice_account_email
subprocessrunr   splitr   PIPESTDOUT
returncodeformatr)   decodeloads)	r   requestenv_allow_executablesr   responsesubject_tokenr+   resultdatar   r   r   retrieve_subject_token   s   







z"Credentials.retrieve_subject_tokenc                       t t| j|fi |S )a  Creates a Pluggable Credentials instance from parsed external account info.

        Args:
            info (Mapping[str, str]): The Pluggable external account info in Google
                format.
            kwargs: Additional arguments to pass to the constructor.

        Returns:
            google.auth.pluggable.Credentials: The constructed
                credentials.

        Raises:
            ValueError: For invalid parameters.
        )r   r   	from_info)clsinfor   r   r   r   rK      s   zCredentials.from_infoc                    rJ   )aj  Creates an Pluggable Credentials instance from an external account json file.

        Args:
            filename (str): The path to the Pluggable external account json file.
            kwargs: Additional arguments to pass to the constructor.

        Returns:
            google.auth.pluggable.Credentials: The constructed
                credentials.
        )r   r   	from_file)rL   filenamer   r   r   r   rN      s   zCredentials.from_filec                 C   s   d|vrt d|d tkrtd|d d|vr t d|d s=d|vs,d|vr0t dtd	|d |d d
|vrH| jrHt dd
|v rY|d
 t k rYtdd|vrat d|d dksm|d dkrq|d S |d dkr{|d S td)Nversionz5The executable response is missing the version field.z+Executable returned unsupported version {}.successz5The executable response is missing the success field.codemessagez;Error code and message fields are required in the response.zAExecutable returned unsuccessful response: code: {}, message: {}.expiration_timezThe executable response must contain an expiration_time for successful responses when an output_file has been specified in the configuration.z0The token returned by the executable is expired.
token_typez8The executable response is missing the token_type field.z$urn:ietf:params:oauth:token-type:jwtz)urn:ietf:params:oauth:token-type:id_tokenid_tokenz&urn:ietf:params:oauth:token-type:saml2saml_responsez+Executable returned unsupported token type.)r    EXECUTABLE_SUPPORTED_MAX_VERSIONr   r3   r@   r   time)r   rE   r   r   r   r2      sL   
z Credentials._parse_subject_token)__name__
__module____qualname____doc__r   r   copy_docstringr   r   rI   classmethodrK   rN   r2   __classcell__r   r   r   r   r   3   s    
Q
Lr   )r]   collections.abcr   ImportErrorcollectionsr/   r,   r:   rY   google.authr   r   r   rX   r   r   r   r   r   <module>   s   