
    !                         d dl Z d dlZd dlZd dlmZ d dlmZ  e j                  e      Z	dZ
ej                  fZ G d de      Zd Zd Zd	 Zd
 Zd Zd Zd Z ej,                   e             Zy)    N)http_client)	_to_bytes)readseektellc                   (    e Zd ZdZd Zd Zd Zd Zy)MemoryCachez8httplib2 Cache implementation which only caches locally.c                     i | _         y Ncache)selfs    )lib/third_party/oauth2client/transport.py__init__zMemoryCache.__init__#   s	    
    c                 8    | j                   j                  |      S r   )r   getr   keys     r   r   zMemoryCache.get&   s    zz~~c""r   c                 "    || j                   |<   y r   r   )r   r   values      r   setzMemoryCache.set)   s    

3r   c                 <    | j                   j                  |d        y r   )r   popr   s     r   deletezMemoryCache.delete,   s    

sD!r   N)__name__
__module____qualname____doc__r   r   r   r    r   r   r	   r	       s    B# "r   r	   c                      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   get_cached_httpr#   0   s
     r   c                  *    t        j                         S )zSReturn a new HTTP object.

    Returns:
        httplib2.Http, an HTTP object.
    )httplib2Httpr    r   r   get_http_objectr'   =   s     ==?r   c                      | 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.
    )dict)headerss    r   _initialize_headersr+   F   s     23d7m3r   c                 <    |d| v r|dz   | d   z   | 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.
    z
user-agent r    )r*   
user_agents     r   _apply_user_agentr/   S   sC     7"%/#%58M%MGL! N %/GL!Nr   c                 D   i }	 t        j                  |       D ]f  \  }}t        |t         j                        st	        |      }t        |t         j                        st	        |      }t        |      |t        |      <   h 	 |S # t        $ r ddlm}  |d      w xY 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.clientr1   )r*   cleankvr1   s        r   clean_headersr<   h   s     E	.MM'*DAqa1Fa1F"+A,E)A, + L  .;!!T1--.s   A>B Bc                      |j                   dddt        j                  df fd	}||_         t        |j                   d        y)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           	         j                   s&t        j                  d       j                         t	        |      }j                  |       t        |j                         d }t        fdt        D              rj                         } | |t        |      ||      \  }}d}	t        |	      D ]  }
|j                  t        vr ||fS t        j                  d|j                  |
dz   |	       j                         j                  |       |j                  |        | |t        |      ||      \  }} ||fS )Nz1Attempting refresh to obtain initial access_tokenc              3   8   K   | ]  }t        |d         y wr   )getattr).0stream_propbodys     r   	<genexpr>z:wrap_http_for_auth.<locals>.new_request.<locals>.<genexpr>   s#      #! 5@wt[$/!s      z&Refreshing due to a %s (attempt %s/%s)   )access_token_LOGGERinfo_refreshr+   applyr/   r.   all_STREAM_PROPERTIESr   r<   rangestatusREFRESH_STATUS_CODESr   )urimethodrD   r*   redirectionsconnection_typebody_stream_positionrespcontentmax_refresh_attemptsrefresh_attemptcredentialsorig_request_methods     `        r   new_requestz'wrap_http_for_auth.<locals>.new_request   sW    ''LL 0 1  !45 &g.'"';#9#9:# #!# ##'99; +C,9',B,8/Kg  !$%9:O{{"66 W} LLAo&9-/   !45g&#/		.//VT0=g0F0<oOMD'  ; W}r   r[   )requestr%   DEFAULT_MAX_REDIRECTSsetattr)r[   httpr]   r\   s   `  @r   wrap_http_for_authrb      sE     ,, !&D$!)!?!?$(*Z DL DLL-5r   c                      |j                   t         |       |j                   dddt        j                  df fd	}||_         y)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.
    r>   Nc                 `   d
j                   v r6
j                  
j                  r
j                  d         	| |||||      S t	        |      }t        |
j                         | j                  dd      d   }
j                  d|i      \  }}d|z   |d<    | ||t        |      ||      S )Naud?rG   r   zBearer Authorization)
_kwargsrH   access_token_expiredrefreshr+   r/   r.   split_create_tokenr<   )rR   rS   rD   r*   rT   rU   uri_roottokenunused_expiryauthenticated_request_methodr[   r\   s            r   r]   z-wrap_http_for_jwt_access.<locals>.new_request   s     K'''((044##D)/VT070?A A *'2Gg{'='=>yya(+H#.#<#<eX=N#O E='05'8GO$&sFD'4W'='3_F Fr   )r^   rb   r%   r_   )r[   ra   r]   rp   r\   s   `  @@r   wrap_http_for_jwt_accessrq      sG     ,,{D)#'<<  !&D$!)!?!?$(F2 DLr   )loggingr%   r2   	six.movesr   oauth2client._helpersr   	getLoggerr   rI   rN   UNAUTHORIZEDrQ   objectr	   r#   r'   r+   r/   r<   rb   rq   r&   r"   r    r   r   <module>rx      s      
 ! + '

H
%-  $002 "& " 

4*6@6F,^ x}}[]+r   