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;
hmac-sha1
pub use self::signature_method::Plaintext;
pub use self::signature_method::HMAC_SHA1;
hmac-sha1
pub use self::signature_method::PLAINTEXT;
alloc
Modules§
- request
- Requests to be authorized with OAuth.
- serializer
- Low-level machinery to convert a
Request
to a signature or a URI query/form string. - signature_
method - Signature methods (RFC 5849 section 3.4.).
Macros§
- skip_
serialize_ oauth_ parameters - Helper macro for implementors of
Serializer
which generates blank implementation ofserialize_oauth_*
methods.
Structs§
- Builder
- A builder for OAuth
Authorization
header string.
Functions§
- authorize
alloc
- Authorizes a request to
uri
with the given credentials. - connect
alloc
- Authorizes a
CONNECT
request touri
with the given credentials. - delete
alloc
- Authorizes a
DELETE
request touri
with the given credentials. - get
alloc
- Authorizes a
GET
request touri
with the given credentials. - head
alloc
- Authorizes a
HEAD
request touri
with the given credentials. - options
alloc
- Authorizes a
OPTIONS
request touri
with the given credentials. - patch
alloc
- Authorizes a
PATCH
request touri
with the given credentials. - post
alloc
- Authorizes a
POST
request touri
with the given credentials. - put
alloc
- Authorizes a
PUT
request touri
with the given credentials. - to_form
alloc
- Serializes a
Request
to anx-www-form-urlencoded
string. - to_
query alloc
- Serializes a
Request
to a query string and appends it to the given URI. - trace
alloc
- Authorizes a
TRACE
request touri
with the given credentials.