a
    |f7                     @   s  d Z ddlZddlZddlmZ ddlmZmZmZm	Z	m
Z
mZmZmZ ddlmZ zddlmZ dZW n ey~   dZY n0 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m Z  ddgZ!ddgZ"dej#dkfe$e%edddZ&e& Z'e'd Z(e'd Z)e'd Z*edfee e
e edddZ+G dd deZ,d7e	e$ef ee$ e-eddd Z.e)fee
e d!d"d#Z/e)d$feee$dd%d&d'Z0e!e.e(e)efee$ e,ee$ef ee$ef e ed(d)d*Z1dde!e.efe
e	e$ef  e
e	e$ef  ee$ e,e eeee$ef f d+d,d-Z2e*e!efeee$f ee$ e ed.d/d0Z3e!efe	e$ef ee$ e ed1d2d3Z4efe$e ed4d5d6Z5dS )8z?
gspread.auth
~~~~~~~~~~~~

Simple authentication with OAuth.

    N)Path)AnyDictIterableMappingOptionalProtocolTupleUnion)CredentialsTF)InstalledAppFlow)Session   Client)
HTTPClientHTTPClientTypez,https://www.googleapis.com/auth/spreadsheetsz%https://www.googleapis.com/auth/drivez5https://www.googleapis.com/auth/spreadsheets.readonlyz.https://www.googleapis.com/auth/drive.readonlyZgspreadnt)config_dir_nameos_is_windowsreturnc                 C   s(   |rt tjd | S t t  d| S dS )zConstruct a config dir path.

    By default:
        * `%APPDATA%\gspread` on Windows
        * `~/.config/gspread` everywhere else

    APPDATAz.configN)r   osenvironhome)r   r    r   FD:\Projects\storyit_web\backend\venv\Lib\site-packages\gspread/auth.pyget_config_dir)   s    
r   zcredentials.jsonzauthorized_user.jsonzservice_account.json)credentialshttp_clientsessionr   c                 C   s   t | ||dS )ad  Login to Google API using OAuth2 credentials.
    This is a shortcut/helper function which
    instantiates a client using `http_client`.
    By default :class:`gspread.HTTPClient` is used (but could also use
    :class:`gspread.BackOffHTTPClient` to avoid rate limiting).

    It can take an additional `requests.Session` object in order to provide
    you own session object.

    .. note::

       When providing your own `requests.Session` object,
       use the value `None` as `credentials`.

    :returns: An instance of the class produced by `http_client`.
    :rtype: :class:`gspread.client.Client`
    )authr    r   r   )r   r   r    r   r   r   	authorize@   s    r"   c                   @   s2   e Zd ZdZdeeef ee ee	dddZ
dS )FlowCallablez"Protocol for OAuth flow callables.r   client_configscopesportr   c                 C   s   d S Nr   )selfr%   r&   r'   r   r   r   __call__]   s    zFlowCallable.__call__N)r   )__name__
__module____qualname____doc__r   strr   r   intr   r*   r   r   r   r   r#   Z   s    r#   r$   c                 C   s   t | |}|j|dS )a  Run an OAuth flow using a local server strategy.

    Creates an OAuth flow and runs `google_auth_oauthlib.flow.InstalledAppFlow.run_local_server <https://google-auth-oauthlib.readthedocs.io/en/latest/reference/google_auth_oauthlib.flow.html#google_auth_oauthlib.flow.InstalledAppFlow.run_local_server>`_.
    This will start a local web server and open the authorization URL in
    the user's browser.

    Pass this function to ``flow`` parameter of :meth:`~gspread.oauth` to run
    a local server flow.
    )r'   )r   Zfrom_client_configZrun_local_server)r%   r&   r'   flowr   r   r   local_server_flowb   s    r2   )filenamer   c                 C   s   |   rt| S d S r(   )existsOAuthCredentialsZfrom_authorized_user_filer3   r   r   r   load_credentialsr   s    
