o
    ë6Wj4  ã                   @   sŠ  d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z
mZmZ ddlmZ z
ddlmZmZ W n eyG   ddlmZmZ Y nw dZd	Ze d
ej¡Ze d¡ZdZe d¡Zd9dd„Z	dd„ Zdd„ Zdd„ Z dd„ Z!e"eƒe"dƒB Z#dd„ Z$dd„ Z%dd„ Z&d d!„ Z'd"efd#d$„Z(d%d&„ Z)d'd(„ Z*d"efd)d*„Z+d+d,„ Z,d:d.d/„Z-d0d1„ Z.d;d3d4„Z/G d5d6„ d6e0ƒZ1G d7d8„ d8ƒZ2dS )<z|
oauthlib.common
~~~~~~~~~~~~~~

This module provides data structures and utilities common
to all implementations of OAuth.
é    N)ÚquoteÚunquoteÚ	urlencodeé   )Ú	get_debug)ÚSystemRandomÚrandbits)r   ÚgetrandbitsÚ>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789z_ !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}z&([^&;]*(?:password|token)[^=]*=)[^&;]+z&%[^0-9A-Fa-f]|%[0-9A-Fa-f][^0-9A-Fa-f]zAABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-Úoauthlibó   /c                 C   s:   t | tƒr
|  d¡n| } t| |ƒ} t | tƒr|  d¡} | S ©Núutf-8)Ú
isinstanceÚstrÚencodeÚ_quoteÚbytesÚdecode)ÚsÚsafe© r   ú^/home/esfera/Documents/content_generation/venv/lib/python3.10/site-packages/oauthlib/common.pyr   +   s
   


r   c                 C   s    t | ƒ} t| tƒr|  d¡} | S r   )Ú_unquoter   r   r   )r   r   r   r   r   5   s   

r   c                 C   s(   t | ƒ}t|ƒ}t|tƒr|S | d¡S r   )Úencode_params_utf8Ú
_urlencoder   r   r   )ÚparamsÚutf8_paramsÚ
urlencodedr   r   r   r   ?   s
   

r   c                 C   óL   g }| D ]\}}|  t|tƒr| d¡n|t|tƒr| d¡n|f¡ q|S )ziEnsures that all parameters in a list of 2-element tuples are encoded to
    bytestrings using UTF-8
    r   )Úappendr   r   r   )r   ÚencodedÚkÚvr   r   r   r   H   ó   þr   c                 C   r   )zfEnsures that all parameters in a list of 2-element tuples are decoded to
    unicode using UTF-8.
    r   )r    r   r   r   )r   Údecodedr"   r#   r   r   r   Údecode_params_utf8T   r$   r&   z=&;:%+~,*@!()/?'$c                 C   sT   | rt | ƒtksd}t|t | ƒt | f ƒ‚t | ¡rtdƒ‚tj| dd�}t|ƒS )a‚  Decode a query string in x-www-form-urlencoded format into a sequence
    of two-element tuples.

    Unlike urlparse.parse_qsl(..., strict_parsing=True) urldecode will enforce
    correct formatting of the query string by validation. If validation fails
    a ValueError will be raised. urllib.parse_qsl will only raise errors if
    any of name-value pairs omits the equals sign.
    z£Error trying to decode a non urlencoded string. Found invalid characters: %s in the string: '%s'. Please ensure the request/response body is x-www-form-urlencoded.z%Invalid hex encoding in query string.T©Úkeep_blank_values)Úsetr   Ú
ValueErrorÚINVALID_HEX_PATTERNÚsearchÚurlparseÚ	parse_qslr&   )ÚqueryÚerrorr   r   r   r   Ú	urldecodec   s   

r1   c                 C   sª   t | ttfƒrzt| ƒ}W |S  ty   d}Y |S w t| dƒrQzt| ƒ W n ty2   d}Y |S  ty=   d}Y |S w tt | tƒrH|  	¡ n| ƒ}t
|ƒ}|S d}|S )a*  Extract parameters and return them as a list of 2-tuples.

    Will successfully extract parameters from urlencoded query strings,
    dicts, or lists of 2-tuples. Empty strings/dicts/lists will return an
    empty list of parameters. Any other input will result in a return
    value of None.
    NÚ__iter__)r   r   r   r1   r*   ÚhasattrÚdictÚ	TypeErrorÚlistÚitemsr&   )Úrawr   r   r   r   Úextract_params„   s.   
