a
    !fm#                     @   s   d Z ddlm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 g d
ZdZdZdZdZdd Ze ZG dd dejZG dd dejZeddddZdd ZdS )zCommand-line tools for authenticating via OAuth 2.0

Do the OAuth 2.0 Web Server dance for a command line application. Stores the
generated credentials in a common file that is used by other example apps in
the same directory.
    )print_functionN)BaseHTTPServer)http_client)input)urllib)_helpers)client)	argparserrun_flowmessage_if_missingzWARNING: Please configure OAuth 2.0

To make this sample run you will need to populate the client_secrets.json file
found at:

   {file_path}

with information from the APIs Console <https://code.google.com/apis/console>.

a  
Failed to start a local webserver listening on either port 8080
or port 8090. Please check your firewall settings and locally
running programs that may be blocking or using those ports.

Falling back to --noauth_local_webserver and continuing with
authorization.
z
Your browser has been opened to visit:

    {address}

If your browser is on a different machine then exit and re-run this
application with the command-line parameter

  --noauth_local_webserver
z:
Go to the following link in your browser:

    {address}
c                  C   s   zdd l } W n ty    Y d S 0 | jdd}|jdddd |jdd	dd
d |jdddgtddd |jddg ddd |S )Nr   F)add_helpz--auth_host_name	localhostz)Hostname when running a local web server.)defaulthelpz--noauth_local_webserver
store_truezDo not run a local web server.)actionr   r   z--auth_host_porti  i  *z!Port web server should listen on.)r   typenargsr   z--logging_levelERROR)DEBUGINFOWARNINGr   CRITICALz Set the logging level of detail.)r   choicesr   )argparseImportErrorArgumentParseradd_argumentint)r   parser r!   S/var/www/html/python-backend/venv/lib/python3.9/site-packages/oauth2client/tools.py_CreateArgumentParserM   s(    r#   c                   @   s   e Zd ZdZi ZdS )ClientRedirectServerzA server to handle OAuth 2.0 redirects back to localhost.

    Waits for a single request and parses the query parameters
    into query_params and then stops serving.
    N)__name__
__module____qualname____doc__query_paramsr!   r!   r!   r"   r$   f   s   r$   c                   @   s    e Zd ZdZdd Zdd ZdS )ClientRedirectHandlerzA handler for OAuth 2.0 redirects back to localhost.

    Waits for a single request and parses the query parameters
    into the servers query_params and then stops serving.
    c                 C   sj   |  tj | dd |   tj| j}t	
|j}|| j_| jd | jd | jd dS )zHandle a GET request.

        Parses the query parameters and prints a message
        if the flow has completed. Note that we can't detect
        if an error occurred.
        zContent-typez	text/htmls7   <html><head><title>Authentication Status</title></head>s3   <body><p>The authentication flow has completed.</p>s   </body></html>N)Zsend_responser   OKZsend_headerZend_headersr   parseurlparsepathr   Zparse_unique_urlencodedqueryserverr)   wfilewrite)selfpartsr/   r!   r!   r"   do_GETv   s    zClientRedirectHandler.do_GETc                 G   s   dS )zADo not log messages to stdout while running as cmd. line program.Nr!   )r3   formatargsr!   r!   r"   log_message   s    z!ClientRedirectHandler.log_messageN)r%   r&   r'   r(   r5   r8   r!   r!   r!   r"   r*   o   s   r*      c              
   C   s  |du rt  }t tt|j |jsd}d}|jD ]:}|}zt	|j
|ft}W n tjyj   Y q:0 d} qvq:| |_|stt |jsdj|j
|d}ntj}|| _|  }	|jsddl}
|
j|	ddd ttj|	d	 nttj|	d	 d}|jsL|  d
|jv r td d|jv r8|jd }ntd td ntd }z| j||d}W n8 tj y } ztd| W Y d}~n
d}~0 0 |!| |"| td |S )a  Core code for a command-line application.

    The ``run()`` function is called from your application and runs
    through all the steps to obtain credentials. It takes a ``Flow``
    argument and attempts to open an authorization server page in the
    user's default web browser. The server asks the user to grant your
    application access to the user's data. If the user grants access,
    the ``run()`` function returns new credentials. The new credentials
    are also stored in the ``storage`` argument, which updates the file
    associated with the ``Storage`` object.

    It presumes it is run from a command-line application and supports the
    following flags:

        ``--auth_host_name`` (string, default: ``localhost``)
           Host name to use when running a local web server to handle
           redirects during OAuth authorization.

        ``--auth_host_port`` (integer, default: ``[8080, 8090]``)
           Port to use when running a local web server to handle redirects
           during OAuth authorization. Repeat this option to specify a list
           of values.

        ``--[no]auth_local_webserver`` (boolean, default: ``True``)
           Run a local web server to handle redirects during OAuth
           authorization.

    The tools module defines an ``ArgumentParser`` the already contains the
    flag definitions that ``run()`` requires. You can pass that
    ``ArgumentParser`` to your ``ArgumentParser`` constructor::

        parser = argparse.ArgumentParser(
            description=__doc__,
            formatter_class=argparse.RawDescriptionHelpFormatter,
            parents=[tools.argparser])
        flags = parser.parse_args(argv)

    Args:
        flow: Flow, an OAuth 2.0 Flow to step through.
        storage: Storage, a ``Storage`` to store the credential in.
        flags: ``argparse.Namespace``, (Optional) The command-line flags. This
               is the object returned from calling ``parse_args()`` on
               ``argparse.ArgumentParser`` as described above. Defaults
               to ``argparser.parse_args()``.
        http: An instance of ``httplib2.Http.request`` or something that
              acts like it.

    Returns:
        Credentials, the obtained credential.
    NFr   Tzhttp://{host}:{port}/)hostport   )newZ	autoraise)addresserrorz$Authentication request was rejected.codez>Failed to find "code" in the query parameters of the redirect.z*Try running with --noauth_local_webserver.zEnter verification code: )httpzAuthentication has failed: {0}zAuthentication successful.)#r	   
parse_argslogging	getLoggersetLevelgetattrZlogging_levelZnoauth_local_webserverZauth_host_portr$   Zauth_host_namer*   socketr?   print_FAILED_START_MESSAGEr6   r   ZOOB_CALLBACK_URNZredirect_uriZstep1_get_authorize_url
webbrowseropen_BROWSER_OPENED_MESSAGE_GO_TO_LINK_MESSAGEhandle_requestr)   sysexitr   stripZstep2_exchangeZFlowExchangeErrorputZ	set_store)ZflowZstorageflagsrA   successZport_numberr;   ZhttpdZoauth_callbackZauthorize_urlrJ   r@   Z
credentialer!   r!   r"   r
      sb    4


&

r
   c                 C   s   t j| dS )zAHelpful message to display if the CLIENT_SECRETS file is missing.)	file_path)_CLIENT_SECRETS_MESSAGEr6   )filenamer!   r!   r"   r      s    r   )NN)r(   
__future__r   rC   rG   rO   Z	six.movesr   r   r   r   Zoauth2clientr   r   __all__rW   rI   rL   rM   r#   r	   Z
HTTPServerr$   ZBaseHTTPRequestHandlerr*   
positionalr
   r   r!   r!   r!   r"   <module>   s,   		p