doc_auto_cfg! {
#[cfg(feature = "hmac-sha1")]
pub mod hmac_sha1;
pub mod plaintext;
#[cfg(feature = "rsa-sha1-06")]
pub mod rsa_sha1_06;
#[cfg(feature = "rsa-sha1-09")]
pub mod rsa_sha1_09;
}
#[cfg(any(
feature = "hmac-sha1",
feature = "rsa-sha1-06",
feature = "rsa-sha1-09"
))]
mod digest_common;
#[cfg(feature = "either")]
mod either;
doc_auto_cfg! {
#[cfg(feature = "hmac-sha1")]
pub use self::hmac_sha1::HmacSha1;
#[cfg(feature = "hmac-sha1")]
pub use self::hmac_sha1::HMAC_SHA1;
pub use self::plaintext::Plaintext;
#[cfg(feature = "alloc")]
pub use self::plaintext::PLAINTEXT;
#[cfg(feature = "rsa-sha1-06")]
pub use self::rsa_sha1_06::RsaSha1;
#[cfg(feature = "rsa-sha1-09")]
pub use self::rsa_sha1_09::RsaSha1 as Rsa09Sha1;
}
use core::fmt::{self, Display, Write};
use crate::util::percent_encode;
pub trait SignatureMethod {
type Sign: Sign;
fn sign_with(self, client_secret: &str, token_secret: Option<&str>) -> Self::Sign;
}
macro_rules! provide {
($(#[doc = $doc:expr])+ $name:ident, $($rest:tt)*) => {
$(#[doc = $doc])+
fn $name<V: Display>(&mut self, value: V) {
self.parameter(concat!("oauth_", stringify!($name)), value);
}
provide! { $($rest)* }
};
($name:ident, $($rest:tt)*) => {
provide! {
#[doc = concat!(
"Feeds `self` with the `oauth_", stringify!($name), "` parameter part of the signature base string.
The default implementation forwards to the `parameter` method with `\"oauth_",
stringify!($name), "\"` as the first argument."
)]
$name, $($rest)*
}
};
() => {};
}
#[cfg_attr(feature = "alloc", doc = " ```")]
#[cfg_attr(not(feature = "alloc"), doc = " ```ignore")]
pub trait Sign {
type Signature: Display;
fn get_signature_method_name(&self) -> &'static str;
fn request_method(&mut self, method: &str);
fn uri<T: Display>(&mut self, uri: T);
fn parameter<V: Display>(&mut self, key: &str, value: V);
fn delimiter(&mut self);
fn end(self) -> Self::Signature;
provide! { callback, consumer_key, nonce, }
fn use_nonce(&self) -> bool {
true
}
fn signature_method(&mut self) {
self.parameter("oauth_signature_method", self.get_signature_method_name());
}
fn timestamp(&mut self, value: u64) {
self.parameter("oauth_timestamp", value);
}
fn use_timestamp(&self) -> bool {
true
}
provide! { token, verifier, }
fn version(&mut self) {
self.parameter("oauth_version", "1.0");
}
}
fn write_signing_key<W: Write>(
dst: &mut W,
client_secret: &str,
token_secret: Option<&str>,
) -> fmt::Result {
write!(dst, "{}", percent_encode(client_secret))?;
dst.write_str("&")?;
if let Some(ts) = token_secret {
write!(dst, "{}", percent_encode(ts))?;
}
Ok(())
}