Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / oauthlib / oauth2 / rfc6749 / utils.py @ 564

History | View | Annotate | Download (2.43 KB)

1
# -*- coding: utf-8 -*-
2
"""
3
oauthlib.utils
4
~~~~~~~~~~~~~~
5

6
This module contains utility methods used by various parts of the OAuth 2 spec.
7
"""
8
from __future__ import absolute_import, unicode_literals
9

    
10
import os
11
import datetime
12
try:
13
    from urllib import quote
14
except ImportError:
15
    from urllib.parse import quote
16
try:
17
    from urlparse import urlparse
18
except ImportError:
19
    from urllib.parse import urlparse
20
from oauthlib.common import unicode_type, urldecode
21

    
22

    
23
def list_to_scope(scope):
24
    """Convert a list of scopes to a space separated string."""
25
    if isinstance(scope, unicode_type) or scope is None:
26
        return scope
27
    elif isinstance(scope, (set, tuple, list)):
28
        return " ".join([unicode_type(s) for s in scope])
29
    else:
30
        raise ValueError("Invalid scope (%s), must be string, tuple, set, or list." % scope)
31

    
32

    
33
def scope_to_list(scope):
34
    """Convert a space separated string to a list of scopes."""
35
    if isinstance(scope, (tuple, list, set)):
36
        return [unicode_type(s) for s in scope]
37
    elif scope is None:
38
        return None
39
    else:
40
        return scope.strip().split(" ")
41

    
42

    
43
def params_from_uri(uri):
44
    params = dict(urldecode(urlparse(uri).query))
45
    if 'scope' in params:
46
        params['scope'] = scope_to_list(params['scope'])
47
    return params
48

    
49

    
50
def host_from_uri(uri):
51
    """Extract hostname and port from URI.
52

53
    Will use default port for HTTP and HTTPS if none is present in the URI.
54
    """
55
    default_ports = {
56
        'HTTP': '80',
57
        'HTTPS': '443',
58
    }
59

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

    
66
    return netloc, port
67

    
68

    
69
def escape(u):
70
    """Escape a string in an OAuth-compatible fashion.
71

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

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

    
79

    
80
def generate_age(issue_time):
81
    """Generate a age parameter for MAC authentication draft 00."""
82
    td = datetime.datetime.now() - issue_time
83
    age = (td.microseconds + (td.seconds + td.days * 24 * 3600)
84
           * 10 ** 6) / 10 ** 6
85
    return unicode_type(age)
86

    
87

    
88
def is_secure_transport(uri):
89
    """Check if the uri is over ssl."""
90
    if os.environ.get('OAUTHLIB_INSECURE_TRANSPORT'):
91
        return True
92
    return uri.lower().startswith('https://')