diff --git a/src/main.rs b/src/main.rs index 8409759..175827d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,7 +36,7 @@ async fn main() -> tide::Result<()> { let mut app = tide::with_state(state); - //app.at("/steam_ost/:username").get(results_get); + app.at("/ldap/update").post(post_update_ldap); app.listen(host_port).await?; Ok(()) @@ -60,7 +60,7 @@ fn get_config() -> Config { }; if let Ok(x) = env::var("LDAP_HOST") { - config.key = x.trim().to_string(); + config.ldap_host = x.trim().to_string(); } if let Ok(x) = env::var("DATABASE") { config.database = x.trim().to_string(); @@ -88,16 +88,23 @@ pub fn hex_to_base64(hex: &str) -> String { } +#[derive(Debug, Deserialize)] +struct LdapUpdate { + user: String, + pass: String, + field: String, + value: String +} async fn post_update_ldap(mut req: Request) -> tide::Result { - let mut ldap = LdapConn::new("ldaps://sso.skynet.ie")?; + let LdapUpdate { user, pass, field, value } = req.body_json().await?; - let user = "silver"; - let pass = ""; - let field = "sshPublicKey"; - let value = "em232323232323"; + let config = &req.state().config; + + // easier to give each request its own connection + let mut ldap = LdapConn::new(&config.ldap_host)?; let dn = format!("uid={},ou=users,dc=skynet,dc=ie", user); - ldap.simple_bind(&dn, pass)?.success()?; + ldap.simple_bind(&dn, &pass)?.success()?; // always assume insecure let mut secure = false; @@ -114,22 +121,22 @@ async fn post_update_ldap(mut req: Request) -> tide::Result { Mod::Replace(field, HashSet::from([value])) ]; - let mut pw_hashset = HashSet::new(); - let pass_tmp; if !secure { let mut hasher = Sha512::new(); - hasher.input_str(pass); + hasher.input_str(&pass); // get it as hex string let hex = hasher.result_str(); // convert it to b64 - pass_tmp = format!("{{SHA512}}{}", hex_to_base64(&hex)); + let pass_tmp = format!("{{SHA512}}{}", hex_to_base64(&hex)); - pw_hashset.insert(pass_tmp.as_str()); - mods.push(Mod::Replace("userPassword", pw_hashset)); + mods.push(Mod::Replace(String::from("userPassword"), HashSet::from([pass_tmp]))); }; + + ldap.modify(&dn, mods)?.success()?; + ldap.unbind()?; Ok(format!("Hello, {}! I've put in an order for {} shoes", "name", "legs").into())