mirror of
https://github.com/ViViDboarder/bitwarden_rs_ldap.git
synced 2024-11-22 03:06:27 +00:00
Fix redirection on auth
This commit is contained in:
parent
ba1705c708
commit
1e849e445e
@ -15,6 +15,7 @@ pub struct Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
|
/// Create new instance of client
|
||||||
pub fn new(url: String, admin_token: String) -> Client {
|
pub fn new(url: String, admin_token: String) -> Client {
|
||||||
Client {
|
Client {
|
||||||
url,
|
url,
|
||||||
@ -24,9 +25,15 @@ impl Client {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Authenticate client
|
||||||
fn auth(&mut self) -> Response {
|
fn auth(&mut self) -> Response {
|
||||||
let cookie_created = Instant::now();
|
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())
|
.post(format!("{}{}", &self.url, "/admin/").as_str())
|
||||||
.form(&[("token", &self.admin_token)])
|
.form(&[("token", &self.admin_token)])
|
||||||
.send()
|
.send()
|
||||||
@ -36,18 +43,17 @@ impl Client {
|
|||||||
|
|
||||||
// TODO: Handle error statuses
|
// TODO: Handle error statuses
|
||||||
|
|
||||||
println!("Auth headers: {:?}", result.headers());
|
|
||||||
|
|
||||||
if let Some(cookie) = result.headers().get(reqwest::header::SET_COOKIE) {
|
if let Some(cookie) = result.headers().get(reqwest::header::SET_COOKIE) {
|
||||||
self.cookie = cookie.to_str().map(|s| String::from(s)).ok();
|
self.cookie = cookie.to_str().map(|s| String::from(s)).ok();
|
||||||
self.cookie_created = Some(cookie_created);
|
self.cookie_created = Some(cookie_created);
|
||||||
} else {
|
} else {
|
||||||
panic!("No cookie to set!")
|
panic!("Could not authenticate.")
|
||||||
}
|
}
|
||||||
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Ensure that the client has a current auth cookie
|
||||||
fn ensure_auth(&mut self) {
|
fn ensure_auth(&mut self) {
|
||||||
match &self.cookie {
|
match &self.cookie {
|
||||||
Some(_) => {
|
Some(_) => {
|
||||||
@ -55,18 +61,17 @@ impl Client {
|
|||||||
.cookie_created
|
.cookie_created
|
||||||
.map_or(true, |created| (created.elapsed() >= COOKIE_LIFESPAN))
|
.map_or(true, |created| (created.elapsed() >= COOKIE_LIFESPAN))
|
||||||
{
|
{
|
||||||
let response = self.auth();
|
self.auth();
|
||||||
println!("Auth response: {:?}", response);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
let response = self.auth();
|
self.auth();
|
||||||
println!("Auth response: {:?}", response);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// TODO: handle errors
|
// TODO: handle errors
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Make an authenticated GET to Bitwarden Admin
|
||||||
fn get(&mut self, path: &str) -> Response {
|
fn get(&mut self, path: &str) -> Response {
|
||||||
self.ensure_auth();
|
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 {
|
fn post(&mut self, path: &str, json: &HashMap<String, String>) -> Response {
|
||||||
self.ensure_auth();
|
self.ensure_auth();
|
||||||
|
|
||||||
@ -114,6 +120,7 @@ impl Client {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Invite user with provided email
|
||||||
pub fn invite(&mut self, email: &str) -> Response {
|
pub fn invite(&mut self, email: &str) -> Response {
|
||||||
let mut json = HashMap::new();
|
let mut json = HashMap::new();
|
||||||
json.insert("email".to_string(), email.to_string());
|
json.insert("email".to_string(), email.to_string());
|
||||||
|
54
src/main.rs
54
src/main.rs
@ -16,25 +16,14 @@ fn main() {
|
|||||||
config.get_bitwarden_admin_token().clone(),
|
config.get_bitwarden_admin_token().clone(),
|
||||||
);
|
);
|
||||||
|
|
||||||
/*
|
// TODO: Use command line args to differentiate if we invite once or start loop
|
||||||
* let auth_response = client.auth();
|
|
||||||
* println!("Auth Response: {:?}", auth_response);
|
|
||||||
*/
|
|
||||||
|
|
||||||
match do_search(&config) {
|
|
||||||
Ok(_) => (),
|
|
||||||
Err(e) => println!("{}", e),
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Err(e) = invite_from_ldap(&config, &mut client) {
|
if let Err(e) = invite_from_ldap(&config, &mut client) {
|
||||||
println!("{}", e);
|
println!("{}", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
if let Err(e) = start_sync_loop(&config, &mut client) {
|
||||||
* if let Err(e) = start_sync_loop(&config, %mut client) {
|
println!("{}", e);
|
||||||
* println!("{}", e);
|
}
|
||||||
* }
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates an LDAP connection, authenticating if necessary
|
/// Creates an LDAP connection, authenticating if necessary
|
||||||
@ -78,20 +67,7 @@ fn search_entries(config: &config::Config) -> Result<Vec<SearchEntry>, Box<Error
|
|||||||
Ok(entries)
|
Ok(entries)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Perform a simple search and list users
|
/// Invite all LDAP users to Bitwarden
|
||||||
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(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn invite_from_ldap(
|
fn invite_from_ldap(
|
||||||
config: &config::Config,
|
config: &config::Config,
|
||||||
client: &mut bw_admin::Client,
|
client: &mut bw_admin::Client,
|
||||||
@ -108,12 +84,14 @@ fn invite_from_ldap(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/// Begin sync loop to invite LDAP users to Bitwarden
|
||||||
* fn start_sync_loop(config: &config::Config) -> Result<(), Box<Error>> {
|
fn start_sync_loop(
|
||||||
* let interval = Duration::from_secs(config.get_ldap_sync_interval_seconds());
|
config: &config::Config,
|
||||||
* loop {
|
client: &mut bw_admin::Client,
|
||||||
* invite_from_ldap(config)?;
|
) -> Result<(), Box<Error>> {
|
||||||
* sleep(interval);
|
let interval = Duration::from_secs(config.get_ldap_sync_interval_seconds());
|
||||||
* }
|
loop {
|
||||||
* }
|
invite_from_ldap(config, client)?;
|
||||||
*/
|
sleep(interval);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user