o
    ~j6h                     @   sr   d Z ddlZddlZddlZddlZddlmZ G dd dejZG dd de	Z
G dd	 d	ejd
Zdd ZdS )a'  OAuth 2.0 Utilities.

This module provides implementations for various OAuth 2.0 utilities.
This includes `OAuth error handling`_ and
`Client authentication for OAuth flows`_.

OAuth error handling
--------------------
This will define interfaces for handling OAuth related error responses as
stated in `RFC 6749 section 5.2`_.
This will include a common function to convert these HTTP error responses to a
:class:`google.auth.exceptions.OAuthError` exception.


Client authentication for OAuth flows
-------------------------------------
We introduce an interface for defining client authentication credentials based
on `RFC 6749 section 2.3.1`_. This will expose the following
capabilities:

    * Ability to support basic authentication via request header.
    * Ability to support bearer token authentication via request header.
    * Ability to support client ID / secret authentication via request body.

.. _RFC 6749 section 2.3.1: https://tools.ietf.org/html/rfc6749#section-2.3.1
.. _RFC 6749 section 5.2: https://tools.ietf.org/html/rfc6749#section-5.2
    N)
exceptionsc                   @   s   e Zd ZdZdZdS )ClientAuthType      N)__name__
__module____qualname__basicrequest_body r   r   b/var/www/html/chefvision.cloud.itp360.com/venv/lib/python3.10/site-packages/google/oauth2/utils.pyr   5   s    r   c                   @   s   e Zd ZdZdddZdS )ClientAuthenticationzDefines the client authentication credentials for basic and request-body
    types based on https://tools.ietf.org/html/rfc6749#section-2.3.1.
    Nc                 C   s   || _ || _|| _dS )a  Instantiates a client authentication object containing the client ID
        and secret credentials for basic and response-body auth.

        Args:
            client_auth_type (google.oauth2.oauth_utils.ClientAuthType): The
                client authentication type.
            client_id (str): The client ID.
            client_secret (Optional[str]): The client secret.
        N)client_auth_type	client_idclient_secret)selfr   r   r   r   r   r   __init__?   s   

zClientAuthentication.__init__N)r   r   r   __doc__r   r   r   r   r   r   :   s    r   c                       s@   e Zd ZdZd fdd	Z	dddZdddZd	d
 Z  ZS )OAuthClientAuthHandlerzUAbstract class for handling client authentication in OAuth-based
    operations.
    Nc                    s   t t|   || _dS )zInstantiates an OAuth client authentication handler.

        Args:
            client_authentication (Optional[google.oauth2.utils.ClientAuthentication]):
                The OAuth client authentication credentials if available.
        N)superr   r   _client_authentication)r   client_authentication	__class__r   r   r   S   s   
zOAuthClientAuthHandler.__init__c                 C   s&   |  || |du r| | dS dS )a  Applies client authentication on the OAuth request's headers or POST
        body.

        Args:
            headers (Mapping[str, str]): The HTTP request header.
            request_body (Optional[Mapping[str, str]]): The HTTP request body
                dictionary. For requests that do not support request body, this
                is None and will be ignored.
            bearer_token (Optional[str]): The optional bearer token.
        N)_inject_authenticated_headers"_inject_authenticated_request_body)r   headersr
   bearer_tokenr   r   r   #apply_client_authentication_options]   s   z:OAuthClientAuthHandler.apply_client_authentication_optionsc                 C   sv   |d urd| |d< d S | j d ur7| j jtju r9| j j}| j jp!d}td||f  	 }d| |d< d S d S d S )Nz	Bearer %sAuthorization z%s:%szBasic %s)
r   r   r   r	   r   r   base64	b64encodeencodedecode)r   r   r   usernamepasswordcredentialsr   r   r   r   p   s   
z4OAuthClientAuthHandler._inject_authenticated_headersc                 C   sR   | j d ur%| j jtju r'|d u rtd| j j|d< | j jp d|d< d S d S d S )Nz*HTTP request does not support request-bodyr   r!   r   )r   r   r   r
   r   
OAuthErrorr   r   )r   r
   r   r   r   r      s   


z9OAuthClientAuthHandler._inject_authenticated_request_bodyr   )NN)	r   r   r   r   r   r   r   r   __classcell__r   r   r   r   r   N   s    

r   )	metaclassc              	   C   s   z4g }t | }|d|d  d|v r |d|d  d|v r.|d|d  d|}W n ttfyA   | }Y nw t|| )zTranslates an error response from an OAuth operation into an
    OAuthError exception.

    Args:
        response_body (str): The decoded response data.

    Raises:
        google.auth.exceptions.OAuthError
    zError code {}errorerror_descriptionz: {}	error_uriz - {}r!   )	jsonloadsappendformatjoinKeyError
ValueErrorr   r)   )response_bodyerror_components
error_dataerror_detailsr   r   r   handle_error_response   s   

r:   )r   abcr"   enumr/   google.authr   Enumr   objectr   ABCMetar   r:   r   r   r   r   <module>   s   B