o
    ~j6hQa                     @   s   d Z ddlmZ ddlZddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddlm
Z
 ddlmZ dd	lmZ eeZd
ZdZG dd de	je	jZG dd de	jZdS )a  OAuth 2.0 Credentials.

This module provides credentials based on OAuth 2.0 access and refresh tokens.
These credentials usually access resources on behalf of a user (resource
owner).

Specifically, this is intended to use access tokens acquired using the
`Authorization Code grant`_ and can refresh those tokens using a
optional `refresh token`_.

Obtaining the initial access and refresh token is outside of the scope of this
module. Consult `rfc6749 section 4.1`_ for complete details on the
Authorization Code grant flow.

.. _Authorization Code grant: https://tools.ietf.org/html/rfc6749#section-1.3.1
.. _refresh token: https://tools.ietf.org/html/rfc6749#section-6
.. _rfc6749 section 4.1: https://tools.ietf.org/html/rfc6749#section-4.1
    )datetimeN)
_cloud_sdk)_helpers)credentials)
exceptions)metrics)reauthz#https://oauth2.googleapis.com/tokenz'https://oauth2.googleapis.com/tokeninfoc                       s  e Zd ZdZddddddddddddddejdf fdd	Zdd Zdd	 Ze	d
d Z
e	dd Ze	dd Ze	dd Ze	dd Ze	dd Ze	dd Ze	dd Ze	dd Ze	dd Zejdd Ze	dd  Zd!d" Zeejd#d$ Zeejd%d& Zeejd'd( Zd)d* Zeej d+d, Z!d-d. Z"eejd/d0 Z#e$d7d1d2Z%e$d7d3d4Z&d7d5d6Z'  Z(S )8Credentialsa  Credentials using OAuth 2.0 access and refresh tokens.

    The credentials are considered immutable except the tokens and the token
    expiry, which are updated after refresh. If you want to modify the quota
    project, use :meth:`with_quota_project` or ::

        credentials = credentials.with_quota_project('myproject-123')

    Reauth is disabled by default. To enable reauth, set the
    `enable_reauth_refresh` parameter to True in the constructor. Note that
    reauth feature is intended for gcloud to use only.
    If reauth is enabled, `pyu2f` dependency has to be installed in order to use security
    key reauth feature. Dependency can be installed via `pip install pyu2f` or `pip install
    google-auth[reauth]`.
    NFc                    s   t t|   || _|
| _|| _|| _|| _|| _|| _	|| _
|| _|| _|	| _|| _|| _|| _|| _|p8tj| _|p=d| _d| _dS )a  
        Args:
            token (Optional(str)): The OAuth 2.0 access token. Can be None
                if refresh information is provided.
            refresh_token (str): The OAuth 2.0 refresh token. If specified,
                credentials can be refreshed.
            id_token (str): The Open ID Connect ID Token.
            token_uri (str): The OAuth 2.0 authorization server's token
                endpoint URI. Must be specified for refresh, can be left as
                None if the token can not be refreshed.
            client_id (str): The OAuth 2.0 client ID. Must be specified for
                refresh, can be left as None if the token can not be refreshed.
            client_secret(str): The OAuth 2.0 client secret. Must be specified
                for refresh, can be left as None if the token can not be
                refreshed.
            scopes (Sequence[str]): The scopes used to obtain authorization.
                This parameter is used by :meth:`has_scopes`. OAuth 2.0
                credentials can not request additional scopes after
                authorization. The scopes must be derivable from the refresh
                token if refresh information is provided (e.g. The refresh
                token scopes are a superset of this or contain a wild card
                scope like 'https://www.googleapis.com/auth/any-api').
            default_scopes (Sequence[str]): Default scopes passed by a
                Google client library. Use 'scopes' for user-defined scopes.
            quota_project_id (Optional[str]): The project ID used for quota and billing.
                This project may be different from the project used to
                create the credentials.
            rapt_token (Optional[str]): The reauth Proof Token.
            refresh_handler (Optional[Callable[[google.auth.transport.Request, Sequence[str]], [str, datetime]]]):
                A callable which takes in the HTTP request callable and the list of
                OAuth scopes and when called returns an access token string for the
                requested scopes and its expiry datetime. This is useful when no
                refresh tokens are provided and tokens are obtained by calling
                some external process on demand. It is particularly useful for
                retrieving downscoped tokens from a token broker.
            enable_reauth_refresh (Optional[bool]): Whether reauth refresh flow
                should be used. This flag is for gcloud to use only.
            granted_scopes (Optional[Sequence[str]]): The scopes that were consented/granted by the user.
                This could be different from the requested scopes and it could be empty if granted
                and requested scopes were same.
            trust_boundary (str): String representation of trust boundary meta.
            universe_domain (Optional[str]): The universe domain. The default
                universe domain is googleapis.com.
            account (Optional[str]): The account associated with the credential.
         N)superr	   __init__tokenexpiry_refresh_token	_id_token_scopes_default_scopes_granted_scopes