ññ
	øøþr9   c                   C   s   t t tdƒƒtƒ  ƒS )aâ  Generate pseudorandom nonce that is unlikely to repeat.

    Per `section 3.3`_ of the OAuth 1 RFC 5849 spec.
    Per `section 3.2.1`_ of the MAC Access Authentication spec.

    A random 64-bit number is appended to the epoch timestamp for both
    randomness and to decrease the likelihood of collisions.

    .. _`section 3.2.1`: https://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-01#section-3.2.1
    .. _`section 3.3`: https://tools.ietf.org/html/rfc5849#section-3.3
    é@   )r   r   Úgenerate_timestampr   r   r   r   Úgenerate_nonce¡   s   r<   c                   C   s   t tt ¡ ƒƒS )aD  Get seconds since epoch (UTC).

    Per `section 3.3`_ of the OAuth 1 RFC 5849 spec.
    Per `section 3.2.1`_ of the MAC Access Authentication spec.

    .. _`section 3.2.1`: https://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-01#section-3.2.1
    .. _`section 3.3`: https://tools.ietf.org/html/rfc5849#section-3.3
    )r   ÚintÚtimer   r   r   r   r;   °   s   	r;   é   c                    s$   t ƒ ‰d ‡ ‡fdd„t| ƒD ƒ¡S )aX  Generates a non-guessable OAuth token

    OAuth (1 and 2) does not specify the format of tokens except that they
    should be strings of random characters. Tokens should not be guessable
    and entropy when generating the random characters is important. Which is
    why SystemRandom is used instead of the default random.choice method.
    Ú c                 3   s   � | ]}ˆ  ˆ ¡V  qd S ©N)Úchoice)Ú.0Úx©ÚcharsÚrandr   r   Ú	<genexpr>Å   ó   € z!generate_token.<locals>.<genexpr>)r   ÚjoinÚrange©ÚlengthrF   r   rE   r   Úgenerate_token¼   s   rN   c                 C   sT   dd l }tj ¡ }|j|tj|jd� dœ}| |j¡ | || d¡}t	|dƒ}|S )Nr   )Úseconds)ÚscopeÚexpÚRS256úUTF-8)
