Struct oauth1_request::Signer

source ·
pub struct Signer<SM: SignatureMethod, State = NotReady> { /* private fields */ }
Expand description

A type that creates a signed Request.

Example

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",
);

Implementations

Returns a Signer that appends query string to uri and returns it as Request.data.

uri must not contain a query part. Otherwise, the Signer will produce a wrong signature.

Panics

In debug builds, panics if uri contains a '?' character.

Same as new except that this uses signature_method as the signature method.

Returns a Signer that creates an x-www-form-urlencoded string and returns it as Request.data.

uri must not contain a query part. Otherwise, the Signer will produce a wrong signature.

Panics

In debug builds, panics if uri contains a '?' character.

Same as new_form except that this uses signature_method as the signature method.

Appends oauth_* parameters to the signing key.

This must be called just after all the keys less than oauth_* in byte order (if any) is appended, and just before a key greater than oauth_* (if any) is appended.

Appends a parameter to the query/form string and signing key.

This percent encodes the value, but not the key.

The parameters must be appended in byte ascending order.

Panics

In debug builds, panics if the key is not appended in ascending order

Appends a parameter to the query/form string and signing key.

Unlike parameter, this will not percent encode the value.

Panics

In debug builds, panics if the key is not appended in ascending order.

Shorthand for self.oauth_parameters(consumer_key, options).finish().

Consumes the Signer and returns a Request.

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.