def test_valid(self): assert_true(is_bytes_or_unicode(random_bytes)) assert_true(is_bytes_or_unicode(unicode_string)) assert_false(is_bytes_or_unicode(False)) assert_false(is_bytes_or_unicode(5)) assert_false(is_bytes_or_unicode(None)) assert_false(is_bytes_or_unicode([])) assert_false(is_bytes_or_unicode(())) assert_false(is_bytes_or_unicode({})) assert_false(is_bytes_or_unicode(object))
def query_unflatten(query_params): """ Given a query string parses it into an un-flattened query parameter dictionary or given a parameter dictionary, un-flattens it. Example:: dict(a=1, b=[1, 2], c="") -> dict(a[1], b=[1, 2], c=[""]) a=1&b=1&b=2&c= -> dict(a[1], b=[1, 2], c=[""]) :param query_params: A query parameter dictionary or a query string. If this argument is ``None`` an empty dictionary will be returned. Any other value will raise a :class:`pyoauth.errors.InvalidQueryParametersError` exception. :returns: An un-flattened query parameter dictionary. """ if is_bytes_or_unicode(query_params): return parse_qs(query_params) elif isinstance(query_params, dict): # Un-flatten the dictionary. d = {} for n, v in query_params.items(): if not isinstance(v, list) and not isinstance(v, tuple): d[n] = [v] else: d[n] = list(v) return d # Alternative, but slower: #return parse_qs(urlencode_s(query_params)) elif query_params is None: return {} else: raise InvalidQueryParametersError("Dictionary or query string required: got `%r` instead" % (query_params, ))
def urlencode_sl(query_params, allow_func=None): """ Serializes a dictionary of query parameters into a list of query parameters, ``(name, value)`` pairs, sorted first by ``name`` then by ``value`` based on the OAuth percent-encoding rules and specification. Behaves like :func:`urllib.urlencode` with ``doseq=1``. :param query_params: Dictionary of query parameters. :param allow_func: A callback that will be called for each query parameter and should return ``False`` or a falsy value if that parameter should not be included. By default, all query parameters are included. The function takes the following method signature:: def allow_func(name, value): return is_name_allowed(name) and is_value_allowed(value) :returns: A list of query parameters, ``(name, value)`` pairs, sorted first by ``name`` and then by ``value`` based on the OAuth percent-encoding rules and specification. """ query_params = query_params or {} encoded_pairs = [] for k, v in query_params.items(): # Keys are also percent-encoded according to OAuth spec. k = percent_encode(unicode_to_utf8(k)) if allow_func and not allow_func(k, v): continue elif is_bytes_or_unicode(v): encoded_pairs.append((k, percent_encode(v),)) else: if is_sequence(v): # Loop over the sequence. if len(v) > 0: for i in v: encoded_pairs.append((k, percent_encode(i), )) # ``urllib.urlencode()`` doesn't preserve blank lists. # Therefore, we're discarding them. #else: # # Preserve blank list values. # encoded_pairs.append((k, "", )) else: encoded_pairs.append((k, percent_encode(v),)) # Sort after encoding according to the OAuth spec. return sorted(encoded_pairs)
def is_valid_callback_url(url): """ Determines whether a specified URl is a valid oauth_callback callback absolute URL as required by http://tools.ietf.org/html/rfc5849#section-2.1 (Temporary Credentials) in the OAuth specification. :param url: The URL to validate. :returns: ``True`` if valid; ``False`` otherwise. """ if not is_bytes_or_unicode(url): return False if url == "oob": return True else: scheme, netloc, _, _, _, _ = urlparse(url) if scheme.lower() in ("http", "https") and netloc: return True else: return False
def build_temporary_credentials_request(self, scopes, xoauth_displayname=None, method="POST", payload_params=None, headers=None, realm=None, oauth_signature_method=SIGNATURE_METHOD_HMAC_SHA1, oauth_callback="oob", **extra_oauth_params): """ :param scopes: A list of scopes to use with the credential request. :param xoauth_displayname: The display name of the application. """ payload_params = payload_params or {} scope = scopes if is_bytes_or_unicode(scopes) else " ".join(scopes) google_params = dict( scope=scope, ) if xoauth_displayname: google_params["xoauth_displayname"] = xoauth_displayname payload_params.update(google_params) GoogleClient._check_signature_method(oauth_signature_method) return super(GoogleClient, self).build_temporary_credentials_request( method=method, payload_params=payload_params, headers=headers, realm=realm, oauth_signature_method=oauth_signature_method, oauth_callback=oauth_callback, **extra_oauth_params )