File: //usr/local/CyberCP/lib/python3.10/site-packages/google/oauth2/__pycache__/id_token.cpython-310.pyc
o
�h#/ � @ s� d Z ddlmZ ddlZddlZddlmZ ddlmZ ddlm Z dZ
dZdd gZd
d� Z
de
dfdd
�Zddd�Zddd�Zddd�Zdd� ZdS )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.comzaccounts.google.comzhttps://accounts.google.comc C s: | |dd�}|j tjkrt�d�|���t�|j� d��S )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.
certs_url (str): The certificate endpoint URL.
Returns:
Mapping[str, str]: A mapping of public key ID to x.509 certificate
data.
�GET)�methodz"Could not fetch certificates at {}zutf-8)
�status�http_client�OKr �TransportError�format�json�loads�data�decode)�request� certs_url�response� r �I/usr/local/CyberCP/lib/python3.10/site-packages/google/oauth2/id_token.py�_fetch_certsQ s �r c C s t ||�}tj| |||d�S )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'}``.
clock_skew_in_seconds (int): The clock skew used for `iat` and `exp`
validation.
Returns:
Mapping[str, Any]: The decoded token.
)�certs�audience�clock_skew_in_seconds)r r r )�id_tokenr r r r r r r r �verify_tokenj s
�r c C s2 t | ||t|d�}|d tvrt�d�t���|S )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 �idinfor r r �verify_oauth2_token� s ���r! c C s t | ||t|d�S )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 r r r �verify_firebase_token� s �r# c
C sH t j�tj�}|rit j�|�rt j�|�st� d��z9t
|d��*}ddlm} t
�|�}|�d�dkrC|jj|| d�W d � W S W d � n1 sMw Y W n tyh } z t� d |�}||�d}~ww z)dd
lm} ddlm} |s�ddl}
|
jjj�� }| �|�r�|j|| dd
�W S W n ttjfy� Y nw t� d��)a� 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�typer% )�target_audienceNzHGOOGLE_APPLICATION_CREDENTIALS is not valid service account credentials.)�compute_engine)� _metadataT)�use_metadata_identity_endpointzGNeither metadata server or valid service account credentials are found.)�os�environ�getr �CREDENTIALS�path�exists�isfiler �DefaultCredentialsError�open�
google.oauth2r% r �load�IDTokenCredentials�from_service_account_info�
ValueError�google.authr( �google.auth.compute_enginer) �google.auth.transport.requests�auth� transport�requests�Request�ping�ImportErrorr
)r r �credentials_filename�fr% �info�
caught_exc�new_excr( r) �googler r r �fetch_id_token_credentials� s\ /
�
��
�������
����rH c C s t || d�}|�| � |jS )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 )rH �refresh�token)r r �id_token_credentialsr r r �fetch_id_token, s &
rL )Nr )N)�__doc__�http.client�clientr r r+ r9 r r r r r"