tiberius/client/
auth.rs

1use std::fmt::Debug;
2
3#[derive(Clone, PartialEq, Eq)]
4pub struct SqlServerAuth {
5    user: String,
6    password: String,
7}
8
9impl SqlServerAuth {
10    pub(crate) fn user(&self) -> &str {
11        &self.user
12    }
13
14    pub(crate) fn password(&self) -> &str {
15        &self.password
16    }
17}
18
19impl Debug for SqlServerAuth {
20    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21        f.debug_struct("SqlServerAuth")
22            .field("user", &self.user)
23            .field("password", &"<HIDDEN>")
24            .finish()
25    }
26}
27
28#[derive(Clone, PartialEq, Eq)]
29#[cfg(any(all(windows, feature = "winauth"), doc))]
30#[cfg_attr(feature = "docs", doc(all(windows, feature = "winauth")))]
31pub struct WindowsAuth {
32    pub(crate) user: String,
33    pub(crate) password: String,
34    pub(crate) domain: Option<String>,
35}
36
37#[cfg(any(all(windows, feature = "winauth"), doc))]
38#[cfg_attr(feature = "docs", doc(all(windows, feature = "winauth")))]
39impl Debug for WindowsAuth {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        f.debug_struct("WindowsAuth")
42            .field("user", &self.user)
43            .field("password", &"<HIDDEN>")
44            .field("domain", &self.domain)
45            .finish()
46    }
47}
48
49/// Defines the method of authentication to the server.
50#[derive(Clone, Debug, PartialEq, Eq)]
51pub enum AuthMethod {
52    /// Authenticate directly with SQL Server.
53    SqlServer(SqlServerAuth),
54    /// Authenticate with Windows credentials.
55    #[cfg(any(all(windows, feature = "winauth"), doc))]
56    #[cfg_attr(feature = "docs", doc(cfg(all(windows, feature = "winauth"))))]
57    Windows(WindowsAuth),
58    /// Authenticate as the currently logged in user. On Windows uses SSPI and
59    /// Kerberos on Unix platforms.
60    #[cfg(any(
61        all(windows, feature = "winauth"),
62        all(unix, feature = "integrated-auth-gssapi"),
63        doc
64    ))]
65    #[cfg_attr(
66        feature = "docs",
67        doc(cfg(any(windows, all(unix, feature = "integrated-auth-gssapi"))))
68    )]
69    Integrated,
70    /// Authenticate with an AAD token. The token should encode an AAD user/service principal
71    /// which has access to SQL Server.
72    AADToken(String),
73    #[doc(hidden)]
74    None,
75}
76
77impl AuthMethod {
78    /// Construct a new SQL Server authentication configuration.
79    pub fn sql_server(user: impl ToString, password: impl ToString) -> Self {
80        Self::SqlServer(SqlServerAuth {
81            user: user.to_string(),
82            password: password.to_string(),
83        })
84    }
85
86    /// Construct a new Windows authentication configuration.
87    #[cfg(any(all(windows, feature = "winauth"), doc))]
88    #[cfg_attr(feature = "docs", doc(cfg(all(windows, feature = "winauth"))))]
89    pub fn windows(user: impl AsRef<str>, password: impl ToString) -> Self {
90        let (domain, user) = match user.as_ref().find('\\') {
91            Some(idx) => (Some(&user.as_ref()[..idx]), &user.as_ref()[idx + 1..]),
92            _ => (None, user.as_ref()),
93        };
94
95        Self::Windows(WindowsAuth {
96            user: user.to_string(),
97            password: password.to_string(),
98            domain: domain.map(|s| s.to_string()),
99        })
100    }
101
102    /// Construct a new configuration with AAD auth token.
103    pub fn aad_token(token: impl ToString) -> Self {
104        Self::AADToken(token.to_string())
105    }
106}