3
@VVZ                 @   s2  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ZddlZddlZddl	Z	dZ
dZdZG dd deZd(d	d
Zdd Zdd Zdd Zd)ddZd*ddZG dd deZG dd deZG dd deZG dd deZG dd deZG d d! d!eZG d"d# d#eZG d$d% d%eZG d&d' d'eZdS )+a1  
The MIT License

Copyright (c) 2007 Leah Culver

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
    Nz1.0ZGET	PLAINTEXTc               @   s   e Zd ZdZdddZdS )
OAuthErrorzGeneric exception class.OAuth error occured.c             C   s
   || _ d S )N)message)selfr    r   -/usr/lib/python3/dist-packages/oauth/oauth.py__init__)   s    zOAuthError.__init__N)r   )__name__
__module____qualname____doc__r	   r   r   r   r   r   '   s   r    c             C   s   dd|  iS )z,Optional WWW-Authenticate header (401 error)zWWW-AuthenticatezOAuth realm="%s"r   )realmr   r   r   build_authenticate_header,   s    r   c             C   s   t jj| ddS )zEscape a URL including any /.~)Zsafe)urllibparseZquote)sr   r   r   escape0   s    r   c             C   s    t | tr| jdS t| S dS )zConvert unicode to utf-8.zutf-8N)
isinstancestrencode)r   r   r   r   	_utf8_str4   s    

r   c               C   s   t tj S )zGet seconds since epoch (UTC).)inttimer   r   r   r   generate_timestamp;   s    r      c             C   s   dj dd t| D S )zGenerate pseudorandom number.r   c             S   s   g | ]}t tjd dqS )r   	   )r   randomrandint).0ir   r   r   
<listcomp>A   s    z"generate_nonce.<locals>.<listcomp>)joinrange)lengthr   r   r   generate_nonce?   s    r'   c             C   s   dj dd t| D S )zGenerate pseudorandom number.r   c             S   s   g | ]}t tjd dqS )r   r   )r   r   r    )r!   r"   r   r   r   r#   E   s    z%generate_verifier.<locals>.<listcomp>)r$   r%   )r&   r   r   r   generate_verifierC   s    r(   c               @   s    e Zd ZdZdZdZdd ZdS )OAuthConsumerzConsumer of OAuth authentication.

    OAuthConsumer is a data type that represents the identity of the Consumer
    via its shared secret with the Service Provider.

    Nc             C   s   || _ || _d S )N)keysecret)r   r*   r+   r   r   r   r	   R   s    zOAuthConsumer.__init__)r
   r   r   r   r*   r+   r	   r   r   r   r   r)   H   s   r)   c               @   sf   e Zd ZdZdZdZdZdZdZdd Z	dd Z
dddZd	d
 Zdd Zdd ZeeZdd ZdS )
OAuthTokenzOAuthToken is a data type that represents an End User via either an access
    or request token.
    
    key -- the token
    secret -- the token secret

    Nc             C   s   || _ || _d S )N)r*   r+   )r   r*   r+   r   r   r   r	   e   s    zOAuthToken.__init__c             C   s   || _ d| _d S )Ntrue)callbackcallback_confirmed)r   r.   r   r   r   set_callbacki   s    zOAuthToken.set_callbackc             C   s   |d k	r|| _ nt | _ d S )N)verifierr(   )r   r1   r   r   r   set_verifierm   s    zOAuthToken.set_verifierc             C   sn   | j rh| jrhtjj| j }|d d \}}}}}}|rFd|| jf }n
d| j }tjj||||||fS | j S )N   z%s&oauth_verifier=%szoauth_verifier=%s)r.   r1   r   r   urlparseZ
urlunparse)r   partsschemenetlocpathparamsZqueryZfragmentr   r   r   get_callback_urls   s    

zOAuthToken.get_callback_urlc             C   s.   | j | jd}| jd k	r"| j|d< tjj|S )N)oauth_tokenoauth_token_secretoauth_callback_confirmed)r*   r+   r/   r   r   Z	urlencode)r   datar   r   r   	to_string   s
    


zOAuthToken.to_stringc             C   s\   t j| dd}|d d }|d d }t||}y|d d |_W n tk
rV   Y nX |S )z] Returns a token from something like:
        oauth_token_secret=xxx&oauth_token=xxx
        F)keep_blank_valuesr;   r   r<   r=   )cgiparse_qsr,   r/   KeyError)r   r9   r*   r+   tokenr   r   r   from_string   s    
