
    (ph4                         S r SSKJ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r
SrSS	/rS
 rS\
S4S jrSS jrSS jrSS jrS rg)a  Google ID Token helpers.

Provides support for verifying `OpenID Connect ID Tokens`_, especially ones
generated by Google infrastructure.

To parse and verify an ID Token issued by Google's OAuth 2.0 authorization
server use :func:`verify_oauth2_token`. To verify an ID Token issued by
Firebase, use :func:`verify_firebase_token`.

A general purpose ID Token verifier is available as :func:`verify_token`.

Example::

    from google.oauth2 import id_token
    from google.auth.transport import requests

    request = requests.Request()

    id_info = id_token.verify_oauth2_token(
        token, request, 'my-client-id.example.com')

    userid = id_info['sub']

By default, this will re-fetch certificates for each verification. Because
Google's public keys are only changed infrequently (on the order of once per
day), you may wish to take advantage of caching to reduce latency and the
potential for network errors. This can be accomplished using an external
library like `CacheControl`_ to create a cache-aware
:class:`google.auth.transport.Request`::

    import cachecontrol
    import google.auth.transport.requests
    import requests

    session = requests.session()
    cached_session = cachecontrol.CacheControl(session)
    request = google.auth.transport.requests.Request(session=cached_session)

.. _OpenID Connect ID Tokens:
    http://openid.net/specs/openid-connect-core-1_0.html#IDToken
.. _CacheControl: https://cachecontrol.readthedocs.io
    N)environment_vars)
exceptions)jwtz*https://www.googleapis.com/oauth2/v1/certszXhttps://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.comzaccounts.google.comzhttps://accounts.google.comc                     U " USS9nUR                   [        R                  :w  a%  [        R                  " SR                  U5      5      e[        R                  " UR                  R                  S5      5      $ )a  Fetches certificates.

Google-style cerificate endpoints return JSON in the format of
``{'key id': 'x509 certificate'}`` or a certificate array according
to the JWK spec (see https://tools.ietf.org/html/rfc7517).

Args:
    request (google.auth.transport.Request): The object used to make
        HTTP requests.
    certs_url (str): The certificate endpoint URL.

Returns:
    Mapping[str, str] | Mapping[str, list]: A mapping of public keys
    in x.509 or JWK spec.
GET)methodz"Could not fetch certificates at {}zutf-8)
statushttp_clientOKr   TransportErrorformatjsonloadsdatadecode)request	certs_urlresponses      I/var/www/html/venv/lib/python3.13/site-packages/google/oauth2/id_token.py_fetch_certsr   Q   sa      y/H+..(''077	B
 	
 ::hmm**7344    c                 *   [        X5      nSU;   aN   SSKnUR                  U5      nUR	                  U 5      n	UR                  U U	R                  U	R                  /US9$ [        R
                  " U UUUS9$ ! [         a  n[        S5      UeSnAff = f)a  Verifies an ID token and returns the decoded token.

Args:
    id_token (Union[str, bytes]): The encoded token.
    request (google.auth.transport.Request): The object used to make
        HTTP requests.
    audience (str or list): The audience or audiences that this token is
        intended for. If None then the audience is not verified.
    certs_url (str): The URL that specifies the certificates to use to
        verify the token. This URL should return JSON in the format of
        ``{'key id': 'x509 certificate'}`` or a certificate array according to
        the JWK spec (see https://tools.ietf.org/html/rfc7517).
    clock_skew_in_seconds (int): The clock skew used for `iat` and `exp`
        validation.

Returns:
    Mapping[str, Any]: The decoded token.
keysr   NzaThe pyjwt library is not installed, please install the pyjwt package to use the jwk certs format.)
algorithmsaudience)certsr   clock_skew_in_seconds)r   r   ImportErrorPyJWKClientget_signing_key_from_jwtr   keyalgorithm_name)
id_tokenr   r   r   r   r   jwt_lib
caught_excjwks_clientsigning_keys
             r   verify_tokenr(   k   s    2 ,E	!
 )))4!::8D~~OO#223	  
 	
 zz"7	
 	
  	s	s   A7 7
