Ejemplo n.º 1
0
 def test_valid_query_params_list(self):
     params = {
         "a2": "r b",
         "b5": "=%3D",
         "a3": ["a", "2 q"],
         "c@": [""],
         "c2": "",
         "non_string": 5,
         "blank_list_value_not_preserved": [],
         OAUTH_PARAM_CONSUMER_KEY: "9djdj82h48djs9d2",
         OAUTH_PARAM_TOKEN: "kkk9d7dh3k39sjv7",
         OAUTH_PARAM_SIGNATURE_METHOD: "HMAC-SHA1",
         OAUTH_PARAM_TIMESTAMP: ["137131201"],
         OAUTH_PARAM_NONCE: "7d8f3e4a",
     }
     valid_params_list = [
         (b("a2"), b("r%20b")),
         (b("a3"), b("2%20q")),
         (b("a3"), b("a")),
         (b("b5"), b("%3D%253D")),
         (b("c%40"), b("")),
         (b("c2"), b("")),
         (b("non_string"), b("5")),
         (utf8_encode(OAUTH_PARAM_CONSUMER_KEY), b("9djdj82h48djs9d2")),
         (utf8_encode(OAUTH_PARAM_NONCE), b("7d8f3e4a")),
         (utf8_encode(OAUTH_PARAM_SIGNATURE_METHOD), b("HMAC-SHA1")),
         (utf8_encode(OAUTH_PARAM_TIMESTAMP), b("137131201")),
         (utf8_encode(OAUTH_PARAM_TOKEN), b("kkk9d7dh3k39sjv7")),
     ]
     self.assertEqual(urlencode_sl(params), valid_params_list)
Ejemplo n.º 2
0
    def __init__(self, identifier, shared_secret):
        """
        OAuth Credentials.

        :param identifier:
            Identifier (old: key)
        :param shared_secret:
            Shared secret (old: secret)
        """
        self._identifier = utf8_encode(identifier)
        self._shared_secret = utf8_encode(shared_secret)
    def test_ignores_body_params_if_content_type_is_not_urlencoded(self):
        oauth_params = dict(
            oauth_consumer_key=RFC_CLIENT_IDENTIFIER,
            oauth_token=RFC_TOKEN_IDENTIFIER,
            oauth_signature_method=SIGNATURE_METHOD_HMAC_SHA1,
            oauth_timestamp=RFC_TIMESTAMP_3,
            oauth_nonce=RFC_NONCE_3,
        )
        self.assertEqual(_OAuthClient._generate_signature(
            HTTP_GET,
            # I Know ^ is a GET request and we're specifying a body in this
            # test. _generate_signature does not validate HTTP methods,
            # but only generates signatures. This example must produce
            # the same signature as in the RFC example, hence the test.
            RFC_RESOURCE_FULL_URL,
            params=None,
            body=b("""
body {
    font-family: "Lucida Grande", serif;
}
a:link {
    text-decoration: none;
}
"""),
            headers={
                HEADER_CONTENT_TYPE: CONTENT_TYPE_TEXT_CSS,
            },
            oauth_consumer_secret=RFC_CLIENT_SECRET,
            oauth_token_secret=RFC_TOKEN_SECRET,
            oauth_params=oauth_params,
        ), utf8_encode(percent_decode(RFC_RESOURCE_REQUEST_SIGNATURE_ENCODED)))
Ejemplo n.º 4
0
 def test_blank_list_value_not_preserved(self):
     params = {
         "blank_list_value_not_preserved": [],
         OAUTH_PARAM_CONSUMER_KEY: "9djdj82h48djs9d2",
         OAUTH_PARAM_TOKEN: "kkk9d7dh3k39sjv7",
         OAUTH_PARAM_SIGNATURE_METHOD: "HMAC-SHA1",
         OAUTH_PARAM_TIMESTAMP: ["137131201"],
         OAUTH_PARAM_NONCE: "7d8f3e4a",
     }
     valid_params_list = [
         (utf8_encode(OAUTH_PARAM_CONSUMER_KEY), b("9djdj82h48djs9d2")),
         (utf8_encode(OAUTH_PARAM_NONCE), b("7d8f3e4a")),
         (utf8_encode(OAUTH_PARAM_SIGNATURE_METHOD), b("HMAC-SHA1")),
         (utf8_encode(OAUTH_PARAM_TIMESTAMP), b("137131201")),
         (utf8_encode(OAUTH_PARAM_TOKEN), b("kkk9d7dh3k39sjv7")),
     ]
     self.assertEqual(urlencode_sl(params), valid_params_list)
