o
    .^V(                     @   s   d Z zddlmZ W n ey   ddlmZ Y nw ddlZddlZddlZddlZddl	m
Z
 ddl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 dZG dd dZG dd dejZG dd deZG dd deZdS )z.OAuth classes for use with lazr.restfulclient.    )ConfigParser)SafeConfigParserN)oauth1)parse_qs	urlencode)HttpAuthorizer)CredentialsFileError)AccessTokenConsumerOAuthAuthorizerSystemWideConsumer1c                   @   s   e Zd ZdZdddZdS )r
   z An OAuth consumer (application). Nc                 C   s   || _ || _|| _dS )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    r   D/usr/lib/python3/dist-packages/lazr/restfulclient/authorize/oauth.py__init__;   s   

zConsumer.__init__r   N)__name__
__module____qualname____doc__r   r   r   r   r   r
   8   s    r
   c                   @   s2   e Zd ZdZd
ddZdd ZeZedd	 ZdS )r	   zAn OAuth access token.r   Nc                 C   s   || _ || _|| _d S N)r   r   context)r   r   r   r   r   r   r   r   M   s   
zAccessToken.__init__c                 C   s   t d| jfd| jfgS )Noauth_token_secretoauth_token)r   r   r   r   r   r   r   	to_stringR   s   zAccessToken.to_stringc                 C   s.   t |dd}|d d }|d d }| ||S )NF)Zkeep_blank_valuesr   r   r   )r   )clssparamsr   r   r   r   r   from_stringZ   s   
zAccessToken.from_stringr   )	r   r   r   r   r   r    __str__classmethodr$   r   r   r   r   r	   J   s    
r	   c                   @   s   e Zd ZdZdd ZeZdS )TruthyStringz&A Unicode string which is always true.c                 C   s   dS )NTr   r   r   r   r   __bool__e   s   zTruthyString.__bool__N)r   r   r   r   r(   Z__nonzero__r   r   r   r   r'   b   s    r'   c                       s2   e Zd ZdZdZd fdd	Zedd Z  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)r   c                    s   t t| | j|| dS )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__r   r   r   t   s   
zSystemWideConsumer.__init__c                 C   sN   z
ddl }| }W n ty   d}Y nw |dkrt }| j|t f S )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socketZgethostname)r   r-   Zdistnamer   r   r   r*      s   zSystemWideConsumer.consumer_key)r   )	r   r   r   r   r2   r   propertyr*   __classcell__r   r   r+   r   r   k   s    r   c                   @   sX   e Zd ZdZ			dddZedd Zd	d
 Zedd Z	dd Z
dd Zdd ZdS )r   zBA client that signs every outgoing request with OAuth credentials.Nr   OAuthc                 C   s,   d | _ |d urt|||| _ || _|| _d S r   )consumerr
   access_tokenoauth_realm)r   Zconsumer_nameconsumer_secretr8   r9   r   r   r   r   r      s   
zOAuthAuthorizer.__init__c                 C   s:   i }| j du r	|S | j j|d< | j jdur| j j|d< |S )zoAny information necessary to identify this user agent.

        In this case, the OAuth consumer name.
        NZoauth_consumerZapplication)r7   r   r   )r   r#   r   r   r   user_agent_params   s   
z!OAuthAuthorizer.user_agent_paramsc                 C   s~   t  }t|d|j}|| |tstdt |td}|td}t||| _|td}|td}t	||| _
dS )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
        Z	read_filezNo configuration for version %sr*   r:   r8   access_secretN)r   getattrZreadfpZhas_sectionCREDENTIALS_FILE_VERSIONr   getr
   r7   r	   r8   )r   Zreadable_fileparserreaderr*   r:   r8   r<   r   r   r   load   s*   
zOAuthAuthorizer.loadc                 C   s&   |  }t |d}|| |  |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)openrB   close)r!   pathZcredentialscredentials_filer   r   r   load_from_path   s
   

zOAuthAuthorizer.load_from_pathc                 C   s   | j du r	td| jdu rtdt }|t |td| j j |td| j j |td| jj |td| jj |	| dS )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 tokenr*   r:   r8   r<   )
r7   r   r8   r   Zadd_sectionr>   setr   r   write)r   Zwritable_filer@   r   r   r   save   s&   
	

zOAuthAuthorizer.savec                 C   sB   t t |t jt jB t jB tjtjB d}| 	| |
  dS )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fdopenrD   O_CREATO_TRUNCO_WRONLYstatS_IREADS_IWRITErK   rE   )r   rF   rG   r   r   r   save_to_path   s   


zOAuthAuthorizer.save_to_pathc           
      C   s   t j| jj| jjt| jjpd| jjt j| jd}t|j	|_	|
|\}}}| D ]\}}	tjr<|d}|	d}	|	||< q+dS )z&Sign a request with OAuth credentials.r   )Zclient_secretresource_owner_keyZresource_owner_secretZsignature_methodZrealmzUTF-8N)r   ZClientr7   r   r   r'   r8   ZSIGNATURE_PLAINTEXTr9   rV   signitemssixZPY2encode)
r   Zabsolute_urimethodbodyZheadersZclient_Zsigned_headersr   valuer   r   r   authorizeRequest  s    


z OAuthAuthorizer.authorizeRequest)Nr   Nr6   N)r   r   r   r   r   r4   r;   rB   r&   rH   rK   rU   r_   r   r   r   r   r      s    



r   )r   configparserr   r   ImportErrorrM   r0   rR   r3   Zoauthlibr   rY   Zsix.moves.urllib.parser   r   Zlazr.restfulclient.authorizer   Zlazr.restfulclient.errorsr   typeZ__metaclass____all__r>   r
   r	   Z	text_typer'   r   r   r   r   r   r   <module>   s.   	'