示例#1
0
文件: hook.py 项目: PyYoshi/Pytumb2
    def get_normalized_parameters(request):
        """
        Returns a string that contains the parameters that must be signed. 
        This function is called by SignatureMethod subclass CustomSignatureMethod_HMAC_SHA1 
        """
        # See issues #10 and #12
        if ('Content-Type' not in request.headers or \
            request.headers.get('Content-Type') == 'application/x-www-form-urlencoded') \
            and not isinstance(request.data, basestring):

            data_and_params = dict(request.data.items() + request.params.items())

            for key,value in data_and_params.items():
                request.data_and_params[to_utf8(key)] = to_utf8(value)

        if request.data_and_params.has_key('oauth_signature'):
            del request.data_and_params['oauth_signature']

        items = []
        for key, value in request.data_and_params.iteritems():
            # 1.0a/9.1.1 states that kvp must be sorted by key, then by value,
            # so we unpack sequence values into multiple items for sorting.
            if isinstance(value, basestring):
                items.append((key, value))
            else:
                try:
                    value = list(value)
                except TypeError, e:
                    assert 'is not iterable' in str(e)
                    items.append((key, value))
                else:
                    items.extend((key, item) for item in value)
示例#2
0
    def get_normalized_parameters(request):
        """
        Returns a string that contains the parameters that must be signed.
        This function is called by SignatureMethod subclass CustomSignatureMethod_HMAC_SHA1
        """
        # See issues #10 and #12
        if ('Content-Type' not in request.headers or \
            request.headers.get('Content-Type').startswith('application/x-www-form-urlencoded')) \
            and not isinstance(request.data, basestring):
            data_and_params = dict(request.data.items() +
                                   request.params.items())

            for key, value in data_and_params.items():
                request.data_and_params[to_utf8(key)] = to_utf8(value)

        if request.data_and_params.has_key('oauth_signature'):
            del request.data_and_params['oauth_signature']

        items = []
        for key, value in request.data_and_params.iteritems():
            # 1.0a/9.1.1 states that kvp must be sorted by key, then by value,
            # so we unpack sequence values into multiple items for sorting.
            if isinstance(value, basestring):
                items.append((key, value))
            else:
                try:
                    value = list(value)
                except TypeError, e:
                    assert 'is not iterable' in str(e)
                    items.append((key, value))
                else:
                    items.extend((key, item) for item in value)
示例#3
0
 def _split_url_string(query_string):
     """
     Turns a `query_string` into a Python dictionary with unquoted values
     """
     parameters = parse_qs(to_utf8(query_string), keep_blank_values=True)
     for key, value in iteritems(request.data_and_params):
         parameters[k] = urllib.unquote(v[0])
     return parameters
示例#4
0
文件: hook.py 项目: PyYoshi/Pytumb2
 def _split_url_string(query_string):
     """
     Turns a `query_string` into a Python dictionary with unquoted values
     """
     parameters = parse_qs(to_utf8(query_string), keep_blank_values=True)
     for k, v in parameters.iteritems():
         parameters[k] = urllib.unquote(v[0])
     return parameters
示例#5
0
文件: hook.py 项目: PyYoshi/Pytumb2
    def to_url(request):
        """Serialize as a URL for a GET request."""
        scheme, netloc, path, query, fragment = urlsplit(to_utf8(request.url))
        query = parse_qs(query)

        for key, value in request.data_and_params.iteritems():
            query.setdefault(key, []).append(value)
            
        query = urllib.urlencode(query, True)
        return urlunsplit((scheme, netloc, path, query, fragment))
示例#6
0
    def to_url(request):
        """Serialize as a URL for a GET request."""
        scheme, netloc, path, query, fragment = urlsplit(to_utf8(request.url))
        query = parse_qs(query)

        for key, value in iteritems(request.data_and_params):
            query.setdefault(key, []).append(value)

        query = urllib.urlencode(query, True)
        return urlunsplit((scheme, netloc, path, query, fragment))
