o
    "4g                     @   sN   d dl Z d dlmZ d dlmZ d dlmZ G dd deZG dd dZ	dS )	    N)	parse_qsl)	urlencode)get_adapterc                   @   s   e Zd ZdS )OAuth2ErrorN)__name__
__module____qualname__ r	   r	   v/var/www/html/authentication-server/venv/lib/python3.10/site-packages/allauth/socialaccount/providers/oauth2/client.pyr   	   s    r   c                   @   s:   e Zd ZdZ			dddZdd Zdd	d
Zdd ZdS )OAuth2Client	client_id NFc
           
      C   s@   || _ || _|| _|| _|| _|| _|| _d | _|| _|	| _	d S N)
requestaccess_token_methodaccess_token_urlcallback_urlconsumer_keyconsumer_secretscope_delimiterstateheaders
basic_auth)
selfr   r   r   r   r   r   r   r   r   r	   r	   r
   __init__   s   
zOAuth2Client.__init__c                 C   sT   | j t|}| j| jd| jd|ddi}| jr| j|d< || d|t|f S )Nredirect_uriscoperesponse_typecoder   z%s?%s)	r   joinsetclient_id_parameterr   r   r   updater   )r   authorization_urlr   extra_paramsparamsr	   r	   r
   get_redirect_url'   s   

zOAuth2Client.get_redirect_urlc           	      C   s  | j d|d}| jrtj| j| j}nd }|| j| jd| ji d }| 	| | j
}| jdkr5|}d }|r=|r=||d< t  j| j|||| j|d}d }|jdv ru|jd d	d
 dksi|jd d dkrn| }ntt|j}|r{d|vrtd|j |S )Nauthorization_code)r   
grant_typer   client_secretGETcode_verifier)r%   datar   auth)      zcontent-type;r   zapplication/json   z{"access_tokenz!Error retrieving access token: %s)r   r   requestsr-   HTTPBasicAuthr   r   r"   r!   _strip_empty_keysr   r   r   get_requests_sessionr   r   status_codesplittextjsondictr   r   content)	r   r   pkce_code_verifierr,   r-   r%   urlrespr2   r	   r	   r
   get_access_token4   sP   



zOAuth2Client.get_access_tokenc                 C   s&   dd |  D }|D ]}||= qdS )zlAdded because the Dropbox OAuth2 flow doesn't
        work when scope is passed in, which is empty.
        c                 S   s   g | ]
\}}|d kr|qS ) r	   ).0kvr	   r	   r
   
<listcomp>l   s    z2OAuth2Client._strip_empty_keys.<locals>.<listcomp>N)items)r   r%   keyskeyr	   r	   r
   r5   h   s   zOAuth2Client._strip_empty_keys)r   NFr   )r   r   r   r!   r   r&   r@   r5   r	   r	   r	   r
   r      s    


4r   )
r3   urllib.parser   django.utils.httpr   allauth.socialaccount.adapterr   	Exceptionr   r   r	   r	   r	   r
   <module>   s    