
                         d   S r SSKrSSKrSSKrSSKrSSKJrJrJrJ	r	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rSS
KJr   " S S\R0                  R2                  5      rS\\\\\4   4   SS4S jrS\SS4S jrS\\\4   SS4S jrS\SS4S jr S\S\	\   4S jr!g)a  Classes and functions to allow google.auth credentials to be used within oauth2client.

In particular, the External Account credentials don't have an equivalent in
oauth2client, so we create helper methods to allow variants of this particular
class to be used in oauth2client workflows.
    N)AnyDictListOptionalTYPE_CHECKING)aws)external_account) external_account_authorized_user)identity_pool)	pluggable)requests)bq_errorc                     ^  \ rS rSrSr\" \" \R                  R                  R                  5      S/-   5      r
    SU 4S jjrSS jr\S\4S j5       r\R                   S	\SS4S
 j5       r\S\R"                  4S j5       r\R                   S	\R"                  4S j5       r\S\\   4S j5       r\R                   S	\\   SS4S j5       rS\4U 4S jjr\S\SS 4S j5       r\S\SS 4S j5       r\S\SS 4S j5       rSrU =r$ )WrappedCredentials   zTA utility class to use Google Auth credentials in place of oauth2client credentials._basereturnNc           
      X  > [        U[        R                  5      (       d5  [        U[        R                  5      (       d  [        R
                  " S5      eXl        [        TU ]!  U R                  R                  U R                  R                  SSU R                  R                  SSS9  g)zInitializes oauth2client credentials based on underlying Google Auth credentials.

Args:
  base_creds: subclass of google.auth.external_account.Credentials or
    google.auth.external_account_authorized_user.Credentials
zInvalid CredentialsN)access_token	client_idclient_secretrefresh_tokentoken_expiry	token_uri
user_agent)
isinstancer	   Credentialsr
   r   BigqueryTypeErrorr   super__init__token	_audienceexpiry)self
base_creds	__class__s     "platform/bq/wrapped_credentials.pyr    WrappedCredentials.__init__#   s     $00 4@@  &&'<==J	GZZ%%**&&ZZ&&      c                     U R                   R                  [        R                  " 5       5        U R                  b  U R                  R                  U 5        g g N)r   refreshr   Requeststore
locked_put)r$   https     r'   _do_refresh_request&WrappedCredentials._do_refresh_requestB   s>    JJx'')*zz
jjD! r)   c                 .    U R                   R                  $ r+   r   r!   r$   s    r'   r   WrappedCredentials.access_tokenG   s    ::r)   valuec                 $    XR                   l        g r+   r4   r$   r7   s     r'   r   r6   K   s    JJr)   c                 .    U R                   R                  $ r+   r   r#   r5   s    r'   r   WrappedCredentials.token_expiryO   s    ::r)   c                 $    XR                   l        g r+   r;   r9   s     r'   r   r<   S   s    JJr)   c                 .    U R                   R                  $ r+   r   _scopesr5   s    r'   scopesWrappedCredentials.scopesW   s    ::r)   c                 4    U(       a  XR                   l        g g r+   r?   r9   s     r'   rA   rB   [   s     jj r)   c                 <  > [         [        U ]  5       n[        R                  " U5      n[
        R
                  " U R                  R                  5      US'   U R                  R                  US'   [        U R                  5      US'   [        R                  " U5      $ )zUtility function that creates JSON representation of a Credentials object.

Returns:
    string, a JSON representation of this instance, suitable to pass to
    from_json().
r   r   r   )r   r   to_jsonjsonloadscopyr   infor!   _parse_expiryr   dumps)r$   serialized_datadeserialized_datar&   s      r'   rE   WrappedCredentials.to_json`   s{     .=?O