示例#7
0
    def get_normalized_parameters(request):
        """
        Returns a string that contains the parameters that must be signed. 
        This function is called by SignatureMethod subclass CustomSignatureMethod_HMAC_SHA1 
        """
        # See issues #10 and #12
        if ('Content-Type' not in request.headers or\
            request.headers.get('Content-Type') == 'application/x-www-form-urlencoded')\
        and not isinstance(request.data, basestring):
            data_and_params = dict(request.data.items() +
                                   request.params.items())

            for key, value in data_and_params.items():
                request.data_and_params[to_utf8(key)] = to_utf8(value)

        if request.data_and_params.has_key('oauth_signature'):
            del request.data_and_params['oauth_signature']

        items = []
        for key, value in iteritems(request.data_and_params):
            # 1.0a/9.1.1 states that kvp must be sorted by key, then by value,
            # so we unpack sequence values into multiple items for sorting.
            if isinstance(value, basestring):
                items.append((key, value))
            else:
                try:
                    value = list(value)
                except TypeError as e:
                    assert 'is not iterable' in str(e)
                    items.append((key, value))
                else:
                    items.extend((key, item) for item in value)

        # Include any query string parameters included in the url
        query_string = urlparse(request.url)[4]
        items.extend([
            (to_utf8(k), to_utf8(v))
            for k, v in OAuthHook._split_url_string(query_string).items()
        ])
        items.sort()

        return urllib.urlencode(items).replace('+', '%20').replace('%7E', '~')
示例#8
0
    def get_normalized_parameters(request):
        """
        Returns a string that contains the parameters that must be signed. 
        This function is called by SignatureMethod subclass CustomSignatureMethod_HMAC_SHA1 
        """
        # See issues #10 and #12
        if ('Content-Type' not in request.headers or\
            request.headers.get('Content-Type') == 'application/x-www-form-urlencoded')\
        and not isinstance(request.data, basestring):
            data_and_params = dict(request.data.items() + request.params.items())

            for key,value in data_and_params.items():
                request.data_and_params[to_utf8(key)] = to_utf8(value)

        if request.data_and_params.has_key('oauth_signature'):
            del request.data_and_params['oauth_signature']

        items = []
        for key, value in iteritems(request.data_and_params):
            # 1.0a/9.1.1 states that kvp must be sorted by key, then by value,
            # so we unpack sequence values into multiple items for sorting.
            if isinstance(value, basestring):
                items.append((key, value))
            else:
                try:
                    value = list(value)
                except TypeError as e:
                    assert 'is not iterable' in str(e)
                    items.append((key, value))
                else:
                    items.extend((key, item) for item in value)

        # Include any query string parameters included in the url
        query_string = urlparse(request.url)[4]
        items.extend([(to_utf8(k), to_utf8(v)) for k, v in OAuthHook._split_url_string(query_string).items()])
        items.sort()

        return urllib.urlencode(items).replace('+', '%20').replace('%7E', '~')
示例#9
0
def to_utf8(x):
    """
    Tries to utf-8 encode x when possible 

    If x is a string returns it encoded, otherwise tries to iter x and 
    encode utf-8 all strings it contains, returning a list.
    """
    if isinstance(x, basestring): 
        return x.encode('utf-8') if isinstance(x, unicode) else x
    try:
        l = iter(x)
    except TypeError:
        return x
    return [to_utf8(i) for i in l]
示例#10
0
def to_utf8(x):
    """
    Tries to utf-8 encode x when possible 

    If x is a string returns it encoded, otherwise tries to iter x and 
    encode utf-8 all strings it contains, returning a list.
    """
    if isinstance(x, basestring):
        return x.encode('utf-8') if isinstance(x, unicode) else x
    try:
        l = iter(x)
    except TypeError:
        return x
    return [to_utf8(i) for i in l]
