From c3cb0b9cd59a94286963f32f99ce2497d3d6cdd7 Mon Sep 17 00:00:00 2001 From: Alex Wright Date: Sat, 3 Aug 2019 20:29:10 +0200 Subject: [PATCH] Clean up error reporting a little --- src/main.rs | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index f7efd2d..9f67dd2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,4 @@ -// #![deny(warnings)] -// extern crate futures; +#![deny(warnings)] extern crate base64; extern crate hyper; extern crate ldap3; @@ -29,6 +28,9 @@ struct BasicAuthentication { pub enum AuthError { Parse, Decode, + LdapBind, + LdapConnection, + LdapSearch, } impl FromStr for BasicAuthentication { @@ -53,19 +55,20 @@ impl FromStr for BasicAuthentication { #[derive(Debug)] struct LdapUser { pub dn: String, + pub mail: Vec, pub services: Vec, } fn auth_user(auth: &BasicAuthentication) -> Result { let ldap = match LdapConn::new("ldap://192.168.122.61:389") { Ok(conn) => conn, - Err(err) => panic!(err), + Err(_err) => return Err(AuthError::LdapConnection), }; let base = format!("uid={},ou=people,dc=xeentech,dc=com", auth.username); match ldap.simple_bind(&base, &auth.password).unwrap().success() { - Ok(ldap) => println!("Connected and authenticated"), - Err(err) => panic!("Failed to bind with dn+password"), + Ok(_ldap) => println!("Connected and authenticated"), + Err(_err) => return Err(AuthError::LdapBind), }; let filter = format!("(uid={})", auth.username); @@ -74,7 +77,7 @@ fn auth_user(auth: &BasicAuthentication) -> Result { let (rs, _) = result.success().unwrap(); rs }, - Err(err) => panic!("Search failed? {:?}", err), + Err(_err) => return Err(AuthError::LdapSearch), }; // Grab the first, if any, result and discard the rest @@ -85,11 +88,12 @@ fn auth_user(auth: &BasicAuthentication) -> Result { }; let mail = match se.attrs.get("mail") { Some(mail) => mail.to_vec(), - None => [].to_ved(), + None => [].to_vec(), }; Ok(LdapUser { dn: base, + mail: mail, services: services, }) } @@ -120,12 +124,26 @@ fn auth_handler(req: Request) -> Response { let user = auth_user(&auth); user }); - let user = worker.join().expect("ldap thread threw?"); + let user = match worker.join().unwrap() { + Ok(ldap_user) => ldap_user, + Err(AuthError::LdapBind) => { + return Response::builder() + .status(StatusCode::UNAUTHORIZED) + .body(Body::from("LDAP bind failed")) + .unwrap(); + }, + _ => { + return Response::builder() + .status(StatusCode::INTERNAL_SERVER_ERROR) + .body(Body::from("Something is broken")) + .unwrap(); + } + }; Response::new(Body::from(format!("BasicAuthentication {:?}", user))) } -fn hello(req: Request) -> Response { +fn hello(_req: Request) -> Response { Response::new(Body::from("Hi!")) }