llldap/src/db/common.rs

39 lines
1.2 KiB
Rust
Raw Normal View History

2026-09-01 19:06:33 +02:00
use tokio::sync::RwLock;
use std::sync::Arc;
2026-09-01 23:27:02 +02:00
use crate::db::error::BoxedError;
2026-09-01 19:06:33 +02:00
use crate::db::{DatabaseInterface, UserRef};
#[derive(Clone, Debug)]
pub struct Database<D: DatabaseInterface> {
pub inner: Arc<RwLock<D>>,
}
impl<D: DatabaseInterface> Database<D> {
pub fn new(db: D) -> Self {
Self {
inner: Arc::new(RwLock::new(db)),
}
}
}
impl<D: DatabaseInterface> Database<D> {
/// Return false if the user doesn't exist, or the password is wrong.
///
/// TODO: should we return something else when the account doesn't exist?
/// or is it a feature to behave in the same way?
///
/// TODO: we may want to make sure the method runs in constant time
/// to avoid leaking information about existing users...
/// or maybe we do not care.
2026-09-01 23:27:02 +02:00
pub async fn check_password(&self, user: &UserRef, password: &str) -> Result<bool, BoxedError> {
let Some(user) = self.get_user(user).await? else {
2026-09-01 21:18:19 +02:00
tracing::debug!("check_password: User not found {user}");
2026-09-01 23:27:02 +02:00
return Ok(false);
2026-09-01 19:06:33 +02:00
};
2026-09-01 21:18:19 +02:00
tracing::debug!("Comparing {} and {}", user.password, password);
2026-09-01 23:27:02 +02:00
Ok(user.password == password)
2026-09-01 19:06:33 +02:00
}
}