_token_uri
_client_id_client_secret_quota_project_id_rapt_tokenrefresh_handler_enable_reauth_refresh_trust_boundaryr   DEFAULT_UNIVERSE_DOMAIN_universe_domain_account_cred_file_path)selfr   refresh_tokenid_token	token_uri	client_idclient_secretscopesdefault_scopesquota_project_idr   
rapt_tokenr   enable_reauth_refreshgranted_scopestrust_boundaryuniverse_domainaccount	__class__ h/var/www/html/chefvision.cloud.itp360.com/venv/lib/python3.10/site-packages/google/oauth2/credentials.pyr   J   s&   A

zCredentials.__init__c                 C   s*   | j  }d|v r|d= d|v r|d= |S )zA __getstate__ method must exist for the __setstate__ to be called
        This is identical to the default implementation.
        See https://docs.python.org/3.7/library/pickle.html#object.__setstate__
        _refresh_handler_refresh_worker)__dict__copy)r    
state_dictr1   r1   r2   __getstate__   s   
zCredentials.__getstate__c                 C   s   | d| _| d| _| d| _| d| _| d| _| d| _| d| _| d| _| d	| _	| d
| _
| d| _| d| _| d| _| d| _| dp[tj| _| d| _d| _d| _| dd| _| dd| _dS )z\Credentials pickled with older versions of the class do not have
        all the attributes.r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   N_use_non_blocking_refreshFr   r
   )getr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r3   r4   r9   r   )r    dr1   r1   r2   __setstate__   s*   zCredentials.__setstate__c                 C      | j S )z+Optional[str]: The OAuth 2.0 refresh token.)r   r    r1   r1   r2   r!         zCredentials.refresh_tokenc                 C   r=   )z/Optional[str]: The OAuth 2.0 permission scopes.)r   r>   r1   r1   r2   r&      r?   zCredentials.scopesc                 C   r=   )zWOptional[Sequence[str]]: The OAuth 2.0 permission scopes that were granted by the user.)r   r>   r1   r1   r2   r+      r?   zCredentials.granted_scopesc                 C   r=   )zOOptional[str]: The OAuth 2.0 authorization server's token endpoint
        URI.)r   r>   r1   r1   r2   r#      s   zCredentials.token_uric                 C   r=   )ad  Optional[str]: The Open ID Connect ID Token.

        Depending on the authorization server and the scopes requested, this
        may be populated when credentials are obtained and updated when
        :meth:`refresh` is called. This token is a JWT. It can be verified
        and decoded using :func:`google.oauth2.id_token.verify_oauth2_token`.
        )r   r>   r1   r1   r2   r"      s   	zCredentials.id_tokenc                 C   r=   )z'Optional[str]: The OAuth 2.0 client ID.)r   r>   r1   r1   r2   r$      r?   zCredentials.client_idc                 C   r=   )z+Optional[str]: The OAuth 2.0 client secret.)r   r>   r1   r1   r2   r%      r?   zCredentials.client_secretc                 C   s   dS )zvFalse: OAuth 2.0 credentials have their scopes set when
        the initial token is requested and can not be changed.Fr1   r>   r1   r1   r2   requires_scopes   s   zCredentials.requires_scopesc                 C   r=   )z&Optional[str]: The reauth Proof Token.)r   r>   r1   r1   r2   r)      r?   zCredentials.rapt_tokenc                 C   r=   )zReturns the refresh handler if available.

        Returns:
           Optional[Callable[[google.auth.transport.Request, Sequence[str]], [str, datetime]]]:
               The current refresh handler.
        )r3   r>   r1   r1   r2   r      s   zCredentials.refresh_handlerc                 C   s"   t |s|durtd|| _dS )a/  Updates the current refresh handler.

        Args:
            value (Optional[Callable[[google.auth.transport.Request, Sequence[str]], [str, datetime]]]):
                The updated value of the refresh handler.

        Raises:
            TypeError: If the value is not a callable or None.
        Nz7The provided refresh_handler is not a callable or None.)callable	TypeErrorr3   )r    valuer1   r1   r2   r   
  s   
