Fix redirection on auth

This commit is contained in:
ViViDboarder 2019-03-29 15:40:26 -07:00
parent ba1705c708
commit 1e849e445e
2 changed files with 31 additions and 46 deletions

View File

@ -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<String, String>) -> 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());

View File

@ -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<Vec<SearchEntry>, Box<Error
Ok(entries)
}
/// Perform a simple search and list users
fn do_search(config: &config::Config) -> Result<(), Box<Error>> {
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<Error>> {
* 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<Error>> {
let interval = Duration::from_secs(config.get_ldap_sync_interval_seconds());
loop {
invite_from_ldap(config, client)?;
sleep(interval);
}
}