
    c                         d 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Zd Zd Zd Zy)z?Common utility functions for sql generate-login-token commands.    )absolute_import)division)unicode_literals)credentials)
exceptions)log)requests)creds)google_auth_credentials)storeNc                    t        j                  ddd      }t        j                  j	                  |      }|t        j                  j
                  k(  rEt        |       t        |j                        z
  }|r"t        j                  dt        |       d      t        ||       }t        j                  |       t        j                  |      r|j                  }n|j                  }|st        j                  d      |S )zGenearete a down-coped access token with given scopes for IAM DB authentication from gcloud credentials.

  Args:
    scopes: scopes to be included in the down-scoped token.

  Returns:
    Down-scoped access token.
  T)allow_account_impersonationuse_google_authcache_only_raptzMissed the following scopes: zK. Please run "gcloud auth login", consent the missing scopes and try again.z?No access token could be obtained from the current credentials.)c_storeLoadc_credsCredentialTypeGoogleAuthFromCredentialsUSER_ACCOUNT	frozensetscopesauth_exceptionsInvalidCredentialsErrorlist_downscope_credentialRefreshIsGoogleAuthCredentialstokenaccess_token)r   cred	cred_typemissing_scoper   s        ?lib/googlecloudsdk/command_lib/sql/generate_login_token_util.py%generate_login_token_from_gcloud_authr%   "   s     
"&
$ ..>>tD)'22???f%	$++(>>M33)$}*=)> ?L L 
 
tV	,$	//$$$T*JJEE	

1
1IK K	,    c                 2   	 t        j                         j                  |       \  }}t        ||       }t        |t        j                        rt        j                  j!                  |      }t#        j$                  d      5  |j'                  t)        j*                                ddd       |S # t        j                  $ rD}t        j                  |d       t        j                  t        j                  |            d}~ww xY w# 1 sw Y   |S xY w)zGenearete a down-coped access token with given scopes for IAM DB authentication from application default credentials.

  Args:
    scopes: scopes to be included in the down-scoped token.

  Returns:
    Down-scoped access token.
  )r   T)exc_infoN)for_adc)r   GetGoogleAuthDefaultdefaultgoogle_auth_exceptionsDefaultCredentialsErrorr   debugc_excToolExceptionsix	text_typer   
isinstancegoogle_auth_credsCredentialsc_google_authFromGoogleAuthUserCredentialsr   'HandleGoogleAuthCredentialsRefreshErrorrefreshr	   GoogleAuthRequest)r   r
   _es       r$   generate_login_token_from_adcr=   H   s    0++-55 6 HE1  v
.% (445%%CCE 66tD	MM(,,./ E	, 
 	7	7 0IIa$


cmmA.
//0 E	,s#   'B2 $D2D	?DD	Dc                    t         j                  j                  |       }|t         j                  j                  t         j                  j                  t         j                  j
                  fvr.t        j                  dj                  |j                               t        | t        j                        r| j                  |      } | S || _        | S )zGenearte a down-scoped credential.

  Args:
    creds: end user credential
    scopes: scopes to be included in the down-scoped credential

  Returns:
    Down-scoped credential.
  z:This command may not work as expected for account type {}.)r   r   r   r   SERVICE_ACCOUNTIMPERSONATED_ACCOUNTr   warningformatkeyr3   r   Scopedwith_scopes_scopes)r
   r   r"   s      r$   r   r   d   s     ..>>uE)&&33&&66&&;;  KK	%vimm4 {))*f%E 
, EM	,r&   )__doc__
__future__r   r   r   google.authr   r   r,   google.oauth2r4   googlecloudsdk.api_lib.authr   googlecloudsdk.callioper/   googlecloudsdk.corer   r	   googlecloudsdk.core.credentialsr
   r   r   r6   r   r   r1   r%   r=   r    r&   r$   <module>rP      sE    F &  ' # < : E 7 # ( < T < 
#L8r&   