U
    6³da  ã                   @   sV   d Z ddlZddlZddlmZ ddlZddlZedƒZ	dd„ Z
dd„ Zdd	d
„ZdS )a  Integration helpers.

This module provides helpers for integrating with `requests-oauthlib`_.
Typically, you'll want to use the higher-level helpers in
:mod:`google_auth_oauthlib.flow`.

.. _requests-oauthlib: http://requests-oauthlib.readthedocs.io/en/latest/
é    N)Ú external_account_authorized_user)Zauth_uriÚ	token_uriÚ	client_idc                 K   sd   d| kr| d }nd| kr$| d }nt dƒ‚t | ¡ ¡sBt dƒ‚tjf |d |dœ|—Ž}|| fS )aK  Creates a :class:`requests_oauthlib.OAuth2Session` from client
    configuration loaded from a Google-format client secrets file.

    Args:
        client_config (Mapping[str, Any]): The client
            configuration in the Google `client secrets`_ format.
        scopes (Sequence[str]): The list of scopes to request during the
            flow.
        kwargs: Any additional parameters passed to
            :class:`requests_oauthlib.OAuth2Session`

    Raises:
        ValueError: If the client configuration is not in the correct
            format.

    Returns:
        Tuple[requests_oauthlib.OAuth2Session, Mapping[str, Any]]: The new
            oauthlib session and the validated client configuration.

    .. _client secrets:
        https://github.com/googleapis/google-api-python-client/blob/main/docs/client-secrets.md
    ZwebÚ	installedz2Client secrets must be for a web or installed app.z,Client secrets is not in the correct format.r   )r   Úscope)Ú
ValueErrorÚ_REQUIRED_CONFIG_KEYSÚissubsetÚkeysÚrequests_oauthlibZOAuth2Session)Úclient_configÚscopesÚkwargsÚconfigÚsession© r   ú@/tmp/pip-unpacked-wheel-8thlrh5s/google_auth_oauthlib/helpers.pyÚsession_from_client_config"   s    

 ÿÿr   c              	   K   s.   t | dƒ}t |¡}W 5 Q R X t||f|ŽS )a±  Creates a :class:`requests_oauthlib.OAuth2Session` instance from a
    Google-format client secrets file.

    Args:
        client_secrets_file (str): The path to the `client secrets`_ .json
            file.
        scopes (Sequence[str]): The list of scopes to request during the
            flow.
        kwargs: Any additional parameters passed to
            :class:`requests_oauthlib.OAuth2Session`

    Returns:
        Tuple[requests_oauthlib.OAuth2Session, Mapping[str, Any]]: The new
            oauthlib session and the validated client configuration.

    .. _client secrets:
        https://github.com/googleapis/google-api-python-client/blob/main/docs/client-secrets.md
    Úr)ÚopenÚjsonÚloadr   )Zclient_secrets_filer   r   Z	json_filer   r   r   r   Ú session_from_client_secrets_fileK   s    r   c                 C   sÐ   |dk	r|ni }| j stdƒ‚d|krhtj| j d | j  d¡| d¡| d¡| d¡| d	¡| jd
}nPtjjj| j d | j  d¡| j  d¡| d¡| d¡| d¡| j| j  d¡d}t	j	 
| j d ¡|_|S )an  Creates :class:`google.oauth2.credentials.Credentials` from a
    :class:`requests_oauthlib.OAuth2Session`.

    :meth:`fetch_token` must be called on the session before before calling
    this. This uses the session's auth token and the provided client
    configuration to create :class:`google.oauth2.credentials.Credentials`.
    This allows you to use the credentials from the session with Google
    API client libraries.

    Args:
        session (requests_oauthlib.OAuth2Session): The OAuth 2.0 session.
        client_config (Mapping[str, Any]): The subset of the client
            configuration to use. For example, if you have a web client
            you would pass in `client_config['web']`.

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

    Raises:
        ValueError: If there is no access token in the session.
    NzDThere is no access token for this session, did you call fetch_token?Z3piZaccess_tokenÚrefresh_tokenr   r   Úclient_secretÚtoken_info_url)Útokenr   Z	token_urlr   r   r   r   Úid_tokenr   )r   r   r   r   r   r   Zgranted_scopesZ
expires_at)r   r   r   ZCredentialsÚgetr   ÚgoogleZoauth2ÚcredentialsÚdatetimeÚutcfromtimestampZexpiry)r   r   r    r   r   r   Úcredentials_from_sessiond   s6    ÿ
ù



ø
r#   )N)Ú__doc__r!   r   Zgoogle.authr   Zgoogle.oauth2.credentialsr   r   Ú	frozensetr   r   r   r#   r   r   r   r   Ú<module>   s   	)