
                         :   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rSS0rSr " S S\	R0                  5      r " S S\5      r " S S\5      r " S S\R6                  5      rS rg)z+Provides utilities for token introspection.    )absolute_import)division)unicode_literalsN)
exceptions)external_account)utils)config)
properties)http_client)urllibz-urn:ietf:params:oauth:token-type:access_tokenzContent-Typez!application/x-www-form-urlencodedz(https://sts.googleapis.com/v1/introspectc                       \ rS rSrSrSrg)Error*   z!A base exception for this module. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       1lib/googlecloudsdk/core/credentials/introspect.pyr   r   *   s    )r   r   c                       \ rS rSrSrSrg)InactiveCredentialsError.   z<Raised when the provided credentials are invalid or expired.r   Nr   r   r   r   r   r   .   s    Dr   r   c                       \ rS rSrSrSrg)TokenIntrospectionError2   zFRaised when an error is encountered while calling token introspection.r   Nr   r   r   r   r   r   2   s    Nr   r   c                   <   ^  \ rS rSrSrSU 4S jjr\4S jrSrU =r	$ )IntrospectionClient6   a  Implements the OAuth 2.0 token introspection spec.

This is based on https://tools.ietf.org/html/rfc7662.
The implementation supports 3 types of client authentication when calling
the endpoints: no authentication, basic header authentication and POST body
authentication.
c                 8   > [         [        U ]  U5        Xl        g)a  Initializes an OAuth introspection client instance.

Args:
  token_introspect_endpoint (str): The token introspection endpoint.
  client_authentication (Optional[oauth2_utils.ClientAuthentication]): The
    optional OAuth client authentication credentials if available.
N)superr!   __init___token_introspect_endpoint)selftoken_introspect_endpointclient_authentication	__class__s      r   r%   IntrospectionClient.__init__?   s     

t-.CD&?#r   c                    [         R                  5       nUUS.nU R                  XE5        U" U R                  SU[        R
                  R                  U5      R                  S5      S9n[        UR                  S5      (       a  UR                  R                  S5      OUR                  nUR                  [        R                  :w  a  [        U5      e[        R                   " U5      nUR#                  S5      (       a  U$ [%        U5      e)a\  Returns the meta-information associated with an OAuth token.

Args:
  request (google.auth.transport.Request): A callable that makes HTTP
    requests.
  token (str): The OAuth token whose meta-information are to be returned.
  token_type_hint (Optional[str]): The optional token type. The default is
    access_token.

Returns:
  Mapping: The active token meta-information returned by the introspection
    endpoint.

Raises:
  InactiveCredentialsError: If the credentials are invalid or expired.
  TokenIntrospectionError: If an error is encountered while calling the
    token introspection endpoint.
)tokentoken_type_hintPOSTzutf-8)urlmethodheadersbodydecodeactive)_URLENCODED_HEADERScopy#apply_client_authentication_optionsr&   r   parse	urlencodeencodehasattrdatar4   statusr   OKr   jsonloadsgetr   )	r'   requestr-   r.   r2   request_bodyresponseresponse_bodyresponse_datas	            r   
introspectIntrospectionClient.introspectJ   s    & "&&(G*L
 	,,WC ++\\##L188A	H 8==(++ 	W%]]  +..(#M22JJ}-M""$]33r   )r&   )N)
r   r   r   r   r   r%   _ACCESS_TOKEN_TYPErH   r   __classcell__)r*   s   @r   r!   r!   6   s    	@ 8J 24 24r   r!   c                    SSK Jn  [        R                  " [        R                  R
                  [        R                  [        R                  5      n[        n[        R                  R                  R                  R                  5       nU R                  nU(       d  U(       a  U=(       d    Un[!        UUS9nUR#                  5       n[%        U [&        R(                  5      (       ai  [+        U S5      (       aX  [-        [/        U S5      5      (       a>  U R1                  5       (       a)   U R3                  5       u  p[4        R6                  " XxU	4S9nU RH                  (       d  U RK                  U5        URM                  XpRN                  5      nURQ                  S5      $ ! [8        [:        [<        R>                  [@        [B        4 a!  n
[D        RF                  " SU
5         Sn
A
NSn
A
ff = f)	am  Returns the external account credentials' identifier.

This requires basic client authentication and only works with external
account credentials that have not been impersonated. The returned username
field is used for the account ID.

Args:
  creds (google.auth.external_account.Credentials): The external account
    credentials whose account ID is to be determined.

Returns:
  Optional(str): The account ID string if determinable.

Raises:
  InactiveCredentialsError: If the credentials are invalid or expired.
  TokenIntrospectionError: If an error is encountered while calling the
    token introspection endpoint.
r   )requests)r(   r)   _mtls_required)certz0Could not get mTLS certificate and key paths: %sNusername))googlecloudsdk.corerM   oauth2_utilsClientAuthenticationClientAuthTypebasicr	   CLOUDSDK_CLIENT_IDCLOUDSDK_CLIENT_NOTSOSECRET(_EXTERNAL_ACCT_TOKEN_INTROSPECT_ENDPOINTr
   VALUESauthtoken_introspection_endpointGettoken_info_urlr!   GoogleAuthRequest
isinstancer   Credentialsr<   callablegetattrrN   _get_mtls_cert_and_key_paths	functoolspartialAttributeError
ValueErrorgoogle_auth_exceptionsGoogleAuthErrorIOErrorOSErrorloggingdebugvalidrefreshrH   r-   rB   )credscore_requestsr)   r[   endpoint_overrideproperty_overrideoauth_introspectionrC   	cert_pathkey_pathe
token_infos               r   GetExternalAccountIdry      s   ( < ';;!!''(( "J '',,IIMMO**+#4#I8I + <1 ++-'
 (4455
%)
*
*
75"23
4
4



 
 !>>@i!!'H0EFg 
	MM'"--g{{C*	
	## 	.. 
 mmFJ

s   *(F   )G*	G%%G*)r   
__future__r   r   r   rd   r@   rl   google.authr   rh   r   google.oauth2r   rR   rQ   r	   r
   	six.movesr   r   rJ   r6   rX   r   r   r   OAuthClientAuthHandlerr!   ry   r   r   r   <module>r      s    2 &  '    < ( / & * * !  E %'JK . )
*J *Eu EOe OF4,== F4RK$r   