Skip to content

Commit

Permalink
Consolidate serialization of HelperIdentity
Browse files Browse the repository at this point in the history
  • Loading branch information
andyleiserson committed May 24, 2023
1 parent aa9849d commit 6292d3a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
19 changes: 15 additions & 4 deletions src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,24 @@ pub const MESSAGE_PAYLOAD_SIZE_BYTES: usize = MessagePayloadArrayLen::USIZE;
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(
feature = "enable-serde",
derive(serde::Serialize, serde::Deserialize),
serde(transparent)
derive(serde::Deserialize),
serde(try_from = "usize")
)]
pub struct HelperIdentity {
id: u8,
}

// Serialize as `serde(transparent)` would. Don't see how to enable that
// for only one of (de)serialization.
impl serde::Serialize for HelperIdentity {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.id.serialize(serializer)
}
}

impl TryFrom<usize> for HelperIdentity {
type Error = String;

Expand Down Expand Up @@ -99,8 +110,8 @@ impl Debug for HelperIdentity {
#[cfg(feature = "web-app")]
impl From<HelperIdentity> for hyper::header::HeaderValue {
fn from(id: HelperIdentity) -> Self {
// does not implement `From<u8>`
hyper::header::HeaderValue::from(u16::from(id.id))
// panic if serializing an integer fails, or is not ASCII
hyper::header::HeaderValue::try_from(serde_json::to_string(&id).unwrap()).unwrap()
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/net/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ impl MpcHelperClient {
error!("certificate identity ignored for HTTP client");
None
}
ClientIdentity::Helper(id) => Some((HTTP_CLIENT_ID_HEADER.clone(), id.into())),
ClientIdentity::Helper(id) => Some((
HTTP_CLIENT_ID_HEADER.clone(),
id.try_into().expect("integer not ascii?"),
)),
ClientIdentity::None => None,
};
(HttpsConnector::new(), auth_header)
Expand Down
16 changes: 4 additions & 12 deletions src/net/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use std::{
io,
net::{Ipv4Addr, SocketAddr, TcpListener},
ops::Deref,
str::FromStr,
task::{Context, Poll},
};
use tokio_rustls::{
Expand Down Expand Up @@ -447,7 +446,7 @@ pub static HTTP_CLIENT_ID_HEADER: HeaderName =
/// Since this allows a client to claim any identity, it is completely
/// insecure. It must only be used in contexts where that is acceptable.
#[derive(Clone)]
pub(super) struct SetClientIdentityFromHeader<S> {
struct SetClientIdentityFromHeader<S> {
inner: S,
}

Expand All @@ -470,16 +469,9 @@ impl<B, S: Service<Request<B>, Response = Response>> Service<Request<B>>
}

fn call(&mut self, mut req: Request<B>) -> Self::Future {
if let Some(header_value) = req.headers().get(HTTP_CLIENT_ID_HEADER.clone()) {
let id_result = header_value
.to_str()
.map_err(Into::into)
.and_then(|value_str| usize::from_str(value_str).map_err(Into::into))
.and_then(|value_int| {
HelperIdentity::try_from(value_int).map_err(|e| {
Error::InvalidHeader(format!("{HTTP_CLIENT_ID_HEADER}: {e:?}").into())
})
});
if let Some(header_value) = req.headers().get(&HTTP_CLIENT_ID_HEADER) {
let id_result = serde_json::from_slice(header_value.as_ref())
.map_err(|e| Error::InvalidHeader(format!("{HTTP_CLIENT_ID_HEADER}: {e}").into()));
match id_result {
Ok(id) => req.extensions_mut().insert(ClientIdentity(id)),
Err(err) => return ready(Ok(err.into_response())).right_future(),
Expand Down

0 comments on commit 6292d3a

Please sign in to comment.