
    m                     6   S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSKJ	r	  SSKJ
r
  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJ
r  SSKJr  SSKJr  SSKJr  SSKrSSKJ
r   " S S\
R4                  5      rSrSr " S S\5      r " S S\5      rS r S r!g)z0Module for wrapping transports with credentials.    )absolute_import)division)unicode_literalsN)base)context_aware)
exceptions)log)
properties)	transport)creds)store)files)clientc                       \ rS rSrSrSrg)Error'   z0Exceptions for the credentials transport module. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       0lib/googlecloudsdk/core/credentials/transport.pyr   r   '   s    8r   r   USER_PROJECT_DENIEDzgoogleapis.comc                   N    \ rS rSrSr SS jrS r\R                  S 5       r	Sr
g)	QuotaHandlerMixin/   z!Mixin for handling quota project.Nc                 p    U(       d  gUc  [         R                  " X#5      n[        R                  " U5      $ )z2Returns None or the quota project for credentials.N)r   LoadIfEnabled
core_credsGetQuotaProject)selfenable_resource_quotaallow_account_impersonationuse_google_authcredentialss        r   QuotaProjectQuotaHandlerMixin.QuotaProject2   s6     !''
%k %%k22r   c                     [         R                  " [         R                  " SU5      5      /nU R                  X5        UR                  $ )z=Returns a request method which adds the quota project header.zX-Goog-User-Project)r   Handler	SetHeaderWrapRequestrequest)r$   http_clientquota_projecthandlerss       r   QuotaWrappedRequest%QuotaHandlerMixin.QuotaWrappedRequestB   sF     	 5}E	
H
 	[+r   c                     g)a  Returns a http_client with quota project handling.

Args:
  http_client: The http client to be wrapped.
  enable_resource_quota: bool, By default, we are going to tell APIs to use
    the quota of the project being operated on. For some APIs we want to use
    gcloud's quota, so you can explicitly disable that behavior by passing
    False here.
  allow_account_impersonation: bool, True to allow use of impersonated
    service account credentials for calls made with this client. If False,
    the active user credentials will always be used.
  use_google_auth: bool, True if the calling command indicates to use
    google-auth library for authentication. If False, authentication will
    fallback to using the oauth2client library. If None, set the value based
    the configuration.
Nr   )r$   r0   r%   r&   r'   s        r   	WrapQuotaQuotaHandlerMixin.WrapQuotaL       r   r   )N)r   r   r   r   r   r)   r3   abcabstractmethodr6   r   r   r   r   r   r   /   s.    ) 3   r   r   c                   L    \ rS rSrSr   SS jr\R                  S 5       rSr	g)CredentialWrappingMixinf   z+Mixin for wrapping authorized http clients.Nc                    [         R                  R                  R                  R	                  5       n[         R                  R                  R
                  R	                  5       n[        XV5      nUc  [        R                  " 5       nUc  [        R                  " X#5      nU(       a  U R                  X5      n[        USU5        U R                  X[        [        R                   ["        R$                  45        U$ )a  Get an http client for working with Google APIs.

Args:
  http_client: The http client to be wrapped.
  allow_account_impersonation: bool, True to allow use of impersonated
    service account credentials for calls made with this client. If False,
    the active user credentials will always be used.
  use_google_auth: bool, True if the calling command indicates to use
    google-auth library for authentication. If False, authentication will
    fallback to using the oauth2client library. If None, set the value based
    the configuration.
  credentials: google.auth.credentials.Credentials, The credentials to use.

Returns:
  An authorized http client with exception handling.

Raises:
  creds_exceptions.Error: If an error loading the credentials occurs.
_googlecloudsdk_credentials)r
   VALUESauthauthority_selectorGetauthorization_token_file_GetIAMAuthHandlersr   UseGoogleAuthr   r!   AuthorizeClientsetattrr.   _HandleAuthErrorr   AccessTokenRefreshErrorgoogle_auth_exceptionsRefreshError)r$   r0   r&   r'   r(   rB   rD   r2   s           r   WrapCredentials'CredentialWrappingMixin.WrapCredentialsi   s    8 $**//BBFFH77;;=  ##5PH**,o''
%k ((Bk k8+F/		'	')?)L)LMO r   c                     g)z=Returns an http_client authorized with the given credentials.Nr   )r$   r0   r(   s      r   rG   'CredentialWrappingMixin.AuthorizeClient   r8   r   r   )TNN)
r   r   r   r   r   rM   r9   r:   rG   r   r   r   r   r<   r<   f   s2    3
 #'2h H Hr   r<   c           	         SnU(       a   [         R                  " U5      n/ nU (       a:  UR                  [        R
                  " [        R                  " SU 5      5      5        U(       aH  UR                  [        R
                  " [        R                  " SUR                  5       5      5      5        U$ ! [         R                   a  n[        U5      eSnAff = f)a  Get the request handlers for IAM authority selctors and auth tokens..

Args:
  authority_selector: str, The authority selector string we want to use for
    the request or None.
  authorization_token_file: str, The file that contains the authorization
    token we want to use for the request or None.

Returns:
  [transport Modifiers]: A list of request modifier functions to use to wrap
  an http request.
Nzx-goog-iam-authority-selectorzx-goog-iam-authorization-token)r   ReadFileContentsr   appendr   r,   r-   strip)rB   rD   authorization_tokener2   s        r   rE   rE      s     !223KL (OO ? 24	56
 OO @ 3 9 9 ;=	>?
 
/! ;; !Hns   B5 5C	CCc                     [         R                  " U 5      n[        R                  " SUSS9  [        R
                  " U 5      (       a  [        R                  " U5      e[        R                  " U5      e)zHandle a generic auth error and raise a nicer message.

Args:
  e: The exception that was caught.

Raises:
  creds_exceptions.TokenRefreshError: If an auth error occurs.
z(Exception caught during HTTP request: %sT)exc_info)	six	text_typer	   debugr   IsContextAwareAccessDeniedErrorcreds_exceptionsTokenRefreshDeniedByCAAErrorTokenRefreshError)rV   msgs     r   rI   rI      sY     	a#))622155

7
7
<<**3//r   )"r   
__future__r   r   r   r9   googlecloudsdk.callioper   googlecloudsdk.corer   r   r	   r
   r   googlecloudsdk.core.credentialsr   r"   r]   r   googlecloudsdk.core.utilr   oauth2clientr   rY   google.authrK   r   USER_PROJECT_ERROR_REASONUSER_PROJECT_ERROR_DOMAINobjectr   r<   rE   rI   r   r   r   <module>rk      s    7 &  ' 
 ( - * # * ) ? J 1 *  
 <9J 9 2 , 4 4n9Hf 9Hx!H0r   