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
sourceimpl<SM: SignatureMethod> Signer<SM, NotReady>
impl<SM: SignatureMethod> Signer<SM, NotReady>
sourcepub fn new<'a>(
method: &str,
uri: impl Display,
consumer_secret: &str,
token_secret: impl Into<Option<&'a str>>
) -> Selfwhere
SM: Default,
pub fn new<'a>(
method: &str,
uri: impl Display,
consumer_secret: &str,
token_secret: impl Into<Option<&'a str>>
) -> Selfwhere
SM: Default,
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.
sourcepub fn with_signature_method<'a>(
signature_method: SM,
method: &str,
uri: impl Display,
consumer_secret: &str,
token_secret: impl Into<Option<&'a str>>
) -> Self
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
Same as new
except that this uses signature_method
as the signature method.
sourcepub fn new_form<'a>(
method: &str,
uri: impl Display,
consumer_secret: &str,
token_secret: impl Into<Option<&'a str>>
) -> Selfwhere
SM: Default,
pub fn new_form<'a>(
method: &str,
uri: impl Display,
consumer_secret: &str,
token_secret: impl Into<Option<&'a str>>
) -> Selfwhere
SM: Default,
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.
sourcepub 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
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
Same as new_form
except that this uses signature_method
as the signature method.
sourcepub fn oauth_parameters<'a>(
self,
consumer_key: &str,
options: impl Into<Option<&'a Options<'a>>>
) -> Signer<SM, Ready>
pub fn oauth_parameters<'a>(
self,
consumer_key: &str,
options: impl Into<Option<&'a Options<'a>>>
) -> Signer<SM, Ready>
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.
sourceimpl<SM: SignatureMethod, State> Signer<SM, State>
impl<SM: SignatureMethod, State> Signer<SM, State>
sourcepub fn parameter(&mut self, k: &str, v: impl Display) -> &mut Self
pub fn parameter(&mut self, k: &str, v: impl Display) -> &mut Self
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