
    @                     P   S r SSKJr  SSKJr  SSKJr  SSKrSSK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rSrSrSrSrSrSrSr\\\\/rSr " S S\R@                  5      r  " S S\ 5      r! " S S\ 5      r"S r#S r$ " S S \RJ                  " \RL                  \'5      5      r( " S! S"\(5      r) " S# S$\(5      r* " S% S&\(5      r+ " S' S(\(5      r, " S) S*\(5      r- " S+ S,\(5      r.S3S- jr/S. r0S/ r1       S4S0 jr2S1 r3S2 r4g)5z#A library to support auth commands.    )absolute_import)division)unicode_literalsN)check_browser)config)
exceptions)log)
properties)yaml)
console_io)creds)fileszH764086051850-6qr4p6gpi6hn506pt8ejuq83di341hur.apps.googleusercontent.comzd-FL95Q19q7MQmFpd7hHD0Tyz.https://www.googleapis.com/auth/cloud-platformz0https://www.googleapis.com/auth/sqlservice.loginz%https://www.googleapis.com/auth/drivez.https://www.googleapis.com/auth/userinfo.emailopenid	installedc                       \ rS rSrSrSrg)Error;   z A base exception for this class. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       'lib/googlecloudsdk/api_lib/auth/util.pyr   r   ;   s    (r   r   c                       \ rS rSrSrSrg)InvalidClientSecretsError@   z:An error for when we fail to load the client secrets file.r   Nr   r   r   r   r   r   @   s    Br   r   c                       \ rS rSrSrSrg)BadCredentialFileExceptionE   z,Raised when credentials file cannot be read.r   Nr   r   r   r   r"   r"   E   s    4r   r"   c                 L    [         R                  " U 5      n[        U[        5      (       d  [        SR	                  U 5      5      eU$ ! [         a  n[        SR	                  X5      5      eSnAf[         R
                   a  n[        SR	                  X5      5      eSnAff = f)a  Returns the JSON content of a credentials config file.

This function is useful when the content of a file need to be inspected first
before determining how to handle it (how to initialize the underlying
credentials). Only UTF-8 JSON files are supported.

Args:
  filename (str): The filepath to the ADC file representing credentials.

Returns:
  Optional(Mapping): The JSON content.

Raises:
  BadCredentialFileException: If JSON parsing of the file fails.
z"File {0} is not utf-8 encoded: {1}Nz!Could not read json file {0}: {1}zCould not read json file {0})r   	load_pathUnicodeDecodeErrorr"   formatYAMLParseError
isinstancedict)filenamecontentes      r   GetCredentialsConfigFromFiler.   J   s    "nnX&G 
GT	"	"
$&--h79 9	. 
 B
$,33H@B B			 
$%H%O%O&  s#   A	 	
B#A--B#BB#c                     SSK Jn  UR                  U 5      (       a/  [        R                  " UR
                  R                  5       5        g[        R                  " U5        g)z2Prints help messages when auth flow throws errors.r   )context_awareN)googlecloudsdk.corer0   IsContextAwareAccessDeniedErrorr	   errorContextAwareAccessErrorGet)excdefault_help_msgr0   s      r   _HandleFlowErrorr8   m   s@     022377IIm33779:IIr   c                   P    \ rS rSrSrSrS	S jr\R                  S 5       r	S r
Srg)

FlowRunnerx   zBase auth flow runner class.

Attributes:
   _scopes: [str], The list of scopes to authorize.
   _client_config: The client configuration in the Google client secrets
     format.
z,There was a problem with web authentication.Nc                 R    Xl         X l        X0l        U R                  5       U l        g N)_scopes_client_config_redirect_uri_CreateFlow_flow)selfscopesclient_configredirect_uris       r   __init__FlowRunner.__init__   s#    L'%!!#DJr   c                     g r=   r   )rC   s    r   rA   FlowRunner._CreateFlow   s    r   c                     SSK Jn   U R                  R                  " S0 UD6$ ! UR                   a  n[        X0R                  5        e S nAff = f)Nr   flowr   )googlecloudsdk.core.credentialsrM   rB   Runr   r8   _FLOW_ERROR_HELP_MSG)rC   kwargsc_flowr-   s       r   rO   FlowRunner.Run   sE    >ZZ^^%f%%<< q334s   $ AA

