o
    b                     @   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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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 rI| j rK| jd}| j }|jd}|r$|r$|j||d d S |jddkrF| jd }|j|dd}|jrD|j||d d S d S t d S d S )	Nrefresh_tokenZtoken_endpoint)r   
grant_typeZclient_credentialsaccess_token)r   )r   )	clienttoken
is_expiredgetmetadatar   fetch_tokenupdate_tokenr	   )selfr   r   urlr   r    r   r/var/www/secure340b-portal/env/lib/python3.10/site-packages/authlib/integrations/requests_client/oauth2_session.pyensure_active_token   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   preparer   headersbodyKeyErrorformat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 N)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                       sZ   e Zd ZdZeZeZdZ						dddZ	dddZ
d fd
d	Ze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 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.
    )
allow_redirectstimeoutcookiesfilesproxieshooksstreamverifycertjsonNheaderc
                 K   s6   t |  tj| f| |||||||||	d
|
 d S )N)
session	client_idclient_secrettoken_endpoint_auth_methodrevocation_endpoint_auth_methodscoperedirect_urir   token_placementr   )r   __init__r   )r   r:   r;   r<   r=   r>   r?   r   r@   r   kwargsr   r   r   rA   T   s   

zOAuth2Session.__init__c                 K   s   | j |fi |S )zAlias for fetch_token.)r   )r   r   rB   r   r   r   fetch_access_tokenf   s   z OAuth2Session.fetch_access_tokenFc                    s<   |s|du r| j st | j}tt| j||fd|i|S )z<Send request with auto refresh token feature (if available).Nauth)r   r
   Z
token_authsuperr   request)r   r-   r   Zwithhold_tokenrD   rB   	__class__r   r   rF   j   s   
zOAuth2Session.requestc                 C   s
   t | |r,   )r   )Z
error_typeZerror_descriptionr   r   r   handle_errors   s   
zOAuth2Session.handle_error)	NNNNNNNr8   Nr,   )FN)r'   r(   r)   r*   r+   Zclient_auth_classr   Ztoken_auth_classZSESSION_REQUEST_PARAMSrA   rC   rF   staticmethodrI   __classcell__r   r   rG   r   r   6   s     

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