示例#11
0
 def get_normalized_parameters(request):
     """Returns a string that contains the parameters that must be signed. 
     This function is called by oauth2 SignatureMethod subclass CustomSignatureMethod_HMAC_SHA1 """
     data_and_params = dict(request.data.items() + request.params.items())
     items = []
     for key, value in data_and_params.iteritems():
         if key == 'oauth_signature':
             continue
         # 1.0a/9.1.1 states that kvp must be sorted by key, then by value,
         # so we unpack sequence values into multiple items for sorting.
         if isinstance(value, basestring):
             items.append((to_utf8_if_string(key), to_utf8(value)))
         else:
             try:
                 value = list(value)
             except TypeError, e:
                 assert 'is not iterable' in str(e)
                 items.append((to_utf8_if_string(key), to_utf8_if_string(value)))
             else:
                 items.extend((to_utf8_if_string(key), to_utf8_if_string(item)) for item in value)
示例#12
0
 def get_normalized_parameters(request):
     """Returns a string that contains the parameters that must be signed. 
     This function is called by oauth2 SignatureMethod subclass CustomSignatureMethod_HMAC_SHA1 """
     data_and_params = dict(request.data.items() + request.params.items())
     items = []
     for key, value in data_and_params.iteritems():
         if key == 'oauth_signature':
             continue
         # 1.0a/9.1.1 states that kvp must be sorted by key, then by value,
         # so we unpack sequence values into multiple items for sorting.
         if isinstance(value, basestring):
             items.append((to_utf8_if_string(key), to_utf8(value)))
         else:
             try:
                 value = list(value)
             except TypeError, e:
                 assert 'is not iterable' in str(e)
                 items.append(
                     (to_utf8_if_string(key), to_utf8_if_string(value)))
             else:
                 items.extend(
                     (to_utf8_if_string(key), to_utf8_if_string(item))
                     for item in value)
示例#13
0
文件: hook.py 项目: PyYoshi/Pytumb2
            # 1.0a/9.1.1 states that kvp must be sorted by key, then by value,
            # so we unpack sequence values into multiple items for sorting.
            if isinstance(value, basestring):
                items.append((key, value))
            else:
                try:
                    value = list(value)
                except TypeError, e:
                    assert 'is not iterable' in str(e)
                    items.append((key, value))
                else:
                    items.extend((key, item) for item in value)

        # Include any query string parameters included in the url
        query_string = urlparse(request.url)[4]
        items.extend([(to_utf8(k), to_utf8(v)) for k, v in OAuthHook._split_url_string(query_string).items()])
        items.sort()

        return urllib.urlencode(items).replace('+', '%20').replace('%7E', '~')

    @staticmethod
    def get_normalized_url(url):
        """
        Returns a normalized url, without params
        """
        scheme, netloc, path, params, query, fragment = urlparse(url)

        # Exclude default port numbers.
        if scheme == 'http' and netloc[-3:] == ':80':
            netloc = netloc[:-3]
        elif scheme == 'https' and netloc[-4:] == ':443':
示例#14
0
            if isinstance(value, basestring):
                items.append((to_utf8_if_string(key), to_utf8(value)))
            else:
                try:
                    value = list(value)
                except TypeError, e:
                    assert 'is not iterable' in str(e)
                    items.append((to_utf8_if_string(key), to_utf8_if_string(value)))
                else:
                    items.extend((to_utf8_if_string(key), to_utf8_if_string(item)) for item in value)

        # Include any query string parameters from the provided URL
        query = urlparse(request.url)[4]

        url_items = OAuthHook._split_url_string(query).items()
        url_items = [(to_utf8(k), to_utf8(v)) for k, v in url_items if k != 'oauth_signature']
        items.extend(url_items)

        items.sort()
        encoded_str = urllib.urlencode(items)
        # Encode signature parameters per OAuth Core 1.0 protocol
        # spec draft 7, section 3.6
        # (http://tools.ietf.org/html/draft-hammer-oauth-07#section-3.6)
        # Spaces must be encoded with "%20" instead of "+"
        return encoded_str.replace('+', '%20').replace('%7E', '~')

    @staticmethod
    def get_normalized_url(url):
        """
        Returns a normalized url, without params
        """
