diff --git a/src/bw_admin.rs b/src/bw_admin.rs index 138a8b8..86bde4b 100644 --- a/src/bw_admin.rs +++ b/src/bw_admin.rs @@ -15,6 +15,7 @@ pub struct Client { } impl Client { + /// Create new instance of client pub fn new(url: String, admin_token: String) -> Client { Client { url, @@ -24,9 +25,15 @@ impl Client { } } + /// Authenticate client fn auth(&mut self) -> Response { let cookie_created = Instant::now(); - let result = reqwest::Client::new() + let client = reqwest::Client::builder() + // Avoid redirects because server will redirect to admin page after auth + .redirect(reqwest::RedirectPolicy::none()) + .build() + .unwrap(); + let result = client .post(format!("{}{}", &self.url, "/admin/").as_str()) .form(&[("token", &self.admin_token)]) .send() @@ -36,18 +43,17 @@ impl Client { // TODO: Handle error statuses - println!("Auth headers: {:?}", result.headers()); - if let Some(cookie) = result.headers().get(reqwest::header::SET_COOKIE) { self.cookie = cookie.to_str().map(|s| String::from(s)).ok(); self.cookie_created = Some(cookie_created); } else { - panic!("No cookie to set!") + panic!("Could not authenticate.") } result } + /// Ensure that the client has a current auth cookie fn ensure_auth(&mut self) { match &self.cookie { Some(_) => { @@ -55,18 +61,17 @@ impl Client { .cookie_created .map_or(true, |created| (created.elapsed() >= COOKIE_LIFESPAN)) { - let response = self.auth(); - println!("Auth response: {:?}", response); + self.auth(); } } None => { - let response = self.auth(); - println!("Auth response: {:?}", response); + self.auth(); } }; // TODO: handle errors } + /// Make an authenticated GET to Bitwarden Admin fn get(&mut self, path: &str) -> Response { self.ensure_auth(); @@ -90,6 +95,7 @@ impl Client { } } + /// Make authenticated POST to Bitwarden Admin with JSON data fn post(&mut self, path: &str, json: &HashMap) -> Response { self.ensure_auth(); @@ -114,6 +120,7 @@ impl Client { } } + /// Invite user with provided email pub fn invite(&mut self, email: &str) -> Response { let mut json = HashMap::new(); json.insert("email".to_string(), email.to_string()); diff --git a/src/main.rs b/src/main.rs index aa2f59e..c84b032 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,25 +16,14 @@ fn main() { config.get_bitwarden_admin_token().clone(), ); - /* - * let auth_response = client.auth(); - * println!("Auth Response: {:?}", auth_response); - */ - - match do_search(&config) { - Ok(_) => (), - Err(e) => println!("{}", e), - } - + // TODO: Use command line args to differentiate if we invite once or start loop if let Err(e) = invite_from_ldap(&config, &mut client) { println!("{}", e); } - /* - * if let Err(e) = start_sync_loop(&config, %mut client) { - * println!("{}", e); - * } - */ + if let Err(e) = start_sync_loop(&config, &mut client) { + println!("{}", e); + } } /// Creates an LDAP connection, authenticating if necessary @@ -78,20 +67,7 @@ fn search_entries(config: &config::Config) -> Result, Box Result<(), Box> { - let mail_field = config.get_ldap_mail_field(); - let entries = search_entries(config)?; - for user in entries { - println!("{:?}", user); - if let Some(user_email) = user.attrs[mail_field.as_str()].first() { - println!("{}", user_email); - } - } - - Ok(()) -} - +/// Invite all LDAP users to Bitwarden fn invite_from_ldap( config: &config::Config, client: &mut bw_admin::Client, @@ -108,12 +84,14 @@ fn invite_from_ldap( Ok(()) } -/* - * fn start_sync_loop(config: &config::Config) -> Result<(), Box> { - * let interval = Duration::from_secs(config.get_ldap_sync_interval_seconds()); - * loop { - * invite_from_ldap(config)?; - * sleep(interval); - * } - * } - */ +/// Begin sync loop to invite LDAP users to Bitwarden +fn start_sync_loop( + config: &config::Config, + client: &mut bw_admin::Client, +) -> Result<(), Box> { + let interval = Duration::from_secs(config.get_ldap_sync_interval_seconds()); + loop { + invite_from_ldap(config, client)?; + sleep(interval); + } +}