ó
 KMc           @   s"  d  Z  d d l m Z d d l Z d d l Z d d l Z d d l Z e d i  ƒ j Z e j	 e j
 e j e j f \ Z	 Z
 Z Z d d l m Z d d l m Z e Z d d d	 d
 g Z d Z d e j	 f d „  ƒ  YZ d e j f d „  ƒ  YZ d
 e f d „  ƒ  YZ d	 e f d „  ƒ  YZ d S(   s.   OAuth classes for use with lazr.restfulclient.iÿÿÿÿ(   t   SafeConfigParserNs   oauth.oauth(   t   HttpAuthorizer(   t   CredentialsFileErrort   AccessTokent   Consumert   OAuthAuthorizert   SystemWideConsumert   1c           B   s   e  Z d  Z d d d „ Z RS(   s    An OAuth consumer (application).t    c         C   s    t  j |  | | ƒ | |  _ d S(   sn  Initialize

        :param key: The OAuth consumer key
        :param secret: The OAuth consumer secret. Don't use this. It's
            a misfeature, and lazr.restful doesn't expect it.
        :param application_name: An application name, if different
            from the consumer key. If present, this will be used in
            the User-Agent header.
        N(   t   OAuthConsumert   __init__t   application_name(   t   selft   keyt   secretR   (    (    sF   /usr/lib/python2.7/dist-packages/lazr/restfulclient/authorize/oauth.pyR
   9   s    
N(   t   __name__t
   __module__t   __doc__t   NoneR
   (    (    (    sF   /usr/lib/python2.7/dist-packages/lazr/restfulclient/authorize/oauth.pyR   6   s   c           B   s   e  Z d  Z d d d „ Z RS(   s   An OAuth access token.R   c         C   s    t  j |  | | ƒ | |  _ d  S(   N(   t
   OAuthTokenR
   t   context(   R   R   R   R   (    (    sF   /usr/lib/python2.7/dist-packages/lazr/restfulclient/authorize/oauth.pyR
   J   s    N(   R   R   R   R   R
   (    (    (    sF   /usr/lib/python2.7/dist-packages/lazr/restfulclient/authorize/oauth.pyR   G   s   c           B   s/   e  Z d  Z d Z d d „ Z e d „  ƒ Z RS(   sÿ   A consumer associated with the logged-in user rather than an app.

    This can be used to share a single OAuth token among multiple
    desktop applications. The OAuth consumer key will be derived from
    system information (platform and hostname).
    s   System-wide: %s (%s)R   c         C   s#   t  t |  ƒ j |  j | | ƒ d S(   s	  Constructor.

        :param application_name: An application name. This will be
            used in the User-Agent header.
        :param secret: The OAuth consumer secret. Don't use this. It's
            a misfeature, and lazr.restful doesn't expect it.
        N(   t   superR   R
   t   consumer_key(   R   R   R   (    (    sF   /usr/lib/python2.7/dist-packages/lazr/restfulclient/authorize/oauth.pyR
   X   s    c         C   sg   y t  j ƒ  \ } } } Wn t k
 r4 } d } n X| d k rP t  j ƒ  } n  |  j | t j ƒ  f S(   s³   The system-wide OAuth consumer key for this computer.

        This key identifies the platform and the computer's
        hostname. It does not identify the active user.
        R   (   t   platformt   linux_distributiont	   Exceptiont   systemt
   KEY_FORMATt   sockett   gethostname(   R   t   distnamet   versiont
   release_idt   e(    (    sF   /usr/lib/python2.7/dist-packages/lazr/restfulclient/authorize/oauth.pyR   c   s    
(   R   R   R   R   R
   t   propertyR   (    (    (    sF   /usr/lib/python2.7/dist-packages/lazr/restfulclient/authorize/oauth.pyR   O   s   c           B   sh   e  Z d  Z d
 d d
 d d
 d „ Z e d „  ƒ Z d „  Z e d „  ƒ Z	 d „  Z
 d „  Z d	 „  Z RS(   sB   A client that signs every outgoing request with OAuth credentials.R   t   OAuthc         C   sC   d  |  _ | d  k	 r- t | | | ƒ |  _ n  | |  _ | |  _ d  S(   N(   R   t   consumerR   t   access_tokent   oauth_realm(   R   t   consumer_namet   consumer_secretR%   R&   R   (    (    sF   /usr/lib/python2.7/dist-packages/lazr/restfulclient/authorize/oauth.pyR
   z   s    		c         C   sR   i  } |  j  d k r | S|  j  j | d <|  j  j d k	 rN |  j  j | d <n  | S(   so   Any information necessary to identify this user agent.

        In this case, the OAuth consumer name.
        t   oauth_consumert   applicationN(   R$   R   R   R   (   R   t   params(    (    sF   /usr/lib/python2.7/dist-packages/lazr/restfulclient/authorize/oauth.pyt   user_agent_params„   s    c         C   s¨   t  ƒ  } | j | ƒ | j t ƒ s8 t d t ƒ ‚ n  | j t d ƒ } | j t d ƒ } t | | ƒ |  _ | j t d ƒ } | j t d ƒ } t | | ƒ |  _	 d S(   sk  Load credentials from a file-like object.

        This overrides the consumer and access token given in the constructor
        and replaces them with the values read from the file.

        :param readable_file: A file-like object to read the credentials from
        :type readable_file: Any object supporting the file-like `read()`
            method
        s   No configuration for version %sR   R(   R%   t   access_secretN(
   R    t   readfpt   has_sectiont   CREDENTIALS_FILE_VERSIONR   t   getR   R$   R   R%   (   R   t   readable_filet   parserR   R(   R%   R-   (    (    sF   /usr/lib/python2.7/dist-packages/lazr/restfulclient/authorize/oauth.pyt   load’   s    	c         C   s3   |  ƒ  } t  | d ƒ } | j | ƒ | j ƒ  | S(   s™  Convenience method for loading credentials from a file.

        Open the file, create the Credentials and load from the file,
        and finally close the file and return the newly created
        Credentials instance.

        :param path: In which file the credential file should be saved.
        :type path: string
        :return: The loaded Credentials instance.
        :rtype: `Credentials`
        t   r(   t   openR4   t   close(   t   clst   patht   credentialst   credentials_file(    (    sF   /usr/lib/python2.7/dist-packages/lazr/restfulclient/authorize/oauth.pyt   load_from_path¯   s
    	
c         C   sÇ   |  j  d k r t d ƒ ‚ n  |  j d k r< t d ƒ ‚ n  t ƒ  } | j t ƒ | j t d |  j  j ƒ | j t d |  j  j	 ƒ | j t d |  j j ƒ | j t d |  j j	 ƒ | j
 | ƒ d S(   sH  Write the credentials to the file-like object.

        :param writable_file: A file-like object to write the credentials to
        :type writable_file: Any object supporting the file-like `write()`
            method
        :raise CredentialsFileError: when there is either no consumer or no
            access token
        s   No consumers   No access tokenR   R(   R%   R-   N(   R$   R   R   R%   R    t   add_sectionR0   t   setR   R   t   write(   R   t   writable_fileR3   (    (    sF   /usr/lib/python2.7/dist-packages/lazr/restfulclient/authorize/oauth.pyt   saveÂ   s    						c         C   sW   t  j t  j | t  j t  j Bt  j Bt j t j Bƒ d ƒ } |  j	 | ƒ | j
 ƒ  d S(   sT  Convenience method for saving credentials to a file.

        Create the file, call self.save(), and close the
        file. Existing files are overwritten. The resulting file will
        be readable and writable only by the user.

        :param path: In which file the credential file should be saved.
        :type path: string
        t   wN(   t   ost   fdopenR6   t   O_CREATt   O_TRUNCt   O_WRONLYt   statt   S_IREADt   S_IWRITERA   R7   (   R   R9   R;   (    (    sF   /usr/lib/python2.7/dist-packages/lazr/restfulclient/authorize/oauth.pyt   save_to_pathÜ   s
    
c         C   sW   t  j |  j |  j d | ƒ} | j t ƒ  |  j |  j ƒ | j | j |  j ƒ ƒ d S(   s&   Sign a request with OAuth credentials.t   http_urlN(	   t   OAuthRequestt   from_consumer_and_tokenR$   R%   t   sign_requestt   OAuthSignatureMethod_PLAINTEXTt   updatet	   to_headerR&   (   R   t   absolute_urit   methodt   bodyt   headerst   oauth_request(    (    sF   /usr/lib/python2.7/dist-packages/lazr/restfulclient/authorize/oauth.pyt   authorizeRequestì   s    N(   R   R   R   R   R
   R"   R,   R4   t   classmethodR<   RA   RK   RX   (    (    (    sF   /usr/lib/python2.7/dist-packages/lazr/restfulclient/authorize/oauth.pyR   w   s   			(   R   t   ConfigParserR    RC   R   RH   R   t
   __import__t   oauthR	   RM   RP   R   t   lazr.restfulclient.authorizeR   t   lazr.restfulclient.errorsR   t   typet   __metaclass__t   __all__R0   R   R   R   R   (    (    (    sF   /usr/lib/python2.7/dist-packages/lazr/restfulclient/authorize/oauth.pyt   <module>   s(   	(