A)r?   rB   r@   r>   r=   )r   r   r   r   r   rP   rG   abcabstractmethodrA   rO   r   r   r   r   r:   r:   x   s4     H$ 	 	r   r:   c                       \ rS rSrSrS rSrg)OobFlowRunner   zA flow runner to run OobFlow.c                     SSK Jn  UR                  R                  U R                  U R
                  [        R                  R                  R                  R                  5       (       + S9$ Nr   rL   autogenerate_code_verifier)rN   rM   OobFlowfrom_client_configr?   r>   r
   VALUESauthdisable_code_verifierGetBoolrC   rR   s     r   rA   OobFlowRunner._CreateFlow   sQ    >>>,,'1'8'8'='=		wwy$) - * *r   r   Nr   r   r   r   r   rA   r   r   r   r   rW   rW      s
    %*r   rW   c                       \ rS rSrSrS rSrg)NoBrowserFlowRunner   z#A flow runner to run NoBrowserFlow.c                     SSK Jn  UR                  R                  U R                  U R
                  [        R                  R                  R                  R                  5       (       + S9$ rZ   )rN   rM   NoBrowserFlowr^   r?   r>   r
   r_   r`   ra   rb   rc   s     r   rA   NoBrowserFlowRunner._CreateFlow   sS    >22'1'8'8'='=		wwy$) 3 * *r   r   Nre   r   r   r   rg   rg      s
    +*r   rg   c                       \ rS rSrSrS rSrg)"RemoteLoginWithAuthProxyFlowRunner   z2A flow runner to run RemoteLoginWithAuthProxyFlow.c                     SSK Jn  UR                  R                  U R                  U R
                  [        R                  R                  R                  R                  5       (       + U R                  S9$ )Nr   rL   )r\   rF   )rN   rM   RemoteLoginWithAuthProxyFlowr^   r?   r>   r
   r_   r`   ra   rb   r@   rc   s     r   rA   .RemoteLoginWithAuthProxyFlowRunner._CreateFlow   s]    >..AA'1'8'8'='=		wwy$)'' B ) )r   r   Nre   r   r   r   rm   rm      s
    :	)r   rm   c                       \ rS rSrSrS rSrg)NoBrowserHelperRunner   z)A flow runner to run NoBrowserHelperFlow.c                 .   SSK Jn   UR                  R                  U R                  U R
                  [        R                  R                  R                  R                  5       (       + S9$ ! UR                   a    [        R                  " S5        e f = f)Nr   rL   r[   zCannot start a local server to handle authorization redirection. Please run this command on a machine where gcloud can start a local server.)rN   rM   NoBrowserHelperFlowr^   r?   r>   r
   r_   r`   ra   rb   LocalServerCreationErrorr	   r3   rc   s     r   rA   !NoBrowserHelperRunner._CreateFlow   s    >
''::



,,)3):):)?)?  &+ ; , ,
 ** 	ii 3 4 	s   A#A, ,(Br   Nre   r   r   r   rs   rs      s
    1r   rs   c                   "    \ rS rSrSrSrS rSrg) BrowserFlowWithOobFallbackRunner   z?A flow runner to try normal web flow and fall back to oob flow.zXThere was a problem with web authentication. Try running again with --no-launch-browser.c                 2   SSK Jn   UR                  R                  U R                  U R
                  [        R                  R                  R                  R                  5       (       + S9$ ! UR                   a  n[        R                  " U5        [        R                  " S5        UR                  R                  U R                  U R
                  [        R                  R                  R                  R                  5       (       + S9s S nA$ S nAff = f)Nr   rL   r[   z"Defaulting to URL copy/paste mode.)rN   rM   FullWebFlowr^   r?   r>   r
   r_   r`   ra   rb   rw   r	   warningr]   rC   rR   r-   s      r   rA   ,BrowserFlowWithOobFallbackRunner._CreateFlow   s    >,22



,,)3):):)?)?  &+ 3 , ,
 ** ,	kk!n	kk67^^..



