
    Z,                     B   d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddl	m
Z
 ddl	mZ dd	l	mZ dd
l	mZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ  ddl!m"Z" ejF                   G d dejH                               Z%d Z&y)zJA command to install Application Default Credentials using a user account.    )absolute_import)division)unicode_literalsN)util)actions)arg_parsers)base)
exceptions)	auth_util)flags)workforce_login_config)config)log)
properties)
console_io)creds)gce)store)filesc                   .    e Zd ZdZddiZed        Zd Zy)Logina	  Acquire new user credentials to use for Application Default Credentials.

  Obtains user access credentials via a web flow and puts them in the
  well-known location for Application Default Credentials (ADC).

  This command is useful when you are developing code that would normally
  use a service account but need to run the code in a local development
  environment where it's easier to provide user credentials. The credentials
  will apply to all API calls that make use of the Application Default
  Credentials client library. Do not set the `GOOGLE_APPLICATION_CREDENTIALS`
  environment variable if you want to use the credentials generated by this
  command in your local development. This command tries to find a quota
  project from gcloud's context and write it to ADC so that Google client
  libraries can use it for billing and quota. Alternatively, you can use
  the `--client-id-file` flag. In this case, the project owning the client ID
  will be used for billing and quota. You can create the client ID file
  at https://console.cloud.google.com/apis/credentials.

  This command has no effect on the user account(s) set up by the
  `gcloud auth login` command.

  Any credentials previously generated by
  `gcloud auth application-default login` will be overwritten.
  EXAMPLESa9            If you want your local application to temporarily use your own user
          credentials for API access, run:

            $ {command}

          If you'd like to login by passing in a file containing your own client
          id, run:

            $ {command} --client-id-file=clientid.json
          c           
         | j                  dd       | j                  dt        j                  d      ddj                  d	j	                  t
        j                              
       | j                  ddt        j                  t        j                  j                  j                               | j                  ddd       t        j                  |        t        j                  | d       | j                   j#                  d       y)z3Set args for gcloud auth application-default login.z--client-id-filezA file containing your own client id to use to login. If --client-id-file is specified, the quota project will not be written to ADC.)help--scopes   )
min_lengthSCOPEa  The names of the scopes to authorize for. By default {0} scopes are used. The list of possible scopes can be found at: [](https://developers.google.com/identity/protocols/googlescopes). To add scopes for applications outside of Google Cloud Platform, such as Google Drive, [create an OAuth Client ID](https://support.google.com/cloud/answer/6158849) and provide it by using the --client-id-file flag. z, )typemetavarr   z--login-configzPath to the login configuration file (workforce pool, generated by the Cloud Console or `gcloud iam workforce-pools create-login-config`))r   actionaccount?a  User account used for authorization. When the account specified has valid credentials in the local credential store these credentials will be re-used. Otherwise new ones will be fetched and replace any stored credential. This caching behavior is only available for user credentials.)nargsr   T)for_adcnoneN)add_argumentr   ArgListformatjoinr   DEFAULT_SCOPESr   StorePropertyr   VALUESauthlogin_config_filer   AddQuotaProjectFlagsAddRemoteLoginFlagsdisplay_info	AddFormat)parsers    -lib/surface/auth/application_default/login.pyArgsz
Login.ArgsS   s       
   A., 
		)223	4  6 < $$Z%6%6%;%;%M%MN  P M	  
 
