From 96f86985eec772a835e7867852ee6452fd9548b6 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Wed, 10 Jan 2024 11:21:52 +0000 Subject: [PATCH] feat: any modifications to ssh keys will return the new array of keys. Should help https://gitlab.skynet.ie/compsoc1/skynet/ldap/frontend/-/merge_requests/12 be displaying accurate information --- src/methods/account_ssh.rs | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/src/methods/account_ssh.rs b/src/methods/account_ssh.rs index 1d6b8fd..5737d06 100644 --- a/src/methods/account_ssh.rs +++ b/src/methods/account_ssh.rs @@ -1,5 +1,5 @@ use crate::{LdapAuth, LdapAuthResult, State}; -use ldap3::{Mod, Scope, SearchEntry}; +use ldap3::{LdapConn, Mod, Scope, SearchEntry}; use std::collections::HashSet; use tide::{ prelude::{json, Deserialize}, @@ -30,7 +30,7 @@ pub async fn add_ssh_key(mut req: Request) -> tide::Result { let mods = vec![Mod::Add("sshPublicKey".to_owned(), HashSet::from([key]))]; let result = match ldap.modify(&dn, mods) { - Ok(_) => Ok(json!({"result": "success"}).into()), + Ok(_) => Ok(json!({"result": "success", "success": get_keys(&mut ldap, &dn) }).into()), Err(e) => { dbg!(e); Ok(json!({"result": "error", "error": "Failed to add key"}).into()) @@ -61,7 +61,7 @@ pub async fn remove_ssh_key(mut req: Request) -> tide::Result { let mods = vec![Mod::Delete("sshPublicKey".to_owned(), HashSet::from([key]))]; let result = match ldap.modify(&dn, mods) { - Ok(_) => Ok(json!({"result": "success"}).into()), + Ok(_) => Ok(json!({"result": "success", "success": get_keys(&mut ldap, &dn) }).into()), Err(e) => { dbg!(e); Ok(json!({"result": "error", "error": "Failed to remove key"}).into()) @@ -92,18 +92,29 @@ pub async fn get_ssh_keys(mut req: Request) -> tide::Result { None => return Ok(json!({"result": "error", "error": "Failed to authenticate"}).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 { - let tmp = SearchEntry::construct(entry); - if tmp.attrs.contains_key("sshPublicKey") { - for key in tmp.attrs["sshPublicKey"].clone() { - keys.push(key); - } - } - } + + let keys = get_keys(&mut ldap, &dn); ldap.unbind()?; Ok(json!({"result": "success", "success": keys}).into()) } + +fn get_keys(ldap: &mut LdapConn, dn: &str) -> Vec { + let mut keys = vec![]; + + if let Ok(result_tmp) = ldap.search(dn, Scope::Base, "(objectClass=*)", vec!["sshPublicKey"]) { + if let Ok((rs, _)) = result_tmp.success() { + for entry in rs { + let tmp = SearchEntry::construct(entry); + if tmp.attrs.contains_key("sshPublicKey") { + for key in tmp.attrs["sshPublicKey"].clone() { + keys.push(key); + } + } + } + } + } + + keys +}