-
-
Notifications
You must be signed in to change notification settings - Fork 724
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce8114b
commit a2defac
Showing
10 changed files
with
162 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//! Mutual (client) TLS filters. | ||
use std::convert::Infallible; | ||
|
||
use tokio_rustls::rustls::Certificate; | ||
|
||
use crate::{ | ||
filter::{filter_fn_one, Filter}, | ||
route::Route, | ||
}; | ||
|
||
/// Creates a `Filter` to get the peer certificates for the TLS connection. | ||
/// | ||
/// If the underlying transport doesn't have peer certificates, this will yield | ||
/// `None`. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// use warp::mtls::Certificates; | ||
/// use warp::Filter; | ||
/// | ||
/// let route = warp::mtls::peer_certificates() | ||
/// .map(|certs: Option<Certificates>| { | ||
/// println!("peer certificates = {:?}", certs.as_ref()); | ||
/// }); | ||
/// ``` | ||
pub fn peer_certificates( | ||
) -> impl Filter<Extract = (Option<Certificates>,), Error = Infallible> + Copy { | ||
filter_fn_one(|route| futures_util::future::ok(Certificates::from_route(route))) | ||
} | ||
|
||
/// Certificates is a iterable container of Certificates. | ||
#[derive(Debug)] | ||
pub struct Certificates(Vec<Certificate>); | ||
|
||
impl Certificates { | ||
fn from_route(route: &Route) -> Option<Certificates> { | ||
route | ||
.peer_certificates() | ||
.read() | ||
.unwrap() | ||
.as_ref() | ||
.map(|certs| Self(certs.to_vec())) | ||
} | ||
} | ||
|
||
impl AsRef<[Certificate]> for Certificates { | ||
fn as_ref(&self) -> &[Certificate] { | ||
self.0.as_ref() | ||
} | ||
} | ||
|
||
impl<'a> IntoIterator for &'a Certificates { | ||
type Item = &'a Certificate; | ||
type IntoIter = std::slice::Iter<'a, Certificate>; | ||
|
||
fn into_iter(self) -> Self::IntoIter { | ||
self.0.iter() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#![deny(warnings)] | ||
#![cfg(feature = "tls")] | ||
|
||
use tokio_rustls::rustls::Certificate; | ||
|
||
#[tokio::test] | ||
async fn peer_certificates_missing() { | ||
let extract_peer_certs = warp::mtls::peer_certificates(); | ||
|
||
let req = warp::test::request(); | ||
let resp = req.filter(&extract_peer_certs).await.unwrap(); | ||
assert!(resp.is_none()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn peer_certificates_present() { | ||
let extract_peer_certs = warp::mtls::peer_certificates(); | ||
|
||
let cert = Certificate(b"TEST CERT".to_vec()); | ||
let req = warp::test::request().peer_certificates([cert.clone()]); | ||
let resp = req.filter(&extract_peer_certs).await.unwrap(); | ||
assert_eq!( | ||
resp.unwrap().as_ref(), | ||
&[cert], | ||
) | ||
} |