Ejemplo n.º 5
0
  def test_does_not_encode_bytes_or_None_to_utf8(self):
    self.assertEqual(text.utf8_encode(None), None)
    self.assertEqual(text.utf8_encode(constants.UTF8_BYTES), constants.UTF8_BYTES)
    self.assertTrue(builtins.is_bytes(text.utf8_encode(constants.UTF8_BYTES)))

    self.assertEqual(text.utf8_encode(constants.LATIN1_BYTES), constants.LATIN1_BYTES)
    self.assertTrue(builtins.is_bytes(text.utf8_encode(constants.LATIN1_BYTES)))

    self.assertEqual(text.utf8_encode(constants.UTF8_BYTES2), constants.UTF8_BYTES2)
    self.assertTrue(builtins.is_bytes(text.utf8_encode(constants.UTF8_BYTES2)))
Ejemplo n.º 6
0
  def test_does_not_encode_bytes_or_None_to_utf8(self):
    self.assertEqual(utf8_encode(None), None)
    self.assertEqual(utf8_encode(utf8_bytes), utf8_bytes)
    self.assertTrue(is_bytes(utf8_encode(utf8_bytes)))

    self.assertEqual(utf8_encode(latin1_bytes), latin1_bytes)
    self.assertTrue(is_bytes(utf8_encode(latin1_bytes)))

    self.assertEqual(utf8_encode(utf8_bytes2), utf8_bytes2)
    self.assertTrue(is_bytes(utf8_encode(utf8_bytes2)))
Ejemplo n.º 7
0
def percent_encode(value):
    """
    Percent-encodes according to the OAuth spec.

    Used in constructing the signature base string and the "Authorization"
    header field::

        percent_encode('c@') -> 'c%40'

    :see: Percent Encoding (http://tools.ietf.org/html/rfc5849#section-3.6)
    :param value:
        Query string parameter value to escape. If the value is a Unicode
        string, it will be encoded to UTF-8. A byte string is considered
        exactly that, a byte string and will not be UTF-8 encoded—however, it
        will be percent-encoded.
    :returns:
        Percent-encoded string.
   """
    # Escapes '/' too
    if not is_bytes(value):
        value = utf8_encode(str(value))
    return quote(value, safe="~").encode("ascii")
 def test_generates_signature_including_urlencoded_body(self):
     oauth_params = dict(
         oauth_consumer_key=RFC_CLIENT_IDENTIFIER,
         oauth_token=RFC_TOKEN_IDENTIFIER,
         oauth_signature_method=SIGNATURE_METHOD_HMAC_SHA1,
         oauth_timestamp=RFC_TIMESTAMP_3,
         oauth_nonce=RFC_NONCE_3,
     )
     self.assertEqual(_OAuthClient._generate_signature(
         HTTP_GET,
         # I Know ^ is a GET request and we're specifying a body in this
         # test. _generate_signature does not validate HTTP methods,
         # but only generates signatures. This example must produce
         # the same signature as in the RFC example, hence the test.
         RFC_RESOURCE_URI,
         params=None,
         body=b("file=vacation.jpg&size=original&oauth_ignored=IGNORED"),
         headers={
             HEADER_CONTENT_TYPE: CONTENT_TYPE_FORM_URLENCODED,
         },
         oauth_consumer_secret=RFC_CLIENT_SECRET,
         oauth_token_secret=RFC_TOKEN_SECRET,
         oauth_params=oauth_params,
     ), utf8_encode(percent_decode(RFC_RESOURCE_REQUEST_SIGNATURE_ENCODED)))
