
    '                        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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  \R>                  " S5      r \R>                  " S5      r!S r"\RF                  \RH                   " S S\RJ                  5      5       5       r&g)a  A git credential helper that provides Google git repository passwords.

Reads a session from stdin that looks a lot like:
  protocol=https
  host=code.google.com
And writes out a session to stdout that looks a lot like:
  username=me
  password=secret

Errors will be reported on stderr.

Note that spaces may be part of key names so, for example, "protocol" must not
be proceeded by leading spaces.
    )absolute_import)division)unicode_literalsN)
exceptions)base)log)
properties)requests)creds)store)files)	platformsz	(.+)=(.*)z^ *$c                  ^    [         R                  " 5       R                  5       u  pU R                  [        R
                  " 5       5        U R                  $ ! [        R                   aA  n[        R                  R                  [        R                  " SU S35      5         SnAgSnAff = f)zObtains an access token using Application Default Credentials.

Writes error messages to stderr and returns None if an error occurs.

Returns:
  str or None: The access token if successful, otherwise None.
z>        ERROR: Failed to get application default credentials: z	
        N)c_credsGetGoogleAuthDefaultdefaultrefreshr
   GoogleAuthRequesttokengoogle_auth_exceptionsDefaultCredentialsErrorsysstderrwritetextwrapdedent)r   _es      lib/surface/auth/git_helper.py_GetAccessTokenFromAdcr    ;   s    	++-557HE	MM(,,./ ;; 
 	7	7 JJX__ *??@c B	&   	s   A	A B,+7B''B,c                       \ rS rSrSrSrSr\\/rSr\	S 5       r
\R                  " \R                  \R                   5      S 5       rS rS	 rS
 rS rSrg)	GitHelperO   zEA git credential helper to provide access to Google git repositories.getr   zgooglesource.comc                 F    U R                  SSS9  U R                  SSSS9  g )Nmethodz!The git credential helper method.)helpz--ignore-unknown
