#oauth #oauth1

oauth1-request

Yet yet yet another OAuth 1 client library

9 releases

✓ Uses Rust 2018 edition

0.3.0 Sep 5, 2019
0.2.6 Sep 5, 2019
0.2.5 Aug 19, 2019
0.2.1 Dec 2, 2018
0.1.2 Oct 18, 2018

#20 in Authentication

Download history 3/week @ 2019-07-25 72/week @ 2019-08-01 35/week @ 2019-08-08 51/week @ 2019-08-15 126/week @ 2019-08-22 31/week @ 2019-08-29 110/week @ 2019-09-05 30/week @ 2019-09-12 131/week @ 2019-09-19 25/week @ 2019-09-26 28/week @ 2019-10-03 13/week @ 2019-10-10 78/week @ 2019-10-17 47/week @ 2019-10-24 65/week @ 2019-10-31

157 downloads per month
Used in 3 crates

MIT/Apache

66KB
1.5K 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]
oauth = { version = "0.3", package = "oauth1-request" }

Pros

  • Customizable crypto implementations (no dependency onring by default).
  • 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.

lib.rs:

Yet yet yet another OAuth 1 client library.

Usage

Add this to your Cargo.toml:

[dependencies]
oauth = { version = "0.2", package = "oauth1-request" }

For brevity we refer to the crate name as oauth throughout the documentation.

Create a GET request

# extern crate oauth1_request as oauth;
#[derive(oauth::Authorize)]
struct SearchComments<'a> {
    article_id: u64,
    text: &'a str,
}

# fn main() {
let client = oauth::Credentials::new("consumer_key", "consumer_secret");
let token = oauth::Credentials::new("token", "token_secret");

let mut builder = oauth::Builder::new(client, oauth::HmacSha1);
builder
    .token(token)
    .nonce("nonce")
    .timestamp(9999999999);

let req = SearchComments {
    article_id: 123456789,
    text: "Rust",
};

let oauth::Request {
    authorization,
    data,
} = builder.get("https://example.com/api/v1/comments/search.json", &req);

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=\"kAkbCLL7obDyzdjz3uJoWSwiLqU%3D\"",
);
assert_eq!(
    data,
    "https://example.com/api/v1/comments/search.json?article_id=123456789&text=Rust",
);
# }

Create an x-www-form-urlencoded request

# extern crate oauth1_request as oauth;
#[derive(oauth::Authorize)]
struct CreateComment<'a> {
    article_id: u64,
    text: &'a str,
}

# fn main() {
# let client = oauth::Credentials::new("consumer_key", "consumer_secret");
# let token = oauth::Credentials::new("token", "token_secret");
# let mut builder = oauth::Builder::new(client, oauth::HmacSha1);
# builder
#     .token(token)
#     .nonce("nonce")
#     .timestamp(9999999999);
let req = CreateComment {
    article_id: 123456789,
    text: "Rust lang is great 🦀",
};

// Use `post_form` method to create an `x-www-form-urlencoded` request.
let oauth::Request {
    authorization,
    data,
} = builder.post_form("https://example.com/api/v1/comments/create.json", &req);

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=\"bbhEIrjfisdDBrZkKnEXKa4ykE4%3D\"",
);
assert_eq!(
    data,
    "article_id=123456789&text=Rust%20lang%20is%20great%20%F0%9F%A6%80",
);
# }

See Authorize for more details on the custom derive macro.

Dependencies

~0.5–1MB
~17K SLoC