a
    !f                     @   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lm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ZdZdddZdd Zdd Zdd ZdS )a2  This module contains the views used by the OAuth2 flows.

Their are two views used by the OAuth2 flow, the authorize and the callback
view. The authorize view kicks off the three-legged OAuth flow, and the
callback view validates the flow and if successful stores the credentials
in the configured storage.    N)http)	shortcuts)settings)urlresolvers)redirect)html)parse)client)django_util)get_storage)signalsZgoogle_oauth2_csrf_tokenzgoogle_oauth2_flow_{0}c              
   C   st   t td }|| jt< t||d}t	j
tjjtjj||| tdd}t|}t|| j|< |S )aM  Creates a Web Server Flow

    Args:
        request: A Django request object.
        scopes: the request oauth2 scopes.
        return_url: The URL to return to after the flow is complete. Defaults
            to the path of the current request.

    Returns:
        An OAuth2 flow object that has been stored in the session.
    i   )
csrf_token
return_urlzgoogle_oauth:callback)	client_idclient_secretscopestateZredirect_uri)hashlibsha256osurandom	hexdigestsession	_CSRF_KEYjsondumpsr	   ZOAuth2WebServerFlowr
   oauth2_settingsr   r   Zbuild_absolute_urir   reverse	_FLOW_KEYformat
jsonpickleencode)requestscopesr   r   r   flowZflow_key r%   g/var/www/html/python-backend/venv/lib/python3.9/site-packages/oauth2client/contrib/django_util/views.py
_make_flow,   s$    

r'   c                 C   s*   |j t| d}|du r dS t|S )a   Looks up the flow in session to recover information about requested
    scopes.

    Args:
        csrf_token: The token passed in the callback request that should
            match the one previously generated and stored in the request on the
            initial authorization view.

    Returns:
        The OAuth2 Flow object associated with this flow based on the
        CSRF token.
    N)r   getr   r   r    decode)r   r"   Zflow_pickler%   r%   r&   _get_flow_for_tokenO   s    r*   c              
   C   s~  d| j v r<| j d| j dd}t|}td|S z| j d }| j d }W n typ   td Y S 0 z| jt	 }W n ty   td Y S 0 zt
|}|d	 }|d
 }W n  ttfy   td Y S 0 ||krtdS t|| }|s
tdS z||}	W n8 tjyP }
 ztd|
W  Y d}
~
S d}
~
0 0 t| |	 tjjtj| |	d t|S )a   View that handles the user's return from OAuth2 provider.

    This view verifies the CSRF state and OAuth authorization code, and on
    success stores the credentials obtained in the storage provider,
    and redirects to the return_url specified in the authorize view and
    stored in the session.

    Args:
        request: Django request.

    Returns:
         A redirect response back to the return_url.
    errorZerror_description zAuthorization failed {0}r   codez+Request missing state or authorization codez"No existing session for this flow.r   r   zInvalid state parameter.zInvalid CSRF token.zMissing Oauth2 flow.zAn error has occurred: {0}N)Zsenderr"   credentials)GETr(   r   escaper   ZHttpResponseBadRequestr   KeyErrorr   r   r   loads
ValueErrorr*   Zstep2_exchanger	   ZFlowExchangeErrorr   putr   Zoauth2_authorizedsendr   r   )r"   reasonZencoded_stater-   Zserver_csrfr   Zclient_csrfr   r$   r.   Zexchange_errorr%   r%   r&   oauth2_callback`   sV    









r7   c                 C   s   | j dd}|s | jdd}| j dtjj}tjjr~| j	 s`t
dtjt|  S t| ||}| r~t
|S t| ||d}| }t
|S )a   View to start the OAuth2 Authorization flow.

     This view starts the OAuth2 authorization flow. If scopes is passed in
     as a  GET URL parameter, it will authorize those scopes, otherwise the
     default scopes specified in settings. The return_url can also be
     specified as a GET parameter, otherwise the referer header will be
     checked, and if that isn't found it will return to the root path.

    Args:
       request: The Django request object.

    Returns:
         A redirect to Google OAuth2 Authorization.
    r   NZHTTP_REFERER/r#   z{0}?next={1})r"   r#   r   )r/   r(   ZMETAgetlistr
   r   r#   Zstorage_modeluseris_authenticatedr   r   r   Z	LOGIN_URLr   quoteZget_full_pathZ
UserOAuth2Zhas_credentialsr'   Zstep1_get_authorize_urlr   )r"   r   r#   Z
user_oauthr$   Zauth_urlr%   r%   r&   oauth2_authorize   s    
r=   )N)__doc__r   r   r   djangor   r   Zdjango.confr   Zdjango.corer   Zdjango.shortcutsr   Zdjango.utilsr   r    Zsix.moves.urllibr   Zoauth2clientr	   Zoauth2client.contribr
   Z oauth2client.contrib.django_utilr   r   r   r   r'   r*   r7   r=   r%   r%   r%   r&   <module>   s*   
#?