U
    Ž“I]¾	  ã                   @   sÔ   d Z ddlmZmZ ddlZddlZddlmZmZ zddl	m
Z
 W n  ek
rd   ddlm
Z
 Y nX zddlmZ W n  ek
r–   ddlmZ Y nX dd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )zp
oauthlib.utils
~~~~~~~~~~~~~~

This module contains utility methods used by various parts of the OAuth 2 spec.
é    )Úabsolute_importÚunicode_literalsN)Úunicode_typeÚ	urldecode)Úquote)Úurlparsec                 C   sJ   t | tƒs| dkr| S t | tttfƒr:d dd„ | D ƒ¡S td|  ƒ‚dS )z5Convert a list of scopes to a space separated string.Nú c                 S   s   g | ]}t |ƒ‘qS © ©r   ©Ú.0Úsr	   r	   ú?/usr/lib/python3/dist-packages/oauthlib/oauth2/rfc6749/utils.pyÚ
<listcomp>   s     z!list_to_scope.<locals>.<listcomp>z8Invalid scope (%s), must be string, tuple, set, or list.)Ú
isinstancer   ÚsetÚtupleÚlistÚjoinÚ
ValueError©Úscoper	   r	   r   Úlist_to_scope   s
    r   c                 C   s<   t | tttfƒrdd„ | D ƒS | dkr*dS |  ¡  d¡S dS )z5Convert a space separated string to a list of scopes.c                 S   s   g | ]}t |ƒ‘qS r	   r
   r   r	   r	   r   r   &   s     z!scope_to_list.<locals>.<listcomp>Nr   )r   r   r   r   ÚstripÚsplitr   r	   r	   r   Úscope_to_list#   s
    r   c                 C   s.   t tt| ƒjƒƒ}d|kr*t|d ƒ|d< |S )Nr   )Údictr   r   Úqueryr   )ÚuriZparamsr	   r	   r   Úparams_from_uri-   s    r   c           	      C   sN   dddœ}t | ƒ\}}}}}}d|kr8| dd¡\}}n| | ¡ ¡}||fS )zuExtract hostname and port from URI.

    Will use default port for HTTP and HTTPS if none is present in the URI.
    Z80Z443)ZHTTPZHTTPSú:é   )r   r   ÚgetÚupper)	r   Zdefault_portsZschZnetlocÚpathZparr   ZfraZportr	   r	   r   Úhost_from_uri4   s    þr%   c                 C   s$   t | tƒstdƒ‚t|  d¡ddS )ztEscape a string in an OAuth-compatible fashion.

    TODO: verify whether this can in fact be used for OAuth 2

    z#Only unicode objects are escapable.zutf-8ó   ~)Zsafe)r   r   r   r   Úencode)Úur	   r	   r   ÚescapeG   s    
r)   c                 C   s8   t j  ¡ |  }|j|j|jd d  d  d }t|ƒS )z9Generate a age parameter for MAC authentication draft 00.é   i  i@B )ÚdatetimeZnowZmicrosecondsZsecondsZdaysr   )Z
issue_timeZtdZager	   r	   r   Úgenerate_ageR   s    ÿÿr,   c                 C   s   t j d¡rdS |  ¡  d¡S )zCheck if the uri is over ssl.ZOAUTHLIB_INSECURE_TRANSPORTTzhttps://)ÚosÚenvironr"   ÚlowerÚ
startswith)r   r	   r	   r   Úis_secure_transportZ   s    r1   )Ú__doc__Z
__future__r   r   r+   r-   Zoauthlib.commonr   r   Zurllibr   ÚImportErrorZurllib.parser   r   r   r   r%   r)   r,   r1   r	   r	   r	   r   Ú<module>   s&   

