o
    !                     @   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ee ZdS )    N)http_client)	_to_bytes)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   ?/tmp/google-cloud-sdk/lib/third_party/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                   C   s   t  S )zSReturn a new HTTP object.

    Returns:
        httplib2.Http, an HTTP object.
    )httplib2Httpr   r   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_headersF   s   
r!   c                 C   s4   |durd| v r|d | d  | d< | S || 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_agentS   s   r$   c                 C   s~   i }z)t | D ] \}}t|t jst|}t|t js t|}t||t|< qW |S  ty>   ddlm} ||d|w )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   UnicodeEncodeErroroauth2client.clientr%   )r    cleankvr%   r   r   r   clean_headersh   s   	r0   c                    s:   |j dddtjdf fdd	}||_ t|j d  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 r- 
 }| | t|||\}}d}	t|	D ]7}
|jtvrL ||fS td|j|
d |	  | |d urj | | | t|||\}}q@||fS )Nz1Attempting refresh to obtain initial access_tokenc                 3   s    | ]	}t  |d V  qd S r   )getattr).0stream_propbodyr   r   	<genexpr>   s    z:wrap_http_for_auth.<locals>.new_request.<locals>.<genexpr>   z&Refreshing due to a %s (attempt %s/%s)   )access_token_LOGGERinfo_refreshr!   applyr$   r#   all_STREAM_PROPERTIESr   r0   rangestatusREFRESH_STATUS_CODESr   )urimethodr6   r    redirectionsconnection_typebody_stream_positionrespcontentmax_refresh_attemptsrefresh_attemptcredentialsorig_request_methodr5   r   new_request   sB   








z'wrap_http_for_auth.<locals>.new_requestrN   )requestr   DEFAULT_MAX_REDIRECTSsetattrrN   httprP   r   rM   r   wrap_http_for_auth   s   -rV   c                    s>   |j t| |j  dddtjdf fdd	}||_ 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.
    r1   Nc           	         s   dj v rjd u sjrd   | |||||S t|}t|j | ddd }d|i\}}d| |d< | ||t	|||S )Naud?r9   r   zBearer Authorization)
_kwargsr:   access_token_expiredrefreshr!   r$   r#   split_create_tokenr0   )	rD   rE   r6   r    rF   rG   uri_roottokenunused_expiryauthenticated_request_methodrN   rO   r   r   rP      s$   


z-wrap_http_for_jwt_access.<locals>.new_request)rQ   rV   r   rR   rT   r   rb   r   wrap_http_for_jwt_access   s   

rd   )loggingr   r&   	six.movesr   oauth2client._helpersr   	getLoggerr   r;   r@   UNAUTHORIZEDrC   objectr   r   r   r!   r$   r0   rV   rd   r   r   r   r   r   r   <module>   s"   
	C/