Skip to content

Commit

Permalink
Make SignatureMethod::sign_with take &str-s instead of opaque type
Browse files Browse the repository at this point in the history
`Authorizer` used to pass percent-encoded secrets to `sign_with`, which
is an undocumented behavior. That behavior is inflexible since cannot
opt to inspect the original secret string. So, this commit also makes
`Authorizer` pass unencoded secrets.
  • Loading branch information
tesaguri committed Oct 18, 2020
1 parent 8529ed6 commit 187522e
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 44 deletions.
13 changes: 3 additions & 10 deletions oauth1-request/src/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,9 @@ mod tests {
impl<SM: SignatureMethod> SignatureMethod for Inspect<SM> {
type Sign = InspectSign<SM::Sign>;

fn sign_with<C: Display, T: Display>(
self,
client_secret: C,
token_secret: Option<T>,
) -> Self::Sign {
println!("client_secret: {:?}", client_secret.to_string());
println!(
"token_secret: {:?}",
token_secret.as_ref().map(ToString::to_string)
);
fn sign_with(self, client_secret: &str, token_secret: Option<&str>) -> Self::Sign {
println!("client_secret: {:?}", client_secret);
println!("token_secret: {:?}", token_secret);
InspectSign(self.0.sign_with(client_secret, token_secret))
}
}
Expand Down
5 changes: 1 addition & 4 deletions oauth1-request/src/serializer/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ impl<'a, SM: SignatureMethod> Authorizer<'a, SM> {
token: Option<Credentials<&'a str>>,
options: &'a Options<'a>,
) -> Self {
let mut sign = signature_method.sign_with(
percent_encode(client.secret),
token.as_ref().map(Credentials::secret).map(percent_encode),
);
let mut sign = signature_method.sign_with(client.secret, token.map(|t| t.secret));

let mut authorization = String::with_capacity(512);
authorization.push_str("OAuth ");
Expand Down
18 changes: 7 additions & 11 deletions oauth1-request/src/signature_method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub use plaintext::Plaintext;

use std::fmt::{Display, Write};

use crate::util::percent_encode;

/// Types that represent a signature method.
///
/// This is used to construct a `Self::Sign` and carries configuration data for them.
Expand All @@ -32,10 +34,7 @@ pub trait SignatureMethod {
type Sign: Sign;

/// Creates a `Self::Sign` that signs a signature base string with the given shared-secrets.
fn sign_with<C, T>(self, client_secret: C, token_secret: Option<T>) -> Self::Sign
where
C: Display,
T: Display;
fn sign_with(self, client_secret: &str, token_secret: Option<&str>) -> Self::Sign;
}

macro_rules! provide {
Expand Down Expand Up @@ -193,13 +192,10 @@ pub trait Sign {
}
}

fn write_signing_key<W: Write>(
dst: &mut W,
client_secret: impl Display,
token_secret: Option<impl Display>,
) {
write!(dst, "{}&", client_secret).unwrap();
fn write_signing_key<W: Write>(dst: &mut W, client_secret: &str, token_secret: Option<&str>) {
write!(dst, "{}", percent_encode(client_secret)).unwrap();
dst.write_str("&").unwrap();
if let Some(ts) = token_secret {
write!(dst, "{}", ts).unwrap();
write!(dst, "{}", percent_encode(ts)).unwrap();
}
}
6 changes: 1 addition & 5 deletions oauth1-request/src/signature_method/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ use super::*;
impl<L: SignatureMethod, R: SignatureMethod> SignatureMethod for Either<L, R> {
type Sign = Either<L::Sign, R::Sign>;

fn sign_with<C, T>(self, client_secret: C, token_secret: Option<T>) -> Self::Sign
where
C: Display,
T: Display,
{
fn sign_with(self, client_secret: &str, token_secret: Option<&str>) -> Self::Sign {
match self {
Either::Left(l) => Either::Left(l.sign_with(client_secret, token_secret)),
Either::Right(r) => Either::Right(r.sign_with(client_secret, token_secret)),
Expand Down
9 changes: 3 additions & 6 deletions oauth1-request/src/signature_method/hmac_sha1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ use sha1::digest::generic_array::{ArrayLength, GenericArray};
use sha1::digest::{BlockInput, FixedOutput, Reset, Update};
use sha1::Sha1;

use super::*;
use crate::util::PercentEncode;

use super::*;

/// The `HMAC-SHA1` signature method.
#[derive(Copy, Clone, Debug, Default)]
pub struct HmacSha1;
Expand Down Expand Up @@ -45,11 +46,7 @@ enum SigningKey<D: BlockInput> {
impl SignatureMethod for HmacSha1 {
type Sign = HmacSha1Sign;

fn sign_with<C, T>(self, client_secret: C, token_secret: Option<T>) -> HmacSha1Sign
where
C: Display,
T: Display,
{
fn sign_with(self, client_secret: &str, token_secret: Option<&str>) -> HmacSha1Sign {
let mut key = SigningKey::new();
write_signing_key(&mut key, client_secret, token_secret);
HmacSha1Sign {
Expand Down
2 changes: 1 addition & 1 deletion oauth1-request/src/signature_method/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct IdentitySign(pub String);
impl SignatureMethod for Identity {
type Sign = IdentitySign;

fn sign_with<C, T>(self, _client_secret: C, _token_secret: Option<T>) -> IdentitySign {
fn sign_with(self, _client_secret: &str, _token_secret: Option<&str>) -> IdentitySign {
IdentitySign(String::new())
}
}
Expand Down
8 changes: 1 addition & 7 deletions oauth1-request/src/signature_method/plaintext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
//!
//! [rfc]: https://tools.ietf.org/html/rfc5849#section-3.4.4
use std::fmt::Display;

use super::*;

/// The `PLAINTEXT` signature method.
Expand All @@ -17,11 +15,7 @@ pub struct PlaintextSign(String);
impl SignatureMethod for Plaintext {
type Sign = PlaintextSign;

fn sign_with<C, T>(self, client_secret: C, token_secret: Option<T>) -> PlaintextSign
where
C: Display,
T: Display,
{
fn sign_with(self, client_secret: &str, token_secret: Option<&str>) -> PlaintextSign {
let mut key = String::with_capacity(128);
write_signing_key(&mut key, client_secret, token_secret);
PlaintextSign(key)
Expand Down

0 comments on commit 187522e

Please sign in to comment.