v&	fd3
!!&)    c                    t         j                  j                  j                  j	                         rt        j                  d      t        j                         j                  r-t        j                  d      }t        j                  |dd       t        j                          |j                   r"|j"                  st%        j&                  dd      |j(                  rP|j*                  sDt-        |j(                        r/t/        j0                  dj3                  |j(                               y|j*                  xs t4        j6                  }t9        |j"                   |j:                   |j<                  	      }|j<                  r5d
|j<                  v r't5        j>                  t@        jB                  fddi| ytE        jF                         }|rZ|j                   rt%        jH                  d      |j*                  rt%        jH                  d      tE        jJ                  |dfi |}nt         j                  j                  jL                  jO                  t4        jP                         t         j                  j                  jR                  jO                  t4        jT                         t4        jV                  |vr3t%        j&                  ddj3                  t4        jV                              t5        j>                  |f|j                   dd|}|sy|j(                  rGtY        |d      r;t        jZ                  |j(                  |      }|j]                  |j(                        }d\  }}	t         j                  j                  j^                  j	                         }
|
rt        j`                  |
      \  }}	|sf|jc                  d      rt        jd                  |d       |S |jf                  rt        jd                  |d       |S t        jh                  |       |S t        jj                  |||	       |S )zRun the authentication command.zauth/access_token_file or --access-token-file was set which is not compatible with this command. Please unset the property and rerun this command.a  
          You are running on a Google Compute Engine virtual machine.
          The service credentials associated with this virtual machine
          will automatically be used by Application Default
          Credentials, so it is not necessary to use this command.

          If you decide to proceed anyway, your user credentials may be visible
          to others with access to this virtual machine. Are you sure you want
          to authenticate with your personal account?
          T)messagethrow_if_unattendedcancel_on_noz--no-launch-browserz`--no-launch-browser` flow no longer works with the `--client-id-file`. Please replace `--no-launch-browser` with `--no-browser`.zx
Valid credentials already exist for {}. To force refresh the existing credentials, omit the "ACCOUNT" positional field.N)no_launch_browser
no_browserremote_bootstrapprovider_nameauth_proxy_redirect_uriz8https://sdk.cloud.google/applicationdefaultauthcode.htmlzI--client-id-file is not currently supported for third party login flows. z@--scopes is not currently supported for third party login flows.r   zO{} scope is required but not requested. Please include it in the --scopes flag.z<https://sdk.cloud.google.com/applicationdefaultauthcode.html)client_id_filer@   with_account)NNrA   F)quota_project_disabled)target_principal	delegates)6r   r-   r.   access_token_fileGetc_store	FlowErrorc_gceMetadata	connectedtextwrapdedentr   PromptContinuecommand_auth_utilPromptIfADCEnvVarIsSetrA   launch_browserc_excInvalidArgumentExceptionr"   scopesShouldUseCachedCredentialsr   warningr)   r   r+   dictbrowserr>   #DoInstalledAppBrowserFlowGoogleAuthr    CLOUDSDK_EXTERNAL_ACCOUNT_SCOPESworkforce_login_config_utilGetWorkforceLoginConfigConflictingArgumentsExceptionDoWorkforceHeadfulLogin	client_idSet%DEFAULT_CREDENTIALS_DEFAULT_CLIENT_IDclient_secret)DEFAULT_CREDENTIALS_DEFAULT_CLIENT_SECRETCLOUD_PLATFORM_SCOPEhasattrExtractAndValidateAccountrB   impersonate_service_accountParseImpersonationAccountsIsSpecifiedDumpADCdisable_quota_projectDumpADCOptionalQuotaProject#DumpImpersonatedServiceAccountToADC)selfargsr9   rU   flow_paramsr/   r   _target_impersonation_principalrE   impersonation_service_accountss              r5   Runz	Login.Run~   s    //335  ~~!! 	! 	g t$H ,,.4#6#6**
  ||DKK	#DLL	1JVDLL!	

 	[[4I44F"111||#..K D4I4I!I33

1
1 I
   4KKM			11 	 
11NP 	P)AA

 e &&**

9
9;**..

=
=?		'	'v	5,,$fY%C%CD
 	

 ;;
,,L	 e ||~6

5
5dllE
Ja  .e0:-"I::>>@ # &

,
,-K
L%y)			*	+!!%F L %%!!%E L 	55e< L	 ;;
9 Lr7   N)__name__
__module____qualname____doc__detailed_helpstaticmethodr6   ru    r7   r5   r   r   *   s3    2 
- (* (*Tr7   r   c                    	 t        j                         }t        j                  |      }	 t        j                  |      }t
        j                  j                  |      }|t
        j                  j                  k(  r|j                  | k7  ryy# t        j                  $ r Y yw xY w# t
        j                  $ r Y yt
        j                  $ r Y yw xY w)zISkip login if the existing ADC was provisioned for the requested account.FT)r   ADCFilePathr   ReadFileContentsErrorcreds_moduleFromJsonGoogleAuthUnknownCredentialsTypeInvalidCredentialsErrorCredentialTypeGoogleAuthFromCredentialsUSER_ACCOUNTr"   )r"   	file_pathdata
credential	cred_types        r5   rV   rV      s    ""$I!!),D006J 33CCJO),77DDDW$	 
 
 
	,	, 		-	- s(   )B B' B$#B$'C<CC)'ry   
__future__r   r   r   rM   googlecloudsdk.api_lib.authr   r   googlecloudsdk.callioper   r   r	   r
   rS   googlecloudsdk.command_lib.authrP   r   r   r\   googlecloudsdk.corer   r   r   googlecloudsdk.core.consoler   googlecloudsdk.core.credentialsr   r   r   rJ   r   rH   googlecloudsdk.core.utilr   UniverseCompatibleCommandr   rV   r|   r7   r5   <module>r      su     Q &  '  9 + / ( 7 J 1 a & # * 2 A 8 < * RDLL R Rjr7   