U
    #FZh                     @   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   Z/home/aprabhat/apps/x.techxrdev.in/venv/lib/python3.8/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   Zclient_authentication	__class__r   r   r   S   s    zOAuthClientAuthHandler.__init__c                 C   s"   |  || |dkr| | 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   sl   |d k	rd| |d< nR| j d k	rh| j jtjkrh| j j}| j jp@d}td||f  	 }d| |d< 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   sL   | j d k	rH| j jtjkrH|d kr,tdn| j j|d< | j jpBd|d< 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_body)N)NN)N)	r   r   r   r   r   r   r   r   __classcell__r   r   r   r   r   N   s      

r   )	metaclassc              	   C   s   zhg }t | }|d|d  d|kr@|d|d  d|kr\|d|d  d|}W n ttfk
r   | }Y nX t|| dS )	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 {}errorZerror_descriptionz: {}Z	error_uriz - {}r   N)	jsonloadsappendformatjoinKeyError
ValueErrorr   r'   )Zresponse_bodyZerror_componentsZ
error_dataZerror_detailsr   r   r   handle_error_response   s    


r2   )r   abcr    enumr+   Zgoogle.authr   Enumr   objectr   ABCMetar   r2   r   r   r   r   <module>   s   B