U
    5d                     @   sl   d Z ddlZddlZddlmZmZ ddlmZ 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.
    N)quoteurlparse)	urldecodec                 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  str.0sr   r   A/tmp/pip-unpacked-wheel-g6yohpa1/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   )uri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.
    80443)HTTPHTTPS:   )r   r   getupper)	r   Zdefault_portsZschnetlocpathparr   Zfraportr   r   r   host_from_uri)   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   ~)safe)r   r   r   r   encode)ur   r   r   escape<   s    
r0   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 )datetimenowmicrosecondssecondsdaysr   )Z
issue_timetdZager   r   r   generate_ageG   s    r8   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_transportO   s    r=   )__doc__r2   r9   urllib.parser   r   Zoauthlib.commonr   r   r   r   r+   r0   r8   r=   r   r   r   r   <module>   s   

