feat: the post request works

This commit is contained in:
silver 2023-05-26 00:38:50 +01:00
parent fe69e0cc9c
commit b1b533226a

View file

@ -36,7 +36,7 @@ async fn main() -> tide::Result<()> {
let mut app = tide::with_state(state); 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?; app.listen(host_port).await?;
Ok(()) Ok(())
@ -60,7 +60,7 @@ fn get_config() -> Config {
}; };
if let Ok(x) = env::var("LDAP_HOST") { 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") { if let Ok(x) = env::var("DATABASE") {
config.database = x.trim().to_string(); 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<State>) -> tide::Result { async fn post_update_ldap(mut req: Request<State>) -> 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 config = &req.state().config;
let pass = "";
let field = "sshPublicKey"; // easier to give each request its own connection
let value = "em232323232323"; let mut ldap = LdapConn::new(&config.ldap_host)?;
let dn = format!("uid={},ou=users,dc=skynet,dc=ie", user); 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 // always assume insecure
let mut secure = false; let mut secure = false;
@ -114,22 +121,22 @@ async fn post_update_ldap(mut req: Request<State>) -> tide::Result {
Mod::Replace(field, HashSet::from([value])) Mod::Replace(field, HashSet::from([value]))
]; ];
let mut pw_hashset = HashSet::new();
let pass_tmp;
if !secure { if !secure {
let mut hasher = Sha512::new(); let mut hasher = Sha512::new();
hasher.input_str(pass); hasher.input_str(&pass);
// get it as hex string // get it as hex string
let hex = hasher.result_str(); let hex = hasher.result_str();
// convert it to b64 // 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(String::from("userPassword"), HashSet::from([pass_tmp])));
mods.push(Mod::Replace("userPassword", pw_hashset));
}; };
ldap.modify(&dn, mods)?.success()?;
ldap.unbind()?; ldap.unbind()?;
Ok(format!("Hello, {}! I've put in an order for {} shoes", "name", "legs").into()) Ok(format!("Hello, {}! I've put in an order for {} shoes", "name", "legs").into())