r7   token)credsr3   stripr   c                 C   sN   |j jddd |d }|| | W d    n1 s@0    Y  d S )NT)parentsexist_okw)parentmkdiropenwriteto_json)r9   r3   r:   fr   r   r   store_credentials{   s    rD   )r&   r1   credentials_filenameauthorized_user_filenamer   r   c                 C   sr   t |}t|d}t|tsft|}t|}W d   n1 sD0    Y  ||| d}t||d t||dS )a9
  Authenticate with OAuth Client ID.

    By default this function will use the local server strategy and open
    the authorization URL in the user's browser::

        gc = gspread.oauth()

    Another option is to run a console strategy. This way, the user is
    instructed to open the authorization URL in their browser. Once the
    authorization is complete, the user must then copy & paste the
    authorization code into the application::

        gc = gspread.oauth(flow=gspread.auth.console_flow)


    ``scopes`` parameter defaults to read/write scope available in
    ``gspread.auth.DEFAULT_SCOPES``. It's read/write for Sheets
    and Drive API::

        DEFAULT_SCOPES =[
            'https://www.googleapis.com/auth/spreadsheets',
            'https://www.googleapis.com/auth/drive'
        ]

    You can also use ``gspread.auth.READONLY_SCOPES`` for read only access.
    Obviously any method of ``gspread`` that updates a spreadsheet
    **will not work** in this case::

        gc = gspread.oauth(scopes=gspread.auth.READONLY_SCOPES)

        sh = gc.open("A spreadsheet")
        sh.sheet1.update_acell('A1', '42')   # <-- this will not work

    If you're storing your user credentials in a place other than the
    default, you may provide a path to that file like so::

        gc = gspread.oauth(
            credentials_filename='/alternative/path/credentials.json',
            authorized_user_filename='/alternative/path/authorized_user.json',
        )

    :param list scopes: The scopes used to obtain authorization.
    :param function flow: OAuth flow to use for authentication.
        Defaults to :meth:`~gspread.auth.local_server_flow`
    :param str credentials_filename: Filepath (including name) pointing to a
        credentials `.json` file.
        Defaults to DEFAULT_CREDENTIALS_FILENAME:

            * `%APPDATA%\gspread\credentials.json` on Windows
            * `~/.config/gspread/credentials.json` everywhere else
    :param str authorized_user_filename: Filepath (including name) pointing to
        an authorized user `.json` file.
        Defaults to DEFAULT_AUTHORIZED_USER_FILENAME:

            * `%APPDATA%\gspread\authorized_user.json` on Windows
            * `~/.config/gspread/authorized_user.json` everywhere else
    :type http_client: :class:`gspread.http_client.HTTPClient`
    :param http_client: A factory function that returns a client class.
        Defaults to :class:`gspread.http_client.HTTPClient` (but could also use
        :class:`gspread.http_client.BackOffHTTPClient` to avoid rate limiting)

    :rtype: :class:`gspread.client.Client`
    r6   Nr%   r&   r!   r   )	r   r7   
isinstancer   r@   jsonloadrD   r   )r&   r1   rE   rF   r   r9   Z	json_filer%   r   r   r   oauth   s    G


