From 311029574f7a707cfe6e4bea8d91d058cdb313f1 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Fri, 26 May 2023 00:51:36 +0100 Subject: [PATCH] fmt: fmt and clippy --- src/lib.rs | 2 +- src/main.rs | 44 +++++++++++++++++++++++--------------------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 67e9187..7afa4b5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,7 @@ pub async fn db_init(database: &str) -> Result, Error> { ) .execute(&pool) .await?; - + */ // set up indexes? diff --git a/src/main.rs b/src/main.rs index 480fd7a..1ad2e20 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,17 @@ -use std::collections::HashSet; -use ldap3::result::Result; -use ldap3::{LdapConn, Scope, SearchEntry, Mod}; -use base64::{Engine as _, engine::general_purpose}; -use crypto::{sha2::Sha512, digest::Digest}; +use ldap3::{LdapConn, Mod, Scope, SearchEntry}; +use std::collections::HashSet; + +use base64::{engine::general_purpose, Engine as _}; +use crypto::{digest::Digest, sha2::Sha512}; // for teh webserver +use dotenv::dotenv; +use skynet_ldap_server::db_init; use sqlx::{Pool, Sqlite}; use std::env; -use dotenv::dotenv; use tide::prelude::*; use tide::{Request, Response}; -use skynet_ldap_server::db_init; - #[derive(Clone)] struct State { @@ -42,7 +41,6 @@ async fn main() -> tide::Result<()> { Ok(()) } - #[derive(Debug, Clone)] struct Config { ldap_host: String, @@ -76,27 +74,31 @@ fn get_config() -> Config { pub fn hex_to_base64(hex: &str) -> String { // Make vector of bytes from octets let mut bytes = Vec::new(); - for i in 0..(hex.len()/2) { - let res = u8::from_str_radix(&hex[2*i .. 2*i+2], 16); + for i in 0..(hex.len() / 2) { + let res = u8::from_str_radix(&hex[2 * i..2 * i + 2], 16); match res { Ok(v) => bytes.push(v), Err(e) => println!("Problem with hex: {}", e), }; - }; + } general_purpose::STANDARD.encode(&bytes) // now convert from Vec to b64-encoded String } - #[derive(Debug, Deserialize)] struct LdapUpdate { user: String, pass: String, field: String, - value: String + value: String, } async fn post_update_ldap(mut req: Request) -> tide::Result { - let LdapUpdate { user, pass, field, value } = req.body_json().await?; + let LdapUpdate { + user, + pass, + field, + value, + } = req.body_json().await?; let config = &req.state().config; @@ -110,26 +112,26 @@ async fn post_update_ldap(mut req: Request) -> tide::Result { let mut pw_keep_same = false; // get the users current password hash - let (rs, _res) = ldap.search(&dn,Scope::Base,"(objectClass=*)",vec!["userPassword"])?.success()?; + let (rs, _res) = ldap.search(&dn, Scope::Base, "(objectClass=*)", vec!["userPassword"])?.success()?; if !rs.is_empty() { let tmp = SearchEntry::construct(rs[0].clone()); if !tmp.attrs["userPassword"].is_empty() && tmp.attrs["userPassword"][0].starts_with("{SHA512}") { pw_keep_same = true; } } - + let mut mods = vec![]; - + // check if the password field itself is being updated let pass_new = if &field != "userPassword" { - mods.push(Mod::Replace(field, HashSet::from([value]))); + mods.push(Mod::Replace(field, HashSet::from([value]))); // retain the older password pass } else { pw_keep_same = false; value }; - + if !pw_keep_same { let mut hasher = Sha512::new(); @@ -145,7 +147,7 @@ async fn post_update_ldap(mut req: Request) -> tide::Result { }; ldap.modify(&dn, mods)?.success()?; - + ldap.unbind()?; Ok(format!("Hello, {}! I've put in an order for {} shoes", "name", "legs").into())