o
    Df|                     @   sF   d dl mZmZmZ d dlmZ ddlmZmZm	Z	 G dd dZ
dS )    )
url_decodeadd_params_to_uriurlparse)
json_loads   )SIGNATURE_HMAC_SHA1SIGNATURE_TYPE_HEADER
ClientAuthc                
   @   s   e Zd ZeZddddddeeddf
ddZedd Z	e	j
dd Z	edd	 Zej
d
d	 ZdddZdd ZdddZdd Zdd Zdd Zedd ZdS )OAuth1ClientNFc                 K   s>   |st d|| _| j||||||	|
||||d| _|| _d S )NzMissing "client_id")
client_secrettokentoken_secretredirect_urisignature_methodsignature_typersa_keyverifierrealmforce_include_body)
ValueErrorsession
auth_classauth_kwargs)selfr   	client_idr   r   r   r   r   r   r   r   r   r   kwargs r   N/home/ubuntu/webapp/venv/lib/python3.10/site-packages/authlib/oauth1/client.py__init__   s   
zOAuth1Client.__init__c                 C   s   | j jS Nr   r   r   r   r   r   r   (   s   zOAuth1Client.redirect_uric                 C   s   || j _d S r    r!   )r   urir   r   r   r   ,   s   c                 C   s   t | jj| jj| jjdS )N)oauth_tokenoauth_token_secretoauth_verifier)dictr   r   r   r   r"   r   r   r   r   0   s
   zOAuth1Client.tokenc                 C   s   |du rd| j _d| j _d| j _dS d|v r4|d | j _d|v r&|d | j _d|v r2|d | j _dS dS d|}| d| dS )zThis token setter is designed for an easy integration for
        OAuthClient. Make sure both OAuth1Session and OAuth2Session
        have token setters.
        Nr$   r%   r&   zoauth_token is missing: missing_token)r   r   r   r   handle_error)r   r   messager   r   r   r   8   s   
c                 K   s2   |p| j j|d< | j jr| j j|d< t|| S )a  Create an authorization URL by appending request_token and optional
        kwargs to url.

        This is the second step in the OAuth 1 workflow. The user should be
        redirected to this authorization URL, grant access to you, and then
        be redirected back to you. The redirection back can either be specified
        during client registration or by supplying a callback URI per request.

        :param url: The authorization endpoint URL.
        :param request_token: The previously obtained request token.
        :param kwargs: Optional parameters to append to the URL.
        :returns: The authorization URL with new parameters embedded.
        r$   oauth_callback)r   r   r   r   items)r   urlrequest_tokenr   r   r   r   create_authorization_urlL   s   z%OAuth1Client.create_authorization_urlc                 K   s   | j |fi |S )a  Method for fetching an access token from the token endpoint.

        This is the first step in the OAuth 1 workflow. A request token is
        obtained by making a signed post request to url. The token is then
        parsed from the application/x-www-form-urlencoded response and ready
        to be used to construct an authorization url.

        :param url: Request Token endpoint.
        :param kwargs: Extra parameters to include for fetching token.
        :return: A Request Token dict.
        )_fetch_token)r   r-   r   r   r   r   fetch_request_token_   s   z OAuth1Client.fetch_request_tokenc                 K   s2   |r|| j _| j js| dd | j|fi |S )a  Method for fetching an access token from the token endpoint.

        This is the final step in the OAuth 1 workflow. An access token is
        obtained using all previously obtained credentials, including the
        verifier from the authorization step.

        :param url: Access Token endpoint.
        :param verifier: A verifier string to prove authorization was granted.
        :param kwargs: Extra parameters to include for fetching access token.
        :return: A token dict.
        missing_verifierzMissing "verifier" value)r   r   r)   r0   )r   r-   r   r   r   r   r   fetch_access_tokenm   s
   zOAuth1Client.fetch_access_tokenc                 C   s   t tt|j}|| _|S )a)  Extract parameters from the post authorization redirect
        response URL.

        :param url: The full URL that resulted from the user being redirected
                    back from the OAuth provider to you, the client.
        :returns: A dict of parameters extracted from the URL.
        )r'   r   r   queryr   )r   r-   r   r   r   r   parse_authorization_response   s   z)OAuth1Client.parse_authorization_responsec                 K   s<   | j j|fd| ji|}| |j|j}|| _d | j_|S )Nr   )r   postr   parse_response_tokenstatus_codetextr   r   )r   r-   r   respr   r   r   r   r0      s
   zOAuth1Client._fetch_tokenc              
   C   s   |dkrd ||}| d| z| }|dr!t|}W |S tt|}W |S  ttfy@ } z	d |}t|d }~ww )Ni  z5Token request failed with code {}, response was '{}'.fetch_token_denied{zUnable to decode token from token response. This is commonly caused by an unsuccessful request where a non urlencoded error message is returned. The decoding error was {})	formatr)   strip
startswithr   r'   r   	TypeErrorr   )r   r8   r9   r*   r   eerrorr   r   r   r7      s&   

	z!OAuth1Client.parse_response_tokenc                 C   s   t |  d| )Nz: )r   )
error_typeerror_descriptionr   r   r   r)      s   zOAuth1Client.handle_errorr    )__name__
__module____qualname__r	   r   r   r   r   propertyr   setterr   r/   r1   r3   r5   r0   r7   staticmethodr)   r   r   r   r   r
      s2    






r
   N)authlib.common.urlsr   r   r   authlib.common.encodingr   rfc5849r   r   r	   r
   r   r   r   r   <module>   s    