1""" 
    2oauthlib.utils 
    3~~~~~~~~~~~~~~ 
    4 
    5This module contains utility methods used by various parts of the OAuth 2 spec. 
    6""" 
    7import datetime 
    8import os 
    9from urllib.parse import quote, urlparse 
    10 
    11from oauthlib.common import urldecode 
    12 
    13 
    14def list_to_scope(scope): 
    15    """Convert a list of scopes to a space separated string.""" 
    16    if isinstance(scope, str) or scope is None: 
    17        return scope 
    18    elif isinstance(scope, (set, tuple, list)): 
    19        return " ".join([str(s) for s in scope]) 
    20    else: 
    21        raise ValueError("Invalid scope (%s), must be string, tuple, set, or list." % scope) 
    22 
    23 
    24def scope_to_list(scope): 
    25    """Convert a space separated string to a list of scopes.""" 
    26    if isinstance(scope, (tuple, list, set)): 
    27        return [str(s) for s in scope] 
    28    elif scope is None: 
    29        return None 
    30    else: 
    31        return scope.strip().split(" ") 
    32 
    33 
    34def params_from_uri(uri): 
    35    params = dict(urldecode(urlparse(uri).query)) 
    36    if 'scope' in params: 
    37        params['scope'] = scope_to_list(params['scope']) 
    38    return params 
    39 
    40 
    41def host_from_uri(uri): 
    42    """Extract hostname and port from URI. 
    43 
    44    Will use default port for HTTP and HTTPS if none is present in the URI. 
    45    """ 
    46    default_ports = { 
    47        'HTTP': '80', 
    48        'HTTPS': '443', 
    49    } 
    50 
    51    sch, netloc, path, par, query, fra = urlparse(uri) 
    52    if ':' in netloc: 
    53        netloc, port = netloc.split(':', 1) 
    54    else: 
    55        port = default_ports.get(sch.upper()) 
    56 
    57    return netloc, port 
    58 
    59 
    60def escape(u): 
    61    """Escape a string in an OAuth-compatible fashion. 
    62 
    63    TODO: verify whether this can in fact be used for OAuth 2 
    64 
    65    """ 
    66    if not isinstance(u, str): 
    67        raise ValueError('Only unicode objects are escapable.') 
    68    return quote(u.encode('utf-8'), safe=b'~') 
    69 
    70 
    71def generate_age(issue_time): 
    72    """Generate a age parameter for MAC authentication draft 00.""" 
    73    td = datetime.datetime.now() - issue_time 
    74    age = (td.microseconds + (td.seconds + td.days * 24 * 3600) 
    75           * 10 ** 6) / 10 ** 6 
    76    return str(age) 
    77 
    78 
    79def is_secure_transport(uri): 
    80    """Check if the uri is over ssl.""" 
    81    if os.environ.get('OAUTHLIB_INSECURE_TRANSPORT'): 
    82        return True 
    83    return uri.lower().startswith('https://')