Crate oauth1_request

source ·
Expand description

Yet yet yet another OAuth 1 client library.

Usage

Creating a GET request:

extern crate oauth1_request as oauth;

let mut sign = oauth::HmacSha1Signer::new(
    "GET",
    "https://example.com/api/v1/get.json",
    "consumer_secret",
    "token_secret", // or `None`
);

// The parameters must be appended in the ascending ordering.
sign.parameter("abc", "value")
    .parameter("lmn", "something");

// Append `oauth_*` parameters.
let mut sign = sign.oauth_parameters(
    "consumer_key",
    &*oauth::Options::new()
        .token("token")
        .nonce("nonce")
        .timestamp(9999999999),
);

sign.parameter("qrs", "stuff")
    .parameter("xyz", "blah-blah");

let oauth::Request { authorization, data } = sign.finish();

assert_eq!(
    authorization,
    "OAuth \
     oauth_consumer_key=\"consumer_key\",\
     oauth_nonce=\"nonce\",\
     oauth_signature_method=\"HMAC-SHA1\",\
     oauth_timestamp=\"9999999999\",\
     oauth_token=\"token\",\
     oauth_signature=\"R1%2B4C7PHNUwA2TyMeNZDo0T8lSM%3D\"",
);
assert_eq!(
    data,
    "https://example.com/api/v1/get.json?abc=value&lmn=something&qrs=stuff&xyz=blah-blah",
);

Creating an x-www-form-urlencoded request:

// Use `new_form` method to create an `x-www-form-urlencoded` string.
let mut sign = oauth::HmacSha1Signer::new_form(
    "POST",
    "https://example.com/api/v1/post.json",
    "consumer_secret",
    "token_secret", // or `None`
);

// ...
// (same as the above example...)

let oauth::Request { authorization, data } = sign.finish();

assert_eq!(
    authorization,
    "OAuth \
     oauth_consumer_key=\"consumer_key\",\
     oauth_nonce=\"nonce\",\
     oauth_signature_method=\"HMAC-SHA1\",\
     oauth_timestamp=\"9999999999\",\
     oauth_token=\"token\",\
     oauth_signature=\"YUOk%2FeMb2r%2BAF5wW0H%2FgEx%2FoLp0%3D\"",
);
assert_eq!(
    data,
    "abc=value&lmn=something&qrs=stuff&xyz=blah-blah",
);

Using the convenience wrapper method:

let oauth::Request { authorization, data } = oauth::Request::new(
    "GET",
    "https://example.com/api/v1/get.json",
    "consumer_key",
    "consumer_secret",
    "token_secret",
    oauth::HmacSha1,
    &*oauth::Options::new()
        .token("token")
        .nonce("nonce")
        .timestamp(9999999999),
    Some(&[
        // Ordering doesn't matter here:
        ("xyz", "blah-blah"),
        ("qrs", "stuff"),
        ("abc", "value"),
        ("lmn", "something"),
    ].iter().cloned().collect()),
);

assert_eq!(
    authorization,
    "OAuth \
     oauth_consumer_key=\"consumer_key\",\
     oauth_nonce=\"nonce\",\
     oauth_signature_method=\"HMAC-SHA1\",\
     oauth_timestamp=\"9999999999\",\
     oauth_token=\"token\",\
     oauth_signature=\"R1%2B4C7PHNUwA2TyMeNZDo0T8lSM%3D\"",
);
assert_eq!(
    data,
    "https://example.com/api/v1/get.json?abc=value&lmn=something&qrs=stuff&xyz=blah-blah",
);

Re-exports

pub use signature_method::Plaintext;
pub use signature_method::HmacSha1;

Modules

Structs

Represents the state of a Signer before oauth_parameters method is called and unready to finish.
Optional OAuth parameters.
Represents the state of a Signer after oauth_parameters method is called and ready to finish.
A pair of an OAuth header and its corresponding query/form string.
A type that creates a signed Request.

Type Definitions

A version of Signer that uses the HMAC-SHA1 signature method.
A version of Signer that uses the PLAINTEXT signature method.