fix : removed unused variables and made structs private

This commit is contained in:
daragh 2023-12-31 00:17:43 +00:00
parent 08ce4c3942
commit 79edb50f65
No known key found for this signature in database

View file

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