o
    b                     @   s   d dl 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mZmZmZmZ ddl	mZmZmZ dd	lmZmZmZ d
ZdZG dd deZdd Zdd ZdS )    Ngenerate_token)extract_params)	to_native   )OAuth1Request)SIGNATURE_HMAC_SHA1SIGNATURE_PLAINTEXTSIGNATURE_RSA_SHA1SIGNATURE_TYPE_HEADERSIGNATURE_TYPE_BODYSIGNATURE_TYPE_QUERY)sign_hmac_sha1sign_rsa_sha1sign_plaintext)prepare_form_encoded_bodyprepare_headersprepare_request_uri_queryz!application/x-www-form-urlencodedzmultipart/form-datac                
   @   sp   e Zd ZeeeeeeiZ	e
dd Zddddddeeddf
ddZdd Zd	d
 Zdd ZdddZdd ZdS )
ClientAuthc                 C   s   || j |< dS )a  Extend client signature methods.

        :param name: A string to represent signature method.
        :param sign: A function to generate signature.

        The ``sign`` method accept 2 parameters::

            def custom_sign_method(client, request):
                # client is the instance of Client.
                return 'your-signed-string'

            Client.register_signature_method('custom-name', custom_sign_method)
        N)SIGNATURE_METHODS)clsnamesign r   a/var/www/secure340b-portal/env/lib/python3.10/site-packages/authlib/oauth1/rfc5849/client_auth.pyregister_signature_method%   s   z$ClientAuth.register_signature_methodNFc                 C   sF   || _ || _|| _|| _|| _|| _|	| _|| _|| _|
| _	|| _
d S N)	client_idclient_secrettokentoken_secretredirect_urisignature_methodsignature_typersa_keyverifierrealmforce_include_body)selfr   r   r   r    r!   r$   r%   r"   r#   r&   r'   r   r   r   __init__6   s   
zClientAuth.__init__c                 C   s4   | j | j}|stdt||||d}|| |S )aw  Get an OAuth signature to be used in signing a request

        To satisfy `section 3.4.1.2`_ item 2, if the request argument's
        headers dict attribute contains a Host item, its value will
        replace any netloc part of the request argument's uri attribute
        value.

        .. _`section 3.4.1.2`: https://tools.ietf.org/html/rfc5849#section-3.4.1.2
        zInvalid signature method.)bodyheaders)r   getr"   
ValueErrorr   )r(   methodurir+   r*   r   requestr   r   r   get_oauth_signatureH   s
   

zClientAuth.get_oauth_signaturec                 C   sh   d|fd|fdd| j fd| jfg}| jr|d| jf | jr'|d| jf | jr2|d| jf |S )	NZoauth_nonceZoauth_timestamp)Zoauth_versionz1.0Zoauth_signature_methodZoauth_consumer_keyZoauth_tokenZoauth_callbackZoauth_verifier)r"   r   r   appendr!   r%   )r(   nonce	timestampoauth_paramsr   r   r   get_oauth_paramsY   s   zClientAuth.get_oauth_paramsc                 C   s~   | j tkrt||| jd}n,| j tkr+t|ddv r*t|p g }t||}t|d< n| j t	kr6t
||}ntd|||fS )N)r&   Content-Type z!Unknown signature type specified.)r#   r   r   r&   r   CONTENT_TYPE_FORM_URLENCODEDr,   r   r   r   r   r-   )r(   r/   r+   r*   r5   Zdecoded_bodyr   r   r   _renderi   s   




zClientAuth._renderc           	      C   s   |du rt  }|du rt }|du rd}t|}|du ri }| ||}| ||||\}}}| ||||}|d|f | ||||\}}}|||fS )a?  Sign the HTTP request, add OAuth parameters and signature.

        :param method: HTTP method of the request.
        :param uri:  URI of the HTTP request.
        :param body: Body payload of the HTTP request.
        :param headers: Headers of the HTTP request.
        :param nonce: A string to represent nonce value. If not configured,
                      this method will generate one for you.
        :param timestamp: Current timestamp. If not configured, this method
                          will generate one for you.
        :return: uri, headers, body
        Nr8   Zoauth_signature)generate_noncegenerate_timestampstrr6   r:   r1   r2   )	r(   r.   r/   r+   r*   r3   r4   r5   sigr   r   r   r   w   s   
zClientAuth.signc                 C   s   t |dd}| jtkrt}n|st|rt}t|v r,t|d< | ||||\}}}n| jr;| ||||\}}}n| |||d\}}}d}|||fS )zAdd OAuth parameters to the request.

        Parameters may be included from the body if the content-type is
        urlencoded, if no content type is set, a guess is made.
        r7   r8   )r   r,   r#   r   r9   r   r   r'   )r(   r.   r/   r+   r*   content_type_r   r   r   prepare   s   

zClientAuth.prepare)NN)__name__
__module____qualname__r   r   r
   r   r	   r   r   classmethodr   r   r)   r1   r6   r:   r   rA   r   r   r   r   r      s&    


#r   c                   C   s   t  S r   r   r   r   r   r   r;      s   r;   c                   C   s   t tt S r   )r=   inttimer   r   r   r   r<      s   r<   )rG   Zauthlib.common.securityr   Zauthlib.common.urlsr   Zauthlib.common.encodingr   wrapperr   	signaturer   r	   r
   r   r   r   r   r   r   
parametersr   r   r   r9   CONTENT_TYPE_MULTI_PARTobjectr   r;   r<   r   r   r   r   <module>   s      