o
    h                     @   s   d 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 ddlmZmZ d	d
 ZG dd deZG dd dZG dd d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
    )	parse_qslurlparse)HttpResponseRedirect)	urlencode)gettext)get_adapter)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/html/pos/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 r[i }| jr|| j t| j| j|d< | jd t| }t| j	| j
d}t  j||d}|jdvrGttdt| j|jf 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      zKInvalid response while obtaining request token from "%s". Response was: %s.oauth_%s_request_token)r   r   updater	   r   r   r   r   r   r   r   r   get_requests_sessionpoststatus_coder   _r   textdictr   session)r!   
get_paramsrt_urloauthresponser   r   r   _get_request_tokenB   s0   

zOAuthClient._get_request_tokenc                 C   s   | j du rZ|  }t| j| j|d |d d}| j}t| jd}|r,|d td|i }t	 
 j||d}|jdvrFt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   r0   r   r   r2   r   r1   r3   )r!   r   r6   at_urlr>   r7   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   r3   r   r   KeyErrorr   r0   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@   rB   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.
        r9   )r9   r#   r$   )r8   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"   r8   rB   r@   rJ   rN   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   rP   r   r   r   r   r"      s   
zOAuth.__init__c                 C   rC   )zT
        Get the saved access token for private resources from the session.
        r?   zNo access token saved for "%s".rD   rF   r   r   r   _get_at_from_session   s   zOAuth._get_at_from_sessionGETNc                 C   sh   |   }t| j| j|d |d d}tt  | ||||d}|jdkr2t	t
dt| j |S )zn
        Request a API endpoint at ``url`` with ``params`` being either the
        POST or GET data.
        r9   r:   r;   )r'   headersrM   r)   z'No access to private resources at "%s".)rQ   r   r   rP   getattrr   r-   lowerr/   r   r0   r   r   )r!   r   methodrM   rS   r    r6   r7   r   r   r   query   s"   
zOAuth.query)rR   NN)r   r   r   __doc__r"   rQ   rW   r   r   r   r   rO      s
    rO   N)rX   urllib.parser   r   django.httpr   django.utils.httpr   django.utils.translationr   r0   allauth.socialaccount.adapterr   1allauth.socialaccount.providers.oauth.oauth1_authr   allauth.utilsr	   r
   r   	Exceptionr   r   rO   r   r   r   r   <module>   s     