o
    ˷e$                     @   s   d 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	d
Z	dddZdd Zdd Z		dddZdS )a  OAuth 2.0 async client.

This is a client for interacting with an OAuth 2.0 authorization server's
token endpoint.

For more information about the token endpoint, see
`Section 3.1 of rfc6749`_

.. _Section 3.1 of rfc6749: https://tools.ietf.org/html/rfc6749#section-3.2
    N)http_client)urllib)
exceptions)jwt)_clientFc                    s  |rdt ji}t|d}ndt ji}tj|d}|r(d	||d< d}	 | d|||dI d	H }|
 I d	H }t|d
rG|dn|}	t|	}
|jtjkrUn*|
dp[d}|
dpbd}tdd ||fD rw|dk rw|d7 }q*|jtjk|
fS |jtjk|
fS )a  Makes a request to the OAuth 2.0 authorization server's token endpoint.
    This function doesn't throw on response errors.

    Args:
        request (google.auth.transport.Request): A callable used to make
            HTTP requests.
        token_uri (str): The OAuth 2.0 authorizations server's token endpoint
            URI.
        body (Mapping[str, str]): The parameters to send in the request body.
        access_token (Optional(str)): The access token needed to make the request.
        use_json (Optional(bool)): Use urlencoded format or json format for the
            content type. The default value is False.

    Returns:
        Tuple(bool, Mapping[str, str]): A boolean indicating if the request is
            successful, and a mapping for the JSON-decoded response data.
    zContent-Typezutf-8z	Bearer {}Authorizationr   TPOST)methodurlheadersbodyNdecodeerror_description errorc                 s   s    | ]}|d kV  qdS )internal_failureN ).0er   r   R/var/www/ideatree/venv/lib/python3.10/site-packages/google/oauth2/_client_async.py	<genexpr>^   s    z3_token_endpoint_request_no_throw.<locals>.<genexpr>   )client_JSON_CONTENT_TYPEjsondumpsencode_URLENCODED_CONTENT_TYPEr   parse	urlencodeformatcontenthasattrr   loadsstatusr   OKgetany)request	token_urir   access_tokenuse_jsonr   retryresponseresponse_body1response_bodyresponse_data
error_desc
error_coder   r   r    _token_endpoint_request_no_throw&   s<   


r3   c                    s0   t | ||||dI dH \}}|st| |S )a  Makes a request to the OAuth 2.0 authorization server's token endpoint.

    Args:
        request (google.auth.transport.Request): A callable used to make
            HTTP requests.
        token_uri (str): The OAuth 2.0 authorizations server's token endpoint
            URI.
        body (Mapping[str, str]): The parameters to send in the request body.
        access_token (Optional(str)): The access token needed to make the request.
        use_json (Optional(bool)): Use urlencoded format or json format for the
            content type. The default value is False.

    Returns:
        Mapping[str, str]: The JSON-decoded response data.

    Raises:
        google.auth.exceptions.RefreshError: If the token endpoint returned
            an error.
    )r*   r+   N)r3   r   _handle_error_response)r(   r)   r   r*   r+   response_status_okr0   r   r   r   _token_endpoint_requesth   s   

r6   c           	   
      s~   |t jd}t| ||I dH }z|d }W n ty4 } ztd|}t|| W Y d}~nd}~ww t |}|||fS )a  Implements the JWT Profile for OAuth 2.0 Authorization Grants.

    For more details, see `rfc7523 section 4`_.

    Args:
        request (google.auth.transport.Request): A callable used to make
            HTTP requests.
        token_uri (str): The OAuth 2.0 authorizations server's token endpoint
            URI.
        assertion (str): The OAuth 2.0 assertion.

    Returns:
        Tuple[str, Optional[datetime], Mapping[str, str]]: The access token,
            expiration, and additional data returned by the token endpoint.

    Raises:
        google.auth.exceptions.RefreshError: If the token endpoint returned
            an error.

    .. _rfc7523 section 4: https://tools.ietf.org/html/rfc7523#section-4
    	assertion
grant_typeNr*   zNo access token in response.)	r   _JWT_GRANT_TYPEr6   KeyErrorr   RefreshErrorsix
raise_from_parse_expiry)	r(   r)   r8   r   r0   r*   
caught_excnew_excexpiryr   r   r   	jwt_grant   s   

rC   c           
   
      s   |t jd}t| ||I dH }z|d }W n ty4 } ztd|}t|| W Y d}~nd}~ww tj	|dd}t
j
|d }	||	|fS )a  Implements the JWT Profile for OAuth 2.0 Authorization Grants, but
    requests an OpenID Connect ID Token instead of an access token.

    This is a variant on the standard JWT Profile that is currently unique
    to Google. This was added for the benefit of authenticating to services
    that require ID Tokens instead of access tokens or JWT bearer tokens.

    Args:
        request (google.auth.transport.Request): A callable used to make
            HTTP requests.
        token_uri (str): The OAuth 2.0 authorization server's token endpoint
            URI.
        assertion (str): JWT token signed by a service account. The token's
            payload must include a ``target_audience`` claim.

    Returns:
        Tuple[str, Optional[datetime], Mapping[str, str]]:
            The (encoded) Open ID Connect ID Token, expiration, and additional
            data returned by the endpoint.

    Raises:
        google.auth.exceptions.RefreshError: If the token endpoint returned
            an error.
    r7   Nid_tokenzNo ID token in response.F)verifyexp)r   r:   r6   r;   r   r<   r=   r>   r   r   datetimeutcfromtimestamp)
r(   r)   r8   r   r0   rD   r@   rA   payloadrB   r   r   r   id_token_jwt_grant   s   
rJ   c           	         sN   t j|||d}|rd||d< |r||d< t| ||I dH }t ||S )a  Implements the OAuth 2.0 refresh token grant.

    For more details, see `rfc678 section 6`_.

    Args:
        request (google.auth.transport.Request): A callable used to make
            HTTP requests.
        token_uri (str): The OAuth 2.0 authorizations server's token endpoint
            URI.
        refresh_token (str): The refresh token to use to get a new access
            token.
        client_id (str): The OAuth 2.0 application's client ID.
        client_secret (str): The Oauth 2.0 appliaction's client secret.
        scopes (Optional(Sequence[str])): Scopes to request. If present, all
            scopes must be authorized for the refresh token. Useful if refresh
            token has a wild card scope (e.g.
            'https://www.googleapis.com/auth/any-api').
        rapt_token (Optional(str)): The reauth Proof Token.

    Returns:
        Tuple[str, Optional[str], Optional[datetime], Mapping[str, str]]: The
            access token, new or current refresh token, expiration, and additional data
            returned by the token endpoint.

    Raises:
        google.auth.exceptions.RefreshError: If the token endpoint returned
            an error.

    .. _rfc6748 section 6: https://tools.ietf.org/html/rfc6749#section-6
    )r9   	client_idclient_secretrefresh_token scoperaptN)r   _REFRESH_GRANT_TYPEjoinr6   _handle_refresh_grant_response)	r(   r)   rM   rK   rL   scopes
rapt_tokenr   r0   r   r   r   refresh_grant   s   (rV   )NF)NN)__doc__rG   r   r=   	six.movesr   r   google.authr   r   google.oauth2r   r   r3   r6   rC   rJ   rV   r   r   r   r   <module>   s$   
C
%/