ÚjwtÚdatetimeÚutcnowrP   Ú	timedeltaÚ
expires_inÚupdateÚclaimsr   Ú
to_unicode)Úprivate_pemÚrequestrT   ÚnowrZ   Útokenr   r   r   Úgenerate_signed_tokenÈ   s   
þ
r`   c                 C   s   dd l }|j|| dgd�S )Nr   rR   )Ú
algorithms)rT   r   )Ú
public_pemr_   rT   r   r   r   Úverify_signed_tokenÚ   s   rc   c                 C   s
   t | |ƒS )z…Generates an OAuth client_id

    OAuth 2 specify the format of client_id in
    https://tools.ietf.org/html/rfc6749#appendix-A.
    )rN   rL   r   r   r   Úgenerate_client_idà   s   
rd   c                 C   s2   t |tƒr	| ¡ }tj| dd�}| |¡ t|ƒS )z)Extend a query with a list of two-tuples.Tr'   )r   r4   r7   r-   r.   Úextendr   )r/   r   Úqueryparamsr   r   r   Úadd_params_to_qsé   s
   

rg   Fc           	      C   sF   t   | ¡\}}}}}}|rt||ƒ}nt||ƒ}t  ||||||f¡S )z5Add a list of two-tuples to the uri query components.)r-   rg   Ú
urlunparse)	Úurir   ÚfragmentÚschÚnetÚpathÚparr/   Úfrar   r   r   Úadd_params_to_uriò   s
   
rp   c                 C   sH   t | ƒt |ƒkr
dS d}t| |ƒD ]\}}|t|ƒt|ƒA O }q|dkS )a   Near-constant time string comparison.

    Used in order to avoid timing attacks on sensitive information such
    as secret keys during request verification (`rootLabs`_).

    .. _`rootLabs`: http://rdist.root.org/2010/01/07/timing-independent-array-comparison/

    Fr   )ÚlenÚzipÚord)ÚaÚbÚresultrD   Úyr   r   r   Úsafe_string_equalsü   s   	rx   rS   c                    sš   t | tƒr| S t | tƒrt| ˆ d�S t| dƒrKzt| ƒ W n ty'   Y | S  ty8   ‡ fdd„| D ƒ Y S w t| dƒrB|  ¡ } ‡ fdd„| D ƒS | S )z:Convert a number of different types of objects to unicode.©Úencodingr2   c                 3   s   � | ]}t |ˆ ƒV  qd S rA   ©r[   )rC   Úiry   r   r   rH     rI   zto_unicode.<locals>.<genexpr>r7   c                    s"   i | ]\}}t |ˆ ƒt |ˆ ƒ“qS r   r{   )rC   r"   r#   ry   r   r   Ú
<dictcomp>"  s   " zto_unicode.<locals>.<dictcomp>)r   r   r   r3   r4   r5   r*   r7   )Údatarz   r   ry   r   r[     s"   



÷þ
r[   c                       sb   e Zd ZdZi Zdd„ Zdd„ Z‡ fdd„Z‡ fdd	„Zddd„Z	‡ fdd„Z
‡ fdd„Z‡  ZS )ÚCaseInsensitiveDictz3Basic case insensitive dict with strings only keys.c                 C   s*   dd„ |D ƒ| _ |D ]}|| | |< q
d S )Nc                 S   s   i | ]}|  ¡ |“qS r   )Úlower)rC   r"   r   r   r   r}   .  s    z0CaseInsensitiveDict.__init__.<locals>.<dictcomp>)Úproxy)Úselfr~   r"   r   r   r   Ú__init__-  s   ÿzCaseInsensitiveDict.__init__c                 C   s   |  ¡ | jv S rA   )r€   r�   )r‚   r"   r   r   r   Ú__contains__2  s   z CaseInsensitiveDict.__contains__c                    s*   | j | ¡  }tƒ  |¡ | j | ¡ = d S rA   )r�   r€   ÚsuperÚ__delitem__©r‚   r"   Úkey©Ú	__class__r   r   r†   5  s   zCaseInsensitiveDict.__delitem__c                    s   | j | ¡  }tƒ  |¡S rA   )r�   r€   r…   Ú__getitem__r‡   r‰   r   r   r‹   :  s   zCaseInsensitiveDict.__getitem__Nc                 C   s   || v r| | S |S rA   r   )r‚   r"   Údefaultr   r   r   Úget>  s   zCaseInsensitiveDict.getc                    s    t ƒ  ||¡ || j| ¡ < d S rA   )r…   Ú__setitem__r�   r€   )r‚   r"   r#   r‰   r   r   rŽ   A  s   zCaseInsensitiveDict.__setitem__c                    s8   t ƒ j|i |¤Ž t|i |¤ŽD ]	}|| j| ¡ < qd S rA   )r…   rY   r4   r�   r€   )r‚   ÚargsÚkwargsr"   r‰   r   r   rY   E  s   ÿzCaseInsensitiveDict.updaterA   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r�   rƒ   r„   r†   r‹   r�   rŽ   rY   Ú__classcell__r   r   r‰   r   r   '  s    
r   c                   @   sR   e Zd ZdZ		ddd„Zdd„ Zd	d
„ Zedd„ ƒZedd„ ƒZ	edd„ ƒZ
dS )ÚRequesta:  A malleable representation of a signable HTTP request.

    Body argument may contain any data, but parameters will only be decoded if
    they are one of:

    * urlencoded query string
    * dict
    * list of 2-tuples

    Anything else will be treated as raw body data to be passed through
    unmolested.
    ÚGETNr   c                    s  ‡ fdd„}||ƒ| _ ||ƒ| _t||pi ƒƒ| _||ƒ| _t| jƒ| _g | _i | _i dd “dd “dd “dd “dd “dd “d	d “d
d “dd “dd “dd “dd “dd “dd “dd “dd “dd “d d d d d d d d d d d d d dœ¥| _	| j	 
tt| jƒƒ¡ | j	 
t| jpƒg ƒ¡ d S )Nc                    s   ˆ rt | ˆ ƒS | S rA   r{   )rD   ry   r   r   r   ]  s   z Request.__init__.<locals>.encodeÚaccess_tokenÚclientÚ	client_idÚclient_secretÚcodeÚcode_challengeÚcode_challenge_methodÚcode_verifierÚextra_credentialsÚ
grant_typeÚredirect_uriÚrefresh_tokenÚrequest_tokenÚresponse_typerP   ÚscopesÚstate)r_   ÚuserÚtoken_type_hintÚresponse_modeÚnonceÚdisplayÚpromptrZ   Úmax_ageÚ
ui_localesÚid_token_hintÚ
login_hintÚ
acr_values)ri   Úhttp_methodr   ÚheadersÚbodyr9   Údecoded_bodyÚoauth_paramsÚvalidator_logÚ_paramsrY   r4   r1   Ú	uri_query)r‚   ri   r³   rµ   r´   rz   r   r   ry   r   rƒ   Z  sv   


ÿþýüûúùø	÷
öõôóòñðï
à"zRequest.__init__c                 C   s   || j v r
| j | S t|ƒ‚rA   )r¹   ÚAttributeError)r‚   Únamer   r   r   Ú__getattr__�  s   

zRequest.__getattr__c                 C   sR   t ƒ sdS | j}| j ¡ }|rt dt|ƒ¡}d|v rd|d< d | j| j	||¡S )Nz<oauthlib.Request SANITIZED>z<SANITIZED>ÚAuthorizationz<SANITIZED>zF<oauthlib.Request url="{}", http_method="{}", headers="{}", body="{}">)
r   rµ   r´   ÚcopyÚSANITIZE_PATTERNÚsubr   Úformatri   r³   )r‚   rµ   r´   r   r   r   Ú__repr__“  s   
ÿzRequest.__repr__c                 C   s   t   | j¡jS rA   )r-   ri   r/   ©r‚   r   r   r   rº   Ÿ  s   zRequest.uri_queryc                 C   s   | j sg S tj| j ddd�S )NT)r(   Ústrict_parsing)rº   r-   r.   rÄ   r   r   r   Úuri_query_params£  s
   
ÿzRequest.uri_query_paramsc                 C   sP   t  t¡}dd„ | jpg | j D ƒ}|D ]
}||  d7  < qdd„ | ¡ D ƒS )Nc                 s   s   � | ]}|d  V  qdS )r   Nr   )rC   Úpr   r   r   rH   ­  s   € ÿz+Request.duplicate_params.<locals>.<genexpr>r   c                 S   s   g | ]
\}}|d kr|‘qS )r   r   )rC   r"   Úcr   r   r   Ú
<listcomp>±  s    z,Request.duplicate_params.<locals>.<listcomp>)ÚcollectionsÚdefaultdictr=   r¶   rÆ   r7   )r‚   Ú	seen_keysÚall_keysr"   r   r   r   Úduplicate_paramsª  s   
ÿzRequest.duplicate_params)r—   NNr   )r‘   r’   r“   r”   rƒ   r½   rÃ   Úpropertyrº   rÆ   rÎ   r   r   r   r   r–   K  s    
ÿ3

r–   )r   )F)rS   )3r”   rÊ   rU   ÚloggingÚrer>   Úurllib.parseÚparser-   r   r   r   r   r   r   r@   r   Úsecretsr   r   ÚImportErrorÚrandomr	   ÚUNICODE_ASCII_CHARACTER_SETÚCLIENT_ID_CHARACTER_SETÚcompileÚ
IGNORECASErÀ   r+   Úalways_safeÚ	getLoggerÚlogr   r&   r)   r   r1   r9   r<   r;   rN   r`   rc   rd   rg   rp   rx   r[   r4   r   r–   r   r   r   r   Ú<module>   sP    ÿ




	!	
	

$