#![doc(html_root_url = "https://docs.rs/oauth1-request/0.4.2")]
#![deny(broken_intra_doc_links)]
#![warn(missing_docs, rust_2018_idioms)]
#[macro_use]
mod util;
pub mod serializer;
pub mod signature_method;
mod request;
#[cfg(feature = "derive")]
#[doc(inline)]
pub use oauth1_request_derive::Request;
#[doc(no_inline)]
pub use oauth_credentials::{Credentials, Token};
pub use request::Request;
#[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 serializer::auth::{self, Authorizer};
use serializer::Urlencoder;
use signature_method::SignatureMethod;
#[derive(Clone, Debug)]
pub struct Builder<'a, SM, C = String, T = C> {
signature_method: SM,
client: Credentials<C>,
token: Option<Credentials<T>>,
options: auth::Options<'a>,
}
impl<'a, SM: SignatureMethod, C: Borrow<str>, T: Borrow<str>> Builder<'a, SM, C, T> {
pub fn new(client: Credentials<C>, signature_method: SM) -> Self {
Builder {
signature_method,
client,
token: None,
options: auth::Options::new(),
}
}
pub fn with_token(token: Token<C, T>, signature_method: SM) -> Self {
let mut ret = Builder::new(token.client, signature_method);
ret.token(token.token);
ret
}
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 verifier(&mut self, verifier: impl Into<Option<&'a str>>) -> &mut Self {
self.options.verifier(verifier);
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<NonZeroU64>>) -> &mut Self {
self.options.timestamp(timestamp);
self
}
pub fn version(&mut self, version: bool) -> &mut Self {
self.options.version(version);
self
}
pub fn get<U: Display, R: Request + ?Sized>(&self, uri: U, request: &R) -> String
where
SM: Clone,
{
self.build("GET", uri, request)
}
pub fn put<U: Display, R: Request + ?Sized>(&self, uri: U, request: &R) -> String
where
SM: Clone,
{
self.build("PUT", uri, request)
}
pub fn post<U: Display, R: Request + ?Sized>(&self, uri: U, request: &R) -> String
where
SM: Clone,
{
self.build("POST", uri, request)
}
pub fn delete<U: Display, R: Request + ?Sized>(&self, uri: U, request: &R) -> String
where
SM: Clone,
{
self.build("DELETE", uri, request)
}
pub fn options<U: Display, R: Request + ?Sized>(&self, uri: U, request: &R) -> String
where
SM: Clone,
{
self.build("OPTIONS", uri, request)
}
pub fn head<U: Display, R: Request + ?Sized>(&self, uri: U, request: &R) -> String
where
SM: Clone,
{
self.build("HEAD", uri, request)
}
pub fn connect<U: Display, R: Request + ?Sized>(&self, uri: U, request: &R) -> String
where
SM: Clone,
{
self.build("CONNECT", uri, request)
}
pub fn patch<U: Display, R: Request + ?Sized>(&self, uri: U, request: &R) -> String
where
SM: Clone,
{
self.build("PATCH", uri, request)
}
pub fn trace<U: Display, R: Request + ?Sized>(&self, uri: U, request: &R) -> String
where
SM: Clone,
{
self.build("TRACE", uri, request)
}
pub fn build<U: Display, R: Request + ?Sized>(
&self,
method: &str,
uri: U,
request: &R,
) -> String
where
SM: Clone,
{
let serializer = Authorizer::with_signature_method(
self.signature_method.clone(),
method,
uri,
self.client.as_ref(),
self.token.as_ref().map(Credentials::as_ref),
&self.options,
);
request.serialize(serializer)
}
pub fn consume<U: Display, R: Request + ?Sized>(
self,
method: &str,
uri: U,
request: &R,
) -> String {
let serializer = Authorizer::with_signature_method(
self.signature_method,
method,
uri,
self.client.as_ref(),
self.token.as_ref().map(Credentials::as_ref),
&self.options,
);
request.serialize(serializer)
}
}
macro_rules! def_shorthand {
($($(#[$attr:meta])* $name:ident($method:expr);)*) => {$(
$(#[$attr])*
pub fn $name<U, R, C, T, SM>(
uri: U,
request: &R,
token: &Token<C, T>,
signature_method: SM,
) -> String
where
U: Display,
R: Request + ?Sized,
C: Borrow<str>,
T: Borrow<str>,
SM: SignatureMethod,
{
authorize($method, uri, request, token, signature_method)
}
)*};
}
def_shorthand! {
get("GET");
put("PUT");
post("POST");
delete("DELETE");
options("OPTIONS");
head("HEAD");
connect("CONNECT");
patch("PATCH");
trace("TRACE");
}
pub fn authorize<U, R, C, T, SM>(
method: &str,
uri: U,
request: &R,
token: &Token<C, T>,
signature_method: SM,
) -> String
where
U: Display,
R: Request + ?Sized,
C: Borrow<str>,
T: Borrow<str>,
SM: SignatureMethod,
{
fn inner<U, R, SM>(
method: &str,
uri: U,
request: &R,
token: Token<&str, &str>,
signature_method: SM,
) -> String
where
U: Display,
R: Request + ?Sized,
SM: SignatureMethod,
{
Builder::with_token(token, signature_method).consume(method, uri, request)
}
inner(method, uri, request, token.as_ref(), signature_method)
}
pub fn to_form_urlencoded<R>(request: &R) -> String
where
R: Request + ?Sized,
{
request.serialize(Urlencoder::form())
}
pub fn to_uri_query<R>(uri: String, request: &R) -> String
where
R: Request + ?Sized,
{
request.serialize(Urlencoder::query(uri))
}