2019-09-21 16:26:11 +00:00
|
|
|
//! Types for setting up Digest middleware verification
|
|
|
|
|
2019-09-13 01:12:35 +00:00
|
|
|
use actix_web::{
|
2019-09-13 23:27:04 +00:00
|
|
|
dev::{Body, Payload, Service, ServiceRequest, ServiceResponse, Transform},
|
2019-09-13 01:12:35 +00:00
|
|
|
error::PayloadError,
|
2020-03-16 00:29:47 +00:00
|
|
|
http::{header::HeaderValue, StatusCode},
|
2019-09-13 23:27:04 +00:00
|
|
|
FromRequest, HttpMessage, HttpRequest, HttpResponse, ResponseError,
|
2019-09-13 01:12:35 +00:00
|
|
|
};
|
2020-03-16 00:29:47 +00:00
|
|
|
use bytes::{Bytes, BytesMut};
|
2019-09-13 01:12:35 +00:00
|
|
|
use futures::{
|
2020-03-16 00:29:47 +00:00
|
|
|
future::{err, ok, ready, Ready},
|
2019-09-13 01:12:35 +00:00
|
|
|
stream::once,
|
2020-03-16 00:29:47 +00:00
|
|
|
Stream, StreamExt,
|
|
|
|
};
|
|
|
|
use std::{
|
|
|
|
cell::RefCell,
|
|
|
|
future::Future,
|
|
|
|
pin::Pin,
|
|
|
|
rc::Rc,
|
|
|
|
task::{Context, Poll},
|
2019-09-13 01:12:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
use super::{DigestPart, DigestVerify};
|
|
|
|
|
2019-09-13 23:27:04 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2019-09-21 16:26:11 +00:00
|
|
|
/// A type implementing FromRequest that can be used in route handler to guard for verified
|
|
|
|
/// digests
|
|
|
|
///
|
|
|
|
/// This is only required when the [`VerifyDigest`] middleware is set to optional
|
2019-09-13 23:27:04 +00:00
|
|
|
pub struct DigestVerified;
|
2019-09-21 16:26:11 +00:00
|
|
|
|
|
|
|
/// The VerifyDigest middleware
|
|
|
|
///
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// let middleware = VerifyDigest::new(MyVerify::new())
|
|
|
|
/// .optional();
|
|
|
|
///
|
|
|
|
/// HttpServer::new(move || {
|
|
|
|
/// App::new()
|
|
|
|
/// .wrap(middleware.clone())
|
|
|
|
/// .route("/protected", web::post().to(|_: DigestVerified| "Verified Digest Header"))
|
|
|
|
/// .route("/unprotected", web::post().to(|| "No verification required"))
|
|
|
|
/// })
|
|
|
|
/// ```
|
2019-09-13 01:12:35 +00:00
|
|
|
pub struct VerifyDigest<T>(bool, T);
|
2019-09-21 16:26:11 +00:00
|
|
|
|
|
|
|
#[doc(hidden)]
|
2019-09-13 01:12:35 +00:00
|
|
|
pub struct VerifyMiddleware<T, S>(Rc<RefCell<S>>, bool, T);
|
2019-09-21 16:26:11 +00:00
|
|
|
|
2020-03-16 00:29:47 +00:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
#[error("Error verifying digest")]
|
2019-09-21 16:26:11 +00:00
|
|
|
#[doc(hidden)]
|
2019-09-13 01:12:35 +00:00
|
|
|
pub struct VerifyError;
|
|
|
|
|
|
|
|
impl<T> VerifyDigest<T>
|
|
|
|
where
|
|
|
|
T: DigestVerify + Clone,
|
|
|
|
{
|
2019-09-21 16:26:11 +00:00
|
|
|
/// Produce a new VerifyDigest with a user-provided [`Digestverify`] type
|
2019-09-13 01:12:35 +00:00
|
|
|
pub fn new(verify_digest: T) -> Self {
|
|
|
|
VerifyDigest(true, verify_digest)
|
|
|
|
}
|
|
|
|
|
2019-09-21 16:26:11 +00:00
|
|
|
/// Mark verifying the Digest as optional
|
|
|
|
///
|
|
|
|
/// If a digest is present in the request, it will be verified, but it is not required to be
|
|
|
|
/// present
|
2019-09-13 01:12:35 +00:00
|
|
|
pub fn optional(self) -> Self {
|
|
|
|
VerifyDigest(false, self.1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-13 23:27:04 +00:00
|
|
|
impl FromRequest for DigestVerified {
|
|
|
|
type Error = VerifyError;
|
2020-03-16 00:29:47 +00:00
|
|
|
type Future = Ready<Result<Self, Self::Error>>;
|
2019-09-13 23:27:04 +00:00
|
|
|
type Config = ();
|
|
|
|
|
|
|
|
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
2020-03-16 00:29:47 +00:00
|
|
|
ready(
|
|
|
|
req.extensions()
|
|
|
|
.get::<Self>()
|
|
|
|
.map(|s| *s)
|
|
|
|
.ok_or(VerifyError),
|
|
|
|
)
|
2019-09-13 23:27:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-13 01:12:35 +00:00
|
|
|
impl<T, S> Transform<S> for VerifyDigest<T>
|
|
|
|
where
|
|
|
|
T: DigestVerify + Clone + 'static,
|
2019-09-13 01:17:29 +00:00
|
|
|
S: Service<
|
|
|
|
Request = ServiceRequest,
|
|
|
|
Response = ServiceResponse<Body>,
|
|
|
|
Error = actix_web::Error,
|
|
|
|
> + 'static,
|
2019-09-13 01:12:35 +00:00
|
|
|
S::Error: 'static,
|
|
|
|
{
|
|
|
|
type Request = ServiceRequest;
|
|
|
|
type Response = ServiceResponse<Body>;
|
|
|
|
type Error = actix_web::Error;
|
|
|
|
type Transform = VerifyMiddleware<T, S>;
|
|
|
|
type InitError = ();
|
2020-03-16 00:29:47 +00:00
|
|
|
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
2019-09-13 01:12:35 +00:00
|
|
|
|
|
|
|
fn new_transform(&self, service: S) -> Self::Future {
|
|
|
|
ok(VerifyMiddleware(
|
|
|
|
Rc::new(RefCell::new(service)),
|
|
|
|
self.0,
|
|
|
|
self.1.clone(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, S> Service for VerifyMiddleware<T, S>
|
|
|
|
where
|
|
|
|
T: DigestVerify + Clone + 'static,
|
2019-09-13 01:17:29 +00:00
|
|
|
S: Service<
|
|
|
|
Request = ServiceRequest,
|
|
|
|
Response = ServiceResponse<Body>,
|
|
|
|
Error = actix_web::Error,
|
|
|
|
> + 'static,
|
2019-09-13 01:12:35 +00:00
|
|
|
S::Error: 'static,
|
|
|
|
{
|
|
|
|
type Request = ServiceRequest;
|
|
|
|
type Response = ServiceResponse<Body>;
|
|
|
|
type Error = actix_web::Error;
|
2020-03-16 00:29:47 +00:00
|
|
|
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
|
2019-09-13 01:12:35 +00:00
|
|
|
|
2020-03-16 00:29:47 +00:00
|
|
|
fn poll_ready(&mut self, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
|
|
self.0.borrow_mut().poll_ready(cx)
|
2019-09-13 01:12:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, mut req: ServiceRequest) -> Self::Future {
|
|
|
|
if let Some(digest) = req.headers().get("Digest") {
|
|
|
|
let vec = match parse_digest(digest) {
|
|
|
|
Some(vec) => vec,
|
2020-03-16 00:29:47 +00:00
|
|
|
None => return Box::pin(err(VerifyError.into())),
|
2019-09-13 01:12:35 +00:00
|
|
|
};
|
2020-03-16 00:29:47 +00:00
|
|
|
let mut payload = req.take_payload();
|
2019-09-13 01:12:35 +00:00
|
|
|
let service = self.0.clone();
|
2019-09-13 22:55:51 +00:00
|
|
|
let mut verify_digest = self.2.clone();
|
2019-09-13 01:12:35 +00:00
|
|
|
|
2020-03-16 00:29:47 +00:00
|
|
|
Box::pin(async move {
|
|
|
|
let mut output_bytes = BytesMut::new();
|
|
|
|
while let Some(res) = payload.next().await {
|
|
|
|
let bytes = res?;
|
|
|
|
output_bytes.extend(bytes);
|
|
|
|
}
|
|
|
|
let bytes = output_bytes.freeze();
|
|
|
|
|
2019-09-13 01:12:35 +00:00
|
|
|
if verify_digest.verify(&vec, &bytes.as_ref()) {
|
|
|
|
req.set_payload(
|
2020-03-16 00:29:47 +00:00
|
|
|
(Box::pin(once(ok(bytes)))
|
|
|
|
as Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static>>)
|
2019-09-13 01:12:35 +00:00
|
|
|
.into(),
|
|
|
|
);
|
|
|
|
|
2019-09-13 23:27:04 +00:00
|
|
|
req.extensions_mut().insert(DigestVerified);
|
|
|
|
|
2020-03-16 00:29:47 +00:00
|
|
|
service.borrow_mut().call(req).await
|
2019-09-13 01:12:35 +00:00
|
|
|
} else {
|
2020-03-16 00:29:47 +00:00
|
|
|
Err(VerifyError.into())
|
2019-09-13 01:12:35 +00:00
|
|
|
}
|
2020-03-16 00:29:47 +00:00
|
|
|
})
|
2019-09-13 22:55:51 +00:00
|
|
|
} else if self.1 {
|
2020-03-16 00:29:47 +00:00
|
|
|
Box::pin(err(VerifyError.into()))
|
2019-09-13 01:12:35 +00:00
|
|
|
} else {
|
2020-03-16 00:29:47 +00:00
|
|
|
Box::pin(self.0.borrow_mut().call(req))
|
2019-09-13 01:12:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_digest(h: &HeaderValue) -> Option<Vec<DigestPart>> {
|
|
|
|
let h = h.to_str().ok()?.split(";").next()?;
|
|
|
|
let v: Vec<_> = h
|
|
|
|
.split(",")
|
|
|
|
.filter_map(|p| {
|
|
|
|
let mut iter = p.splitn(2, "=");
|
|
|
|
iter.next()
|
|
|
|
.and_then(|alg| iter.next().map(|value| (alg, value)))
|
|
|
|
})
|
|
|
|
.map(|(alg, value)| DigestPart {
|
|
|
|
algorithm: alg.to_owned(),
|
|
|
|
digest: value.to_owned(),
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
if v.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ResponseError for VerifyError {
|
2020-03-16 00:29:47 +00:00
|
|
|
fn status_code(&self) -> StatusCode {
|
|
|
|
StatusCode::BAD_REQUEST
|
2019-09-13 01:12:35 +00:00
|
|
|
}
|
|
|
|
|
2020-03-16 00:29:47 +00:00
|
|
|
fn error_response(&self) -> HttpResponse {
|
|
|
|
HttpResponse::BadRequest().finish()
|
2019-09-13 01:12:35 +00:00
|
|
|
}
|
|
|
|
}
|