store_truezTProduce no output and exit with 0 when given an unknown method (e.g. store) or host.)actionr'   )add_argument)parsers    r   ArgsGitHelper.ArgsZ   s4    
'JK
6	      c           	        ^^ [         R                  R                  R                  R	                  S5        UR
                  [        R                  ;  a]  UR                  (       a  g[        R                  " SR                  UR
                  SR                  [        R                  5      S95      eU R                  5       nS[        R                  /mSSS	S
[        R                  -   /m[         R                  R                  R                   R#                  5       nU(       a   TR%                  UR'                  S5      5        UR)                  S5      nUU4S jnU" U5      (       d5  UR                  (       d#  [        R                  " SR                  US95      egUR
                  [        R*                  :X  a  U R-                  5       nU(       d  gU R/                  5         U[        R                  :X  d'  UR1                  S
[        R                  -   5      (       a  SnO2[         R                  R                  R2                  R#                  5       n[4        R6                  R9                  [:        R<                  " SU SU S35      5        gUR
                  [        R>                  :X  Ga  [@        RB                  RE                  5       [@        RB                  RF                  :X  a  [H        RJ                  " S5         SR                  UR)                  S5      UR)                  S5      S9n[H        RJ                  " SU5        [L        RN                  " SS/[L        RP                  [L        RP                  [L        RP                  S9n	U	RS                  U5      u  pU	RT                  (       a  [H        RJ                  " SU
U5        gggg! [V         a  n[H        RJ                  " SSS9   SnAgSnAff = f) zRun the helper command.FNz8Unexpected method [{meth}]. One of [{methods}] expected.z, )methmethodssource.developers.google.comz.sourcemanager.devz.blueoryx.devz.developerconnect.dev.,hostc                 V   > U T;   a  gT H  nU R                  U5      (       d  M    g   g)NTF)endswith)r5   suffixcredentialed_domainscredentialed_domains_suffixs     r   _ValidateHost$GitHelper.Run.<locals>._ValidateHost   s0    	%	%/&==   0 r.   zUnknown host [{host}].)r5   zgit-accountz          username=z
          password=z
          zClearing OSX credential cache.z!protocol={protocol}
host={host}

protocol)r=   r5   zCalling erase with input:
%szgit-credential-osxkeychainerase)stdinstdoutr   z7Failed to clear OSX keychain:
stdout: {%s}
stderr: {%s}zFailed to clear OSX keychainT)exc_info),r	   VALUESauth#service_account_use_self_signed_jwtSetr&   r"   METHODSignore_unknownauth_exceptionsGitCredentialHelperErrorformatjoin_ParseInputGOOGLESOURCEcore credentialed_hosted_repo_domainsGetextendsplitr$   GET_GetAccessToken_CheckNetrcr7   accountr   r@   r   r   r   STOREr   OperatingSystemCurrentMACOSXr   debug
subprocessPopenPIPEcommunicate
returncode	Exception)selfargsinfoextrar5   r;   access_tokensent_accountinput_stringpouterrr   r9   r:   s                @@r   RunGitHelper.Runf   s*    >>BB5I{{)+++			44
D
K
K;;		)2C2C(D L   D&
 	i$$$	# ""CCGGIE!!%++c"2388FD   66$+++6
 	
 {{imm#))+l

 )((
(]]3!7!7788$!((--5599;	jjx , > " > "(   
		' 
#
#
+
+
-&&--. 			23	C?FFxx
+$((62B G , ))3\
B+W5OO____	! ]]<0(#\\IIK !. 
(D  	C
))2T
B	Cs   >CO 
O/O**O/c                    [         R                  R                  R                  R	                  5       n [
        R                  " U5      n[
        R                  " U5        UR                  $ ! [        R                   aA  n[        R                  R                  [        R                  " SU S35      5         SnAgSnAf[          a-  n[        R                  R                  SU S35         SnAgSnAff = f)zObtains an access token using gcloud credentials.

Writes error messages to stderr and returns None if an error occurs.

Returns:
  str or None: The access token if successful, otherwise None.
z          ERROR: z8
          Run 'gcloud auth login' to log in.
          Nz&ERROR: Failed to obtain access token: 
)r	   rB   rN   rV   rP   c_storeLoadRefreshr   creds_exceptionsErrorr   r   r   r   r   ra   )rb   rV   credr   s       r   _GetAccessTokenFromGcloud#GitHelper._GetAccessTokenFromGcloud   s     $$,,002G\\'"dood ZZ !! 	jjx , (    	jj?s"EFs#   ,A, ,C6 7B<<C6	#C11C6c                     [         R                  R                  R                  R	                  5       (       a
  [        5       $ U R                  5       $ )zObtains an access token using either ADC or gcloud credentials.

Writes error messages to stderr and returns None if an error occurs.

Returns:
  str or None: The access token if successful, otherwise None.
)r	   rB   rC   git_helper_use_adcGetBoolr    rv   )rb   s    r   rT   GitHelper._GetAccessToken   s=     0088::#%%++--r.   c                 R   0 n[         R                   H  n[        R                  U5      (       a  M  [        R                  U5      nU(       d2  [
        R                  " SR                  UR                  S5      S95      eUR                  5       u  pEUR                  5       X'   M     SU;  a  [
        R                  " S5      eSU;  a  [
        R                  " S5      eUR                  S5      S:w  a2  [
        R                  " S	R                  UR                  S5      S
95      eU$ )z[Parse the fields from stdin.

Returns:
  {str: str}, The parsed parameters given on stdin.
z&Invalid input line format: [{format}].ro   )rJ   r=   z Required key "protocol" missing.r5   zRequired key "host" missing.httpsz*Invalid protocol [{p}].  "https" expected.)ri   )r   r?   _BLANK_LINE_REmatch
_KEYVAL_RErH   rI   rJ   rstripgroupsstripr$   )rb   rd   liner   keyvals         r   rL   GitHelper._ParseInput   s    D					d	#	#t$e664;;{{4( < 
 	

 hc))+di  44
,  T44
(  xx
w&44
6
=
=$ >  
 Kr.   c                     S nU" [         R                  " [        R                  R	                  SS5      5      5        U" [         R                  " [        R                  R	                  SS5      5      5        g)z:Warn on stderr if ~/.netrc contains redundant credentials.c                 *   [         R                  R                  U 5      (       d  g [        R                  " U 5      nSU;   aA  [
        R                  R                  [        R                  " SR                  U S95      5        gg! [         a     gf = f)z9Warn about other credential helpers that will be ignored.Nr2   zYou have credentials for your Google repository in [{path}]. This repository's
git credential helper is set correctly, so the credentials in [{path}] will not
be used, but you may want to remove them to avoid confusion.
)path)osr   existsr   ReadFileContentsr   r   r   r   r   rJ   ra   )ri   datas     r   Check$GitHelper._CheckNetrc.<locals>.Check   s}    WW^^A
%%a()T1
**

8?? , FFN	  2  s   AB 
BB~z.netrc_netrcN)r   ExpandHomeDirr   r   rK   )rb   r   s     r   rU   GitHelper._CheckNetrc  sM      
%

bggll39
:;	%

bggll39
:;r.    N)__name__
__module____qualname____firstlineno____doc__rS   rW   rF   rM   staticmethodr,   c_excRaiseErrorInsteadOfrH   AuthenticationErrorr   GoogleAuthErrorrl   rv   rT   rL   rU   __static_attributes__r   r.   r   r"   r"   O   s~     N#
%%L'#,	 	 	_@@3CCEhCEhCT0.$L<r.   r"   )'r   
__future__r   r   r   r   rer\   r   r   google.authr   r   googlecloudsdk.api_lib.authrH   googlecloudsdk.callioper   r   googlecloudsdk.corer   r	   r
   googlecloudsdk.core.credentialsr   r   rs   r   rp   googlecloudsdk.core.utilr   r   compiler   r~   r    HiddenDefaultUniverseOnlyCommandr"   r   r.   r   <module>r      s     '  ' 	 	  
  < E ( 7 # * ( < J < * . ZZ%
G$( `< `<  `<r.   