a
    1_                     @   s   d Z ddlZddlmZ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
 ZG dd deZG dd deZG dd deZdS )z
Parts derived from socialregistration and authorized by: alen, pinda
Inspired by:
    http://github.com/leah/python-oauth/blob/master/oauth/example/client.py
    http://github.com/facebook/tornado/blob/master/tornado/auth.py
    N)	parse_qslurlparse)HttpResponseRedirect)	urlencode)gettext)OAuth1)build_absolute_uriget_request_paramc                 C   s
   t | jS )a  
    Returns a prefix for the token to store in the session so we can hold
    more than one single oauth provider's access key in the session.

    Example:

        The request token url ``http://twitter.com/oauth/request_token``
        returns ``twitter.com``

    )r   netloc)url r   d/var/www/html/Ranjet/env/lib/python3.9/site-packages/allauth/socialaccount/providers/oauth/client.pyget_token_prefix   s    r   c                   @   s   e Zd ZdS )
OAuthErrorN)__name__
__module____qualname__r   r   r   r   r   "   s   r   c                   @   s>   e Zd ZdddZdd Zdd Zdd	 Zd
d Zdd ZdS )OAuthClientNc	           	      C   sF   || _ || _|| _|| _|| _|| _|| _|| _g | _d | _	d | _
d S N)requestrequest_token_urlaccess_token_urlconsumer_keyconsumer_secret
parameterscallback_urlprovidererrorsrequest_tokenaccess_token)	selfr   r   r   r   r   r   r   r   r   r   r   __init__'   s    zOAuthClient.__init__c                 C   s   | j du ri }| jr || j t| j| j|d< | jd t| }t| j	| j
d}tj||d}|jdvrttdt| j tt|j| _ | j | jjdt| j < | j S )	z
        Obtain a temporary request token to authorize an access token and to
        sign the request to obtain the access token
        Noauth_callback?)client_secretr   auth      z9Invalid response while obtaining request token from "%s".oauth_%s_request_token)r   r   updater   r   r   r   r   r   r   r   requestspoststatus_coder   _r   dictr   textsession)r    
get_paramsZrt_urloauthresponser   r   r   _get_request_tokenD   s,    

zOAuthClient._get_request_tokenc                 C   s   | j du r|  }t| j| j|d |d d}| j}t| jd}|rX|d td|i }t	j
||d}|jdvrttd	t| j tt|j| _ | j | jjd
t| j < | j S )zb
        Obtain the access token to access private resources at the API
        endpoint.
        Noauth_tokenoauth_token_secretr$   Zresource_owner_keyZresource_owner_secretoauth_verifierr#   r%   r'   z8Invalid response while obtaining access token from "%s".oauth_%s_access_token)r   _get_rt_from_sessionr   r   r   r   r	   r   r   r,   r-   r.   r   r/   r   r   r0   r   r1   r2   )r    r   r4   Zat_urlr:   r5   r   r   r   get_access_token^   s2    

zOAuthClient.get_access_tokenc                 C   sF   z| j jdt| j  W S  ty@   ttdt| j Y n0 dS )zc
        Returns the request token cached in the session by
        ``_get_request_token``
        r*   z No request token saved for "%s".Nr   r2   r   r   KeyErrorr   r/   r    r   r   r   r<      s    z OAuthClient._get_rt_from_sessionc              
   C   sR   z|    |   W n8 tyL } z | j|jd  W Y d }~dS d }~0 0 dS )Nr   FT)r<   r=   r   r   appendargs)r    er   r   r   is_valid   s    zOAuthClient.is_validc                 C   sB   |   }|d | j| jd}|| |d t| }t|S )z
        Returns a ``HttpResponseRedirect`` object to redirect the user
        to the URL the OAuth provider handles authorization.
        r7   )r7   r"   r#   )r6   r   r   r   r+   r   r   )r    Zauthorization_urlZextra_paramsr   paramsr   r   r   r   get_redirect   s    
zOAuthClient.get_redirect)NN)	r   r   r   r!   r6   r=   r<   rD   rF   r   r   r   r   r   &   s   	  
!	r   c                   @   s*   e Zd ZdZdd Zdd Zd
dd	ZdS )OAuthz
    Base class to perform oauth signed requests from access keys saved
    in a user's session. See the ``OAuthTwitter`` class below for an
    example.
    c                 C   s   || _ || _|| _|| _d S r   )r   r   
secret_keyr   )r    r   r   rH   r   r   r   r   r!      s    zOAuth.__init__c                 C   sF   z| j jdt| j  W S  ty@   ttdt| j Y n0 dS )zT
        Get the saved access token for private resources from the session.
        r;   zNo access token saved for "%s".Nr>   r@   r   r   r   _get_at_from_session   s    zOAuth._get_at_from_sessionGETNc                 C   sd   |   }t| j| j|d |d d}tt| ||||d}|jdkr^tt	dt
| j |jS )zn
        Request a API endpoint at ``url`` with ``params`` being either the
        POST or GET data.
        r7   r8   r9   )r&   headersrE   r(   z'No access to private resources at "%s".)rI   r   r   rH   getattrr,   lowerr.   r   r/   r   r   r1   )r    r   methodrE   rK   r   r4   r5   r   r   r   query   s"    
zOAuth.query)rJ   NN)r   r   r   __doc__r!   rI   rO   r   r   r   r   rG      s   rG   )rP   r,   urllib.parser   r   Zdjango.httpr   Zdjango.utils.httpr   Zdjango.utils.translationr   r/   Zrequests_oauthlibr   Zallauth.utilsr   r	   r   	Exceptionr   objectr   rG   r   r   r   r   <module>   s    