zOAuthToken.from_stringc             C   s   | j  S )N)r?   )r   r   r   r   __str__   s    zOAuthToken.__str__)N)r
   r   r   r   r*   r+   r.   r/   r1   r	   r0   r2   r:   r?   rE   staticmethodrF   r   r   r   r   r,   W   s   
	r,   c               @   s   e Zd ZdZdZeZdZeZ	eddfddZ
dd Zdd Zd	d
 Zdd Zd(ddZdd Zdd Zdd Zdd Zdd Zdd Zdd Zd)ddZeeZdddeddfd d!ZeeZdeddfd"d#ZeeZd$d% ZeeZd&d' ZeeZdS )*OAuthRequestas  OAuthRequest represents the request and can be serialized.

    OAuth parameters:
        - oauth_consumer_key 
        - oauth_token
        - oauth_signature_method
        - oauth_signature 
        - oauth_timestamp 
        - oauth_nonce
        - oauth_version
        - oauth_verifier
        ... any additional parameters, as defined by the Service Provider.
    Nc             C   s   || _ || _|pi | _d S )N)http_methodhttp_url
parameters)r   rI   rJ   rK   r   r   r   r	      s    zOAuthRequest.__init__c             C   s   || j |< d S )N)rK   )r   	parametervaluer   r   r   set_parameter   s    zOAuthRequest.set_parameterc          	   C   s(   y
| j | S    td| Y nX d S )NzParameter not found: %s)rK   r   )r   rL   r   r   r   get_parameter   s    
zOAuthRequest.get_parameterc             C   s   | j d| j dfS )Noauth_timestampoauth_nonce)rO   )r   r   r   r   _get_timestamp_nonce   s    z!OAuthRequest._get_timestamp_noncec             C   s6   i }x,| j j D ]\}}|jddk r|||< qW |S )zGet any non-OAuth parameters.oauth_r   )rK   itemsfind)r   rK   kvr   r   r   get_nonoauth_parameters   s
    z$OAuthRequest.get_nonoauth_parametersr   c             C   sV   d| }| j rNx>| j j D ]0\}}|dd dkr|d|tt|f 7 }qW d|iS )z.Serialize as a header for an HTTPAuth request.zOAuth realm="%s"Nr3   rS   z	, %s="%s"Authorization)rK   rT   r   r   )r   r   auth_headerrV   rW   r   r   r   	to_header   s    zOAuthRequest.to_headerc             C   s   dj dd | jj D S )z*Serialize as post data for a POST request.&c             S   s,   g | ]$\}}d t t|t t|f qS )z%s=%s)r   r   )r!   rV   rW   r   r   r   r#      s   z,OAuthRequest.to_postdata.<locals>.<listcomp>)r$   rK   rT   )r   r   r   r   to_postdata   s    
zOAuthRequest.to_postdatac             C   s   d| j  | j f S )z%Serialize as a URL for a GET request.z%s?%s)get_normalized_http_urlr]   )r   r   r   r   to_url   s    zOAuthRequest.to_urlc          	   C   sP   | j }y
|d= W n   Y nX dd t|j D }|j  djdd |D S )zAReturn a string that contains the parameters that must be signed.oauth_signaturec             S   s(   g | ] \}}t t|t t|fqS r   )r   r   )r!   rV   rW   r   r   r   r#      s   z:OAuthRequest.get_normalized_parameters.<locals>.<listcomp>r\   c             S   s   g | ]\}}d ||f qS )z%s=%sr   )r!   rV   rW   r   r   r   r#      s    )rK   listrT   sortr$   )r   r9   Z
key_valuesr   r   r   get_normalized_parameters   s    
z&OAuthRequest.get_normalized_parametersc             C   s
   | j j S )zUppercases the http method.)rI   upper)r   r   r   r   get_normalized_http_method   s    z'OAuthRequest.get_normalized_http_methodc             C   sx   t jj| j}|dd \}}}|dkrF|d	d dkrF|dd
 }n$|dkrj|dd dkrj|dd }d|||f S )z8Parses the URL and rebuilds it to be scheme://host/path.N   Zhttpz:80Zhttps   z:443z	%s://%s%srh   ri   )r   r   r4   rJ   )r   r5   r6   r7   r8   r   r   r   r^      s    z$OAuthRequest.get_normalized_http_urlc             C   s*   | j d|j  | j d| j||| dS )z=Set the signature parameter to the result of build_signature.oauth_signature_methodr`   N)rN   get_namebuild_signature)r   signature_methodconsumerrD   r   r   r   sign_request   s    
