U
    [eV(                     @   s   d Z zddlmZ W n  ek
r4   ddlmZ Y nX 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d	d
ddg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/var/www/html/services/stratfitenv/lib/python3.8/site-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   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_string)r   N)	r   r   r   r   r   r   __str__classmethodr$   r   r   r   r   r	   J   s   
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   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   sP   zddl }| }W n tk
r,   d}Y nX |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socketgethostname)r   r.   distnamer   r   r   r+      s    
zSystemWideConsumer.consumer_key)r   )	r   r   r   r   r3   r   propertyr+   __classcell__r   r   r,   r   r   k   s
   c                   @   sR   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 k	rt|||| _ || _|| _d S r   )consumerr
   access_tokenoauth_realm)r   Zconsumer_nameconsumer_secretr;   r<   r   r   r   r   r      s      zOAuthAuthorizer.__init__c                 C   s:   i }| j dkr|S | j j|d< | j jdk	r6| j j|d< |S )zoAny information necessary to identify this user agent.

        In this case, the OAuth consumer name.
        NZoauth_consumerZ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s2t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   Zreadable_fileparserreaderr+   r=   r;   r@   r   r   r   load   s2    
    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)openrH   close)r!   pathcredentialscredentials_filer   r   r   load_from_path   s
    

zOAuthAuthorizer.load_from_pathc                 C   s   | j dkrtd| jdk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;   r@   )
r:   r   r;   r   add_sectionrD   setr   r   write)r   Zwritable_filerF   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fdopenrJ   O_CREATO_TRUNCO_WRONLYstatS_IREADS_IWRITErS   rK   )r   rL   rN   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rx|d}|	d}	|	||< qVdS )z&Sign a request with OAuth credentials.r   )Zclient_secretresource_owner_keyZresource_owner_secretZsignature_methodrealmzUTF-8N)r   ZClientr:   r   r   r'   r;   ZSIGNATURE_PLAINTEXTr<   r^   signitemssixPY2encode)
r   Zabsolute_urimethodbodyheadersclient_Zsigned_headersr   valuer   r   r   authorizeRequest  s    

z OAuthAuthorizer.authorizeRequest)Nr   Nr9   N)r   r   r   r   r   r7   r>   rH   r&   rO   rS   r]   rk   r   r   r   r   r      s        



)r   configparserr   r   ImportErrorrU   r1   rZ   r4   Zoauthlibr   rb   Zsix.moves.urllib.parser   r   Zlazr.restfulclient.authorizer   Zlazr.restfulclient.errorsr   typeZ__metaclass____all__rD   r
   r	   	text_typer'   r   r   r   r   r   r   <module>   s2   	'