cfg_if::cfg_if! {
if #[cfg(feature = "hmac-sha1")] {
pub mod hmac_sha1;
pub use hmac_sha1::HmacSha1;
}
}
pub mod identity;
pub mod plaintext;
#[cfg(feature = "either")]
mod either;
pub use identity::Identity;
pub use plaintext::Plaintext;
use std::fmt::{Display, Write};
pub trait SignatureMethod {
type Sign: Sign;
fn sign_with(
self,
consumer_secret: impl Display,
token_secret: Option<impl Display>,
) -> Self::Sign;
}
macro_rules! provide {
($(#[doc = $doc:expr])+ $name:ident, $($rest:tt)*) => {
$(#[doc = $doc])+
fn $name(&mut self, default_key: &'static str, value: impl Display) {
self.parameter(default_key, value);
}
provide! { $($rest)* }
};
($name:ident, $($rest:tt)*) => {
provide! {
#[doc = concat!(
"Feeds `self` with the `oauth_", stringify!($name), "` parameter part of the signature base string.
`default_key` argument is passed just for the convenience of implementors and is always `\"oauth_",
stringify!($name), "\"`.
The default implementation forwards to the `parameter` method."
)]
$name, $($rest)*
}
};
() => {};
}
pub trait Sign {
type Signature: Display;
fn get_signature_method_name(&self) -> &'static str;
fn request_method(&mut self, method: &str);
fn uri(&mut self, uri: impl Display);
fn parameter(&mut self, key: &str, value: impl Display);
fn delimiter(&mut self);
fn finish(self) -> Self::Signature;
provide! { callback, consumer_key, nonce, }
fn use_nonce(&self) -> bool {
true
}
fn signature_method(&mut self, default_key: &'static str, default_value: &'static str) {
self.parameter(default_key, default_value);
}
fn timestamp(&mut self, default_key: &'static str, value: u64) {
self.parameter(default_key, value);
}
fn use_timestamp(&self) -> bool {
true
}
provide! { token, verifier, }
fn version(&mut self, default_key: &'static str, default_value: &'static str) {
self.parameter(default_key, default_value);
}
}
fn write_signing_key<W: Write>(w: &mut W, cs: impl Display, ts: Option<impl Display>) {
write!(w, "{}&", cs).unwrap();
if let Some(ts) = ts {
write!(w, "{}", ts).unwrap();
}
}