Exemple #1
0
def _generate_plaintext_signature(client_shared_secret,
                                  token_shared_secret=None,
                                  _percent_encode=True):
    """
    Calculates the PLAINTEXT signature.

    :param client_shared_secret:
        Client (consumer) shared secret.
    :param token_shared_secret:
        Token/temporary credentials shared secret if available.
    :param _percent_encode:
        (DEBUG)

        Must be ``True`` to be compatible with OAuth 1.0 RFC5849 & OAuth 1.0a;
        We have added this parameter to enable better debugging by the
        signature verification routines. If this is set to ``False``, the
        signature elements will not be percent-encoded before the plaintext
        signature is generated.
    :returns:
        PLAINTEXT signature.
    """
    client_shared_secret = client_shared_secret or SYMBOL_EMPTY_BYTES
    token_shared_secret = token_shared_secret or SYMBOL_EMPTY_BYTES
    if _percent_encode:
        return SYMBOL_AMPERSAND.join(map(percent_encode,
                            (client_shared_secret, token_shared_secret)))
    else:
        # User clients can forget to do this and this has been fixed
        # by OAuth 1.0a, so we use this piece of code to detect whether
        # the user's OAuth client library complies with the specification
        # when in debugging mode.
        return SYMBOL_AMPERSAND.join((client_shared_secret,
                                      token_shared_secret))
Exemple #2
0
def urlencode_s(query_params, predicate=None):
    """
    Serializes a dictionary of query parameters into a string of query
    parameters, ``name=value`` pairs separated by ``&``, 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 predicate:
        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 predicate(name, value):
                return is_name_allowed(name) and is_value_allowed(value)
    :returns:
        A string of query parameters, ``name=value`` pairs separated by ``&``,
        sorted first by ``name`` and then by ``value`` based on the OAuth
        percent-encoding rules and specification.
    """
    return SYMBOL_AMPERSAND.join(
        key + SYMBOL_EQUAL + value
        for key, value in urlencode_sl(query_params, predicate))
Exemple #3
0
def query_append(*queries):
    """
    Appends additional query parameters to a query string. The additional
    query parameters appear after the initial query string.

    :param queries:
        Additional query parameters dictionary or query string.
    :returns:
        Concatenated query string.
    """
    sub_queries = []
    for query in queries:
        query_s = urlencode_s(query_unflatten(query))
        if query_s:
            sub_queries.append(query_s)
    return SYMBOL_AMPERSAND.join(sub_queries)
Exemple #4
0
def generate_base_string(method, url, oauth_params):
    """
    Calculates a signature base string based on the URL, method, and
    oauth parameters.

    Any query parameter by the name "oauth_signature" will be excluded
    from the base string.

    :see: Signature base string
    (http://tools.ietf.org/html/rfc5849#section-3.4.1)

    :param method:
        HTTP request method.
    :param url:
        The URL. If this includes a query string, query parameters are first
        extracted and encoded as well. All protocol-specific parameters
        will be ignored from the query string.
    :param oauth_params:
        Protocol-specific parameters must be specified in this dictionary.
        All non-protocol parameters will be ignored.
    :returns:
        Base string.
    """
    allowed_methods = HTTP_METHODS
    method_normalized = method.upper()
    if method_normalized not in allowed_methods:
        raise InvalidHttpMethodError(
            "Method must be one of the HTTP methods %s: "\
            "got `%s` instead" % (allowed_methods, method))
    if not url:
        raise InvalidUrlError("URL must be specified: got `%r`" % url)
    if not isinstance(oauth_params, dict):
        raise InvalidOAuthParametersError("Dictionary required: got `%r`" %
                                          oauth_params)

    scheme, netloc, path, matrix_params, query, _ = urlparse_normalized(url)
    query_string = generate_base_string_query(query, oauth_params)
    normalized_url = urlunparse((
        scheme,
        netloc,
        path,
        matrix_params,
        None,
        None
    ))
    return SYMBOL_AMPERSAND.join(map(percent_encode,
                           (method_normalized, normalized_url, query_string)))