Skip to content

Commit 4be803c

Browse files
author
CommanderKeynes
committed
Format
1 parent 9eb64dc commit 4be803c

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

src/client.rs

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use ldap3::{LdapConnAsync, LdapConnSettings};
21
use crate::errors::{ClientIdentifier, Error};
32
use crate::pool::BanReason;
43
/// Handle clients by pretending to be a PostgreSQL server.
54
use bytes::{Buf, BufMut, BytesMut};
5+
use ldap3::{LdapConnAsync, LdapConnSettings};
66
use log::{debug, error, info, trace, warn};
77
use once_cell::sync::Lazy;
88
use std::collections::{HashMap, VecDeque};
@@ -414,14 +414,18 @@ pub async fn startup_tls(
414414
}
415415
}
416416

417-
418417
// Pass in username and password to authenticate against LDAP
419-
async fn authenticate_ldap(username: &str, password: &str, ldapurl: &str, ldapsuffix: &str) -> bool {
418+
async fn authenticate_ldap(
419+
username: &str,
420+
password: &str,
421+
ldapurl: &str,
422+
ldapsuffix: &str,
423+
) -> bool {
420424
// Connection to the LDAP Server
421425
let ldap_conn_settings = LdapConnSettings::new();
422-
let (conn, mut ldap) =
423-
LdapConnAsync::with_settings(
424-
ldap_conn_settings, ldapurl).await.unwrap();
426+
let (conn, mut ldap) = LdapConnAsync::with_settings(ldap_conn_settings, ldapurl)
427+
.await
428+
.unwrap();
425429
ldap3::drive!(conn);
426430

427431
// Takes the username provided and converts it into an email for validation
@@ -430,7 +434,11 @@ async fn authenticate_ldap(username: &str, password: &str, ldapurl: &str, ldapsu
430434

431435
// Attempts a simple bind using the passed in values of username and Password
432436
println!("{:?}", password);
433-
let result = ldap.simple_bind(email.as_str(), &password).await.unwrap().success();
437+
let result = ldap
438+
.simple_bind(email.as_str(), &password)
439+
.await
440+
.unwrap()
441+
.success();
434442
ldap.unbind().await.unwrap();
435443

436444
// If the authentication is successful return true, else return false.
@@ -440,7 +448,6 @@ async fn authenticate_ldap(username: &str, password: &str, ldapurl: &str, ldapsu
440448
}
441449
}
442450

443-
444451
impl<S, T> Client<S, T>
445452
where
446453
S: tokio::io::AsyncRead + std::marker::Unpin,
@@ -575,8 +582,7 @@ where
575582

576583
return Err(error);
577584
}
578-
}
579-
else if let "ldap" = config.general.admin_auth_type.as_str() {
585+
} else if let "ldap" = config.general.admin_auth_type.as_str() {
580586
clear_text_challenge(&mut write).await?;
581587
let code = match read.read_u8().await {
582588
Ok(p) => p,
@@ -624,14 +630,15 @@ where
624630
&str_password,
625631
&config.general.admin_auth_ldapurl.unwrap(),
626632
&config.general.admin_auth_ldapsuffix.unwrap(),
627-
).await;
633+
)
634+
.await;
628635
if unsuccessful_auth {
629-
wrong_password(&mut write, username).await?;
636+
wrong_password(&mut write, username).await?;
630637

631-
return Err(Error::ClientGeneralError(
632-
"Invalid password".into(),
633-
client_identifier,
634-
));
638+
return Err(Error::ClientGeneralError(
639+
"Invalid password".into(),
640+
client_identifier,
641+
));
635642
}
636643
}
637644
(false, generate_server_parameters_for_admin())
@@ -790,9 +797,7 @@ where
790797
));
791798
}
792799
}
793-
}
794-
795-
else if let "ldap" = pool.settings.user.auth_type.as_str() {
800+
} else if let "ldap" = pool.settings.user.auth_type.as_str() {
796801
clear_text_challenge(&mut write).await?;
797802
let code = match read.read_u8().await {
798803
Ok(p) => p,
@@ -840,16 +845,16 @@ where
840845
&str_password,
841846
&pool.settings.user.auth_ldapurl.clone().unwrap(),
842847
&pool.settings.user.auth_ldapsuffix.clone().unwrap(),
843-
).await;
848+
)
849+
.await;
844850
if unsuccessful_auth {
845-
wrong_password(&mut write, username).await?;
851+
wrong_password(&mut write, username).await?;
846852

847-
return Err(Error::ClientGeneralError(
848-
"Invalid password".into(),
849-
client_identifier,
850-
));
853+
return Err(Error::ClientGeneralError(
854+
"Invalid password".into(),
855+
client_identifier,
856+
));
851857
}
852-
853858
}
854859
let transaction_mode = pool.settings.pool_mode == PoolMode::Transaction;
855860
prepared_statements_enabled =

0 commit comments

Comments
 (0)