use std::fmt::Debug;
#[derive(Clone, PartialEq, Eq)]
pub struct SqlServerAuth {
user: String,
password: String,
}
impl SqlServerAuth {
pub(crate) fn user(&self) -> &str {
&self.user
}
pub(crate) fn password(&self) -> &str {
&self.password
}
}
impl Debug for SqlServerAuth {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SqlServerAuth")
.field("user", &self.user)
.field("password", &"<HIDDEN>")
.finish()
}
}
#[derive(Clone, PartialEq, Eq)]
#[cfg(any(all(windows, feature = "winauth"), doc))]
#[cfg_attr(feature = "docs", doc(all(windows, feature = "winauth")))]
pub struct WindowsAuth {
pub(crate) user: String,
pub(crate) password: String,
pub(crate) domain: Option<String>,
}
#[cfg(any(all(windows, feature = "winauth"), doc))]
#[cfg_attr(feature = "docs", doc(all(windows, feature = "winauth")))]
impl Debug for WindowsAuth {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("WindowsAuth")
.field("user", &self.user)
.field("password", &"<HIDDEN>")
.field("domain", &self.domain)
.finish()
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AuthMethod {
SqlServer(SqlServerAuth),
#[cfg(any(all(windows, feature = "winauth"), doc))]
#[cfg_attr(feature = "docs", doc(cfg(all(windows, feature = "winauth"))))]
Windows(WindowsAuth),
#[cfg(any(
all(windows, feature = "winauth"),
all(unix, feature = "integrated-auth-gssapi"),
doc
))]
#[cfg_attr(
feature = "docs",
doc(cfg(any(windows, all(unix, feature = "integrated-auth-gssapi"))))
)]
Integrated,
AADToken(String),
#[doc(hidden)]
None,
}
impl AuthMethod {
pub fn sql_server(user: impl ToString, password: impl ToString) -> Self {
Self::SqlServer(SqlServerAuth {
user: user.to_string(),
password: password.to_string(),
})
}
#[cfg(any(all(windows, feature = "winauth"), doc))]
#[cfg_attr(feature = "docs", doc(cfg(all(windows, feature = "winauth"))))]
pub fn windows(user: impl AsRef<str>, password: impl ToString) -> Self {
let (domain, user) = match user.as_ref().find('\\') {
Some(idx) => (Some(&user.as_ref()[..idx]), &user.as_ref()[idx + 1..]),
_ => (None, user.as_ref()),
};
Self::Windows(WindowsAuth {
user: user.to_string(),
password: password.to_string(),
domain: domain.map(|s| s.to_string()),
})
}
pub fn aad_token(token: impl ToString) -> Self {
Self::AADToken(token.to_string())
}
}