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
This commit is contained in:
silver 2024-01-10 11:21:52 +00:00
parent 1ec21d22dd
commit 96f86985ee

View file

@ -1,5 +1,5 @@
use crate::{LdapAuth, LdapAuthResult, State}; use crate::{LdapAuth, LdapAuthResult, State};
use ldap3::{Mod, Scope, SearchEntry}; use ldap3::{LdapConn, Mod, Scope, SearchEntry};
use std::collections::HashSet; use std::collections::HashSet;
use tide::{ use tide::{
prelude::{json, Deserialize}, prelude::{json, Deserialize},
@ -30,7 +30,7 @@ pub async fn add_ssh_key(mut req: Request<State>) -> tide::Result {
let mods = vec![Mod::Add("sshPublicKey".to_owned(), HashSet::from([key]))]; let mods = vec![Mod::Add("sshPublicKey".to_owned(), HashSet::from([key]))];
let result = match ldap.modify(&dn, mods) { 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) => { Err(e) => {
dbg!(e); dbg!(e);
Ok(json!({"result": "error", "error": "Failed to add key"}).into()) Ok(json!({"result": "error", "error": "Failed to add key"}).into())
@ -61,7 +61,7 @@ pub async fn remove_ssh_key(mut req: Request<State>) -> tide::Result {
let mods = vec![Mod::Delete("sshPublicKey".to_owned(), HashSet::from([key]))]; let mods = vec![Mod::Delete("sshPublicKey".to_owned(), HashSet::from([key]))];
let result = match ldap.modify(&dn, mods) { 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) => { Err(e) => {
dbg!(e); dbg!(e);
Ok(json!({"result": "error", "error": "Failed to remove key"}).into()) Ok(json!({"result": "error", "error": "Failed to remove key"}).into())
@ -92,8 +92,19 @@ pub async fn get_ssh_keys(mut req: Request<State>) -> tide::Result {
None => return Ok(json!({"result": "error", "error": "Failed to authenticate"}).into()), None => return Ok(json!({"result": "error", "error": "Failed to authenticate"}).into()),
Some(x) => x, Some(x) => x,
}; };
let mut keys: Vec<String> = vec![];
let (rs, _res) = ldap.search(&dn, Scope::Base, "(objectClass=*)", vec!["sshPublicKey"])?.success()?; 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<String> {
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 { for entry in rs {
let tmp = SearchEntry::construct(entry); let tmp = SearchEntry::construct(entry);
if tmp.attrs.contains_key("sshPublicKey") { if tmp.attrs.contains_key("sshPublicKey") {
@ -102,8 +113,8 @@ pub async fn get_ssh_keys(mut req: Request<State>) -> tide::Result {
} }
} }
} }
}
ldap.unbind()?; }
Ok(json!({"result": "success", "success": keys}).into()) keys
} }