
    (ph/#                         S 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	r
S
rSr\R                  " SS9r " S S\R                  5      rg)z'Experimental GDCH credentials support.
    N)_helpers)_service_account_info)credentials)
exceptions)jwt)_clientz/urn:ietf:params:oauth:token-type:token-exchangez-urn:ietf:params:oauth:token-type:access_tokenz.urn:k8s:params:oauth:token-type:serviceaccounti  )secondsc                      ^  \ rS rSrSrU 4S jrS r\R                  " \	R                  5      S 5       rS r\S 5       r\S 5       r\S	 5       rS
rU =r$ )ServiceAccountCredentials"   a  Credentials for GDCH (`Google Distributed Cloud Hosted`_) for service
    account users.

    .. _Google Distributed Cloud Hosted:
        https://cloud.google.com/blog/topics/hybrid-cloud/            announcing-google-distributed-cloud-edge-and-hosted

    To create a GDCH service account credential, first create a JSON file of
    the following format::

        {
            "type": "gdch_service_account",
            "format_version": "1",
            "project": "<project name>",
            "private_key_id": "<key id>",
            "private_key": "-----BEGIN EC PRIVATE KEY-----
<key bytes>
-----END EC PRIVATE KEY-----
",
            "name": "<service identity name>",
            "ca_cert_path": "<CA cert path>",
            "token_uri": "https://service-identity.<Domain>/authenticate"
        }

    The "format_version" field stands for the format of the JSON file. For now
    it is always "1". The `private_key_id` and `private_key` is used for signing.
    The `ca_cert_path` is used for token server TLS certificate verification.

    After the JSON file is created, set `GOOGLE_APPLICATION_CREDENTIALS` environment
    variable to the JSON file path, then use the following code to create the
    credential::

        import google.auth

        credential, _ = google.auth.default()
        credential = credential.with_gdch_audience("<the audience>")

    We can also create the credential directly::

        from google.oauth import gdch_credentials

        credential = gdch_credentials.ServiceAccountCredentials.from_service_account_file("<the json file path>")
        credential = credential.with_gdch_audience("<the audience>")

    The token is obtained in the following way. This class first creates a
    self signed JWT. It uses the `name` value as the `iss` and `sub` claim, and
    the `token_uri` as the `aud` claim, and signs the JWT with the `private_key`.
    It then sends the JWT to the `token_uri` to exchange a final token for
    `audience`.
    c                 r   > [         [        U ]  5         Xl        X l        X0l        X@l        XPl        X`l        g)a  
Args:
    signer (google.auth.crypt.Signer): The signer used to sign JWTs.
    service_identity_name (str): The service identity name. It will be
        used as the `iss` and `sub` claim in the self signed JWT.
    project (str): The project.
    audience (str): The audience for the final token.
    token_uri (str): The token server uri.
    ca_cert_path (str): The CA cert path for token server side TLS
        certificate verification. If the token server uses well known
        CA, then this parameter can be `None`.
N)	superr   __init___signer_service_identity_name_project	_audience
_token_uri_ca_cert_path)selfsignerservice_identity_nameprojectaudience	token_urica_cert_path	__class__s          Q/var/www/html/venv/lib/python3.13/site-packages/google/oauth2/gdch_credentials.pyr   "ServiceAccountCredentials.__init__S   s3     	'79&;#!#)    c                 h   [         R                  " 5       nU[        -   nSR                  U R                  U R
                  5      nUUU R                  [         R                  " U5      [         R                  " U5      S.n[         R                  " [        R                  " U R                  U5      5      $ )Nzsystem:serviceaccount:{}:{})isssubaudiatexp)r   utcnowJWT_LIFETIMEformatr   r   r   datetime_to_secs
from_bytesr   encoder   )r   nowexpiryiss_sub_valuepayloads        r   _create_jwt%ServiceAccountCredentials._create_jwtj   s    oo|#5<<MM466

 ! ??,,S1,,V4
 ""3::dllG#DEEr    c           	         SS K n[        XR                  R                  R                  R
                  5      (       d  [        R                  " S5      eU R                  5       n[        U R                  [        U[        S.n[        R                  " UU R                  US SU R                   S9n[        R"                  " US 5      u  U l        o`l        ng )Nr   zeFor GDCH service account credentials, request must be a google.auth.transport.requests.Request object)
