
    !e'                        d Z 	 ddlm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 ddlmZ ddlmZ eZg d	Zd
Z G d d      Z G d d      Z G d de      Z G d de      Z G d de      Zy# e$ r	 ddlmZ Y uw xY w)z.OAuth classes for use with lazr.restfulclient.    )ConfigParser)SafeConfigParserN)oauth1)parse_qs	urlencode)HttpAuthorizer)CredentialsFileError)AccessTokenConsumerOAuthAuthorizerSystemWideConsumer1c                       e Zd ZdZddZy)r   z An OAuth consumer (application).Nc                 .    || _         || _        || _        y)an  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)keysecretapplication_name)selfr   r   r   s       D/usr/lib/python3/dist-packages/lazr/restfulclient/authorize/oauth.py__init__zConsumer.__init__8   s      0     N)__name__
__module____qualname____doc__r    r   r   r   r   5   s
    *1r   r   c                   2    e Zd ZdZddZd ZeZed        Zy)r
   zAn OAuth access token.Nc                 .    || _         || _        || _        y N)r   r   context)r   r   r   r"   s       r   r   zAccessToken.__init__J   s    r   c                 L    t        d| j                  fd| j                  fg      S )Noauth_token_secretoauth_token)r   r   r   r   s    r   	to_stringzAccessToken.to_stringO   s,    %t{{3)
 	
r   c                 N    t        |d      }|d   d   }|d   d   } | ||      S )NF)keep_blank_valuesr%   r   r$   )r   )clssparamsr   r   s        r   from_stringzAccessToken.from_stringY   s:    !u5]#A&,-a03r   r   )	r   r   r   r   r   r'   __str__classmethodr-   r   r   r   r
   r
   G   s(     

 G   r   r
   c                       e Zd ZdZd ZeZy)TruthyStringz&A Unicode string which is always true.c                      y)NTr   r&   s    r   __bool__zTruthyString.__bool__d   s    r   N)r   r   r   r   r3   __nonzero__r   r   r   r1   r1   a   s    0 Kr   r1   c                   8     e Zd ZdZdZd fd	Zed        Z xZS )r   zA 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).
    zSystem-wide: %s (%s)c                 D    t         t        |   | j                  ||       y)a	  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)superr   r   consumer_key)r   r   r   	__class__s      r   r   zSystemWideConsumer.__init__t   s#     	 $0v'7	
r   c                     	 ddl }|j                         }|dk(  rt        j                         }| j
                  |t        j                         fz  S # t        $ r d}Y Iw xY w)zThe 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   Nr   )distroname	Exceptionplatformsystem
KEY_FORMATsocketgethostname)r   r;   distnames      r   r8   zSystemWideConsumer.consumer_key   s_    	{{}H
 r>(H(F,>,>,@!AAA  	 H	s   A A A )r   )	r   r   r   r   r@   r   propertyr8   __classcell__)r9   s   @r   r   r   j   s*     (J

 B Br   r   c                   Z    e Zd ZdZ	 	 	 	 	 d
dZed        Zd Zed        Z	d Z
d Zd	 Zy)r   zBA client that signs every outgoing request with OAuth credentials.Nc                 V    d | _         |t        |||      | _         || _        || _        y r!   )consumerr   access_tokenoauth_realm)r   consumer_nameconsumer_secretrI   rJ   r   s         r   r   zOAuthAuthorizer.__init__   s7     $$0@DM )&r   c                     i }| j                   |S | j                   j                  |d<   | j                   j                  | j                   j                  |d<   |S )zoAny information necessary to identify this user agent.

        In this case, the OAuth consumer name.
        oauth_consumerapplication)rH   r   r   )r   r,   s     r   user_agent_paramsz!OAuthAuthorizer.user_agent_params   sV     == M#'==#4#4 ==))5$(MM$B$BF=!r   c                    t               }t        |d      r|j                  }n|j                  } ||       |j	                  t
              st        dt
        z        |j                  t
        d      }|j                  t
        d      }t        ||      | _	        |j                  t
        d      }|j                  t
        d      }t        ||      | _        y)ak  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
        	read_filezNo configuration for version %sr8   rL   rI   access_secretN)r   hasattrrR   readfphas_sectionCREDENTIALS_FILE_VERSIONr	   getr   rH   r
   rI   )r   readable_fileparserreaderr8   rL   rI   rS   s           r   loadzOAuthAuthorizer.load   s     "#6;'%%F]]F} !!":;&14LL  zz":NK **$&7
 !?zz":NK

#;_M'mDr   c                 n     |        }t        |d      }|j                  |       |j                          |S )a  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`
        r)openr\   close)r*   pathcredentialscredentials_files       r   load_from_pathzOAuthAuthorizer.load_from_path   s8     ec?)* r   c                    | j                   t        d      | j                  t        d      t               }|j	                  t
               |j                  t
        d| j                   j                         |j                  t
        d| j                   j                         |j                  t
        d| j                  j                         |j                  t
        d| j                  j                         |j                  |       y)aH  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
        NzNo consumerzNo access tokenr8   rL   rI   rS   )
rH   r	   rI   r   add_sectionrW   setr   r   write)r   writable_filerZ   s      r   savezOAuthAuthorizer.save   s     == &}55$&'899!#34

+^T]]=N=NO

$&79M9M	
 	

$nd6G6G6K6K	
 	

$ot7H7H7O7O	
 	]#r   c                 :   t        j                  t        j                  |t         j                  t         j                  z  t         j
                  z  t        j                  t        j                  z        d      }| j                  |       |j                          y)aT  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
        wN)osfdopenr_   O_CREATO_TRUNCO_WRONLYstatS_IREADS_IWRITErj   r`   )r   ra   rc   s      r   save_to_pathzOAuthAuthorizer.save_to_path  sk     99GGbjj(2;;6-
 
 			"# r   c                    t        j                  | j                  j                  | j                  j                  t        | j                  j                  xs d      | j                  j                  t         j                  | j                        }t        |j                        |_	        |j                  |      \  }}}|j                         D ]
  \  }}	|	||<    y)z&Sign a request with OAuth credentials.r   )client_secretresource_owner_keyresource_owner_secretsignature_methodrealmN)r   ClientrH   r   r   r1   rI   SIGNATURE_PLAINTEXTrJ   rx   signitems)
r   absolute_urimethodbodyheadersclient_signed_headersr   values
             r   authorizeRequestz OAuthAuthorizer.authorizeRequest  s    MM--..+D,=,=,A,A,GRH"&"3"3":":#77""
 %11J1J$K!%{{<8>1(..0 	!JC GCL	!r   )Nr   NOAuthN)r   r   r   r   r   rD   rP   r\   r/   rd   rj   ru   r   r   r   r   r   r      sY    L '   E@  $$8!*!r   r   )r   configparserr   r   ImportErrorrm   r>   rA   rr   oauthlibr   urllib.parser   r   lazr.restfulclient.authorizer   lazr.restfulclient.errorsr	   type__metaclass____all__rW   r   r
   strr1   r   r   r   r   r   <module>r      s   & 5.= 
     , 7 :  
1 1$   43 'B 'BTV!n V!w  .-.s   A2 2B ?B 