o
    Df                     @   s   d dl mZ d dlmZ d dlmZ d dlmZmZ ddl	m
Z
mZmZmZ ddlmZ d	d
gZG dd
 d
eeZG dd deeZG dd	 d	eeZdS )    )Session)AuthBase)OAuth2Client)
ClientAuth	TokenAuth   )
OAuthErrorInvalidTokenErrorMissingTokenErrorUnsupportedTokenTypeError   )update_session_configureOAuth2Session
OAuth2Authc                   @   s    e Zd ZdZdd Zdd ZdS )r   zFSign requests for OAuth 2.0, currently only bearer token is supported.c                 C   s"   | j r| j | jst d S d S N)clientensure_active_tokentokenr	   )self r   l/home/ubuntu/webapp/venv/lib/python3.10/site-packages/authlib/integrations/requests_client/oauth2_session.pyr      s   zOAuth2Auth.ensure_active_tokenc              
   C   s`   |    z| |j|j|j\|_|_|_W |S  ty/ } zdt| }t|dd }~ww )NzUnsupported token_type: )description)r   prepareurlheadersbodyKeyErrorstrr   )r   reqerrorr   r   r   r   __call__   s   
zOAuth2Auth.__call__N)__name__
__module____qualname____doc__r   r    r   r   r   r   r      s    c                   @   s   e Zd ZdZdd ZdS )OAuth2ClientAuthzFAttaches OAuth Client Authentication to the given Request object.
    c                 C   s(   |  |j|j|j|j\|_|_|_|S r   )r   methodr   r   r   )r   r   r   r   r   r    %   s   zOAuth2ClientAuth.__call__N)r!   r"   r#   r$   r    r   r   r   r   r%   "   s    r%   c                       sR   e Zd ZdZeZeZeZ	dZ
						dddZdddZd fd
d	Z  ZS )r   a  Construct a new OAuth 2 client requests session.

    :param client_id: Client ID, which you get from client registration.
    :param client_secret: Client Secret, which you get from registration.
    :param authorization_endpoint: URL of the authorization server's
        authorization endpoint.
    :param token_endpoint: URL of the authorization server's token endpoint.
    :param token_endpoint_auth_method: client authentication method for
        token endpoint.
    :param revocation_endpoint: URL of the authorization server's OAuth 2.0
        revocation endpoint.
    :param revocation_endpoint_auth_method: client authentication method for
        revocation endpoint.
    :param scope: Scope that you needed to access user resources.
    :param state: Shared secret to prevent CSRF attack.
    :param redirect_uri: Redirect URI you registered as callback.
    :param token: A dict of token attributes such as ``access_token``,
        ``token_type`` and ``expires_at``.
    :param token_placement: The place to put token in HTTP request. Available
        values: "header", "body", "uri".
    :param update_token: A function for you to update token. It accept a
        :class:`OAuth2Token` as parameter.
    :param default_timeout: If settled, every requests will have a default timeout.
    )
allow_redirectstimeoutcookiesfilesproxieshooksstreamverifycertjsonNheaderc                 K   sH   t |  || _t| | tj| f| |||||||||	|
d| d S )N)session	client_idclient_secrettoken_endpoint_auth_methodrevocation_endpoint_auth_methodscopestateredirect_urir   token_placementupdate_token)r   __init__default_timeoutr   r   )r   r3   r4   r5   r6   r7   r8   r9   r   r:   r;   r=   kwargsr   r   r   r<   M   s    


zOAuth2Session.__init__c                 K   s   | j |fi |S )zAlias for fetch_token.)fetch_token)r   r   r>   r   r   r   fetch_access_tokena   s   z OAuth2Session.fetch_access_tokenFc                    sL   | j r
|d| j  |s|du r| jst | j}t j||fd|i|S )z<Send request with auto refresh token feature (if available).r(   Nauth)r=   
setdefaultr   r
   
token_authsuperrequest)r   r&   r   withhold_tokenrA   r>   	__class__r   r   rE   e   s   zOAuth2Session.request)NNNNNNNNr1   NNr   )FN)r!   r"   r#   r$   r%   client_auth_classr   token_auth_classr   oauth_error_classSESSION_REQUEST_PARAMSr<   r@   rE   __classcell__r   r   rG   r   r   ,   s    

N)requestsr   requests.authr   authlib.oauth2.clientr   authlib.oauth2.authr   r   base_clientr   r	   r
   r   utilsr   __all__r   r%   r   r   r   r   r   <module>   s    
