a
    !f(                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZ e eZ	dZ
ejfZG dd deZdd Zd	d
 Zdd Zdd Zdd Zdd Zdd ZdddejdfddZee ZdS )    N)http_client)_helpers)readseektellc                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )MemoryCachez8httplib2 Cache implementation which only caches locally.c                 C   s
   i | _ d S Ncache)self r   W/var/www/html/python-backend/venv/lib/python3.9/site-packages/oauth2client/transport.py__init__#   s    zMemoryCache.__init__c                 C   s   | j |S r   )r
   getr   keyr   r   r   r   &   s    zMemoryCache.getc                 C   s   || j |< d S r   r	   )r   r   valuer   r   r   set)   s    zMemoryCache.setc                 C   s   | j |d  d S r   )r
   popr   r   r   r   delete,   s    zMemoryCache.deleteN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r       s
   r   c                   C   s   t S )a  Return an HTTP object which caches results returned.

    This is intended to be used in methods like
    oauth2client.client.verify_id_token(), which calls to the same URI
    to retrieve certs.

    Returns:
        httplib2.Http, an HTTP object with a MemoryCache
    )_CACHED_HTTPr   r   r   r   get_cached_http0   s    
r   c                  O   s   t j| i |S )a>  Return a new HTTP object.

    Args:
        *args: tuple, The positional arguments to be passed when
               contructing a new HTTP object.
        **kwargs: dict, The keyword arguments to be passed when
                  contructing a new HTTP object.

    Returns:
        httplib2.Http, an HTTP object.
    )httplib2Http)argskwargsr   r   r   get_http_object=   s    r    c                 C   s   | du ri S t | S )zCreates a copy of the headers.

    Args:
        headers: dict, request headers to copy.

    Returns:
        dict, the copied headers or a new dictionary if the headers
        were None.
    N)dict)headersr   r   r   _initialize_headersL   s    
r#   c                 C   s2   |dur.d| v r&|d | d  | d< n|| d< | S )a,  Adds a user-agent to the headers.

    Args:
        headers: dict, request headers to add / modify user
                 agent within.
        user_agent: str, the user agent to add.

    Returns:
        dict, the original headers passed in, but modified if the
        user agent is not None.
    Nz
user-agent r   )r"   
user_agentr   r   r   _apply_user_agentY   s
    r&   c                 C   s   i }zTt | D ]D\}}t|t js,t|}t|t js@t|}t||t|< qW n* ty   ddlm	} ||d|Y n0 |S )a  Forces header keys and values to be strings, i.e not unicode.

    The httplib module just concats the header keys and values in a way that
    may make the message header a unicode string, which, if it then tries to
    contatenate to a binary request body may result in a unicode decode error.

    Args:
        headers: dict, A dictionary of headers.

    Returns:
        The same dictionary but with all the keys converted to strings.
    r   )NonAsciiHeaderErrorz: )
six	iteritems
isinstancebinary_typestrr   	_to_bytesUnicodeEncodeErrorZoauth2client.clientr'   )r"   cleankvr'   r   r   r   clean_headersn   s    r2   c                    s4   |j dddtjdf fdd	}||_  |j _dS )a  Prepares an HTTP object's request method for auth.

    Wraps HTTP requests with logic to catch auth failures (typically
    identified via a 401 status code). In the event of failure, tries
    to refresh the token used and then retry the original request.

    Args:
        credentials: Credentials, the credentials used to identify
                     the authenticated user.
        http: httplib2.Http, an http object to be used to make
              auth requests.
    GETNc              	      s   j std  t|}| t|j d }t fddt	D rZ 
 }t| | t|||\}}d}	t|	D ]j}
|jtvr qtd|j|
d |	  | |d urЈ | t| | t|||\}}q||fS )Nz1Attempting refresh to obtain initial access_tokenc                 3   s   | ]}t  |d V  qd S r   getattr).0Zstream_propbodyr   r   	<genexpr>       z:wrap_http_for_auth.<locals>.new_request.<locals>.<genexpr>   z&Refreshing due to a %s (attempt %s/%s)   )access_token_LOGGERinfoZ_refreshr#   applyr&   r%   all_STREAM_PROPERTIESr   requestr2   rangestatusREFRESH_STATUS_CODESr   )urimethodr8   r"   redirectionsconnection_typeZbody_stream_positionrespcontentZmax_refresh_attemptsZrefresh_attemptcredentialsorig_request_methodr7   r   new_request   s@    










z'wrap_http_for_auth.<locals>.new_request)rC   r   DEFAULT_MAX_REDIRECTSrN   rN   httprP   r   rM   r   wrap_http_for_auth   s    -rT   c                    sF   |j t| |j  dddtjdf fdd	}||_ |j _dS )a  Prepares an HTTP object's request method for JWT access.

    Wraps HTTP requests with logic to catch auth failures (typically
    identified via a 401 status code). In the event of failure, tries
    to refresh the token used and then retry the original request.

    Args:
        credentials: _JWTAccessCredentials, the credentials used to identify
                     a service account that uses JWT access tokens.
        http: httplib2.Http, an http object to be used to make
              auth requests.
    r3   Nc           	         s   dj v r8jd u sjr$d  t | |||||S t|}t|j | ddd }	d|i\}}d| |d< t| ||t
|||S d S )NZaud?r<   r   zBearer Authorization)_kwargsr=   Zaccess_token_expiredrefreshrC   r#   r&   r%   splitZ_create_tokenr2   )	rG   rH   r8   r"   rI   rJ   Zuri_roottokenZunused_expiryZauthenticated_request_methodrN   rO   r   r   rP      s$    



z-wrap_http_for_jwt_access.<locals>.new_request)rC   rT   r   rQ   rN   rR   r   r[   r   wrap_http_for_jwt_access   s    
r\   r3   c                 C   s    t | d| }|||||||dS )a  Make an HTTP request with an HTTP object and arguments.

    Args:
        http: httplib2.Http, an http object to be used to make requests.
        uri: string, The URI to be requested.
        method: string, The HTTP method to use for the request. Defaults
                to 'GET'.
        body: string, The payload / body in HTTP request. By default
              there is no payload.
        headers: dict, Key-value pairs of request headers. By default
                 there are no headers.
        redirections: int, The number of allowed 203 redirects for
                      the request. Defaults to 5.
        connection_type: httplib.HTTPConnection, a subclass to be used for
                         establishing connection. If not set, the type
                         will be determined from the ``uri``.

    Returns:
        tuple, a pair of a httplib2.Response with the status code and other
        headers and the bytes of the content returned.
    rC   )rH   r8   r"   rI   rJ   r4   )rS   rG   rH   r8   r"   rI   rJ   Zhttp_callabler   r   r   rC      s
    
rC   )loggingr   r(   Z	six.movesr   Zoauth2clientr   	getLoggerr   r>   rB   UNAUTHORIZEDrF   objectr   r   r    r#   r&   r2   rT   r\   rQ   rC   r   r   r   r   r   r   <module>   s(   
C2