zOAuthRequest.sign_requestc             C   s   |j | ||S )z=Calls the build signature method within the signature method.)rl   )r   rm   rn   rD   r   r   r   rl     s    zOAuthRequest.build_signaturec       
   
   C   s   |dkri }|rjd|krj|d }|dd dkrj|dd }yt j|}|j| W n   tdY nX |rt j|}|j| tjj|d }t j|}	|j|	 |rt | ||S dS )z$Combines multiple parameter sources.NrY   r3   zOAuth z;Unable to parse OAuth parameters from Authorization header.rg   )rH   _split_headerupdater   _split_url_stringr   r   r4   )
rI   rJ   ZheadersrK   Zquery_stringrZ   Zheader_paramsZquery_params	param_strZ
url_paramsr   r   r   from_request	  s(    




zOAuthRequest.from_requestc             C   sp   |si }| j t t tjd}|j| |}|rX|j |d< |jrJ|j|d< |rd||d< n|rd||d< t|||S )N)oauth_consumer_keyrP   rQ   oauth_versionr;   oauth_callbackoauth_verifier)r*   r   r'   rH   versionrq   r.   )oauth_consumerrD   r.   r1   rI   rJ   rK   Zdefaultsr   r   r   from_consumer_and_token-  s"    




z$OAuthRequest.from_consumer_and_tokenc             C   s*   |si }| j |d< |r||d< t|||S )Nr;   rw   )r*   rH   )rD   r.   rI   rJ   rK   r   r   r   from_token_and_callbackK  s    
z$OAuthRequest.from_token_and_callbackc             C   sb   i }| j d}xN|D ]F}|jddkr(q|j }|j dd}tjj|d jd||d < qW |S )z+Turn Authorization: header into parameters.,r      ="r   )splitrU   stripr   r   unquote)headerr9   r5   ZparamZparam_partsr   r   r   rp   X  s    

"zOAuthRequest._split_headerc             C   s<   t j| dd}x(|j D ]\}}tjj|d ||< qW |S )z Turn URL string into parameters.F)r@   r   )rA   rB   rT   r   r   r   )rs   rK   rV   rW   r   r   r   rr   i  s    zOAuthRequest._split_url_string)r   )NNN)r
   r   r   r   rK   HTTP_METHODrI   rJ   VERSIONry   r	   rN   rO   rR   rX   r[   r]   r_   rc   re   r^   ro   rl   rt   rG   r{   r|   rp   rr   r   r   r   r   rH      s@   	

	 
!
rH   c               @   s   e Zd ZdZdZeZdZdZd*ddZ	dd Z
dd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zd+ddZdd Zdd Zdd Zd,d d!Zd"d# Zd$d% Zd&d' Zd(d) ZdS )-OAuthServerzAA worker to check the validity of a request against a data store.i,  Nc             C   s   || _ |pi | _d S )N)
data_storesignature_methods)r   r   r   r   r   r   r	   x  s    zOAuthServer.__init__c             C   s
   || _ d S )N)r   )r   r   r   r   r   set_data_store|  s    zOAuthServer.set_data_storec             C   s   | j S )N)r   )r   r   r   r   get_data_store  s    zOAuthServer.get_data_storec             C   s   || j |j < | j S )N)r   rk   )r   rm   r   r   r   add_signature_method  s    z OAuthServer.add_signature_methodc             C   s   y| j |d}W nl tk
r|   | j|}| j|}y| j|}W n tk
rZ   d}Y nX | j||d | jj||}Y nX |S )z\Processes a request_token request and returns the
        request token on success.
        requestN)
_get_tokenr   _get_version_get_consumerget_callback_check_signaturer   fetch_request_token)r   oauth_requestrD   ry   rn   r.   r   r   r   r     s    


zOAuthServer.fetch_request_tokenc             C   sj   | j |}| j|}y| j|}W n tk
r:   d}Y nX | j|d}| j||| | jj|||}|S )z[Processes an access_token request and returns the
        access token on success.
        Nr   )r   r   _get_verifierr   r   r   r   fetch_access_token)r   r   ry   rn   r1   rD   Z	new_tokenr   r   r   r     s    


