
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
,[M&                 @   s   d Z ddlmZmZ ddlZddlZddlmZ ddlm	Z	 ddl
mZmZmZ ddlmZ ydd	lmZ W n  ek
r   dd	lmZ Y nX G d
d deZdddZdd Zd ddZd!ddZd"ddZdd ZG dd deZG dd deZdS )#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
    )absolute_importunicode_literalsN)
b2a_base64)common)add_params_to_qsadd_params_to_uriunicode_type   )utils)urlparsec                   sr   e Zd Zd fdd	Zedd Zedd Zedd	 Zed
d Zedd Z	edd Z
edd Z  ZS )OAuth2TokenNc                sr   t t| j| d | _d|kr:|d r:ttj|d | _|d k	rfttj|| _| jd krn| j| _n| j| _d S )Nscope)superr   __init__
_new_scopesetr
   Zscope_to_list
_old_scope)selfZparams	old_scope)	__class__ /usr/lib/python3.6/tokens.pyr      s    

zOAuth2Token.__init__c             C   s   | j | jkS )N)r   r   )r   r   r   r   scope_changed-   s    zOAuth2Token.scope_changedc             C   s   t j| jS )N)r
   list_to_scoper   )r   r   r   r   r   1   s    zOAuth2Token.old_scopec             C   s
   t | jS )N)listr   )r   r   r   r   
old_scopes5   s    zOAuth2Token.old_scopesc             C   s   t j| jS )N)r
   r   r   )r   r   r   r   r   9   s    zOAuth2Token.scopec             C   s
   t | jS )N)r   r   )r   r   r   r   scopes=   s    zOAuth2Token.scopesc             C   s   t | j| j S )N)r   r   r   )r   r   r   r   missing_scopesA   s    zOAuth2Token.missing_scopesc             C   s   t | j| j S )N)r   r   r   )r   r   r   r   additional_scopesE   s    zOAuth2Token.additional_scopes)N)__name__
__module____qualname__r   propertyr   r   r   r   r   r   r   __classcell__r   r   )r   r   r      s   r    
hmac-sha-1c             C   s:  |j  }tj|\}}|j dkr*tj}n|j dkr>tj}ntd|
dkrj|pfdjtj	|	t
j }nt
j }t
j }t|\}}}}}}|r|d | }n|}|dk	r|
dkr|jd}t||j dd jd}nd
}g }|
dkr|j| n|j| |j| |j|j   |j| |j| |j| |
dkrN|j| |j|pZd
 dj|d }t|tr|jd}tj||jd|}t|j dd jd}g }|jd|   |
dkr|jd|  |jd|  |r|jd|  |r|jd|  |jd|  |p&i }dj||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 uri: Request URI.
    :param headers: Request headers as a dictionary.
    :param http_method: HTTP Request method.
    :param key: MAC given provided by token endpoint.
    :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.
    z
hmac-sha-1zhmac-sha-256zunknown hash algorithmr   z{0}:{1}?Nzutf-8r	   r$   
zMAC id="%s"zts="%s"z
nonce="%s"zbodyhash="%s"zext="%s"zmac="%s"z, Authorizationr)   )upperr
   Zhost_from_urilowerhashlibZsha1Zsha256
ValueErrorformatZgenerate_ager   Zgenerate_nonceZgenerate_timestampr   encoder   Zdigestdecodeappendjoin
isinstancer   hmacnew)tokenurikeyZhttp_methodZnonceheadersbodyZextZhash_algorithmZ
issue_timeZdrafthostZporthZtsZschZnetpathZparZqueryZfraZrequest_uriZbodyhashbaseZbase_stringZsignheaderr   r   r   prepare_mac_headerJ   sd    $
 









r@   c             C   s   t |d| fgS )zAdd 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
    access_token)r   )r6   r7   r   r   r   prepare_bearer_uri   s    rB   c             C   s   |pi }d|  |d< |S )zAdd a `Bearer Token`_ to the request URI.
    Recommended method of passing bearer tokens.

    Authorization: Bearer h480djs93hd8

    .. _`Bearer Token`: https://tools.ietf.org/html/rfc6750
    z	Bearer %sr(   r   )r6   r9   r   r   r   prepare_bearer_headers   s    rC   c             C   s   t |d| fgS )zAdd a `Bearer Token`_ to the request body.

    access_token=h480djs93hd8

    .. _`Bearer Token`: https://tools.ietf.org/html/rfc6750
    rA   )r   )r6   r:   r   r   r   prepare_bearer_body   s    rD   Fc             C   s   t j S )N)r   Zgenerate_token)requestrefresh_tokenr   r   r   random_token_generator   s    rG   c                s    fdd}|S )Nc                s    | _ tj| S )N)Zclaimsr   Zgenerate_signed_token)rE   )kwargsprivate_pemr   r   signed_token_generator   s    z6signed_token_generator.<locals>.signed_token_generatorr   )rI   rH   rJ   r   )rH   rI   r   rJ      s    rJ   c               @   s&   e Zd Zd	ddZdd Zdd ZdS )
	TokenBaseFc             C   s   t dd S )Nz&Subclasses must implement this method.)NotImplementedError)r   rE   rF   r   r   r   __call__   s    zTokenBase.__call__c             C   s   t dd S )Nz&Subclasses must implement this method.)rL   )r   rE   r   r   r   validate_request   s    zTokenBase.validate_requestc             C   s   t dd S )Nz&Subclasses must implement this method.)rL   )r   rE   r   r   r   estimate_type   s    zTokenBase.estimate_typeN)F)r   r    r!   rM   rN   rO   r   r   r   r   rK      s   
rK   c               @   s4   e Zd ZdZdddZdd
dZdd Zdd ZdS )BearerTokenrequest_validatortoken_generatorrefresh_token_generator
expires_inNc             C   s*   || _ |pt| _|p| j| _|p"d| _d S )Ni  )rQ   rG   rR   rS   rT   )r   rQ   rR   rT   rS   r   r   r   r      s    
zBearerToken.__init__FTc             C   s   t | jr| j|}n| j}||_| j||dd}|jdk	rNdj|j|d< |jdk	rb|j|d< |r|jr| jj| r|j|d< n| j	||d< |j
|jpi  t|}|r| jj|| |S )z7Create a BearerToken, by default without refresh token.Bearer)rA   rT   
token_typeN r   staterF   )callablerT   rR   r   r2   rX   rF   rQ   Zrotate_refresh_tokenrS   updateZextra_credentialsr   Zsave_bearer_token)r   rE   rF   Z
save_tokenrT   r6   r   r   r   create_token   s*    



zBearerToken.create_tokenc             C   s<   d }d|j kr$|j jddd  }n|j}| jj||j|S )Nr(      )r9   getrA   rQ   Zvalidate_bearer_tokenr   )r   rE   r6   r   r   r   rN   "  s    
zBearerToken.validate_requestc             C   s.   |j jddjdrdS |jd k	r&dS dS d S )Nr(   r$   rU   	      r   )r9   r]   
startswithrA   )r   rE   r   r   r   rO   +  s
    
zBearerToken.estimate_type)rQ   rR   rS   rT   )NNNN)FT)r   r    r!   	__slots__r   r[   rN   rO   r   r   r   r   rP      s     

&	rP   )NNNr$   r%   Nr   )N)r$   )F)__doc__Z
__future__r   r   r,   r4   Zbinasciir   Zoauthlibr   Zoauthlib.commonr   r   r   r$   r
   r   ImportErrorZurllib.parsedictr   r@   rB   rC   rD   rG   rJ   objectrK   rP   r   r   r   r   <module>	   s4   .      
b



