name : tokens.cpython-39.pyc
a

\��`Y+�@s�dZddlZddlZddlZddlmZddlmZddlm	Z	ddl
mZmZddl
mZGd	d
�d
e�Zd d
d�Zdd�Zd!dd�Zd"dd�Zd#dd�Zdd�Zdd�ZGdd�d�ZGdd�de�ZdS)$z�
oauthlib.oauth2.rfc6749.tokens
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This module contains methods for adding two types of access tokens to requests.

- Bearer https://tools.ietf.org/html/rfc6750
- MAC https://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-01
�N)�
b2a_base64)�urlparse)�common)�add_params_to_qs�add_params_to_uri�)�utilscsreZdZd�fdd�	Zedd��Zedd��Zedd	��Zed
d��Zedd
��Z	edd��Z
edd��Z�ZS)�OAuth2TokenNcsnt��|�d|_d|vr6|dr6tt�|d��|_|durbtt�|��|_|jdurj|j|_n|j|_dS)N�scope)�super�__init__�
_new_scope�setrZ
scope_to_list�
_old_scope)�self�params�	old_scope��	__class__��B/usr/lib/python3.9/site-packages/oauthlib/oauth2/rfc6749/tokens.pyrs

zOAuth2Token.__init__cCs|j|jkS�N)r
r�rrrr�
scope_changed&szOAuth2Token.scope_changedcCst�|j�Sr)r�
list_to_scoperrrrrr*szOAuth2Token.old_scopecCs
t|j�Sr)�listrrrrr�
old_scopes.szOAuth2Token.old_scopescCst�|j�Sr)rrr
rrrrr
2szOAuth2Token.scopecCs
t|j�Sr)rr
rrrr�scopes6szOAuth2Token.scopescCst|j|j�Sr)rrr
rrrr�missing_scopes:szOAuth2Token.missing_scopescCst|j|j�Sr)rr
rrrrr�additional_scopes>szOAuth2Token.additional_scopes)N)
�__name__�
__module__�__qualname__r�propertyrrrr
rrr�
__classcell__rrrrr	s





r	��
hmac-sha-1cCs:|��}t�|�\}}|��dkr*tj}
n|��dkr>tj}
ntd��|
dkrj|pfd�t�	|	�t
���}nt
��}t
��}t
|�\}}}}}}|r�|d|}n|}|dur�|
dkr�|�d�}t|
|����dd	��d�}nd
}g}|
dkr�|�|�n|�|�|�|�|�|���|�|�|�|�|�|�|
dk�rN|�|�|�|�pZd
�d�|�d}t|t��r�|�d�}t�||�d�|
�}t|���dd	��d�}g}|�d|�|
dk�r�|�d
|�|�d|�|�r�|�d|�|�r|�d|�|�d|�|�p&i}d�|�|d<|S)a_Add an `MAC Access Authentication`_ signature to headers.

    Unlike OAuth 1, this HMAC signature does not require inclusion of the
    request payload/body, neither does it use a combination of client_secret
    and token_secret but rather a mac_key provided together with the access
    token.

    Currently two algorithms are supported, "hmac-sha-1" and "hmac-sha-256",
    `extension algorithms`_ are not supported.

    Example MAC Authorization header, linebreaks added for clarity

    Authorization: MAC id="h480djs93hd8",
                       nonce="1336363200:dj83hs9s",
                       mac="bhCQXTVyfj5cmA9uKkPFx1zeOXM="

    .. _`MAC Access Authentication`: https://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-01
    .. _`extension algorithms`: https://tools.ietf.org/html/draft-ietf-oauth-v2-http-mac-01#section-7.1

    :param token:
    :param uri: Request URI.
    :param key: MAC given provided by token endpoint.
    :param http_method: HTTP Request method.
    :param nonce:
    :param headers: Request headers as a dictionary.
    :param body:
    :param ext:
    :param hash_algorithm: HMAC algorithm provided by token endpoint.
    :param issue_time: Time when the MAC credentials were issued (datetime).
    :param draft: MAC authentication specification version.
    :return: headers dictionary with the authorization field added.
    r&zhmac-sha-256zunknown hash algorithmrz{}:{}�?Nzutf-8���r%�
zMAC id="%s"zts="%s"z
nonce="%s"z
bodyhash="%s"zext="%s"zmac="%s"z, �
Authorization)�upperrZ
host_from_uri�lower�hashlibZsha1Zsha256�
ValueError�formatZgenerate_agerZgenerate_nonceZgenerate_timestampr�encoder�digest�decode�append�join�
isinstance�str�hmac�new)�token�uri�keyZhttp_methodZnonce�headers�body�extZhash_algorithmZ
issue_timeZdraft�host�port�h�tsZschZnet�pathZpar�queryZfraZrequest_uriZbodyhash�baseZbase_string�sign�headerrrr�prepare_mac_headerCsf(�
 









rHcCst|d|fg�S)aAdd a `Bearer Token`_ to the request URI.
    Not recommended, use only if client can't use authorization header or body.

    http://www.example.com/path?access_token=h480djs93hd8

    .. _`Bearer Token`: https://tools.ietf.org/html/rfc6750

    :param token:
    :param uri:
    �access_token)r)r9r:rrr�prepare_bearer_uri�srJcCs|pi}d||d<|S)z�Add a `Bearer Token`_ to the request URI.
    Recommended method of passing bearer tokens.

    Authorization: Bearer h480djs93hd8

    .. _`Bearer Token`: https://tools.ietf.org/html/rfc6750

    :param token:
    :param headers:
    z	Bearer %sr*r)r9r<rrr�prepare_bearer_headers�srKcCst|d|fg�S)z�Add a `Bearer Token`_ to the request body.

    access_token=h480djs93hd8

    .. _`Bearer Token`: https://tools.ietf.org/html/rfc6750

    :param token:
    :param body:
    rI)r)r9r=rrr�prepare_bearer_body�s
rLFcCst��S)zp
    :param request: OAuthlib request.
    :type request: oauthlib.common.Request
    :param refresh_token:
    )rZgenerate_token)�request�
refresh_tokenrrr�random_token_generator�srOcs��fdd�}|S)z
    :param private_pem:
    cs�|_t��|�Sr)ZclaimsrZgenerate_signed_token)rM��kwargs�private_pemrr�signed_token_generator�sz6signed_token_generator.<locals>.signed_token_generatorr)rRrQrSrrPrrS�srScCsNd}d|jvrD|j�d���}t|�dkrJ|d��dkrJ|d}n|j}|S)z�
    Helper function to extract a token from the request header.

    :param request: OAuthlib request.
    :type request: oauthlib.common.Request
    :return: Return the token or None if the Authorization header is malformed.
    Nr*�r�bearerr)r<�get�split�lenr,rI)rMr9Zsplit_headerrrr�get_token_from_header�s

rYc@s&eZdZd	dd�Zdd�Zdd�ZdS)
�	TokenBaseFcCstd��dS)N�&Subclasses must implement this method.��NotImplementedError)rrMrNrrr�__call__szTokenBase.__call__cCstd��dS��b
        :param request: OAuthlib request.
        :type request: oauthlib.common.Request
        r[Nr\�rrMrrr�validate_requestszTokenBase.validate_requestcCstd��dSr_r\rarrr�
estimate_typeszTokenBase.estimate_typeN)F)r r!r"r^rbrcrrrrrZs
rZc@s4eZdZdZddd�Zd
dd�Zdd	�Zd
d�ZdS)�BearerToken)�request_validator�token_generator�refresh_token_generator�
expires_inNcCs*||_|pt|_|p|j|_|p"d|_dS)Ni)rerOrfrgrh)rrerfrhrgrrrrs

�zBearerToken.__init__FcKs�d|vrt�dt�t|j�r*|�|�}n|j}||_|�|�|dd�}|jdurbd�|j�|d<|r�|jr�|j	�
|�s�|j|d<n|�|�|d<|�|j
p�i�t|�S)	z�
        Create a BearerToken, by default without refresh token.

        :param request: OAuthlib request.
        :type request: oauthlib.common.Request
        :param refresh_token:
        Z
save_tokenzx`save_token` has been deprecated, it was not called internally.If you do, call `request_validator.save_token()` instead.ZBearer)rIrh�
token_typeN� r
rN)�warnings�warn�DeprecationWarning�callablerhrfrr4rNreZrotate_refresh_tokenrg�updateZextra_credentialsr	)rrMrNrQrhr9rrr�create_token&s,�
�	

�zBearerToken.create_tokencCst|�}|j�||j|�S)r`)rYreZvalidate_bearer_tokenr)rrMr9rrrrbPs�zBearerToken.validate_requestcCs:|j�dd��d�d��dkr$dS|jdur2dSdSdS)	r`r*r%rjrrU�	N�)r<rVrWr,rIrarrrrcYs
 
zBearerToken.estimate_type)NNNN)F)r r!r"�	__slots__rrprbrcrrrrrds�
	
*	rd)NNNr%r&Nr)N)r%)F)�__doc__r-r7rkZbinasciir�urllib.parserZoauthlibrZoauthlib.commonrrr%r�dictr	rHrJrKrLrOrSrYrZrdrrrr�<module>s2	.�
m


	

© 2025 Cubjrnet7