Skip to content

utils

oauthlib.utils ~~~~~~~~~~~~~~

This module contains utility methods used by various parts of the OAuth 2 spec.

escape(u)

Escape a string in an OAuth-compatible fashion.

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

Source code in server/vendor/oauthlib/oauth2/rfc6749/utils.py
60
61
62
63
64
65
66
67
68
def escape(u):
    """Escape a string in an OAuth-compatible fashion.

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

    """
    if not isinstance(u, str):
        raise ValueError('Only unicode objects are escapable.')
    return quote(u.encode('utf-8'), safe=b'~')

generate_age(issue_time)

Generate a age parameter for MAC authentication draft 00.

Source code in server/vendor/oauthlib/oauth2/rfc6749/utils.py
71
72
73
74
75
76
def generate_age(issue_time):
    """Generate a age parameter for MAC authentication draft 00."""
    td = datetime.datetime.now() - issue_time
    age = (td.microseconds + (td.seconds + td.days * 24 * 3600)
           * 10 ** 6) / 10 ** 6
    return str(age)

host_from_uri(uri)

Extract hostname and port from URI.

Will use default port for HTTP and HTTPS if none is present in the URI.

Source code in server/vendor/oauthlib/oauth2/rfc6749/utils.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def host_from_uri(uri):
    """Extract hostname and port from URI.

    Will use default port for HTTP and HTTPS if none is present in the URI.
    """
    default_ports = {
        'HTTP': '80',
        'HTTPS': '443',
    }

    sch, netloc, path, par, query, fra = urlparse(uri)
    if ':' in netloc:
        netloc, port = netloc.split(':', 1)
    else:
        port = default_ports.get(sch.upper())

    return netloc, port

is_secure_transport(uri)

Check if the uri is over ssl.

Source code in server/vendor/oauthlib/oauth2/rfc6749/utils.py
79
80
81
82
83
def is_secure_transport(uri):
    """Check if the uri is over ssl."""
    if os.environ.get('OAUTHLIB_INSECURE_TRANSPORT'):
        return True
    return uri.lower().startswith('https://')

list_to_scope(scope)

Convert a list of scopes to a space separated string.

Source code in server/vendor/oauthlib/oauth2/rfc6749/utils.py
14
15
16
17
18
19
20
21
def list_to_scope(scope):
    """Convert a list of scopes to a space separated string."""
    if isinstance(scope, str) or scope is None:
        return scope
    elif isinstance(scope, (set, tuple, list)):
        return " ".join([str(s) for s in scope])
    else:
        raise ValueError("Invalid scope (%s), must be string, tuple, set, or list." % scope)

scope_to_list(scope)

Convert a space separated string to a list of scopes.

Source code in server/vendor/oauthlib/oauth2/rfc6749/utils.py
24
25
26
27
28
29
30
31
def scope_to_list(scope):
    """Convert a space separated string to a list of scopes."""
    if isinstance(scope, (tuple, list, set)):
        return [str(s) for s in scope]
    elif scope is None:
        return None
    else:
        return scope.strip().split(" ")