o
    ~j6h'                     @   s   d Z ddlZddlm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	d
Z	dddZdddZd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)_exponential_backoff)
exceptions)jwt)_clientFTc              	      s  |rdt ji}t|d}ndt ji}tj|d}|r(d	||d< i }d}t
 }	|	D ]S}
| d|||dI dH }| I dH }t|d	rP|dn|}zt|}W n tyd   |}Y nw |jtjkrrd
|df  S t j|j|d}|r~|sd||f  S q2d||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.
        can_retry (bool): Enable or disable request retry behavior.

    Returns:
        Tuple(bool, Mapping[str, str], Optional[bool]): A boolean indicating
          if the request is successful, a mapping for the JSON-decoded response
          data and in the case of an error a boolean indicating if the error
          is retryable.
    zContent-Typezutf-8z	Bearer {}AuthorizationFPOST)methodurlheadersbodyNdecodeT)status_coderesponse_data)client_JSON_CONTENT_TYPEjsondumpsencode_URLENCODED_CONTENT_TYPEurllibparse	urlencodeformatr   ExponentialBackoffcontenthasattrr   loads
ValueErrorstatushttp_clientOK
_can_retry)request	token_urir   access_tokenuse_json	can_retryr
   r   retryable_errorretries_responseresponse_body1response_body r-   j/var/www/html/chefvision.cloud.itp360.com/venv/lib/python3.10/site-packages/google/oauth2/_client_async.py _token_endpoint_request_no_throw%   sD   


r/   c           	         s6   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.
        can_retry (bool): Enable or disable request retry behavior.

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

    Raises:
        google.auth.exceptions.RefreshError: If the token endpoint returned
            an error.
    )r$   r%   r&   N)r/   r   _handle_error_response)	r"   r#   r   r$   r%   r&   response_status_okr   r'   r-   r-   r.   _token_endpoint_requestj   s   r2   c           
   
      st   |t jd}t| |||dI dH }z|d }W n ty/ } ztjd|dd}||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.
        can_retry (bool): Enable or disable request retry behavior.

    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_typer&   Nr$   zNo access token in response.F	retryable)r   _JWT_GRANT_TYPEr2   KeyErrorr   RefreshError_parse_expiry)
r"   r#   r4   r&   r   r   r$   
caught_excnew_excexpiryr-   r-   r.   	jwt_grant   s    

r@   c              
      s   |t jd}t| |||dI dH }z|d }W n ty/ } ztjd|dd}||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.
        can_retry (bool): Enable or disable request retry behavior.

    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.
    r3   r6   Nid_tokenzNo ID token in response.Fr7   )verifyexp)
r   r9   r2   r:   r   r;   r   r   datetimeutcfromtimestamp)r"   r#   r4   r&   r   r   rA   r=   r>   payloadr?   r-   r-   r.   id_token_jwt_grant   s"   
rG   c           
         sR   t j|||d}|rd||d< |r||d< t| |||dI dH }	t |	|S )a0  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.
        can_retry (bool): Enable or disable request retry behavior.

    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
    )r5   	client_idclient_secretrefresh_token scoperaptr6   N)r   _REFRESH_GRANT_TYPEjoinr2   _handle_refresh_grant_response)
r"   r#   rJ   rH   rI   scopes
rapt_tokenr&   r   r   r-   r-   r.   refresh_grant   s   *rS   )NFT)T)NNT)__doc__rD   http.clientr   r   r   r   google.authr   r   r   google.oauth2r   r/   r2   r@   rG   rS   r-   r-   r-   r.   <module>   s&   
F

%
*4