Crate oauth1_request
source · [−]Expand description
Yet yet yet another OAuth 1.0 client library.
Usage
Add this to your Cargo.toml
:
[dependencies]
oauth = { version = "0.6", package = "oauth1-request" }
For brevity, we refer to the crate name as oauth
throughout the documentation,
since the API is designed in favor of qualified paths like oauth::get
.
Create a request
A typical authorization flow looks like this:
// Define a type to represent your request.
#[derive(oauth::Request)]
struct CreateComment<'a> {
article_id: u64,
text: &'a str,
}
let uri = "https://example.com/api/v1/comments/create.json";
let request = CreateComment {
article_id: 123456789,
text: "A request signed with OAuth & Rust 🦀 🔏",
};
// Prepare your credentials.
let token =
oauth::Token::from_parts("consumer_key", "consumer_secret", "token", "token_secret");
// Create the `Authorization` header.
let authorization_header = oauth::post(uri, &request, &token, oauth::HMAC_SHA1);
// `oauth_nonce` and `oauth_timestamp` vary on each execution.
assert_eq!(
authorization_header,
"OAuth \
oauth_consumer_key=\"consumer_key\",\
oauth_nonce=\"Dk-OGluFEQ4f\",\
oauth_signature_method=\"HMAC-SHA1\",\
oauth_timestamp=\"1234567890\",\
oauth_token=\"token\",\
oauth_signature=\"n%2FrUgos4CFFZbZK8Z8wFR7drU4c%3D\"",
);
// You can create an `x-www-form-urlencoded` string or a URI with query pairs from the request.
let form = oauth::to_form(&request);
assert_eq!(
form,
"article_id=123456789&text=A%20request%20signed%20with%20OAuth%20%26%20Rust%20%F0%9F%A6%80%20%F0%9F%94%8F",
);
let uri = oauth::to_query(uri.to_owned(), &request);
assert_eq!(
uri,
"https://example.com/api/v1/comments/create.json?article_id=123456789&text=A%20request%20signed%20with%20OAuth%20%26%20Rust%20%F0%9F%A6%80%20%F0%9F%94%8F",
);
See Request
for more details on the derive macro.
If you want to authorize a request with dynamic keys, use
oauth::ParameterList
.
use std::fmt::Display;
let request = oauth::ParameterList::new([
("article_id", &123456789 as &dyn Display),
("text", &"A request signed with OAuth & Rust 🦀 🔏"),
]);
let form = oauth::to_form(&request);
assert_eq!(
form,
"article_id=123456789&text=A%20request%20signed%20with%20OAuth%20%26%20Rust%20%F0%9F%A6%80%20%F0%9F%94%8F",
);
Use oauth::Builder
if you need to specify a callback URI or verifier:
let uri = "https://example.com/oauth/request_temp_credentials";
let callback = "https://client.example.net/oauth/callback";
let client = oauth::Credentials::new("consumer_key", "consumer_secret");
let authorization_header = oauth::Builder::<_, _>::new(client, oauth::HmacSha1::new())
.callback(callback)
.post(uri, &());
Re-exports
pub use oauth_credentials::Credentials;
pub use oauth_credentials::Token;
pub use self::request::ParameterList;
pub use self::request::Request;
pub use self::signature_method::HmacSha1;
pub use self::signature_method::Plaintext;
pub use self::signature_method::HMAC_SHA1;
pub use self::signature_method::PLAINTEXT;
Modules
Requests to be authorized with OAuth.
Low-level machinery to convert a Request
to a signature or a URI query/form string.
Signature methods (RFC 5849 section 3.4.).
Macros
Helper macro for implementors of Serializer
which generates blank implementation of
serialize_oauth_*
methods.
Structs
A builder for OAuth Authorization
header string.
Functions
alloc
Authorizes a request to uri
with the given credentials.
alloc
Authorizes a CONNECT
request to uri
with the given credentials.
alloc
Authorizes a DELETE
request to uri
with the given credentials.
alloc
Authorizes a GET
request to uri
with the given credentials.
alloc
Authorizes a HEAD
request to uri
with the given credentials.
alloc
Authorizes a OPTIONS
request to uri
with the given credentials.
alloc
Authorizes a PATCH
request to uri
with the given credentials.
alloc
Authorizes a POST
request to uri
with the given credentials.
alloc
Authorizes a PUT
request to uri
with the given credentials.
alloc
Serializes a Request
to an x-www-form-urlencoded
string.
alloc
Serializes a Request
to a query string and appends it to the given URI.
alloc
Authorizes a TRACE
request to uri
with the given credentials.