BBBc                     [        U UU[        US9nUS   [        ;  a)  [        R                  " SR                  [        5      5      eU$ )a  Verifies an ID Token issued by Google's OAuth 2.0 authorization server.

Args:
    id_token (Union[str, bytes]): The encoded token.
    request (google.auth.transport.Request): The object used to make
        HTTP requests.
    audience (str): The audience that this token is intended for. This is
        typically your application's OAuth 2.0 client ID. If None then the
        audience is not verified.
    clock_skew_in_seconds (int): The clock skew used for `iat` and `exp`
        validation.

Returns:
    Mapping[str, Any]: The decoded token.

Raises:
    exceptions.GoogleAuthError: If the issuer is invalid.
    ValueError: If token verification fails
r   r   r   issz6Wrong issuer. 'iss' should be one of the following: {})r(   _GOOGLE_OAUTH2_CERTS_URL_GOOGLE_ISSUERSr   GoogleAuthErrorr   )r#   r   r   r   idinfos        r   verify_oauth2_tokenr0      sV    ( *3F e}O+((DKK
 	
 Mr   c                 $    [        U UU[        US9$ )a  Verifies an ID Token issued by Firebase Authentication.

Args:
    id_token (Union[str, bytes]): The encoded token.
    request (google.auth.transport.Request): The object used to make
        HTTP requests.
    audience (str): The audience that this token is intended for. This is
        typically your Firebase application ID. If None then the audience
        is not verified.
    clock_skew_in_seconds (int): The clock skew used for `iat` and `exp`
        validation.

Returns:
    Mapping[str, Any]: The decoded token.
r*   )r(   _GOOGLE_APIS_CERTS_URL)r#   r   r   r   s       r   verify_firebase_tokenr3      s       (3 r   c                 Z   [         R                  R                  [        R                  5      nU(       Ga  [         R
                  R                  U5      (       a$  [         R
                  R                  U5      (       d  [        R                  " S5      e [        US5       nSSKJn  [        R                  " U5      nUR                  S5      S:X  a"  UR                  R!                  XPS9sSSS5        $ UR                  S5      S	:X  a;  SS
KJn  UR&                  R)                  U5      nUR                  UU SS9sSSS5        $  SSS5         SSKJn
  SSKJn  U(       d2  SSKnUR4                  R6                  R8                  R;                  5       nUR=                  U5      (       a  U
R                  XSS9$  [        R                  " S5      e! , (       d  f       N= f! [*         a  n[        R                  " SU5      n	XeSnAff = f! [>        [        R@                  4 a     Nnf = f)ak  Create the ID Token credentials from the current environment.

This function acquires ID token from the environment in the following order.
See https://google.aip.dev/auth/4110.

1. If the environment variable ``GOOGLE_APPLICATION_CREDENTIALS`` is set
   to the path of a valid service account JSON file, then ID token is
   acquired using this service account credentials.
2. If the application is running in Compute Engine, App Engine or Cloud Run,
   then the ID token are obtained from the metadata server.
3. If metadata server doesn't exist and no valid service account credentials
   are found, :class:`~google.auth.exceptions.DefaultCredentialsError` will
   be raised.

Example::

    import google.oauth2.id_token
    import google.auth.transport.requests

    request = google.auth.transport.requests.Request()
    target_audience = "https://pubsub.googleapis.com"

    # Create ID token credentials.
    credentials = google.oauth2.id_token.fetch_id_token_credentials(target_audience, request=request)

    # Refresh the credential to obtain an ID token.
    credentials.refresh(request)

    id_token = credentials.token
    id_token_expiry = credentials.expiry

Args:
    audience (str): The audience that this ID token is intended for.
    request (Optional[google.auth.transport.Request]): A callable used to make
        HTTP requests. A request object will be created if not provided.

Returns:
    google.auth.credentials.Credentials: The ID token credentials.

Raises:
    ~google.auth.exceptions.DefaultCredentialsError:
        If metadata server doesn't exist and no valid service account
        credentials are found.
zCGOOGLE_APPLICATION_CREDENTIALS path is either not found or invalid.rr   )service_accounttyper6   )target_audienceNimpersonated_service_account)impersonated_credentialsT)target_credentialsr8   include_emailzHGOOGLE_APPLICATION_CREDENTIALS is not valid service account credentials.)compute_engine)	_metadata)use_metadata_identity_endpointzGNeither metadata server or valid service account credentials are found.)!osenvirongetr   CREDENTIALSpathexistsisfiler   DefaultCredentialsErroropengoogle.oauth2r6   r   loadIDTokenCredentialsfrom_service_account_infogoogle.authr:   Credentials&from_impersonated_service_account_info
ValueErrorr=   google.auth.compute_enginer>   google.auth.transport.requestsauth	transportrequestsRequestpingr   r   )r   r   credentials_filenamefr6   infor:   r;   r%   new_excr=   r>   googles                r   fetch_id_token_credentialsr]      s   ^ ::>>*:*F*FGGGNN/0034444U 	**C0A9yy|88F#'88*==WW X  10 XXf%)GGD)A)M)M)t)t*& 4FF+=(0&* G  10 H 1:.8 1kk++44<<>G>>'""!44$ 5   # 
,
,Q ] 10(  	* 88ZG )	*. 223 s]   G" !A
G+	G" 5AG;	G" G" A*H 
GG" G" "
H
,HH
H*)H*c                 N    [        XS9nUR                  U 5        UR                  $ )a  Fetch the ID Token from the current environment.

This function acquires ID token from the environment in the following order.
See https://google.aip.dev/auth/4110.

1. If the environment variable ``GOOGLE_APPLICATION_CREDENTIALS`` is set
   to the path of a valid service account JSON file, then ID token is
   acquired using this service account credentials.
2. If the application is running in Compute Engine, App Engine or Cloud Run,
   then the ID token are obtained from the metadata server.
3. If metadata server doesn't exist and no valid service account credentials
   are found, :class:`~google.auth.exceptions.DefaultCredentialsError` will
   be raised.

Example::

    import google.oauth2.id_token
    import google.auth.transport.requests

    request = google.auth.transport.requests.Request()
    target_audience = "https://pubsub.googleapis.com"

    id_token = google.oauth2.id_token.fetch_id_token(request, target_audience)

Args:
    request (google.auth.transport.Request): A callable used to make
        HTTP requests.
    audience (str): The audience that this ID token is intended for.

Returns:
    str: The ID token.

Raises:
    ~google.auth.exceptions.DefaultCredentialsError:
        If metadata server doesn't exist and no valid service account
        credentials are found.
)r   )r]   refreshtoken)r   r   id_token_credentialss      r   fetch_id_tokenrb   J  s*    L 6hP  )%%%r   )Nr   )N)__doc__http.clientclientr
   r   r@   rM   r   r   r   r,   r2   r-   r   r(   r0   r3   r]   rb    r   r   <module>rg      sl   )V "  	 ( " 
 H 
. 
 )*GH5: &0
f#L2jZ(&r   