a
    w=ic&                     @   s   d Z zddlmZ W n ey2   ddlmZ Y n0 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 G dd dejZdS )	a  Identity Pool Credentials.

This module provides credentials to access Google Cloud resources from on-prem
or non-Google Cloud platforms which support external credentials (e.g. OIDC ID
tokens) retrieved from local file locations or local servers. This includes
Microsoft Azure and OIDC identity providers (e.g. K8s workloads registered with
Hub with Hub workload identity enabled).

These credentials are recommended over the use of service account credentials
in on-prem/non-Google Cloud platforms as they do not involve the management of
long-live service account private keys.

Identity Pool Credentials are initialized using external_account
arguments which are typically loaded from an external credentials file or
an external credentials URL. 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.
    )MappingN)_helpers)
exceptions)external_accountc                       sv   e Zd ZdZ fddZeejdd Z	dd Z
dd	 Zd
d ZdddZe fddZe fddZ  ZS )Credentialsz9External account credentials sourced from files and URLs.c                    s   t t| j|||||d| t|ts8d| _d| _n|d| _|d| _|d| _|di }|dptd| _	d	|v rt
d
| j	dvrt
d| j	| j	dkr|d| _| jdu rt
dnd| _| jr| jrt
d| js| jst
ddS )a  Instantiates an external account credentials object from a file/URL.

        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 url-sourced credential::

                    {
                        "url": "http://www.example.com",
                        "format": {
                            "type": "json",
                            "subject_token_field_name": "access_token",
                        },
                        "headers": {"foo": "bar"},
                    }

                Example credential_source for file-sourced credential::

                    {
                        "file": "/path/to/token/file.txt"
                    }
            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_sourceNfileurlheadersformattypetextZenvironment_idz>Invalid Identity Pool credential_source field 'environment_id')r   jsonz%Invalid credential_source format '{}'r   subject_token_field_namezBMissing subject_token_field_name for JSON credential_source formatzEAmbiguous credential_source. 'file' is mutually exclusive with 'url'.z>Missing credential_source. A 'file' or 'url' must be provided.)superr   __init__
isinstancer   _credential_source_file_credential_source_urlget_credential_source_headers_credential_source_format_type
ValueErrorr   _credential_source_field_name)selfr   r   r	   r
   argskwargsZcredential_source_format	__class__ j/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/google/auth/identity_pool.pyr   5   sZ    0




zCredentials.__init__c                 C   s   |  | || j| jS N)_parse_token_data_get_token_datar   r   r   requestr"   r"   r#   retrieve_subject_token   s
    z"Credentials.retrieve_subject_tokenc                 C   s(   | j r| | j S | || j| jS d S r$   )r   _get_file_data_get_url_datar   r   r'   r"   r"   r#   r&      s
    
zCredentials._get_token_datac                 C   s\   t j|std|tj|ddd}| |fW  d    S 1 sN0    Y  d S )NzFile '{}' was not found.rutf-8)encoding)	ospathexistsr   RefreshErrorr   ioopenread)r   filenameZfile_objr"   r"   r#   r*      s    zCredentials._get_file_datac                 C   sJ   ||d|d}t |jdr&|jdn|j}|jdkrBtd|||fS )NGET)r   methodr   decoder-      z.Unable to retrieve Identity Pool subject token)hasattrdatar9   statusr   r2   )r   r(   r   r   responseZresponse_bodyr"   r"   r#   r+      s    

zCredentials._get_url_datar   Nc              	   C   sh   |\}}|dkr|}n@zt |}|| }W n( ttfyT   td||Y n0 |sdtd|S )Nr   z@Unable to parse subject_token from JSON file '{}' using key '{}'z3Missing subject_token in the credential_source file)r   loadsKeyErrorr   r   r2   r   )r   Ztoken_contentZformat_typer   contentr6   tokenZresponse_datar"   r"   r#   r%      s"    

zCredentials._parse_token_datac                    s   t t| j|fi |S )a  Creates an Identity Pool Credentials instance from parsed external account info.

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

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

        Raises:
            ValueError: For invalid parameters.
        )r   r   	from_info)clsinfor   r    r"   r#   rC      s    zCredentials.from_infoc                    s   t t| j|fi |S )at  Creates an IdentityPool Credentials instance from an external account json file.

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

        Returns:
            google.auth.identity_pool.Credentials: The constructed
                credentials.
        )r   r   	from_file)rD   r6   r   r    r"   r#   rF      s    zCredentials.from_file)r   N)__name__
__module____qualname____doc__r   r   Zcopy_docstringr   r   r)   r&   r*   r+   r%   classmethodrC   rF   __classcell__r"   r"   r    r#   r   2   s   f

 
r   )rJ   collections.abcr   ImportErrorcollectionsr3   r   r/   Zgoogle.authr   r   r   r   r"   r"   r"   r#   <module>   s   