(rL   )r   authorized_user_infor&   r1   r   r   c                 C   sJ   d}|durt ||}|s0| dur0|| |d}t||d}||dfS )ai	  Authenticate with OAuth Client ID.

    By default this function will use the local server strategy and open
    the authorization URL in the user's browser::

        gc = gspread.oauth_from_dict()

    Another option is to run a console strategy. This way, the user is
    instructed to open the authorization URL in their browser. Once the
    authorization is complete, the user must then copy & paste the
    authorization code into the application::

        gc = gspread.oauth_from_dict(flow=gspread.auth.console_flow)


    ``scopes`` parameter defaults to read/write scope available in
    ``gspread.auth.DEFAULT_SCOPES``. It's read/write for Sheets
    and Drive API::

        DEFAULT_SCOPES =[
            'https://www.googleapis.com/auth/spreadsheets',
            'https://www.googleapis.com/auth/drive'
        ]

    You can also use ``gspread.auth.READONLY_SCOPES`` for read only access.
    Obviously any method of ``gspread`` that updates a spreadsheet
    **will not work** in this case::

        gc = gspread.oauth_from_dict(scopes=gspread.auth.READONLY_SCOPES)

        sh = gc.open("A spreadsheet")
        sh.sheet1.update_acell('A1', '42')   # <-- this will not work

    This function requires you to pass the credentials directly as
    a python dict. After the first authentication the function returns
    the authenticated user info, this can be passed again to authenticate
    the user without the need to run the flow again.

    ..
        code block below must be explicitly announced using code-block

    .. code-block:: python

        gc = gspread.oauth_from_dict(
                credentials=my_creds,
                authorized_user_info=my_auth_user
        )

    :param dict credentials: The credentials from google cloud platform
    :param dict authorized_user_info: The authenticated user
        if already authenticated.
    :param list scopes: The scopes used to obtain authorization.
    :param function flow: OAuth flow to use for authentication.
        Defaults to :meth:`~gspread.auth.local_server_flow`
    :type http_client: :class:`gspread.http_client.HTTPClient`
    :param http_client: A factory function that returns a client class.
        Defaults to :class:`gspread.http_client.HTTPClient` (but could also use
        :class:`gspread.http_client.BackOffHTTPClient` to avoid rate limiting)

    :rtype: (:class:`gspread.client.Client`, str)
    NrG   rH   r8   )r5   Zfrom_authorized_user_infor   rB   )r   rM   r&   r1   r   r9   clientr   r   r   oauth_from_dict   s    ErO   )r3   r&   r   r   c                 C   s   t j| |d}t||dS )a  Authenticate using a service account.

    ``scopes`` parameter defaults to read/write scope available in
    ``gspread.auth.DEFAULT_SCOPES``. It's read/write for Sheets
    and Drive API::

        DEFAULT_SCOPES =[
            'https://www.googleapis.com/auth/spreadsheets',
            'https://www.googleapis.com/auth/drive'
        ]

    You can also use ``gspread.auth.READONLY_SCOPES`` for read only access.
    Obviously any method of ``gspread`` that updates a spreadsheet
    **will not work** in this case.

    :param str filename: The path to the service account json file.
    :param list scopes: The scopes used to obtain authorization.
    :type http_client: :class:`gspread.http_client.HTTPClient`
    :param http_client: A factory function that returns a client class.
        Defaults to :class:`gspread.HTTPClient` (but could also use
        :class:`gspread.BackOffHTTPClient` to avoid rate limiting)

    :rtype: :class:`gspread.client.Client`
    )r&   rH   )SACredentialsZfrom_service_account_filer   )r3   r&   r   r9   r   r   r   service_account,  s    rQ   )infor&   r   r   c                 C   s   t j| |d}t||dS )a  Authenticate using a service account (json).

    ``scopes`` parameter defaults to read/write scope available in
    ``gspread.auth.DEFAULT_SCOPES``. It's read/write for Sheets
    and Drive API::

        DEFAULT_SCOPES =[
            'https://www.googleapis.com/auth/spreadsheets',
            'https://www.googleapis.com/auth/drive'
        ]

    You can also use ``gspread.auth.READONLY_SCOPES`` for read only access.
    Obviously any method of ``gspread`` that updates a spreadsheet
    **will not work** in this case.

    :param info (Mapping[str, str]): The service account info in Google format
    :param list scopes: The scopes used to obtain authorization.
    :type http_client: :class:`gspread.http_client.HTTPClient`
    :param http_client: A factory function that returns a client class.
        Defaults to :class:`gspread.http_client.HTTPClient` (but could also use
        :class:`gspread.http_client.BackOffHTTPClient` to avoid rate limiting)

    :rtype: :class:`gspread.client.Client`
    )rR   r&   rH   )rP   Zfrom_service_account_infor   )rR   r&   r   r9   r   r   r   service_account_from_dictM  s
    rS   )r8   r   r   c                 C   s$   t du rtdt| }t||dS )ah  Authenticate using an API key.

    Allows you to open public spreadsheet files.

    .. warning::

       This method only allows you to open public spreadsheet files.
       It does not work for private spreadsheet files.

    :param token str: The actual API key to use
    :type http_client: :class:`gspread.http_client.HTTPClient`
    :param http_client: A factory function that returns a client class.
        Defaults to :class:`gspread.http_client.HTTPClient` (but could also use
        :class:`gspread.http_client.BackOffHTTPClient` to avoid rate limiting)

    :rtype: :class:`gspread.client.Client`

    Fzkapi_key is only available with package google.auth>=2.4.0.Install it with "pip install google-auth>=2.4.0".rH   )GOOGLE_AUTH_API_KEY_AVAILABLENotImplementedErrorAPIKeyCredentialsr   )r8   r   r9   r   r   r   api_keyq  s    rW   )r   )6r.   rJ   r   pathlibr   typingr   r   r   r   r   r   r	   r
   Zgoogle.auth.credentialsr   Zgoogle.auth.api_keyrV   rT   ImportErrorZgoogle.oauth2.credentialsr5   Zgoogle.oauth2.service_accountrP   Zgoogle_auth_oauthlib.flowr   requestsr   rN   r   r   r   r   ZDEFAULT_SCOPESZREADONLY_SCOPESnamer/   boolr   ZDEFAULT_CONFIG_DIRZDEFAULT_CREDENTIALS_FILENAMEZ DEFAULT_AUTHORIZED_USER_FILENAMEZ DEFAULT_SERVICE_ACCOUNT_FILENAMEr"   r#   r0   r2   r7   rD   rL   rO   rQ   rS   rW   r   r   r   r   <module>   s   (

	 

TU
#
$