diff --git a/src/methods/account_ssh.rs b/src/methods/account_ssh.rs index 9488df3..641edde 100644 --- a/src/methods/account_ssh.rs +++ b/src/methods/account_ssh.rs @@ -7,7 +7,7 @@ use tide::{ }; #[derive(Debug, Deserialize)] -pub struct SSHKey { +struct SSHKey { auth: LdapAuth, key: String, } @@ -22,24 +22,24 @@ pub async fn add_ssh_key(mut req: Request) -> tide::Result { let LdapAuthResult { mut ldap, dn, - is_skynet_user: _, + .. } = match crate::auth_user(&auth, config).await { None => return Ok(json!({"result": "error", "error": "Failed to authenticate"}).into()), Some(x) => x, }; - let mods = vec![Mod::Add("sshPublicKey".to_string(), HashSet::from([key]))]; - match ldap.modify(&dn, mods) { - Ok(_) => { - ldap.unbind()?; - Ok(json!({"result": "success"}).into()) - } + let mods = vec![Mod::Add("sshPublicKey".to_owned(), HashSet::from([key]))]; + let result = match ldap.modify(&dn, mods) { + Ok(_) => Ok(json!({"result": "success"}).into()), Err(e) => { dbg!(e); - ldap.unbind()?; Ok(json!({"result": "error", "error": "Failed to add key"}).into()) } - } + }; + + ldap.unbind()?; + + result } pub async fn remove_ssh_key(mut req: Request) -> tide::Result { @@ -52,63 +52,46 @@ pub async fn remove_ssh_key(mut req: Request) -> tide::Result { let LdapAuthResult { mut ldap, dn, - is_skynet_user: _, + .. } = match crate::auth_user(&auth, config).await { None => return Ok(json!({"result": "error", "error": "Failed to authenticate"}).into()), - Some(x) => { - if x.is_skynet_user { - x - } else { - return Ok(json!({"result": "error", "error": "Not a skynet user"}).into()); - } + Some(x) => x, + }; + + let mods = vec![Mod::Delete("sshPublicKey".to_owned(), HashSet::from([key]))]; + + let result = match ldap.modify(&dn, mods) { + Ok(_) => Ok(json!({"result": "success"}).into()), + Err(e) => { + dbg!(e); + Ok(json!({"result": "error", "error": "Failed to add key"}).into()) } }; - let mods = vec![Mod::Delete("sshPublicKey".to_string(), HashSet::from([key]))]; + ldap.unbind()?; - match ldap.modify(&dn, mods) { - Ok(_) => { - ldap.unbind()?; - Ok(json!({"result": "success"}).into()) - } - Err(e) => { - dbg!(e); - ldap.unbind()?; - Ok(json!({"result": "error", "error": "Failed to remove key"}).into()) - } - } + result +} + +#[derive(Debug, Deserialize)] +struct SSHKeyGet { + auth: LdapAuth, } pub async fn get_ssh_keys(mut req: Request) -> tide::Result { - let LdapAuth { - user, - pass, + let SSHKeyGet { + auth, } = req.body_json().await?; let config = &req.state().config; let LdapAuthResult { mut ldap, dn, - is_skynet_user: _, - } = match crate::auth_user( - &LdapAuth { - user, - pass, - }, - config, - ) - .await - { + .. + } = match crate::auth_user(&auth, config).await { None => return Ok(json!({"result": "error", "error": "Failed to authenticate"}).into()), - Some(x) => { - if x.is_skynet_user { - x - } else { - return Ok(json!({"result": "error", "error": "Not a skynet user"}).into()); - } - } + Some(x) => x, }; - let mut keys: Vec = vec![]; let (rs, _res) = ldap.search(&dn, Scope::Base, "(objectClass=*)", vec!["sshPublicKey"])?.success()?; for entry in rs { @@ -119,6 +102,7 @@ pub async fn get_ssh_keys(mut req: Request) -> tide::Result { } } } + ldap.unbind()?; Ok(json!({"result": "success", "success": keys}).into())