o
    ˷e                     @   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   c/var/www/ideatree/venv/lib/python3.10/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                   @   sB   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 rUi }| jr|| j t| j| j|d< | jd t| }t| j	| j
d}tj||d}|jdvrAt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_paramsrt_urloauthresponser   r   r   _get_request_tokenD   s,   

zOAuthClient._get_request_tokenc                 C   s   | j du rW|  }t| j| j|d |d d}| j}t| jd}|r,|d td|i }t	j
||d}|jdvrCt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$   resource_owner_key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   r5   at_urlr=   r6   r   r   r   get_access_token^   s2   

zOAuthClient.get_access_tokenc                 C   >   z| j jdt| j  W S  ty   ttdt| j w )zc
        Returns the request token cached in the session by
        ``_get_request_token``
        r*   z No request token saved for "%s".r   r2   r   r   KeyErrorr   r/   r    r   r   r   r?      s   z OAuthClient._get_rt_from_sessionc              
   C   sP   z|    |   W dS  ty' } z| j|jd  W Y d }~dS d }~ww )Nr   FT)r?   rA   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.
        r8   )r8   r"   r#   )r7   r   r   r   r+   r   r   )r    authorization_urlextra_paramsr   paramsr   r   r   r   get_redirect   s   
zOAuthClient.get_redirect)NN)	r   r   r   r!   r7   rA   r?   rI   rM   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   rO   r   r   r   r   r!      s   
zOAuth.__init__c                 C   rB   )zT
        Get the saved access token for private resources from the session.
        r>   zNo access token saved for "%s".rC   rE   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.
        r8   r9   r:   )r&   headersrL   r(   z'No access to private resources at "%s".)rP   r   r   rO   getattrr,   lowerr.   r   r/   r   r   r1   )r    r   methodrL   rR   r   r5   r6   r   r   r   query   s"   
zOAuth.query)rQ   NN)r   r   r   __doc__r!   rP   rV   r   r   r   r   rN      s
    rN   )rW   r,   urllib.parser   r   django.httpr   django.utils.httpr   django.utils.translationr   r/   requests_oauthlibr   allauth.utilsr   r	   r   	Exceptionr   objectr   rN   r   r   r   r   <module>   s     