def get_oauth_signature(self, request): """Get an OAuth signature to be used in signing a request To satisfy `section 3.4.1.2`_ item 2, if the request argument's headers dict attribute contains a Host item, its value will replace any netloc part of the request argument's uri attribute value. .. _`section 3.4.1.2`: http://tools.ietf.org/html/rfc5849#section-3.4.1.2 """ if self.signature_method == SIGNATURE_PLAINTEXT: # fast-path return signature.sign_plaintext(self.client_secret, self.resource_owner_secret) uri, headers, body = self._render(request) # escape more chars than what "requests" wants... At least [] sch, net, path, par, query, fra = urlparse.urlparse(uri) unq_path = unquote_unreserved(path).encode('utf-8') path = quote(unq_path, safe=b"%/") uri = urlparse.urlunparse((sch, net, path, par, query, fra)) collected_params = signature.collect_parameters( uri_query=urlparse.urlparse(uri).query, body=body, headers=headers) log.debug("Collected params: {0}".format(collected_params)) normalized_params = signature.normalize_parameters(collected_params) normalized_uri = signature.normalize_base_string_uri(uri, headers.get('Host', None)) log.debug("Normalized params: {0}".format(normalized_params)) log.debug("Normalized URI: {0}".format(normalized_uri)) base_string = signature.construct_base_string(request.http_method, normalized_uri, normalized_params) log.debug("Base signing string: {0}".format(base_string)) if self.signature_method == SIGNATURE_HMAC: sig = signature.sign_hmac_sha1(base_string, self.client_secret, self.resource_owner_secret) elif self.signature_method == SIGNATURE_RSA: sig = signature.sign_rsa_sha1(base_string, self.rsa_key) else: sig = signature.sign_plaintext(self.client_secret, self.resource_owner_secret) log.debug("Signature: {0}".format(sig)) return sig
def _generate_auth_url(self, path, params): """Returns the path and query string portion of the request URL, first adding any necessary parameters. :param path: The path portion of the URL. :type path: string :param params: URL parameters. :type params: dict or list of key/value tuples :returns: encoded URL :rtype: string """ if type(params) is dict: params = sorted(dict(**params).items()) # Only auto-add API key when using ORS. If own instance, API key must # be explicitly added to params # if self.key: # params.append(("api_key", self.key)) return path + "?" + unquote_unreserved(urlencode(params))
def test_unquote_unreserved(uri, expected): assert unquote_unreserved(uri) == expected