?3!%4::??!;g(,

(8(8n%(5d6G6G(Hn%::'((r)   filenamec                 (    [        U5      nU " U5      $ r+   )+_get_external_account_credentials_from_fileclsrO   credss      r'   for_external_account'WrappedCredentials.for_external_accounto   s    7AEu:r)   c                 (    [        U5      nU " U5      $ r+   );_get_external_account_authorized_user_credentials_from_filerR   s      r'   $for_external_account_authorized_user7WrappedCredentials.for_external_account_authorized_usert   s     HE u:r)   	json_datac                 Z   [         R                  " U5      nUR                  S5      nSnUR                  S5      S:X  a  [        U5      nO UR                  S5      S:X  a  [	        U5      nU " U5      nUR                  S5      Ul        UR                  S5      (       aa  [        US   [        R                  5      (       d?   [        R                  R                  US   [        R                  R                  5      US'   UR                  S5      Ul        U$ ! [         a    SUS'    N)f = f)zInstantiate a Credentials object from a JSON description of it.

The JSON should have been produced by calling .to_json() on the object.

Args:
    json_data: dict, A deserialized JSON object.

Returns:
    An instance of a Credentials subclass.
r   Ntyper	   r
   r   r   )rF   rG   get+_get_external_account_credentials_from_info;_get_external_account_authorized_user_credentials_from_infor   r   datetimestrptimeoauth2client_4_0clientEXPIRY_FORMAT
ValueErrorr   )rS   r[   databaser%   rT   s         r'   	from_jsonWrappedCredentials.from_json}   s    ::i D88GDJxx-->tDj	&	?	?N
j 
OE.1Exx
^h//) )$'0099 "2"9"9"G"G 
^
 .1EL	  $#^$s   >D D*)D*)r   )r%   zKexternal_account.Credentials | external_account_authorized_user.Credentialsr   N)r0   zrequests.Requestr   N)__name__
__module____qualname____firstlineno____doc__	frozensetlistrc   rd   OAuth2CredentialsNON_SERIALIZED_MEMBERSr    r1   propertystrr   setterra   r   r   rA   rE   classmethodrU   rY   ri   __static_attributes____classcell__)r&   s   @r'   r   r      s   \$
""44KKL		
9 >"
 C       H--    1 1   d3i   
==!$s) ! ! !)s ) # 2F     % %(< % %r)   r   rI   r   zexternal_account.Credentialsc                 H   [         R                  " 5       nU R                  S5      S:X  a  [        R                  R                  XS9$ U R                  S0 5      R                  S5      b  [        R                  R                  XS9$ [        R                  R                  XS9$ )zCreate External Account Credentials using the mapping provided as json data.

Finds a relevant subclass of external_account.Credentials and instantiates.

Args:
    info: dict, A deserialized JSON object.

Returns:
    An instance of a Credentials class
subject_token_typez+urn:ietf:params:aws:token-type:aws4_requestrA   credential_source
executable)bq_utilsGetClientScopesFromFlagsr^   r   r   	from_infor   r   rI   rA   s     r'   r_   r_      s     ,,.&
hh#$	67 ??$$T$99xx#R(,,\:F  **4*?? $$..t.CCr)   rO   c                     [         R                  " U SSS9 n[        R                  " U5      n[	        U5      sS S S 5        $ ! , (       d  f       g = fNrzutf-8)encoding)ioopenrF   loadr_   rO   	json_filerg   s      r'   rQ   rQ      s8     
wwxw/999YD6t< 0//   !A
Az,external_account_authorized_user.Credentialsc                     [         R                  " 5       nU R                  US9  [        R                  R                  U 5      $ )zCreate External Account Authorized User Credentials using the mapping provided as json data.

Args:
    info: dict, A deserialized JSON object.

Returns:
    An instance of a Credentials class
r|   )r   GetClientScopesFor3piupdater
   r   r   r   s     r'   r`   r`      s7     ))+&++V+	)	5	5	?	?	EEr)   c                     [         R                  " U SSS9 n[        R                  " U5      n[	        U5      sS S S 5        $ ! , (       d  f       g = fr   )r   r   rF   r   r`   r   s      r'   rX   rX      s8     
wwxw/999YDFtL 0//r   r#   c                     U (       aH  [        U [        R                  5      (       a)  U R                  [        R                  R
                  5      $ g r+   )r   ra   strftimerc   rd   re   )r#   s    r'   rJ   rJ      s6    
68#4#455??+22@@AAr)   )"ro   rH   ra   r   rF   typingr   r   r   r   r   google.authr   r	   r
   r   r   google.auth.transportr   oauth2client_4_0.clientrc   r   utilsr   rd   rr   r   ru   objectr_   rQ   r`   rX   rJ    r)   r'   <module>r      s      	  ; ;  ( 8 % ! *   H)00BB HVD
sDf%%
&D#D:==#=F
sF{
F3F MM3M# (3- r)   