zOAuthServer.fetch_access_tokenc             C   s@   | j |}| j|}| j|d}| j||| |j }|||fS )z3Verifies an api call and checks all the parameters.access)r   r   r   r   rX   )r   r   ry   rn   rD   rK   r   r   r   verify_request  s    

zOAuthServer.verify_requestc             C   s   | j j||S )zAuthorize a request token.)r   authorize_request_token)r   rD   userr   r   r   authorize_token  s    zOAuthServer.authorize_tokenc             C   s
   |j dS )zGet the callback URL.rw   )rO   )r   r   r   r   r   r     s    zOAuthServer.get_callbackr   c             C   s   dd| iS )z-Optional support for the authenticate header.zWWW-AuthenticatezOAuth realm="%s"r   )r   r   r   r   r   r     s    z%OAuthServer.build_authenticate_headerc          
   C   sB   y|j d}W n   t}Y nX |r>|| jkr>tdt| |S )z3Verify the correct version request for this server.rv   zOAuth version %s not supported.)rO   r   ry   r   r   )r   r   ry   r   r   r   r     s    
zOAuthServer._get_versionc             C   sd   y|j d}W n   t}Y nX y| j| }W n0   djt| jj }td||f Y nX |S )z,Figure out the signature with some defaults.rj   z, z>Signature method %s not supported try one of the following: %s)rO   SIGNATURE_METHODr   r$   ra   keysr   )r   r   rm   Zsignature_method_namesr   r   r   _get_signature_method  s    

z!OAuthServer._get_signature_methodc             C   s&   |j d}| jj|}|s"td|S )Nru   zInvalid consumer.)rO   r   lookup_consumerr   )r   r   Zconsumer_keyrn   r   r   r   r     s
    
zOAuthServer._get_consumerr   c             C   s0   |j d}| jj||}|s,td||f |S )z9Try to find the token for the provided request token key.r;   zInvalid %s token: %s)rO   r   lookup_tokenr   )r   r   
token_typeZtoken_fieldrD   r   r   r   r     s
    
zOAuthServer._get_tokenc             C   s
   |j dS )Nrx   )rO   )r   r   r   r   r   r     s    zOAuthServer._get_verifierc             C   s   |j  \}}| j| | j||| | j|}y|jd}W n   tdY nX |j||||}|s|j|||\}	}
