use nestrs::prelude::*;
use nestrs_ldap::{LdapAuthOptions, LdapAuthenticator};
#[derive(Default)]
#[injectable]
struct AppState;
#[controller(prefix = "/login")]
struct LoginController;
#[routes(state = AppState)]
impl LoginController {
#[post("/")]
async fn login(Json(body): Json<serde_json::Value>) -> Result<&'static str, (axum::http::StatusCode, String)> {
let user = body["username"].as_str().unwrap_or("");
let pass = body["password"].as_str().unwrap_or("");
let auth = LdapAuthenticator::new(LdapAuthOptions {
url: std::env::var("LDAP_URL").unwrap_or_else(|_| "ldap://127.0.0.1:389".into()),
bind_dn_template: "cn={username},ou=people,dc=example,dc=com".into(),
});
auth.authenticate(user, pass)
.await
.map_err(|e| (axum::http::StatusCode::UNAUTHORIZED, e.to_string()))?;
Ok("ok")
}
}