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)Ú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)   Ú__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   zÿ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).
    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 )z³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   Nr   )ÚdistroÚnameÚ	ExceptionÚplatformÚsystemÚ
KEY_FORMATÚsocketÚgethostname)r   r/   Údistnamer   r   r   r,      s   ýzSystemWideConsumer.consumer_key)r   )	r   r   r   r   r4   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   Úconsumer_nameÚconsumer_secretr<   r=   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.
        NÚoauth_consumerÚapplication)r;   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
        Ú	read_filezNo configuration for version %sr,   r?   r<   Úaccess_secretN)r   ÚgetattrÚreadfpÚhas_sectionÚCREDENTIALS_FILE_VERSIONr   Úgetr
   r;   r	   r<   )r   Úreadable_fileÚparserÚreaderr,   r?   r<   rD   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)ÚopenrM   Úclose)r"   ÚpathÚ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?   r<   rD   )
r;   r   r<   r   Úadd_sectionrH   Úsetr   r   Úwrite)r   Úwritable_filerK   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ÚfdopenrO   ÚO_CREATÚO_TRUNCÚO_WRONLYÚstatÚS_IREADÚS_IWRITErY   rP   )r   rQ   rS   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   )Úclient_secretÚresource_owner_keyÚresource_owner_secretÚsignature_methodÚrealmzUTF-8N)r   ÚClientr;   r   r   r(   r<   ÚSIGNATURE_PLAINTEXTr=   re   ÚsignÚitemsÚsixÚPY2Úencode)
r   Úabsolute_uriÚmethodÚbodyÚheadersÚclientÚ_Úsigned_headersr   Úvaluer   r   r   ÚauthorizeRequest  s    ú


úz OAuthAuthorizer.authorizeRequest)Nr   Nr:   N)r   r   r   r   r   r8   rB   rM   r'   rT   rY   rc   rx   r   r   r   r   r   ’   s    
þ


r   )r   Úconfigparserr   r   ÚImportErrorr[   r2   r`   r5   Úoauthlibr   rm   Úsix.moves.urllib.parser   r   Úlazr.restfulclient.authorizer   Úlazr.restfulclient.errorsr   ÚtypeÚ__metaclass__Ú__all__rH   r
   r	   Ú	text_typer(   r   r   r   r   r   r   Ú<module>   s.   ÿ	'