[][src]Struct oauth1_request::signer::Signer

pub struct Signer<SM: SignatureMethod, State = NotReady> { /* fields omitted */ }

A low-level type for creating a signed Request.

Example

Creating a GET request:

use oauth::signer;

let mut sign = signer::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 = signer::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

impl<SM: SignatureMethod> Signer<SM, NotReady>[src]

pub fn new<'a>(
    method: &str,
    uri: impl Display,
    consumer_secret: &str,
    token_secret: impl Into<Option<&'a str>>
) -> Self where
    SM: Default
[src]

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.

pub fn with_signature_method<'a>(
    signature_method: SM,
    method: &str,
    uri: impl Display,
    consumer_secret: &str,
    token_secret: impl Into<Option<&'a str>>
) -> Self
[src]

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

pub fn new_form<'a>(
    method: &str,
    uri: impl Display,
    consumer_secret: &str,
    token_secret: impl Into<Option<&'a str>>
) -> Self where
    SM: Default
[src]

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.

pub fn form_with_signature_method<'a>(
    signature_method: SM,
    method: &str,
    uri: impl Display,
    consumer_secret: &str,
    token_secret: impl Into<Option<&'a str>>
) -> Self
[src]

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

pub fn oauth_parameters<'a>(
    self,
    consumer_key: &str,
    options: impl Into<Option<&'a Options<'a>>>
) -> Signer<SM, Ready>
[src]

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.

impl<SM: SignatureMethod, State> Signer<SM, State>[src]

pub fn parameter(&mut self, k: &str, v: impl Display) -> &mut Self[src]

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

pub fn parameter_encoded(&mut self, k: &str, v: impl Display) -> &mut Self[src]

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.

impl<SM: SignatureMethod> Signer<SM, NotReady>[src]

pub fn finish<'a>(
    self,
    consumer_key: &str,
    options: impl Into<Option<&'a Options<'a>>>
) -> Request
[src]

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

impl<SM: SignatureMethod> Signer<SM, Ready>[src]

pub fn finish(self) -> Request[src]

Consumes the Signer and returns a Request.

Trait Implementations

impl<SM: Clone + SignatureMethod, State: Clone> Clone for Signer<SM, State> where
    SM::Sign: Clone
[src]

impl<SM: Debug + SignatureMethod, State: Debug> Debug for Signer<SM, State> where
    SM::Sign: Debug
[src]

Auto Trait Implementations

impl<SM, State> RefUnwindSafe for Signer<SM, State> where
    <SM as SignatureMethod>::Sign: RefUnwindSafe

impl<SM, State> Send for Signer<SM, State> where
    <SM as SignatureMethod>::Sign: Send

impl<SM, State> Sync for Signer<SM, State> where
    <SM as SignatureMethod>::Sign: Sync

impl<SM, State> Unpin for Signer<SM, State> where
    <SM as SignatureMethod>::Sign: Unpin

impl<SM, State> UnwindSafe for Signer<SM, State> where
    <SM as SignatureMethod>::Sign: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,