td|
 |j|||}d S )Nr`   zMissing signature.z5Invalid signature. Expected signature base string: %s)	rR   _check_timestamp_check_noncer   rO   r   check_signaturebuild_signature_base_stringrl   )r   r   rn   rD   	timestampnoncerm   	signatureZ	valid_sigr*   basebuiltr   r   r   r     s     

zOAuthServer._check_signaturec             C   s>   t |}t tj }|| }|| jkr:td||| jf dS )z#Verify that timestamp is recentish.zQExpired timestamp: given %d and now %s has a greater difference than threshold %dN)r   r   timestamp_thresholdr   )r   r   ZnowZlapsedr   r   r   r     s    
zOAuthServer._check_timestampc             C   s(   | j j|||}|r$tdt| dS )z#Verify that the nonce is uniqueish.zNonce already used: %sN)r   lookup_noncer   r   )r   rn   rD   r   r   r   r   r     s    zOAuthServer._check_nonce)NN)r   )r   )r
   r   r   r   r   r   ry   r   r   r	   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   q  s.   




r   c               @   sH   e Zd ZdZdZdZdd Zdd Zdd Zd	d
 Z	dd Z
dd ZdS )OAuthClientz8OAuthClient is a worker to attempt to execute a request.Nc             C   s   || _ || _d S )N)rn   rD   )r   rz   r;   r   r   r   r	     s    zOAuthClient.__init__c             C   s   | j S )N)rn   )r   r   r   r   get_consumer  s    zOAuthClient.get_consumerc             C   s   | j S )N)rD   )r   r   r   r   	get_token  s    zOAuthClient.get_tokenc             C   s   t dS )z-> OAuthToken.N)NotImplementedError)r   r   r   r   r   r   !  s    zOAuthClient.fetch_request_tokenc             C   s   t dS )z-> OAuthToken.N)r   )r   r   r   r   r   r   %  s    zOAuthClient.fetch_access_tokenc             C   s   t dS )z-> Some protected resource.N)r   )r   r   r   r   r   access_resource)  s    zOAuthClient.access_resource)r
   r   r   r   rn   rD   r	   r   r   r   r   r   r   r   r   r   r     s   r   c               @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )OAuthDataStorez;A database abstraction used to lookup consumers and tokens.c             C   s   t dS )z-> OAuthConsumer.N)r   )r   r*   r   r   r   r   1  s    zOAuthDataStore.lookup_consumerc             C   s   t dS )z-> OAuthToken.N)r   )r   rz   r   Ztoken_tokenr   r   r   r   5  s    zOAuthDataStore.lookup_tokenc             C   s   t dS )z-> OAuthToken.N)r   )r   rz   r;   r   r   r   r   r   9  s    zOAuthDataStore.lookup_noncec             C   s   t dS )z-> OAuthToken.N)r   )r   rz   rw   r   r   r   r   =  s    z"OAuthDataStore.fetch_request_tokenc             C   s   t dS )z-> OAuthToken.N)r   )r   rz   r;   rx   r   r   r   r   A  s    z!OAuthDataStore.fetch_access_tokenc             C   s   t dS )z-> OAuthToken.N)r   )r   r;   r   r   r   r   r   E  s    z&OAuthDataStore.authorize_request_tokenN)
r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   .  s   r   c               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )OAuthSignatureMethodz4A strategy class that implements a signature method.c             C   s   t dS )z-> str.N)r   )r   r   r   r   rk   L  s    zOAuthSignatureMethod.get_namec             C   s   t dS )z-> str key, str raw.N)r   )r   r   rz   r;   r   r   r   r   P  s    z0OAuthSignatureMethod.build_signature_base_stringc             C   s   t dS )z-> str.N)r   )r   r   rz   r;   r   r   r   rl   T  s    z$OAuthSignatureMethod.build_signaturec             C   s   | j |||}||kS )N)rl   )r   r   rn   rD   r   r   r   r   r   r   X  s    z$OAuthSignatureMethod.check_signatureN)r
   r   r   r   rk   r   rl   r   r   r   r   r   r   J  s
   r   c               @   s$   e Zd Zdd Zdd Zdd ZdS )OAuthSignatureMethod_HMAC_SHA1c             C   s   dS )Nz	HMAC-SHA1r   )r   r   r   r   rk   _  s    z'OAuthSignatureMethod_HMAC_SHA1.get_namec             C   sT   t |j t |j t |j f}dt |j }|rB|t |j7 }dj|}||fS )Nz%s&r\   )r   re   r^   rc   r+   r$   )r   r   rn   rD   sigr*   rawr   r   r   r   b  s    


z:OAuthSignatureMethod_HMAC_SHA1.build_signature_base_stringc       	   
   C   sh   | j |||\}}yddl}tj|||j}W n"   ddl}tj|||}Y nX tj|j dd S )z!Builds the base signature string.r   Nr~   r   )	r   hashlibhmacnewZsha1shabinasciiZ
b2a_base64Zdigest)	r   r   rn   rD   r*   r   r   Zhashedr   r   r   r   rl   o  s    
z.OAuthSignatureMethod_HMAC_SHA1.build_signatureN)r
   r   r   rk   r   rl   r   r   r   r   r   ]  s   r   c               @   s$   e Zd Zdd Zdd Zdd ZdS )OAuthSignatureMethod_PLAINTEXTc             C   s   dS )Nr   r   )r   r   r   r   rk     s    z'OAuthSignatureMethod_PLAINTEXT.get_namec             C   s(   dt |j }|r |t |j }||fS )z)Concatenates the consumer key and secret.z%s&)r   r+   )r   r   rn   rD   r   r   r   r   r     s    z:OAuthSignatureMethod_PLAINTEXT.build_signature_base_stringc             C   s   | j |||\}}|S )N)r   )r   r   rn   rD   r*   r   r   r   r   rl     s    
z.OAuthSignatureMethod_PLAINTEXT.build_signatureN)r
   r   r   rk   r   rl   r   r   r   r   r     s   r   )r   )r   )r   )r   rA   Zurllib.requestr   Zurllib.parseZurllib.errorr   r   r   r   r   r   r   RuntimeErrorr   r   r   r   r   r'   r(   objectr)   r,   rH   r   r   r   r   r   r   r   r   r   r   <module>   s8   


E V "#