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)
Exemple #2
0
def generate_normalized_authorization_header_value(oauth_params,
                                              realm=None,
                                              param_delimiter=","):
    """
    Builds the Authorization header value.

    Please note that the generated authorization header value MUST be
    on a SINGLE line.

    :see: Authorization Header http://tools.ietf.org/html/rfc5849#section-3.5.1
    :param oauth_params:
        Protocol-specific parameters excluding the ``realm`` parameter.
    :param realm:
        If specified, the realm is included into the Authorization header.
        The realm is never percent-encoded according to the OAuth spec.
    :param param_delimiter:
        The delimiter used to separate header value parameters.
        According to the Specification, this must be a comma ",". However,
        certain services like Yahoo! use "&" instead. Comma is default.

        See https://github.com/oauth/oauth-ruby/pull/12
    :returns:
        A properly formatted Authorization header value.
    """
    if realm:
        s = 'OAuth realm="' + unicode_to_utf8(realm) + '"' + param_delimiter
    else:
        s = 'OAuth '
    oauth_params = request_protocol_params_sanitize(oauth_params)
    normalized_param_pairs = urlencode_sl(oauth_params)
    s += param_delimiter.join([k + '="' + v + '"' for k, v in normalized_param_pairs])
    return s
 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_consumer_key": "9djdj82h48djs9d2",
         "oauth_token": "kkk9d7dh3k39sjv7",
         "oauth_signature_method": "HMAC-SHA1",
         "oauth_timestamp": ["137131201"],
         "oauth_nonce": "7d8f3e4a",
     }
     valid_params_list = [
         ("a2", "r%20b"),
         ("a3", "2%20q"),
         ("a3", "a"),
         ("b5", "%3D%253D"),
         ("c%40", ""),
         ("c2", ""),
         ("non_string", "5"),
         ("oauth_consumer_key", "9djdj82h48djs9d2"),
         ("oauth_nonce", "7d8f3e4a"),
         ("oauth_signature_method", "HMAC-SHA1"),
         ("oauth_timestamp", "137131201"),
         ("oauth_token", "kkk9d7dh3k39sjv7"),
     ]
     assert_equal(urlencode_sl(params), valid_params_list)
 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)
 def test_blank_list_value_not_preserved(self):
     params = {
         "blank_list_value_not_preserved": [],
         "oauth_consumer_key": "9djdj82h48djs9d2",
         "oauth_token": "kkk9d7dh3k39sjv7",
         "oauth_signature_method": "HMAC-SHA1",
         "oauth_timestamp": ["137131201"],
         "oauth_nonce": "7d8f3e4a",
     }
     valid_params_list = [
         ("oauth_consumer_key", "9djdj82h48djs9d2"),
         ("oauth_nonce", "7d8f3e4a"),
         ("oauth_signature_method", "HMAC-SHA1"),
         ("oauth_timestamp", "137131201"),
         ("oauth_token", "kkk9d7dh3k39sjv7"),
     ]
     assert_equal(urlencode_sl(params), valid_params_list)
Exemple #6
0
def generate_authorization_header(oauth_params,
                                  realm=None,
                                  param_delimiter=","):
    """
    Builds the Authorization header value.

    Please note that the generated authorization header value will be
    on a single line.

    ::

        {"oauth_b": "http://example.com/c", ...}, "example foo"
        -> 'OAuth realm="example foo",oauth_b="http%3A%2F%2Fexample.com%2Fc"...'

    :see: Authorization Header http://tools.ietf.org/html/rfc5849#section-3.5.1
    :param oauth_params:
        Protocol-specific parameters excluding the ``realm`` parameter.
    :param realm:
        If specified, the realm is included into the Authorization header.
        The realm is never percent-encoded according to the OAuth spec.
    :param param_delimiter:
        The delimiter used to separate header value parameters.
        According to the Specification, this must be a comma ",". However,
        certain services like Yahoo! use "&" instead. Comma is default.

        See https://github.com/oauth/oauth-ruby/pull/12
    :returns:
        A properly formatted Authorization header value.
    """
    param_delimiter = utf8_encode_if_unicode(param_delimiter)
    if realm:
        value = b('OAuth realm="') + utf8_encode_if_unicode(realm) + \
                SYMBOL_INVERTED_DOUBLE_QUOTE + param_delimiter
    else:
        value = b("OAuth ")
    oauth_params = request_query_remove_non_oauth(oauth_params)
    normalized_param_pairs = urlencode_sl(oauth_params)
    value += param_delimiter.join([k +
                                   SYMBOL_EQUAL +
                                   SYMBOL_INVERTED_DOUBLE_QUOTE +
                                   v +
                                   SYMBOL_INVERTED_DOUBLE_QUOTE
                               for k, v in normalized_param_pairs])
    return value