#![cfg_attr(all(feature = "derive", feature = "hmac-sha1"), doc = " ```")]
#![cfg_attr(
not(all(feature = "derive", feature = "hmac-sha1")),
doc = " ```ignore"
)]
#![cfg_attr(feature = "alloc", doc = " ```")]
#![cfg_attr(not(feature = "alloc"), doc = " ```ignore")]
#![cfg_attr(all(feature = "alloc", feature = "hmac-sha1"), doc = " ```")]
#![cfg_attr(not(all(feature = "alloc", feature = "hmac-sha1")), doc = " ```ignore")]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![doc(html_root_url = "https://docs.rs/oauth1-request-ios/0.0.1")]
#![warn(missing_docs, rust_2018_idioms)]
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "alloc")]
extern crate alloc;
#[macro_use]
mod util;
pub mod request;
pub mod serializer;
pub mod signature_method;
doc_auto_cfg! {
#[cfg_attr(feature = "alloc", doc = " ```")]
#[cfg_attr(not(feature = "alloc"), doc = " ```ignore")]
#[cfg(feature = "derive")]
#[doc(inline)]
pub use oauth1_request_derive_ios::Request;
}
#[doc(no_inline)]
pub use oauth_credentials_ios::{Credentials, Token};
doc_auto_cfg! {
pub use self::request::ParameterList;
pub use self::request::Request;
#[cfg(feature = "hmac-sha1")]
pub use self::signature_method::HmacSha1;
pub use self::signature_method::Plaintext;
#[cfg(feature = "hmac-sha256")]
pub use self::signature_method::HmacSha256;
#[cfg(feature = "rsa-sha1-06")]
pub use self::signature_method::RsaSha1;
#[cfg(feature = "rsa-sha1-09")]
pub use self::signature_method::Rsa09Sha1;
#[cfg(feature = "hmac-sha1")]
pub use self::signature_method::HMAC_SHA1;
#[cfg(feature = "hmac-sha256")]
pub use self::signature_method::HMAC_SHA256;
#[cfg(feature = "alloc")]
pub use self::signature_method::PLAINTEXT;
}
#[cfg(feature = "alloc")]
use alloc::string::String;
use core::fmt::Debug;
use core::fmt::{Display, Write};
use core::num::NonZeroU64;
use core::str;
use self::serializer::auth;
use self::signature_method::SignatureMethod;
cfg_type_param_hack! {
#[derive(Clone, Debug)]
pub struct Builder<
'a,
SM,
#[cfg(feature = "alloc")] C = String,
#[cfg(not(feature = "alloc"))] C,
T = C,
> {
signature_method: SM,
client: Credentials<C>,
token: Option<Credentials<T>>,
options: auth::Options<'a>,
}
}
macro_rules! builder_authorize_shorthand {
($($name:ident($method:expr);)*) => {doc_auto_cfg! {$(
#[doc = concat!("Authorizes a `", $method, "` request to `uri`,")]
#[cfg(feature = "alloc")]
pub fn $name<U, R>(&self, uri: U, request: &R) -> String
where
U: Display,
R: Request + ?Sized,
SM: Clone,
{
self.authorize($method, uri, request)
}
)*}};
}
macro_rules! builder_to_form_shorthand {
($($name:ident($method:expr);)*) => {doc_auto_cfg! {$(
#[doc = concat!("Authorizes a `", $method, "` request to `uri`,")]
#[cfg(feature = "alloc")]
pub fn $name<U, R>(&self, uri: U, request: &R) -> String
where
U: Display,
R: Request + ?Sized,
SM: Clone,
{
self.to_form($method, uri, request)
}
)*}};
}
macro_rules! builder_to_query_shorthand {
($($name:ident($method:expr);)*) => {$(
doc_coerce_expr! {
#[doc = concat!("Authorizes a `", $method, "` request to `uri`, appending")]
pub fn $name<W, R>(&self, uri: W, request: &R) -> W
where
W: Display + Write,
R: Request + ?Sized,
SM: Clone,
{
self.to_query($method, uri, request)
}
}
)*};
}
impl<'a, SM: SignatureMethod, C: AsRef<str>, T: AsRef<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
}
builder_authorize_shorthand! {
get("GET");
put("PUT");
post("POST");
delete("DELETE");
options("OPTIONS");
head("HEAD");
connect("CONNECT");
patch("PATCH");
trace("TRACE");
}
builder_to_form_shorthand! {
put_form("PUT");
post_form("POST");
options_form("OPTIONS");
patch_form("PATCH");
}
builder_to_query_shorthand! {
get_query("GET");
put_query("PUT");
post_query("POST");
delete_query("DELETE");
options_query("OPTIONS");
head_query("HEAD");
connect_query("CONNECT");
patch_query("PATCH");
trace_query("TRACE");
}
doc_auto_cfg! {
#[cfg(feature = "alloc")]
pub fn authorize<U, R>(&self, method: &str, uri: U, request: &R) -> String
where
U: Display,
R: Request + ?Sized,
SM: Clone,
{
let serializer = serializer::auth::Authorizer::authorization(
method,
uri,
self.client.as_ref(),
self.token.as_ref().map(Credentials::as_ref),
&self.options,
self.signature_method.clone(),
);
request.serialize(serializer)
}
#[cfg(feature = "alloc")]
pub fn to_form<U, R>(&self, method: &str, uri: U, request: &R) -> String
where
U: Display,
R: Request + ?Sized,
SM: Clone,
{
let serializer = serializer::auth::Authorizer::form(
method,
uri,
self.client.as_ref(),
self.token.as_ref().map(Credentials::as_ref),
&self.options,
self.signature_method.clone(),
);
request.serialize(serializer)
}
}
pub fn to_query<W, R>(&self, method: &str, uri: W, request: &R) -> W
where
W: Display + Write,
R: Request + ?Sized,
SM: Clone,
{
let serializer = serializer::auth::Authorizer::query(
method,
uri,
self.client.as_ref(),
self.token.as_ref().map(Credentials::as_ref),
&self.options,
self.signature_method.clone(),
);
request.serialize(serializer)
}
pub fn authorize_with_buf<W, U, R>(&self, buf: W, method: &str, uri: U, request: &R) -> W
where
W: Write,
U: Display,
R: Request + ?Sized,
SM: Clone,
{
let serializer = serializer::auth::Authorizer::authorization_with_buf(
buf,
method,
uri,
self.client.as_ref(),
self.token.as_ref().map(Credentials::as_ref),
&self.options,
self.signature_method.clone(),
);
request.serialize(serializer)
}
doc_auto_cfg! {
#[cfg(feature = "alloc")]
pub fn to_form_with_buf<W, U, R>(&self, buf: W, method: &str, uri: U, request: &R) -> W
where
W: Write,
U: Display,
R: Request + ?Sized,
SM: Clone,
{
let serializer = serializer::auth::Authorizer::form_with_buf(
buf,
method,
uri,
self.client.as_ref(),
self.token.as_ref().map(Credentials::as_ref),
&self.options,
self.signature_method.clone(),
);
request.serialize(serializer)
}
#[cfg(feature = "alloc")]
pub fn into_authorization<U, R>(self, method: &str, uri: U, request: &R) -> String
where
U: Display,
R: Request + ?Sized,
{
let serializer = serializer::auth::Authorizer::authorization(
method,
uri,
self.client.as_ref(),
self.token.as_ref().map(Credentials::as_ref),
&self.options,
self.signature_method,
);
request.serialize(serializer)
}
#[cfg(feature = "alloc")]
pub fn into_form<U, R>(self, method: &str, uri: U, request: &R) -> String
where
U: Display,
R: Request + ?Sized,
{
let serializer = serializer::auth::Authorizer::form(
method,
uri,
self.client.as_ref(),
self.token.as_ref().map(Credentials::as_ref),
&self.options,
self.signature_method,
);
request.serialize(serializer)
}
}
pub fn into_query<W, R>(self, method: &str, uri: W, request: &R) -> W
where
W: Display + Write,
R: Request + ?Sized,
{
let serializer = serializer::auth::Authorizer::query(
method,
uri,
self.client.as_ref(),
self.token.as_ref().map(Credentials::as_ref),
&self.options,
self.signature_method,
);
request.serialize(serializer)
}
pub fn into_authorization_with_buf<W, U, R>(
self,
buf: W,
method: &str,
uri: U,
request: &R,
) -> W
where
W: Write,
U: Display,
R: Request + ?Sized,
SM: Clone,
{
let serializer = serializer::auth::Authorizer::authorization_with_buf(
buf,
method,
uri,
self.client.as_ref(),
self.token.as_ref().map(Credentials::as_ref),
&self.options,
self.signature_method,
);
request.serialize(serializer)
}
pub fn into_form_with_buf<W, U, R>(self, buf: W, method: &str, uri: U, request: &R) -> W
where
W: Write,
U: Display,
R: Request + ?Sized,
{
let serializer = serializer::auth::Authorizer::form_with_buf(
buf,
method,
uri,
self.client.as_ref(),
self.token.as_ref().map(Credentials::as_ref),
&self.options,
self.signature_method,
);
request.serialize(serializer)
}
}
macro_rules! authorize_shorthand {
($($name:ident($method:expr);)*) => {doc_auto_cfg! {$(
#[doc = concat!("Authorizes a `", $method, "` request to `uri` with the given credentials.")]
#[cfg(feature = "alloc")]
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: AsRef<str>,
T: AsRef<str>,
SM: SignatureMethod,
{
authorize($method, uri, request, token, signature_method)
}
)*}};
}
authorize_shorthand! {
get("GET");
put("PUT");
post("POST");
delete("DELETE");
options("OPTIONS");
head("HEAD");
connect("CONNECT");
patch("PATCH");
trace("TRACE");
}
doc_auto_cfg! {
#[cfg(feature = "alloc")]
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: AsRef<str>,
T: AsRef<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).into_authorization(method, uri, request)
}
inner(method, uri, request, token.as_ref(), signature_method)
}
#[cfg(feature = "alloc")]
pub fn to_form<R>(request: &R) -> String
where
R: Request + ?Sized,
{
request.serialize(serializer::Urlencoder::form())
}
#[cfg(feature = "alloc")]
pub fn to_query<R>(uri: String, request: &R) -> String
where
R: Request + ?Sized,
{
request.serialize(serializer::Urlencoder::query(uri))
}
}