Clean up error reporting a little
This commit is contained in:
parent
9f8c47712a
commit
c3cb0b9cd5
36
src/main.rs
36
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<String>,
|
||||
pub services: Vec<String>,
|
||||
}
|
||||
|
||||
fn auth_user(auth: &BasicAuthentication) -> Result<LdapUser, AuthError> {
|
||||
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<LdapUser, AuthError> {
|
|||
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<LdapUser, AuthError> {
|
|||
};
|
||||
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<Body>) -> Response<Body> {
|
|||
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<Body>) -> Response<Body> {
|
||||
fn hello(_req: Request<Body>) -> Response<Body> {
|
||||
Response::new(Body::from("Hi!"))
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue