#![doc(html_root_url = "https://docs.rs/oauth1-request/0.3.3")]
#![deny(intra_doc_link_resolution_failure)]
#![warn(missing_docs, rust_2018_idioms)]
pub mod authorize;
pub mod signature_method;
pub mod signer;
#[macro_use]
mod util;
#[cfg(feature = "derive")]
pub use oauth1_request_derive::Authorize;
pub use authorize::Authorize;
#[cfg(feature = "hmac-sha1")]
pub use signature_method::HmacSha1;
pub use signature_method::Plaintext;
use std::borrow::Borrow;
use std::fmt::{Debug, Display};
use std::num::NonZeroU64;
use std::str;
use signature_method::SignatureMethod;
use signer::Signer;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Request {
pub authorization: String,
pub data: String,
}
#[derive(Clone, Debug)]
pub struct Builder<'a, SM, T = String> {
signature_method: SM,
client: Credentials<T>,
token: Option<Credentials<T>>,
options: Options<'a>,
}
pub type Credentials<T = String> = oauth_credentials::Credentials<T>;
options! {
#[derive(Clone, Debug, Default)]
pub struct Options<'a> {
new;
callback: Option<&'a str>,
nonce: Option<&'a str>,
timestamp: Option<NonZeroU64>,
token: Option<&'a str>,
verifier: Option<&'a str>,
version: bool,
}
}
impl<'a, SM: SignatureMethod, T: Borrow<str>> Builder<'a, SM, T> {
pub fn new(client: Credentials<T>, signature_method: SM) -> Self {
Builder {
signature_method,
client,
token: None,
options: Options::new(),
}
}
pub fn token(&mut self, token: impl Into<Option<Credentials<T>>>) -> &mut Self {
self.token = token.into();
self
}
pub fn callback(&mut self, callback: impl Into<Option<&'a str>>) -> &mut Self {
self.options.callback(callback);
self
}
pub fn nonce(&mut self, nonce: impl Into<Option<&'a str>>) -> &mut Self {
self.options.nonce(nonce);
self
}
pub fn timestamp(&mut self, timestamp: impl Into<Option<u64>>) -> &mut Self {
self.options.timestamp(timestamp);
self
}
pub fn verifier(&mut self, verifier: impl Into<Option<&'a str>>) -> &mut Self {
self.options.verifier(verifier);
self
}
pub fn version(&mut self, version: bool) -> &mut Self {
self.options.version(version);
self
}
pub fn get<U: Display, A: Authorize>(&self, uri: U, request: A) -> Request
where
SM: Copy,
{
self.build("GET", uri, request)
}
pub fn put_form<U: Display, A: Authorize>(&self, uri: U, request: A) -> Request
where
SM: Copy,
{
self.build_form("PUT", uri, request)
}
pub fn post_form<U: Display, A: Authorize>(&self, uri: U, request: A) -> Request
where
SM: Copy,
{
self.build_form("POST", uri, request)
}
pub fn delete<U: Display, A: Authorize>(&self, uri: U, request: A) -> Request
where
SM: Copy,
{
self.build("DELETE", uri, request)
}
pub fn options<U: Display, A: Authorize>(&self, uri: U, request: A) -> Request
where
SM: Copy,
{
self.build("OPTIONS", uri, request)
}
pub fn head<U: Display, A: Authorize>(&self, uri: U, request: A) -> Request
where
SM: Copy,
{
self.build("HEAD", uri, request)
}
pub fn connect<U: Display, A: Authorize>(&self, uri: U, request: A) -> Request
where
SM: Copy,
{
self.build("CONNECT", uri, request)
}
pub fn patch_form<U: Display, A: Authorize>(&self, uri: U, request: A) -> Request
where
SM: Copy,
{
self.build_form("PATCH", uri, request)
}
pub fn trace<U: Display, A: Authorize>(&self, uri: U, request: A) -> Request
where
SM: Copy,
{
self.build("TRACE", uri, request)
}
pub fn build<U: Display, A: Authorize>(&self, method: &str, uri: U, request: A) -> Request
where
SM: Copy,
{
#[allow(deprecated)]
self.build_(method, uri, request, true)
}
pub fn build_form<U: Display, A: Authorize>(&self, method: &str, uri: U, request: A) -> Request
where
SM: Copy,
{
#[allow(deprecated)]
self.build_(method, uri, request, false)
}
#[deprecated(since = "0.3.1", note = "This method was made public by mistake")]
#[doc(hidden)]
pub fn build_<U, A>(&self, method: &str, uri: U, request: A, q: bool) -> Request
where
SM: Copy,
U: Display,
A: Authorize,
{
let mut options;
let (options, token_secret) = if let Some(ref token) = self.token {
options = self.options.clone();
options.token(token.identifier.borrow());
(&options, Some(token.secret.borrow()))
} else {
(&self.options, None)
};
let signer = Signer::new_(
method,
uri,
self.client.secret.borrow(),
token_secret,
self.signature_method,
q,
);
request.authorize_with(signer, self.client.identifier.borrow(), Some(options))
}
}