c                 C   r=   )zlstr: The user account associated with the credential. If the account is unknown an empty string is returned.)r   r>   r1   r1   r2   r.     r?   zCredentials.accountc                 C   sR   | j | j| j| j| j| j| j| j| j| j	| j
| j| j| j| j| jd}| j|_|S )N)r!   r"   r#   r$   r%   r&   r'   r+   r(   r)   r*   r,   r-   r.   )r0   r   r!   r"   r#   r$   r%   r&   r'   r+   r(   r)   r   r   r   r   r   )r    credr1   r1   r2   
_make_copy  s&   zCredentials._make_copyc                 C   s*   | j r| j dd}| jr| j|d< |S d S )Nzuser credentials)credential_sourcecredential_type	principal)r   r.   )r    	cred_infor1   r1   r2   get_cred_info3  s   
zCredentials.get_cred_infoc                 C      |   }||_|S N)rE   r   )r    r(   rD   r1   r1   r2   with_quota_project?     zCredentials.with_quota_projectc                 C   rK   rL   )rE   r   )r    r#   rD   r1   r1   r2   with_token_uriE  rN   zCredentials.with_token_uric                 C   rK   )zReturns a copy of these credentials with a modified account.

        Args:
            account (str): The account to set

        Returns:
            google.oauth2.credentials.Credentials: A new credentials instance.
        )rE   r   )r    r.   rD   r1   r1   r2   with_accountK  s   	zCredentials.with_accountc                 C   rK   rL   )rE   r   )r    r-   rD   r1   r1   r2   with_universe_domainX  rN   z Credentials.with_universe_domainc                 C   s   t jS rL   )r   CRED_TYPE_USERr>   r1   r1   r2   _metric_header_for_usage^  s   z$Credentials._metric_header_for_usagec              
   C   s  | j tjkrtd| j | jd ur| jn| j}| jd u rU| j	rU| j	||d\}}t
|ts5tdt
|ts?tdt |tj krMtd|| _|| _d S | jd u si| jd u si| jd u si| jd u rntdtj|| j| j| j| j|| j| jd\}}}}}|| _|| _|| _|d| _|| _|rd	|v rt|}	|d	  | _t| j}
|	|
 }|rtd
d | d S d S d S d S )Na  User credential refresh is only supported in the default googleapis.com universe domain, but the current universe domain is {}. If you created the credential with an access token, it's likely that the provided token is expired now, please update your code with a valid token.)r&   z3The refresh_handler returned token is not a string.z=The refresh_handler returned expiry is not a datetime object.zDThe credentials returned by the refresh_handler are already expired.zThe credentials do not contain the necessary fields need to refresh the access token. You must specify refresh_token, token_uri, client_id, and client_secret.)r&   r)   r*   r"   scopezUNot all requested scopes were granted by the authorization server, missing scopes {}., )!r   r   r   r   RefreshErrorformatr   r   r   r   
isinstancestrr   r   utcnowREFRESH_THRESHOLDr   r   r   r   r   r   refresh_grantr   r   r:   r   	frozensetsplitr   _LOGGERwarningjoin)r    requestr&   r   r   access_tokenr!   grant_responser)   requested_scopesr+    scopes_requested_but_not_grantedr1   r1   r2   refresha  s   







zCredentials.refreshc                 C   s   t d}|| }|rtdd||d}|r-t|	d
dd d}nt tj }|d	u rKd
|v rK|d
}t|trK|
d}| |d|dt||d|d|d||d|d|d|dddS )a  Creates a Credentials instance from parsed authorized user info.

        Args:
            info (Mapping[str, str]): The authorized user info in Google
                format.
            scopes (Sequence[str]): Optional list of scopes to include in the
                credentials.

        Returns:
            google.oauth2.credentials.Credentials: The constructed
                credentials.

        Raises:
            ValueError: If the info is not in the expected format.
        )r!   r$   r%   zGAuthorized user info was not in the expected format, missing fields {}.rU   r   Z.r   z%Y-%m-%dT%H:%M:%SNr&    r   r!   r$   r%   r(   r)   r,   r-   r.   r
   )r   r!   r#   r&   r$   r%   r(   r   r)   r,   r-   r.   )set
differencekeys
ValueErrorrW   ra   r:   r   strptimerstripr^   r   rZ   r[   rX   rY   _GOOGLE_OAUTH2_TOKEN_ENDPOINT)clsinfor&   keys_neededmissingr   r1   r1   r2   from_authorized_user_info  s>   