grant_typer   requested_token_typesubject_tokensubject_token_typeT)access_tokenuse_jsonverify)google.auth.transport.requests
isinstanceauth	transportrequestsRequestr   RefreshErrorr1   TOKEN_EXCHANGE_TYPEr   ACCESS_TOKEN_TOKEN_TYPESERVICE_ACCOUNT_TOKEN_TYPEr   _token_endpoint_requestr   r   _handle_refresh_grant_responsetokenr.   )r   requestgoogle	jwt_tokenrequest_bodyresponse_data_s          r   refresh!ServiceAccountCredentials.refresh{   s    -';;#8#8#A#A#I#IJJ))w 
 $$&	-$;&"<
  77OO%%
 )0(N(N4)
%
A{Ar    c                     U R                  U R                  U R                  U R                  UU R                  U R
                  5      $ )zCreate a copy of GDCH credentials with the specified audience.

Args:
    audience (str): The intended audience for GDCH credentials.
)r   r   r   r   r   r   )r   r   s     r   with_gdch_audience,ServiceAccountCredentials.with_gdch_audience   s?     ~~LL''MMOO
 	
r    c                 v    US   S:w  a  [        S5      eU " UUS   US   SUS   UR                  SS5      5      $ )	a|  Creates a Credentials instance from a signer and service account
info.

Args:
    signer (google.auth.crypt.Signer): The signer used to sign JWTs.
    info (Mapping[str, str]): The service account info.

Returns:
    google.oauth2.gdch_credentials.ServiceAccountCredentials: The constructed
        credentials.

Raises:
    ValueError: If the info is not in the expected format.
format_version1z"Only format version 1 is supportednamer   Nr   r   )
ValueErrorget)clsr   infos      r   _from_signer_and_info/ServiceAccountCredentials._from_signer_and_info   sU       !S(ABBLOHH^T*
 	
r    c                 T    [         R                  " U/ SQSS9nU R                  X!5      $ )a  Creates a Credentials instance from parsed service account info.

Args:
    info (Mapping[str, str]): The service account info in Google
        format.
    kwargs: Additional arguments to pass to the constructor.

Returns:
    google.oauth2.gdch_credentials.ServiceAccountCredentials: The constructed
        credentials.

Raises:
    ValueError: If the info is not in the expected format.
rT   private_key_idprivate_keyrV   r   r   Frequireuse_rsa_signer)r   	from_dictr[   )rY   rZ   r   s      r   from_service_account_info3ServiceAccountCredentials.from_service_account_info   s4      '00 !
 ((66r    c                 X    [         R                  " U/ SQSS9u  p#U R                  X25      $ )a1  Creates a Credentials instance from a service account json file.

Args:
    filename (str): The path to the service account json file.
    kwargs: Additional arguments to pass to the constructor.

Returns:
    google.oauth2.gdch_credentials.ServiceAccountCredentials: The constructed
        credentials.
r^   Fra   )r   from_filenamer[   )rY   filenamerZ   r   s       r   from_service_account_file3ServiceAccountCredentials.from_service_account_file   s6     -:: !
 ((66r    )r   r   r   r   r   r   r.   rG   )__name__
__module____qualname____firstlineno____doc__r   r1   r   copy_docstringr   CredentialsrN   rQ   classmethodr[   re   rj   __static_attributes____classcell__)r   s   @r   r   r   "   sz    .`*.F" [445
 6
<
 
 
6 7 7: 7 7r    r   )rp   datetimegoogle.authr   r   r   r   r   google.oauth2r   rB   rC   rD   	timedeltar(   rr   r    r    r   <module>r{      sZ       - # "  ! H I M !!$/Y7 7 7 Y7r    