Skip to content
This repository has been archived by the owner on Mar 2, 2020. It is now read-only.

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberb committed Oct 12, 2019
1 parent 81219b1 commit 655dd38
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/env/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

mod file_locations;
mod general;
mod ldap;
mod root_config;
mod web;
mod ldap;

pub use self::root_config::Config;
2 changes: 1 addition & 1 deletion src/env/config/root_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// Please see LICENSE file for your rights under this license.

use crate::{
env::config::{file_locations::Files, general::General, web::WebConfig, ldap::LdapConfig},
env::config::{file_locations::Files, general::General, ldap::LdapConfig, web::WebConfig},
util::{Error, ErrorKind}
};
use failure::{Fail, ResultExt};
Expand Down
31 changes: 12 additions & 19 deletions src/routes/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
// This file is copyright under the latest version of the EUPL.
// Please see LICENSE file for your rights under this license.

use ldap3::LdapConn;
use crate::{
env::Env
env::Env,
util::{reply_data, reply_success, Error, ErrorKind, Reply}
};
use crate::util::{reply_success, reply_data, Error, ErrorKind, Reply};
use failure::ResultExt;
use ldap3::LdapConn;
use rocket::{
http::{Cookie, Cookies},
request::{self, FromRequest, Request, State},
Outcome
};
use std::sync::atomic::{AtomicUsize, Ordering};
use failure::ResultExt;

const USER_ATTR: &str = "user_id";
const AUTH_HEADER: &str = "X-Pi-hole-Authenticate";
Expand Down Expand Up @@ -94,7 +94,6 @@ impl<'a, 'r> FromRequest<'a, 'r> for User {
let ldap_config = &env.config().ldap;

if ldap_config.enabled {
println!("LDAP is enabled");
let key = match key_opt {
Some(key) => key,
None => return Error::from(ErrorKind::LdapMissingKey).into_outcome()
Expand All @@ -108,9 +107,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for User {
Ok(_) => Outcome::Success(User::create_and_store_user(request, &auth_data)),
Err(e) => e.into_outcome()
}

} else {

// Check if a key is required for authentication
if !auth_data.key_required() {
return Outcome::Success(User::create_and_store_user(request, &auth_data));
Expand All @@ -126,21 +123,19 @@ impl<'a, 'r> FromRequest<'a, 'r> for User {
Error::from(ErrorKind::Unauthorized).into_outcome()
}
} else {
// A key is required but not provided
// A key is required but not provided
Error::from(ErrorKind::Unauthorized).into_outcome()
}

}
}
}
}

fn ldap_login(ldap_address: &str, bind_dn: &str, username: &str, key: &str) -> Result<(), Error> {
println!("LDAP address: {}, user: {}", ldap_address, username);
let bind_dn = bind_dn.replace("{}", username);
LdapConn::new(&ldap_address)
.map_err(|e| ErrorKind::LdapConnectError(format!("{:?}", e)))?
.context(ErrorKind::LdapConnectError)?
.simple_bind(&bind_dn, key)
.map_err(|e| ErrorKind::LdapBindError(format!("{:?}", e)))?
.context(ErrorKind::LdapBindError)?
.success()
.context(ErrorKind::LdapUnauthorized)?;
Ok(())
Expand Down Expand Up @@ -181,12 +176,10 @@ impl AuthData {
#[get("/auth/mode")]
pub fn get_auth_mode(env: State<Env>) -> Reply {
let ldap_config = &env.config().ldap;
let mode = if ldap_config.enabled {
"ldap"
} else {
"key"
};
reply_data(AuthMode { mode: mode.to_owned() })
let mode = if ldap_config.enabled { "ldap" } else { "key" };
reply_data(AuthMode {
mode: mode.to_owned()
})
}

/// Provides an endpoint to authenticate or check if already authenticated
Expand Down
29 changes: 14 additions & 15 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ pub enum ErrorKind {
LdapMissingKey,
#[fail(display = "Missing username")]
LdapMissingUsername,
#[fail(display = "Bind error: {}", _0)]
LdapBindError(String),
#[fail(display = "Bind error")]
LdapBindError,
#[fail(display = "Unauthorized")]
LdapUnauthorized,
#[fail(display = "Connection error: {}", _0)]
LdapConnectError(String)
#[fail(display = "Connection error")]
LdapConnectError
}

impl Error {
Expand Down Expand Up @@ -252,8 +252,8 @@ impl ErrorKind {
ErrorKind::GravityDatabase => "gravity_database",
ErrorKind::LdapMissingKey => "ldap_missing_key",
ErrorKind::LdapMissingUsername => "ldap_missing_username",
ErrorKind::LdapConnectError(_) => "ldap_connection_error",
ErrorKind::LdapBindError(_) => "ldap_bind_error",
ErrorKind::LdapConnectError => "ldap_connection_error",
ErrorKind::LdapBindError => "ldap_bind_error",
ErrorKind::LdapUnauthorized => "ldap_unauthorized"
}
}
Expand All @@ -263,10 +263,12 @@ impl ErrorKind {
match self {
ErrorKind::NotFound => Status::NotFound,
ErrorKind::AlreadyExists => Status::Conflict,
ErrorKind::InvalidDomain | ErrorKind::BadRequest | ErrorKind::InvalidSettingValue => {
Status::BadRequest
}
ErrorKind::Unauthorized | ErrorKind::LdapUnauthorized => Status::Unauthorized,
ErrorKind::InvalidDomain
| ErrorKind::BadRequest
| ErrorKind::InvalidSettingValue
| ErrorKind::LdapMissingUsername
| ErrorKind::LdapMissingKey => Status::BadRequest,
ErrorKind::Unauthorized | ErrorKind::LdapUnauthorized => Status::Unauthorized,
ErrorKind::Unknown
| ErrorKind::GravityError
| ErrorKind::FtlConnectionFail
Expand All @@ -284,11 +286,8 @@ impl ErrorKind {
| ErrorKind::SharedMemoryVersion(_, _)
| ErrorKind::FtlDatabase
| ErrorKind::GravityDatabase
| ErrorKind::LdapBindError(_)
| ErrorKind::LdapConnectError(_)
| ErrorKind::LdapMissingUsername
| ErrorKind::LdapMissingKey
=> Status::InternalServerError
| ErrorKind::LdapBindError
| ErrorKind::LdapConnectError => Status::InternalServerError
}
}

Expand Down

0 comments on commit 655dd38

Please sign in to comment.