z%Credentials.from_authorized_user_infoc                 C   sJ   t j|ddd}t|}| ||W  d   S 1 sw   Y  dS )a  Creates a Credentials instance from an authorized user json file.

        Args:
            filename (str): The path to the authorized user json file.
            scopes (Sequence[str]): Optional list of scopes to include in the
                credentials.

        Returns:
            google.oauth2.credentials.Credentials: The constructed
                credentials.

        Raises:
            ValueError: If the file is not in the expected format.
        rzutf-8)encodingN)ioopenjsonloadrv   )rr   filenamer&   	json_filedatar1   r1   r2   from_authorized_user_file  s   

$z%Credentials.from_authorized_user_filec              
      s|   | j | j| j| j| j| j| j| j| jd	}| j	r!| j	
 d |d< dd | D } dur9 fdd| D }t|S )a  Utility function that creates a JSON representation of a Credentials
        object.

        Args:
            strip (Sequence[str]): Optional list of members to exclude from the
                                   generated JSON.

        Returns:
            str: A JSON representation of this instance. When converted into
            a dictionary, it can be passed to from_authorized_user_info()
            to create a new credential instance.
        )	r   r!   r#   r$   r%   r&   r)   r-   r.   rh   r   c                 S   s   i | ]\}}|d ur||qS rL   r1   .0kvr1   r1   r2   
<dictcomp>#      z'Credentials.to_json.<locals>.<dictcomp>Nc                    s   i | ]\}}| vr||qS r1   r1   r   stripr1   r2   r   '  r   )r   r!   r#   r$   r%   r&   r)   r   r   r   	isoformatitemsr{   dumps)r    r   prepr1   r   r2   to_json  s    
zCredentials.to_jsonrL   ))__name__
__module____qualname____doc__r   r   r   r8   r<   propertyr!   r&   r+   r#   r"   r$   r%   r@   r)   r   setterr.   rE   r   copy_docstringr	   rJ   CredentialsWithQuotaProjectrM   CredentialsWithTokenUrirO   rP   CredentialsWithUniverseDomainrQ   rS   rg   classmethodrv   r   r   __classcell__r1   r1   r/   r2   r	   9   s~    U










	











Y7r	   c                       sZ   e Zd ZdZd fdd	Zdd Zeej	dd Z
d	d
 Zeejdd Z  ZS )UserAccessTokenCredentialsa  Access token credentials for user account.

    Obtain the access token for a given user account or the current active
    user account with the ``gcloud auth print-access-token`` command.

    Args:
        account (Optional[str]): Account to get the access token for. If not
            specified, the current active account will be used.
        quota_project_id (Optional[str]): The project ID used for quota
            and billing.
    Nc                    s(   t d tt|   || _|| _d S )NzUserAccessTokenCredentials is deprecated, please use google.oauth2.credentials.Credentials instead. To use that credential type, simply run `gcloud auth application-default login` and let the client libraries pick up the application default credentials.)warningswarnr   r   r   r   r   )r    r.   r(   r/   r1   r2   r   9  s   
z#UserAccessTokenCredentials.__init__c                 C   s   | j || jdS )a  Create a new instance with the given account.

        Args:
            account (str): Account to get the access token for.

        Returns:
            google.oauth2.credentials.UserAccessTokenCredentials: The created
                credentials with the given account.
        r.   r(   )r0   r   )r    r.   r1   r1   r2   rP   E  s   
z'UserAccessTokenCredentials.with_accountc                 C   s   | j | j|dS )Nr   )r0   r   )r    r(   r1   r1   r2   rM   Q  s   z-UserAccessTokenCredentials.with_quota_projectc                 C   s   t | j| _dS )a{  Refreshes the access token.

        Args:
            request (google.auth.transport.Request): This argument is required
                by the base class interface but not used in this implementation,
                so just set it to `None`.

        Raises:
            google.auth.exceptions.UserAccessTokenError: If the access token
                refresh failed.
        N)r   get_auth_access_tokenr   r   )r    rb   r1   r1   r2   rg   U  s   z"UserAccessTokenCredentials.refreshc                 C   s   |  | | | d S rL   )rg   apply)r    rb   methodurlheadersr1   r1   r2   before_requestc  s   
z)UserAccessTokenCredentials.before_request)NN)r   r   r   r   r   rP   r   r   r   r   rM   rg   r	   r   r   r1   r1   r/   r2   r   ,  s    


r   )r   r   ry   r{   loggingr   google.authr   r   r   r   r   google.oauth2r   	getLoggerr   r_   rq   "_GOOGLE_OAUTH2_TOKEN_INFO_ENDPOINTReadOnlyScopedr   r	   r   r1   r1   r1   r2   <module>   s(   
   v