Ë
    ¹ÿj@1  ã                   óœ   — d Z ddlmZ ddlZddlZddlZddlmZm	Z	m
Z
 ddlmZ ddlmZ d„ Zdej"                  dfd„Z	 dd„Z	 dd	„Zdd
„Zy)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_async
    from google.auth.transport import aiohttp_requests

    request = aiohttp_requests.Request()

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

    if id_info['iss'] != 'https://accounts.google.com':
        raise ValueError('Wrong issuer.')

    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 Token:
    http://openid.net/specs/openid-connect-core-1_0.html#IDToken
.. _CacheControl: https://cachecontrol.readthedocs.io
é    N)Úenvironment_varsÚ
exceptionsÚjwt)Úrequests)Úid_tokenc              ƒ   ó  K  —  | |d¬«      ƒ d{  –—† }|j                   t        j                  k7  r$t        j                  dj                  |«      «      ‚|j                  «       ƒ d{  –—† }t        j                  |«      S 7 Œr7 Œ­w)a¸  Fetches certificates.

    Google-style cerificate endpoints return JSON in the format of
    ``{'key id': 'x509 certificate'}``.

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

    Returns:
        Mapping[str, str]: A mapping of public key ID to x.509 certificate
            data.
    ÚGET)ÚmethodNz"Could not fetch certificates at {})	ÚstatusÚhttp_clientÚOKr   ÚTransportErrorÚformatÚcontentÚjsonÚloads)ÚrequestÚ	certs_urlÚresponseÚdatas       úW/root/telegram-saas/.venv/lib/python3.12/site-packages/google/oauth2/_id_token_async.pyÚ_fetch_certsr   G   su   è ø€ ñ ˜Y¨uÔ5×5€Hà‡�œ+Ÿ.™.Ò(Ü×'Ñ'Ø0×7Ñ7¸	ÓBó
ð 	
ð ×!Ñ!Ó#×#€Dä�:‰:�dÓÐð 6øð $ús"   ‚B�B‘ABÁ)BÁ*BÂBc              ƒ   óh   K  — t        ||«      ƒ d{  –—† }t        j                  | |||¬«      S 7 Œ­w)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. This must be an aiohttp request.
        audience (str): The audience 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'}``.
        clock_skew_in_seconds (int): The clock skew used for `iat` and `exp`
            validation.

    Returns:
        Mapping[str, Any]: The decoded token.
    N)ÚcertsÚaudienceÚclock_skew_in_seconds)r   r   Údecode)r   r   r   r   r   r   s         r   Úverify_tokenr   b   s9   è ø€ ô0 ˜w¨	Ó2×2€Eä�:‰:ØØØØ3ô	ð ð 3ús   ‚2’0“2c              ƒ   óì   K  — t        | ||t        j                  |¬«      ƒ d{  –—† }|d   t        j                  vr2t	        j
                  dj                  t        j                  «      «      ‚|S 7 ŒM­w)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. This must be an aiohttp request.
        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.
    ©r   r   r   NÚissz6Wrong issuer. 'iss' should be one of the following: {})r   Úsync_id_tokenÚ_GOOGLE_OAUTH2_CERTS_URLÚ_GOOGLE_ISSUERSr   ÚGoogleAuthErrorr   )r   r   r   r   Úidinfos        r   Úverify_oauth2_tokenr'   „   sv   è ø€ ô*  ØØØÜ×8Ñ8Ø3ô÷ €Fð ˆe�}œM×9Ñ9Ñ9Ü×(Ñ(ØD×KÑKÜ×-Ñ-óó
ð 	
ð €Mðús   ‚"A4¤A2¥AA4c              ƒ   ó*  K  — |st        j                  dt        d¬«       t        | ||t        j
                  |¬«      ƒ d{  –—† }|j                  d«      }t        |t        «      r|st        j                  d«      ‚dj                  t        j                  |«      }|j                  d	«      |k7  r$t        j                  d
j                  |«      «      ‚|j                  d«      }t        |t        «      r|st        j                  d«      ‚|S 7 ŒÎ­w)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. This must be an aiohttp request.
        audience (Optional[Union[str, Iterable[str]]]): The audience that this
            token is intended for. This is your Firebase project ID. If
            ``None`` or empty, the audience will be left unverified, and a
            ``UserWarning`` is issued. Passing a non-empty ``audience`` will be
            required in a future release.
        clock_skew_in_seconds (int): The clock skew used for `iat` and `exp`
            validation.

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

    Raises:
        exceptions.InvalidValue: If the issuer, audience, or subject is invalid.
        ValueError: If token verification fails.
    zºCalling verify_firebase_token without an 'audience' leaves the token's audience claim unverified. Pass your Firebase project ID as 'audience', which will be required in a future release.é   )Ú
