o
    ~j6h+                     @   sJ   d Z ddlmZ ddlZddlZddlmZ ddiZG dd dej	Z
dS )a  OAuth 2.0 Token Exchange Spec.

This module defines a token exchange utility based on the `OAuth 2.0 Token
Exchange`_ spec. This will be mainly used to exchange external credentials
for GCP access tokens in workload identity pools to access Google APIs.

The implementation will support various types of client authentication as
allowed in the spec.

A deviation on the spec will be for additional Google specific options that
cannot be easily mapped to parameters defined in the RFC.

The returned dictionary response will be based on the `rfc8693 section 2.2.1`_
spec JSON response.

.. _OAuth 2.0 Token Exchange: https://tools.ietf.org/html/rfc8693
.. _rfc8693 section 2.2.1: https://tools.ietf.org/html/rfc8693#section-2.2.1
    N)utilszContent-Typez!application/x-www-form-urlencodedc                       sL   e Zd ZdZd fdd	Zdd Z								dddZd	d
 Z  ZS )ClientzcImplements the OAuth 2.0 token exchange spec based on
    https://tools.ietf.org/html/rfc8693.
    Nc                    s   t t| | || _dS )a5  Initializes an STS client instance.

        Args:
            token_exchange_endpoint (str): The token exchange endpoint.
            client_authentication (Optional(google.oauth2.oauth2_utils.ClientAuthentication)):
                The optional OAuth client authentication credentials if available.
        N)superr   __init___token_exchange_endpoint)selftoken_exchange_endpointclient_authentication	__class__ `/var/www/html/chefvision.cloud.itp360.com/venv/lib/python3.10/site-packages/google/oauth2/sts.pyr   1   s   
zClient.__init__c           
      C   s   t  }|rt| D ]\}}|||< q| || || jd|tj|	dd}t
|jdr7|jdn|j}|jtjkrEt| t|}	|	S )NPOSTzutf-8)urlmethodheadersbodydecode)_URLENCODED_HEADERScopydictitems#apply_client_authentication_optionsr   urllibparse	urlencodeencodehasattrdatar   statushttp_clientOKr   handle_error_responsejsonloads)
r   requestr   request_bodyrequest_headerskvresponseresponse_bodyresponse_datar   r   r   _make_request<   s&   

	

zClient._make_requestc                 C   sx   |||d |pg ||||	|
dd
}|rtjt||d< t| D ]\}}|du s1|dkr4||= q%| |||S )aT  Exchanges the provided token for another type of token based on the
        rfc8693 spec.

        Args:
            request (google.auth.transport.Request): A callable used to make
                HTTP requests.
            grant_type (str): The OAuth 2.0 token exchange grant type.
            subject_token (str): The OAuth 2.0 token exchange subject token.
            subject_token_type (str): The OAuth 2.0 token exchange subject token type.
            resource (Optional[str]): The optional OAuth 2.0 token exchange resource field.
            audience (Optional[str]): The optional OAuth 2.0 token exchange audience field.
            scopes (Optional[Sequence[str]]): The optional list of scopes to use.
            requested_token_type (Optional[str]): The optional OAuth 2.0 token exchange requested
                token type.
            actor_token (Optional[str]): The optional OAuth 2.0 token exchange actor token.
            actor_token_type (Optional[str]): The optional OAuth 2.0 token exchange actor token type.
            additional_options (Optional[Mapping[str, str]]): The optional additional
                non-standard Google specific options.
            additional_headers (Optional[Mapping[str, str]]): The optional additional
                headers to pass to the token exchange endpoint.

        Returns:
            Mapping[str, str]: The token exchange JSON-decoded response data containing
                the requested token and its expiration time.

        Raises:
            google.auth.exceptions.OAuthError: If the token endpoint returned
                an error.
         N)

grant_typeresourceaudiencescoperequested_token_typesubject_tokensubject_token_typeactor_tokenactor_token_typeoptionsr8    )	joinr   r   quoter#   dumpsr   r   r-   )r   r%   r/   r4   r5   r0   r1   scopesr3   r6   r7   additional_optionsadditional_headersr&   r(   r)   r   r   r   exchange_token_   s$   .zClient.exchange_tokenc                 C   s   |  |dd|dS )a  Exchanges a refresh token for an access token based on the
        RFC6749 spec.

        Args:
            request (google.auth.transport.Request): A callable used to make
                HTTP requests.
            subject_token (str): The OAuth 2.0 refresh token.
        Nrefresh_token)r/   rA   )r-   )r   r%   rA   r   r   r   rA      s
   
zClient.refresh_token)N)NNNNNNNN)	__name__
__module____qualname____doc__r   r-   r@   rA   __classcell__r   r   r
   r   r   ,   s    )
Cr   )rE   http.clientclientr    r#   r   google.oauth2r   r   OAuthClientAuthHandlerr   r   r   r   r   <module>   s   