#oauth #oauth1

oauth1-request

Yet yet yet another OAuth 1 client library

3 releases

0.1.2 Oct 18, 2018
0.1.1 Oct 15, 2018
0.1.0 Oct 14, 2018

#35 in Authentication

Download history 93/week @ 2018-10-19 4/week @ 2018-10-26

32 downloads per month

MIT/Apache

56KB
1K SLoC

oauth1-request

Build Status Current Version Documentation

Yet yet yet another OAuth 1 client library for Rust.

Usage

Add this to your Cargo.toml:

[dependencies]
oauth1-request = "0.2"

and this to your crate root:

extern crate oauth1_request;

Pros

  • No dependency on ring (which can cause some compatibility issues).
  • Slightly lower memory footprint (maybe): it avoids allocating memory for sorting query pairs unlike other crates.

Cons

  • Only dogfed on Twitter and likely to break on other sites.
  • Less ergonomic API: it requires you to explicitly handle the ordering of query/OAuth parameters.

lib.rs:

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:

# extern crate oauth1_request as oauth;
// 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...)
# sign.parameter("abc", "value").parameter("lmn", "something");
# 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=\"YUOk%2FeMb2r%2BAF5wW0H%2FgEx%2FoLp0%3D\"",
);
assert_eq!(
    data,
    "abc=value&lmn=something&qrs=stuff&xyz=blah-blah",
);

Using the convenience wrapper method:

# extern crate oauth1_request as oauth;
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",
);

Dependencies

~552KB