stacklevelr    NÚaudz+Token does not contain a valid 'aud' claim.z{}{}r!   z#Wrong issuer. 'iss' should be '{}'.Úsubz+Token does not contain a valid 'sub' claim.)ÚwarningsÚwarnÚUserWarningr   r"   Ú_GOOGLE_APIS_CERTS_URLÚgetÚ
isinstanceÚstrr   ÚInvalidValuer   Ú_FIREBASE_ISSUER_PREFIX)r   r   r   r   r&   Ú	token_audÚexpected_issuerÚ	token_subs           r   Úverify_firebase_tokenr9   «   sù   è ø€ ñ0 Ü�‰ðIô Øõ	
ô  ØØØÜ×6Ñ6Ø3ô÷ €Fð —
‘
˜5Ó!€IÜ�i¤Ô%©YÜ×%Ñ%Ð&SÓTÐTà—m‘m¤M×$IÑ$IÈ9ÓU€OØ‡z�z�%Ó˜OÒ+Ü×%Ñ%Ø1×8Ñ8¸ÓIó
ð 	
ð —
‘
˜5Ó!€IÜ�i¤Ô%©YÜ×%Ñ%Ð&SÓTÐTà€Mð-ús   ‚A DÁDÁCDc              ƒ   óÒ  K  — t         j                  j                  t        j                  «      }|rät         j
                  j                  |«      rt         j
                  j                  |«      st        j                  d«      ‚	 t        |d«      5 }ddlm} t        j                  |«      }|j                  d«      dk(  rK|j                  j!                  ||¬«      }|j#                  | «      ƒ d{  –—†  |j$                  cddd«       S 	 ddd«       	 dd
lm}
 ddlm} t1        j2                  «       }|j5                  |«      r2|
j                  ||d|¬«      }|j#                  |«       |j$                  S 	 t        j                  d«      ‚7 Œœ# 1 sw Y   Œ…xY w# t&        $ r}t        j                  d	|«      }	|	|‚d}~ww xY w# t6        t        j8                  f$ r Y Œhw xY w­w)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_async
        import google.auth.transport.aiohttp_requests

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

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

    Args:
        request (google.auth.transport.aiohttp_requests.Request): A callable used to make
            HTTP requests.
        audience (str): The audience that this ID token is intended for.
        bind_id_token (Optional[bool]): Controls whether to request a
            certificate-bound ID token from the metadata server identity
            endpoint. If ``True``, requests a bound token whenever a valid
            Agent Identity certificate is available and token binding is not
            disabled via ``GOOGLE_API_ENABLE_RUNTIME_BOUND_TOKEN``, falling back
            to an unbound token otherwise (or raising
            :class:`~google.auth.exceptions.RefreshError` if a configured
            certificate is not found after retries). If ``False``, always
            requests an unbound token. If ``None`` (default), token binding is
            determined automatically by the library. Set ``True`` or ``False``
            explicitly if your application requires a specific behavior. Has no
            effect when credentials are loaded from
            ``GOOGLE_APPLICATION_CREDENTIALS``.

    Returns:
        str: The ID token.

    Raises:
        ~google.auth.exceptions.DefaultCredentialsError:
            If metadata server doesn't exist and no valid service account
            credentials are found.
        ~google.auth.exceptions.RefreshError:
            If an error occurred while fetching the ID token or if a required
            certificate file is not found after retries.
    zCGOOGLE_APPLICATION_CREDENTIALS path is either not found or invalid.Úrr   )Ú_service_account_asyncÚtypeÚservice_account)Útarget_audienceNzHGOOGLE_APPLICATION_CREDENTIALS is not valid service account credentials.)Úcompute_engine)Ú	_metadataT)Úuse_metadata_identity_endpointÚbind_id_tokenzGNeither metadata server or valid service account credentials are found.)ÚosÚenvironr1   r   ÚCREDENTIALSÚpathÚexistsÚisfiler   ÚDefaultCredentialsErrorÚopenÚgoogle.oauth2r<   r   ÚloadÚIDTokenCredentialsÚfrom_service_account_infoÚrefreshÚtokenÚ
ValueErrorÚgoogle.authr@   Úgoogle.auth.compute_enginerA   r   ÚRequestÚpingÚImportErrorr   )r   r   rC   Úcredentials_filenameÚfr>   ÚinfoÚcredentialsÚ
caught_excÚnew_excr@   rA   Úrequest_news                r   Úfetch_id_tokenr_   å   sÌ  è ø€ ôp Ÿ:™:Ÿ>™>Ô*:×*FÑ*FÓGÐÙä�G‰G�N‰NÐ/Ô0Ü—‘—‘Ð3Ô4ä×4Ñ4ØUóð ð	*ÜÐ*¨CÓ0ð -°AÝSä—y‘y “|�Ø—8‘8˜FÓ#Ð'8Ò8à'×:Ñ:×TÑTØ °(ð Uó ð  ð
 &×-Ñ-¨gÓ6×6Ð6Ø&×,Ñ,÷-ñ -ð 9÷	-ð*Ý.Ý8ä×&Ñ&Ó(ˆØ�>‰>˜+Ô&Ø(×;Ñ;ØØØ/3Ø+ð	 <ó ˆKð ×Ñ Ô,Ø×$Ñ$Ð$ð 'ô ×
,Ñ
,ØQóð ð9 7ø÷-ð -ûô ò 	*Ü ×8Ñ8ØZØóˆGð ˜zÐ)ûð	*ûô. œ×2Ñ2Ð3ò Ùðüs€   ‚BG'ÂF ÂA!FÃ3FÃ4FÄ	F ÄG'ÄF ÄA"G Å9G'ÆFÆFÆF Æ	GÆ'G Ç GÇG'ÇG$Ç!G'Ç#G$Ç$G')Nr   )N)Ú__doc__Úhttp.clientÚclientr   r   rD   r-   rS   r   r   r   Úgoogle.auth.transportr   rL   r   r"   r   r#   r   r'   r9   r_   © ó    r   ú<module>rf      s]   ðñ,õ\ "Û Û 	Û ç 9Ñ 9Ý *Ý 3òð< Ø×4Ñ4ØóðF =>ó$ðP =>ó7ôtkre   