Ejemplo n.º 9
0
  def test_encodes_only_unicode_to_utf8(self):
    self.assertEqual(text.utf8_encode(constants.UNICODE_STRING), constants.UTF8_BYTES)
    self.assertTrue(builtins.is_bytes(text.utf8_encode(constants.UNICODE_STRING)))

    self.assertEqual(text.utf8_encode(constants.UNICODE_STRING2), constants.UTF8_BYTES2)
    self.assertTrue(builtins.is_bytes(text.utf8_encode(constants.UNICODE_STRING2)))
Ejemplo n.º 10
0
    def _build_request(cls, method, url, params, body, headers,
                       oauth_params, realm, use_authorization_header):
        """
        Builds a request based on the HTTP arguments and OAuth protocol
        parameters.

        :param method:
            HTTP method.
        :param url:
            Request URL
        :param params:
            Additional query/payload parameters.
            If a `body` argument to this function is specified,
            the parameters are appended to the URL query string.
            If a `body` is not specified and a method other than GET is used
            the parameters will be added to the entity body.
        :param body:
            Entity body.
        :param oauth_params:
            Protocol-specific parameters.
        :param realm:
            OAuth authorization realm.
        :param use_authorization_header:
            ``True`` if the Authorization HTTP header should be used;
            ``False`` otherwise.
        :returns:
            An instance of :class:`pyoauth.http.RequestAdapter`.
        """
        # http://tools.ietf.org/html/rfc5849#section-3.6
        if HEADER_AUTHORIZATION_CAPS in headers or \
           HEADER_AUTHORIZATION in headers:
            raise InvalidAuthorizationHeaderError(
                "Authorization field is already present in headers: %r" % \
                headers
            )
        if use_authorization_header:
            headers[HEADER_AUTHORIZATION_CAPS] = \
                generate_authorization_header(oauth_params, realm)
            # Empty oauth params so that they are not included again below.
            oauth_params = None

        # OAuth requests can contain payloads.
        if body or method == HTTP_GET:
            # Append params to query string.
            url = url_append_query(url_add_query(url, params), oauth_params)
            if body and method == HTTP_GET:
                raise InvalidHttpRequestError(
                    "HTTP method GET does not take an entity body"
                )
            if body and \
               HEADER_CONTENT_LENGTH not in headers and \
               HEADER_CONTENT_LENGTH_CAPS not in headers:
                raise ValueError("You must set the `content-length` header.")
        else:
            if params or oauth_params:
                # Append to payload and set content type.
                body = utf8_encode(query_append(params, oauth_params))
                headers[HEADER_CONTENT_TYPE] = CONTENT_TYPE_FORM_URLENCODED
                headers[HEADER_CONTENT_LENGTH] = str(len(body)).encode("ascii")
            else:
                # Zero-length body.
                body = SYMBOL_EMPTY_BYTES
                headers[HEADER_CONTENT_LENGTH] = SYMBOL_ZERO
        return RequestAdapter(method, url, body, headers)
Ejemplo n.º 11
0
 def test_raises_error_when_invalid_type(self):
   self.assertRaises(TypeError, json_encode, utf8_encode(unicode_value))
   self.assertRaises(TypeError, json_encode, x_byte)
Ejemplo n.º 12
0
 def test_raises_error_when_invalid_type(self):
   self.assertRaises(TypeError, json.json_encode, text.utf8_encode(constants.UNICODE_VALUE))
   self.assertRaises(TypeError, json.json_encode, constants.X_BYTE)
Ejemplo n.º 13
0
  def test_encodes_only_unicode_to_utf8(self):
    self.assertEqual(utf8_encode(unicode_string), utf8_bytes)
    self.assertTrue(is_bytes(utf8_encode(unicode_string)))

    self.assertEqual(utf8_encode(unicode_string2), utf8_bytes2)
    self.assertTrue(is_bytes(utf8_encode(unicode_string2)))