,,)3):):)?)?  &+ / , ,,   A#A, ,D<BDDDr   Nr   r   r   r   r   rP   rA   r   r   r   r   rz   rz      s    GH,r   rz   c                   "    \ rS rSrSrSrS rSrg)&BrowserFlowWithNoBrowserFallbackRunner   zEA flow runner to try normal web flow and fall back to NoBrowser flow.zQThere was a problem with web authentication. Try running again with --no-browser.c                 2   SSK Jn   UR                  R                  U R                  U R
                  [        R                  R                  R                  R                  5       (       + S9$ ! UR                   a  n[        R                  " U5        [        R                  " S5        UR                  R                  U R                  U R
                  [        R                  R                  R                  R                  5       (       + S9s S nA$ S nAff = f)Nr   rL   r[   z Defaulting to --no-browser mode.)rN   rM   r}   r^   r?   r>   r
   r_   r`   ra   rb   rw   r	   r~   rj   r   s      r   rA   2BrowserFlowWithNoBrowserFallbackRunner._CreateFlow   s    >,22



,,)3):):)?)?  &+ 3 , ,
 ** ,	kk!n	kk45!!44



,,)3):):)?)?  &+ 5 , ,,r   r   Nr   r   r   r   r   r      s    MA,r   r   c                     U (       a6  [         R                  " U 5       n[        R                  " U5      sSSS5        $ [	        5       $ ! , (       d  f       [	        5       $ = f)zECreates a client config from a client id file or gcloud's properties.N)r   
FileReaderjsonload+_CreateGoogleAuthClientConfigFromProperties)client_id_filefs     r   _CreateGoogleAuthClientConfigr     sC    			.	)QYYq\ 
*	)	4	66 
*	)	4	66s   A
Ac                  d   [         R                  R                  R                  R	                  SS9n [
        R                  " 5       n[         R                  R                  R                  R	                  SS9n[         R                  R                  R                  R	                  SS9nSUUU US.0$ )z1Creates a client config from gcloud's properties.T)requiredr   )	client_idclient_secretauth_uri	token_uri)	r
   r_   r`   	auth_hostr5   r   GetDefaultTokenUrir   r   )r   r   r   r   s       r   r   r     s    ##--1141@(&&()$$..22D2A)##((66::D:I- ( 	
 r   c                 @    U S   S   [         R                  [        4;   $ )Nr   r   )r   CLOUDSDK_CLIENT_ID%DEFAULT_CREDENTIALS_DEFAULT_CLIENT_ID)rE   s    r   _IsGoogleOwnedClientIDr   $  s*    

$[
1'')NOP Qr   c                 T   SSK Jn  SSKJn	  SSKJn
  U(       a  [        U5        U(       d  [        U5      nU(       d  0 n[        R                  " SS9nU(       a  [        X5      R                  " S0 UD6nOU(       a6  U(       d  U
R                  S5      e[        X5      R                  " SSU0UD6nOU(       a  [        XU5      R                  " S0 UD6nOrU(       dP  U(       a,  [        U5      (       d  [        X5      R                  " S0 UD6nO8[        XU5      R                  " S0 UD6nO[!        X5      R                  " S0 UD6nU(       aX  [#        XR$                  5      (       a!  SS	KJn  UR$                  R)                  U5      $ [#        XR$                  5      (       a  U$ g
g
)a  Launches a 3LO oauth2 flow to get google-auth credentials.

Args:
  scopes: [str], The list of scopes to authorize.
  client_id_file: str, The path to a file containing the client id and secret
    to use for the flow.  If None, the default client id for the Cloud SDK is
    used.
  client_config: Optional[Mapping], the client secrets and urls that should be
    used for the OAuth flow.
  no_launch_browser: bool, True if users specify --no-launch-browser flag to
    use the remote login with auth proxy flow.
  no_browser: bool, True if users specify --no-browser flag to ask another
    gcloud instance to help with authorization.
  remote_bootstrap: str, The auth parameters specified by --remote-bootstrap
    flag. Once used, it means the command is to help authorize another
    gcloud (i.e. gcloud without access to browser).
  query_params: Optional[Mapping], extra params to pass to the flow during
    `Run`. These params end up getting used as query
    params for authorization_url.
  auth_proxy_redirect_uri: str, The uri where OAuth service will redirect the
    user to once the authentication is complete for a remote login with auth
    proxy flow.
Returns:
  core.credentials.google_auth_credentials.Credentials, The credentials
    obtained from the flow.
r   ) external_account_authorized_user)credentialsrL   T)attempt_launch_browserzbCannot launch browser. Please run this command on a machine where gcloud can launch a web browser.partial_auth_url)google_auth_credentialsNr   )google.authr   google.oauth2r   rN   rM   !AssertClientSecretIsInstalledTyper   r   ShouldLaunchBrowserrg   rO   WebBrowserInaccessiblers   rm   r   r   r)   Credentialsr   FromGoogleAuthUserCredentials)rD   r   rE   no_launch_browser
no_browserremote_bootstrapquery_paramsauth_proxy_redirect_urir   oauth2_credentialsrR   can_launch_browser
user_credsc_google_auths                 r   #DoInstalledAppBrowserFlowGoogleAuthr   )  s   F ;=< %n5	1.AM	L$88!#$V;??O,OJ))34 4 'v=AA ;);-9;J36	c
 J  4]CC&v=AA j 6
!8 j 8"s# 3%13J*<<== [&&DDZPP*JJKK L r   c                    Sn [         R                  " [        R                  " U 5      5      n[        U5      S:w  a  [        SU 35      e[        U5      S   nU[        :w  a  [        S	[         S
U SU 35      eg! [        R                   a    [        SU  S35      e[         R                   a    [        SU  SU 35      ef = f)zDAssert that the file is a valid json file for installed application.zTo obtain a valid client ID file, create a Desktop App following the steps outlined in https://support.google.com/cloud/answer/6158849?hl=en#zippy=%2Cnative-applications%2Cdesktop-apps.zCannot read file: "z".zClient ID file z is not a valid JSON file.    zNExpected a JSON object with a single property for an "installed" application. r   zOnly client IDs of type 'z%' are allowed, but encountered type 'z'. N)
r   loadsr   ReadFileContentsr   r   JSONDecodeErrorlentupleCLIENT_SECRET_INSTALLED_TYPE)r   actionable_messageobjclient_types       r   r   r     s    l 

**U++N;
<C 	X]
#	+,	.  c
1+00
#
#$@#A B""-c2D1E	G  1 
 N
#&9.9I$L
MM			 
#
.) *	! s   *A< <ACc                    [         R                  R                  R                  R	                  5       nX :X  a  g[
        R                  " S5      R                  XU5      n[        R                  " US9nU(       aY  [         R                  " [         R                  R                  R                  U 5        [        R                  R                  S5        gg)a?  Prompt the user to update the universe domain if there is conflict.

If the given universe domain is different from the core/universe_domain
property, prompt the user to update the core/universe_domain property.

Args:
  new_universe_domain: str, The given new universe domain.
  account: str, The account name to use.
Nz        WARNING: This account [{0}] is from the universe domain [{1}],
        which does not match the current core/universe property [{2}].

        Do you want to set property [core/universe_domain] to [{1}]? [Y/N]
        )messagez(Updated property [core/universe_domain].)r
   r_   coreuniverse_domainr5   textwrapdedentr'   r   PromptContinuePersistPropertyr	   statusPrint)new_universe_domainaccountcurrent_universe_domainr   should_update_universe_domains        r   HandleUniverseDomainConflictr     s     '--22BBFFH3
OO   VG2IJ	 

 #-";";G"L"..0C JJ?@	 #r   r=   )NNFFNNN)5r   
__future__r   r   r   rT   r   r   googlecloudsdk.command_lib.utilr   r1   r   r   r	   r
   r   googlecloudsdk.core.consoler   rN   r   googlecloudsdk.core.utilr   sixr   )DEFAULT_CREDENTIALS_DEFAULT_CLIENT_SECRETCLOUD_PLATFORM_SCOPESQL_LOGIN_SCOPEGOOGLE_DRIVE_SCOPEUSER_EMAIL_SCOPEOPENIDDEFAULT_SCOPESr   r   r   r"   r.   r8   with_metaclassABCMetaobjectr:   rW   rg   rm   rs   rz   r   r   r   r   r   r   r   r   r   r   <module>r      sX    * '  ' 
   9 & * # * $ 2 1 * 

 )s %,F )G D< C 	 	  + J 
 
 
 F ##CKK8 @*J *** *) )J (,z ,4,Z ,47"Q 8<6::?389=59@DUp:Ar   