示例#15
0
__FILENAME__ = auth
import binascii
import hmac
import random
import urllib
from urlparse import urlparse, urlunparse
try:
    from hashlib import sha1
    sha = sha1
except ImportError:
    # hashlib was added in Python 2.5
    import sha


escape = lambda url: urllib.quote(to_utf8(url), safe='~')

def to_utf8(x):
    """
    Tries to utf-8 encode x when possible 

    If x is a string returns it encoded, otherwise tries to iter x and 
    encode utf-8 all strings it contains, returning a list.
    """
    if isinstance(x, basestring): 
        return x.encode('utf-8') if isinstance(x, unicode) else x
    try:
        l = iter(x)
    except TypeError:
        return x
    return [to_utf8(i) for i in l]
示例#16
0
__FILENAME__ = auth
import binascii
import hmac
import random
import urllib
from urlparse import urlparse, urlunparse
try:
    from hashlib import sha1
    sha = sha1
except ImportError:
    # hashlib was added in Python 2.5
    import sha

escape = lambda url: urllib.quote(to_utf8(url), safe='~')


def to_utf8(x):
    """
    Tries to utf-8 encode x when possible 

    If x is a string returns it encoded, otherwise tries to iter x and 
    encode utf-8 all strings it contains, returning a list.
    """
    if isinstance(x, basestring):
        return x.encode('utf-8') if isinstance(x, unicode) else x
    try:
        l = iter(x)
    except TypeError:
        return x
    return [to_utf8(i) for i in l]
示例#17
0
            # so we unpack sequence values into multiple items for sorting.
            if isinstance(value, basestring):
                items.append((key, value))
            else:
                try:
                    value = list(value)
                except TypeError, e:
                    assert 'is not iterable' in str(e)
                    items.append((key, value))
                else:
                    items.extend((key, item) for item in value)

        # Include any query string parameters included in the url
        query_string = urlparse(request.url)[4]
        items.extend([
            (to_utf8(k), to_utf8(v))
            for k, v in OAuthHook._split_url_string(query_string).items()
        ])
        items.sort()

        return urllib.urlencode(items).replace('+', '%20').replace('%7E', '~')

    @staticmethod
    def get_normalized_url(url):
        """
        Returns a normalized url, without params
        """
        scheme, netloc, path, params, query, fragment = urlparse(url)

        # Exclude default port numbers.
        if scheme == 'http' and netloc[-3:] == ':80':
示例#18
0
                try:
                    value = list(value)
                except TypeError, e:
                    assert 'is not iterable' in str(e)
                    items.append(
                        (to_utf8_if_string(key), to_utf8_if_string(value)))
                else:
                    items.extend(
                        (to_utf8_if_string(key), to_utf8_if_string(item))
                        for item in value)

        # Include any query string parameters from the provided URL
        query = urlparse(request.url)[4]

        url_items = OAuthHook._split_url_string(query).items()
        url_items = [(to_utf8(k), to_utf8(v)) for k, v in url_items
                     if k != 'oauth_signature']
        items.extend(url_items)

        items.sort()
        encoded_str = urllib.urlencode(items)
        # Encode signature parameters per OAuth Core 1.0 protocol
        # spec draft 7, section 3.6
        # (http://tools.ietf.org/html/draft-hammer-oauth-07#section-3.6)
        # Spaces must be encoded with "%20" instead of "+"
        return encoded_str.replace('+', '%20').replace('%7E', '~')

    @staticmethod
    def get_normalized_url(url):
        """
        Returns a normalized url, without params