
    g                         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ej                        Z G d de	      Z
 G d d	ej                  
      Zd Zy)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                       e Zd ZdZdZy)ClientAuthType      N)__name__
__module____qualname__basicrequest_body     H/var/www/openai/venv/lib/python3.12/site-packages/google/oauth2/utils.pyr   r   5   s    ELr   r   c                       e Zd ZdZddZy)ClientAuthenticationzDefines the client authentication credentials for basic and request-body
    types based on https://tools.ietf.org/html/rfc6749#section-2.3.1.
    Nc                 .    || _         || _        || _        y)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   s       r   __init__zClientAuthentication.__init__?   s     !1"*r   N)r   r	   r
   __doc__r   r   r   r   r   r   :   s    +r   r   c                   <     e Zd ZdZd fd	Z	 ddZddZd Z xZS )OAuthClientAuthHandlerzUAbstract class for handling client authentication in OAuth-based
    operations.
    c                 8    t         t        |           || _        y)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__s     r   r   zOAuthClientAuthHandler.__init__S   s     	$d46&;#r   c                 P    | j                  ||       || j                  |       yy)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_tokens       r   #apply_client_authentication_optionsz:OAuthClientAuthHandler.apply_client_authentication_options]   s-     	**7LA33LA  r   c                 ^   |	d|z  |d<   y | j                   | j                   j                  t        j                  u ro| j                   j                  }| j                   j
                  xs d}t        j                  |d|j                               j                         }d|z  |d<   y y y )Nz	Bearer %sAuthorization :zBasic %s)
r   r   r   r   r   r   base64	b64encodeencodedecode)r   r$   r%   usernamepasswordcredentialss         r   r"   z4OAuthClientAuthHandler._inject_authenticated_headersp   s    #'2\'AGO$''3++<<@T@TT22<<H22@@FBH **$h/779fh  (2K'?GO$ U 4r   c                    | j                   u| j                   j                  t        j                  u rN|t	        j
                  d      | j                   j                  |d<   | j                   j                  xs d|d<   y y y )Nz*HTTP request does not support request-bodyr   r)   r   )r   r   r   r   r   
OAuthErrorr   r   )r   r   s     r   r#   z9OAuthClientAuthHandler._inject_authenticated_request_body   s    ''3++<<**+ # ++@  -1,G,G,Q,Q[)//==C _-+ 4r   r   )NN)	r   r	   r
   r   r   r&   r"   r#   __classcell__)r    s   @r   r   r   N   s#    < 8<B&@r   r   )	metaclassc                    	 g }t        j                  |       }|j                  dj                  |d                d|v r#|j                  dj                  |d                d|v r#|j                  dj                  |d                dj	                  |      }t        j                  ||       # t
        t        f$ r | }Y )w xY w)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   r3   )response_bodyerror_components
error_dataerror_detailss       r   handle_error_responserE      s    &ZZ.
 6 6z'7J KL*,##FMM*=P2Q$RS*$##GNN:k3J$KL 01
 

}
== j! &%&s   BB1 1CC)r   abcr+   enumr:   google.authr   Enumr   objectr   ABCMetar   rE   r   r   r   <module>rL      sM   8     "
TYY 
+6 +(?s{{ ?D>r   