Skip to content

Commit

Permalink
Add MutualTlsUser request guard
Browse files Browse the repository at this point in the history
  • Loading branch information
akuanti committed Jun 25, 2018
1 parent df77594 commit a2a4861
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
3 changes: 3 additions & 0 deletions core/http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub mod uri;
#[cfg(feature = "tls")]
pub mod tls;

#[cfg(feature = "tls")]
pub mod mtls;

#[macro_use]
mod docify;
#[macro_use]
Expand Down
19 changes: 19 additions & 0 deletions core/http/src/mtls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use tls::Certificate;

#[derive(Debug)]
pub struct MutualTlsUser {
peer_certs: Vec<Certificate>,
}

impl MutualTlsUser {
pub fn new(peer_certs: Vec<Certificate>) -> MutualTlsUser {
MutualTlsUser {
peer_certs
}
}

/// Get the common name
pub fn name(&self) -> String {
unimplemented!();
}
}
13 changes: 13 additions & 0 deletions core/lib/src/request/from_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use outcome::Outcome::*;

use http::{Status, ContentType, Accept, Method, Cookies};
use http::uri::Uri;
#[cfg(feature = "tls")]
use http::mtls::MutualTlsUser;

/// Type alias for the `Outcome` of a `FromRequest` conversion.
pub type Outcome<S, E> = outcome::Outcome<S, (Status, E), ()>;
Expand Down Expand Up @@ -312,3 +314,14 @@ impl<'a, 'r, T: FromRequest<'a, 'r>> FromRequest<'a, 'r> for Option<T> {
}
}

#[cfg(feature = "tls")]
impl <'a, 'r> FromRequest<'a, 'r> for MutualTlsUser {
type Error = ();

fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error> {
match request.get_peer_certificates() {
Some(certs) => Success(MutualTlsUser::new(certs)),
None => Forward(())
}
}
}
4 changes: 2 additions & 2 deletions core/lib/src/request/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,8 +707,8 @@ impl<'r> Request<'r> {

/// Get the peer certificates
#[cfg(feature = "tls")]
pub(crate) fn get_peer_certificates(&self) -> &Option<Vec<Certificate>> {
&self.peer_certs
pub(crate) fn get_peer_certificates(&self) -> Option<Vec<Certificate>> {
self.peer_certs.clone()
}
}

Expand Down

0 comments on commit a2a4861

Please sign in to comment.