feat: DB operations can fail

This commit is contained in:
selfhoster selfhoster 2026-09-01 23:27:02 +02:00
commit 7527ba7a09
6 changed files with 50 additions and 19 deletions

View file

@ -1,13 +1,15 @@
use ldap3_proto::proto::{LdapBindCred, LdapBindRequest, LdapBindResponse, LdapOp, LdapResult};
use ldap3_proto::{LdapMsg, LdapResultCode};
use crate::db::error::BoxedError;
use crate::db::{Database, DatabaseInterface};
use crate::ldap::{
Dn, InvalidDnError, LdapReturnError, LdapStream, LdapStreamError, NotUserDnError,
};
#[derive(Debug)]
pub enum BindError {
// TODO: DB Error
Db(BoxedError),
InvalidCredentials,
InvalidDn(InvalidDnError),
NotUserDn(NotUserDnError),
@ -20,6 +22,7 @@ impl BindError {
stream: &mut LdapStream,
msgid: i32,
) -> Result<(), LdapStreamError> {
tracing::debug!(error=?self, "Bind failed");
let resp_msg = LdapMsg {
msgid,
op: LdapOp::BindResponse(LdapBindResponse {
@ -48,6 +51,7 @@ impl From<InvalidDnError> for BindError {
impl LdapReturnError for BindError {
fn code(&self) -> LdapResultCode {
match self {
Self::Db(_e) => LdapResultCode::Unavailable,
Self::InvalidCredentials => LdapResultCode::InvalidCredentials,
Self::InvalidDn(e) => e.code(),
Self::NotUserDn(e) => e.code(),
@ -57,6 +61,7 @@ impl LdapReturnError for BindError {
fn message(&self) -> String {
match self {
Self::Db(e) => format!("Database error: {e}"),
Self::InvalidCredentials => "Wrong username or password".to_string(),
Self::InvalidDn(e) => e.message(),
Self::NotUserDn(e) => e.message(),
@ -117,7 +122,17 @@ pub async fn op_bind<D: DatabaseInterface>(
}
};
if db.check_password(&user_ref, &password).await {
let success = match db.check_password(&user_ref, &password).await {
Ok(success) => success,
Err(e) => {
// tracing::error!(error = &*e as &dyn std::error::Error, "Database failure");
tracing::error!(error = e, "Database failure");
BindError::Db(e).error_message(stream, msgid).await?;
return Ok(None);
}
};
if success {
bind_success(stream, msgid).await?;
Ok(Some(dn))
} else {