
    B7                     n   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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K$J&r&  Sr' " S S\
RP                  5      r( " S S\(\RR                  5      r* " S S\(\RR                  5      r+ " S S\(\RX                  5      r- " S S \R\                  5      r.  S)S! jr/S" r0\Rb                  " S#\0S$9S% 5       r2S& r3 " S' S(\	R\                  5      r4g)*z5Customizations of google auth credentials for gcloud.    )absolute_import)division)unicode_literalsN)_helpers)credentials)
exceptions) external_account_authorized_user)_client)reauth)context_aware)http)log)
properties)
console_io)retry)client)errors)http_client)urllibz$https://oauth2.googleapis.com/revokec                       \ rS rSrSrSrg)Error0   z2Exceptions for the google_auth_credentials module. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       >lib/googlecloudsdk/core/credentials/google_auth_credentials.pyr   r   0   s    :r!   r   c                       \ rS rSrSrSrg)ReauthRequiredError4   z#Exceptions when reauth is required.r   Nr   r   r!   r"   r$   r$   4   s    +r!   r$   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )ContextAwareAccessDeniedError8   z!Exceptions when access is denied.c                 d   > [         [        U ]  [        R                  R                  5       5        g N)superr'   __init__r   ContextAwareAccessErrorGet)self	__class__s    r"   r,   &ContextAwareAccessDeniedError.__init__;   s%    	
'7--113r!   r   )r   r   r   r   r   r,   r    __classcell__r0   s   @r"   r'   r'   8   s    ) r!   r'   c                       \ rS rSrSrSrg)TokenRevokeErrorA   z<Exceptions when revoking google auth user credentials fails.r   Nr   r   r!   r"   r5   r5   A   s    Dr!   r5   c                   j   ^  \ rS rSrSrU 4S jrU 4S jr\S 5       rS r	S r
S r\S	 5       rS
rU =r$ )CredentialsH   zExtends user credentials of the google auth library for reauth.

reauth is not supported by the google auth library. However, gcloud supports
reauth. This class is to override the refresh method to handle reauth.
c                 d   > SU;   a  US   U l         US	 OS U l         [        [        U ]  " U0 UD6  g )N
rapt_token)_rapt_tokenr+   r8   r,   )r/   argskwargsr0   s      r"   r,   Credentials.__init__O   s<    v-d

d	+t%t6v6r!   c                 X   > [         [        U ]  U5        UR                  S5      U l        g )Nr<   )r+   r8   __setstate__getr<   )r/   dr0   s     r"   rA   Credentials.__setstate__W   s#    	+t)!,uu]+Dr!   c                     U R                   $ )zReauth proof token.)r<   r/   s    r"   r;   Credentials.rapt_token[   s     r!   c                 T    U R                  U5      $ ! [         Ga    [        R                  " 5       (       d  [        R
                  " S5      e[        R                  R                  R                  R                  5       (       a  [        R                  " S5         SSKJn  SUl        [         R"                  " UU R$                  U R&                  U R(                  U R*                  [-        U R.                  =(       d    / 5      5      U l         GO	! [2        R4                   a  n[        R
                  " S5      UeSnAf[6         a    [        R8                  " S5      ef = f[        R                  " S	5        [:        R<                  (       a  SOS
n[>        R@                  " US9RB                  n[D        RF                  " UU R$                  U R&                  U R(                  U R*                  [-        U R.                  =(       d    / 5      5      U l         Of = fU R                  U5      $ )zRefreshes the access token and handles reauth request when it is asked.

Args:
  request: google.auth.transport.Request, a callable used to make HTTP
    requests.
z/cannot prompt during non-interactive execution.zusing google-auth reauthr   )customauthenticator   ziA security key reauthentication challenge was issued but no key was found. Try manually reauthenticating.NzgFailed to obtain reauth rapt token. Did you touch the security key within the 15 second timeout window?zusing oauth2client reauthzutf-8)response_encoding)$_Refreshr$   r   	CanPromptgoogle_auth_exceptionsReauthFailErrorr   VALUESauthreauth_use_google_authGetBoolr   debugpyu2f.conveniencerI   U2F_SIGNATURE_TIMEOUT_SECONDSgoogle_auth_reauthget_rapt_token
_client_id_client_secret_refresh_token
_token_urilistscopesr<   pyu2f_errors
OsHidErrorKeyErrorRefreshErrorsixPY2r   Httprequestr   GetRaptToken)r/   rf   rI   erK   http_requests         r"   refreshCredentials.refresh`   s   <
]]7## :
!!##$44=
 	
 
					6	6	>	>	@	@		,- 	
; ?A

;/>>oo!!!!oo4;;$"%$
 && 	 '66; 
  		 '339 		 			-.$'GGDyy3DEMM!..OOOO"#
g:
x ==!!s0    BHA1DE$D;;$EB4HHc           	         U R                   b'  U R                  b  U R                  b  U R                  c  [        R
                  " S5      e[        U SS 5      n[        XR                  U R                   U R                  U R                  U R                  U5      u  p4pVX0l	        XPl
        X@l         UR                  S5      U l        UR                  S5      U l        U R                  (       ax  SU;   aq  [        U R                  5      n[        US   R                  5       5      nXx-
  n	U	(       a4  [        R
                  " SR!                  SR#                  U	5      5      5      eg g g )NzThe credentials do not contain the necessary fields need to refresh the access token. You must specify refresh_token, token_uri, client_id, and client_secret.r<   id_tokenscopezUNot all requested scopes were granted by the authorization server, missing scopes {}.z, )r[   r\   rY   rZ   rN   rb   getattr_RefreshGrant_scopestokenexpiryrB   	_id_tokenid_tokenb64	frozensetsplitformatjoin)
r/   rf   r;   access_tokenrefresh_tokenrs   grant_responserequested_scopesgranted_scopes scopes_requested_but_not_granteds
             r"   rL   Credentials._Refresh   s>   #t'>4#6#6#>"//56 6 }d3J:G$"5"5tT\\:;77L JK'#''
3DN &))*5D||>1"4<<0 !8!>!>!@An)9)J&	)$1177=v		:;8=> 	> 
*	 2|r!   c                    SU R                   =(       d    U R                  0n[        R                  " [        U5      nS[
        R                  0nU" X4SS9nUR                  [        R                  :w  ac  [        R                  " UR                  5      n[        R                  " U5      nUR                  S5      nUR                  S5      n	[!        X5      eg )Nrr   content-typePOST)headersmethoderrorerror_description)r{   rr   r   update_queryGOOGLE_REVOKE_URIgoogle_auth_client_URLENCODED_CONTENT_TYPEstatusr   OKrc   ensure_textdatajsonloadsrB   r5   )
r/   rf   query_paramstoken_revoke_urir   responseresponse_dataresponse_jsonr   r   s
             r"   revokeCredentials.revoke   s    T//=4::>L,,->M*CCG 'HH+..(oohmm4mjj/m(e'++,?@U66 )r!   c                 D   [        U[        R                  5      (       ap  U " UR                  UR                  UR
                  UR                  UR                  UR                  UR                  UR                  S9nUR                  Ul        U$ [        U[        R                  5      (       a]  U " UR                  UR                  UR                  UR                  UR                  UR                  UR                  UR                  S9$ [        R                  " S5      e)a  Creates an object from creds of google.oauth2.credentials.Credentials.

Args:
  creds: Union[
      google.oauth2.credentials.Credentials,
      google.auth.external_account_authorized_user.Credentials
  ], The input credentials.
Returns:
  Credentials of Credentials.
)r{   rm   	token_uri	client_idclient_secretr^   quota_project_id)rs   r{   r   r   r   r^   r   zInvalid Credentials)
isinstancer   r8   rr   r{   rm   r   r   r   r^   r   rs   ,google_auth_external_account_authorized_user	token_urlr   InvalidCredentials)clscredsress      r"   FromGoogleAuthUserCredentials)Credentials.FromGoogleAuthUserCredentials   s     %0011
++++>>OOOO++ 113c <<cjj%>JJL L
++++OOOO++ 113 3 
'
'(=
>>r!   )rt   r<   r[   rs   ru   rr   )r   r   r   r   r   r,   rA   propertyr;   rj   rL   r   classmethodr   r    r2   r3   s   @r"   r8   r8   H   sK    7,  E"N>>7 $? $?r!   r8   c                    S[         R                  4SU4SU4SU4/nU(       a"  UR                  SSR                  U5      45        U(       a  UR                  SU45        [	        XU5      n US   n	UR                  SU5      n[         R                  " U5      nW	X,U4$ ! [
         a7  n
[        R                  " S	U5      n[        R                  " X5         S
n
A
NiS
n
A
ff = f)z>Prepares the request to send to auth server to refresh tokens.
grant_typer   r   r{   rn    raptrz   zNo access token in response.N)r   _REFRESH_GRANT_TYPEappendry   _TokenEndpointRequestWithRetryra   rN   rb   rc   
raise_fromrB   _parse_expiry)rf   r   r{   r   r   r^   r;   bodyr   rz   
caught_excnew_excrs   s                r"   rp   rp      s     ';;<I&&	
$ KK#((6*+,KK$%0TJ-( 0L  ##O]C-++M:&	}m	;; 
 ($11&7GNN7''(s   -B 
C )-CC c                 :    AAAU [         :g  =(       a	    U [        :g  $ )a  Whether to retry the request when receive errors.

Do not retry reauth-related errors or context aware access errors.
Retrying won't help in those situations.

Args:
  exc_type: type of the raised exception.
  exc_value: the instance of the raise the exception.
  exc_traceback: Traceback, traceback encapsulating  the call stack at the the
    point where the exception occurred.
  state: RetryerState, state of the retryer.

Returns:
  True if exception and is not due to reauth-related errors or context-aware
  access restriction.
)r$   r'   )exc_type	exc_valueexc_tracebackstates       r"   _ShouldRetryServerInternalErrorr     s'    " 
)
) 4
3
35r!      )max_retrialsshould_retry_ifc                 6   [         R                  R                  U5      nS[        R                  0nU " SXUS9n[
        R                  " UR                  5      nUR                  [        R                  :w  a  [        U5        [        R                  " U5      nU$ )af  Makes a request to the OAuth 2.0 authorization server's token endpoint.

Args:
    request: google.auth.transport.Request, A callable used to make HTTP
      requests.
    token_uri: str, The OAuth 2.0 authorizations server's token endpoint URI.
    body: {str: str}, The parameters to send in the request body.

Returns:
    The JSON-decoded response data.
r   r   )r   urlr   r   )r   parse	urlencoder   r   rc   r   r   r   r   r   _HandleErrorResponser   r   )rf   r   r   r   r   response_bodyr   s          r"   r   r   3  sy     
			%$(AA' F	N(//(--0-__&'**]+-	r!   c                    [         R                  " U 5      nUR                  S5      nUR                  S5      nU[        R                  :X  a3  U[        R
                  :X  d  U[        R                  :X  a  [        S5      e [        R                  " US5        g! [        R                   a+  n[        R                  " U5      (       a
  [        5       ee SnAff = f)au  "Translates an error response into an exception.

Args:
    response_body: str, The decoded response data.

Raises:
    google.auth.exceptions.RefreshError: If the token endpoint returned
        an server internal error.
    ContextAwareAccessDeniedError: if the error was due to a context aware
        access restriction.
    ReauthRequiredError: If reauth is required.
r   error_subtypezreauth is required.FN)r   r   rB   oauth2client_clientREAUTH_NEEDED_ERROR REAUTH_NEEDED_ERROR_INVALID_RAPT!REAUTH_NEEDED_ERROR_RAPT_REQUIREDr$   r   _handle_error_responserN   rb   r   IsContextAwareAccessDeniedErrorr'   )r   
error_data
error_coder   rh   s        r"   r   r   R  s     zz-(*~~g&*..1-&:::*KKK*LLL
3
44
--j%@		,	, 
44Q77)++	
s   B C-&CCc                   B   ^  \ rS rSrSrU 4S jr\S 5       rS rSr	U =r
$ )AccessTokenCredentialsio  z,A credential represented by an access token.c                 6   > [         [        U ]  5         Xl        g r*   )r+   r   r,   rr   )r/   rr   r0   s     r"   r,   AccessTokenCredentials.__init__r  s    	
 $02Jr!   c                     g)NFr   rF   s    r"   expiredAccessTokenCredentials.expiredv  s    r!   c                     Ag r*   r   )r/   rf   s     r"   rj   AccessTokenCredentials.refreshz  s    r!   )rr   )r   r   r   r   r   r,   r   r   rj   r    r2   r3   s   @r"   r   r   o  s)    4  	 	r!   r   )NN)5r   
__future__r   r   r   r   google.authr   r   google_auth_credentialsr   rN   r	   r   google.oauth2r
   r   r   rW   googlecloudsdk.corer   r   r   r   googlecloudsdk.core.consoler   googlecloudsdk.core.utilr   oauth2clientr   r   oauth2client.contribpyu2fr   r_   rc   	six.movesr   r   r   r   rb   r$   r'   GoogleAuthErrorr5   r8   rp   r   RetryOnExceptionr   r   r   r   r!   r"   <module>r      s   < '  '    > < h 7 % 6 - * $ # * 2 * 6 ' ( 
 ! : ;J ;,%!7!D!D ,E+A+N+N Eu4DD Eq?+)) q?r !<B5, $CEE:
:	4@@ 	r!   