serenade-security 1.0.0

AuthN/Z hooks: users, tokens, voters, firewall, CSRF, and password hashing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! LDAP directory bind helpers (feature `ldap`).
//!
//! Shapes config and a sync bind trait so apps can plug `ldap3` (or another
//! client) without Serenade owning the network stack. This is **not** an LDAP
//! server.

use std::sync::Arc;

use crate::firewall::Authenticator;
use crate::{InMemoryUser, SecurityError, UsernamePasswordToken};

/// Placeholder token replaced by [`LdapBindConfig::user_dn`].
pub const LDAP_USERNAME_PLACEHOLDER: &str = "{username}";

/// Connection / DN templates for an LDAP bind authenticator.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LdapBindConfig {
    uri: String,
    base_dn: String,
    /// DN template with [`LDAP_USERNAME_PLACEHOLDER`] (for example `uid={username},ou=people,dc=example,dc=com`).
    user_dn_template: String,
}

impl LdapBindConfig {
    /// Creates bind settings. `user_dn_template` should contain `{username}`.
    #[must_use]
    pub fn new(
        uri: impl Into<String>,
        base_dn: impl Into<String>,
        user_dn_template: impl Into<String>,
    ) -> Self {
        Self {
            uri: uri.into(),
            base_dn: base_dn.into(),
            user_dn_template: user_dn_template.into(),
        }
    }

    /// LDAP URI (for example `ldaps://ldap.example.com:636`).
    #[must_use]
    pub fn uri(&self) -> &str {
        &self.uri
    }

    /// Search / base DN.
    #[must_use]
    pub fn base_dn(&self) -> &str {
        &self.base_dn
    }

    /// User DN template containing [`LDAP_USERNAME_PLACEHOLDER`].
    #[must_use]
    pub fn user_dn_template(&self) -> &str {
        &self.user_dn_template
    }

    /// Expands [`LDAP_USERNAME_PLACEHOLDER`] in the DN template.
    ///
    /// # Errors
    ///
    /// Returns [`SecurityError::Ldap`] when `username` is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use serenade_security::LdapBindConfig;
    ///
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let config = LdapBindConfig::new(
    ///     "ldaps://ldap.example.com",
    ///     "dc=example,dc=com",
    ///     "uid={username},ou=people,dc=example,dc=com",
    /// );
    /// assert_eq!(
    ///     config.user_dn("alice")?,
    ///     "uid=alice,ou=people,dc=example,dc=com"
    /// );
    /// # Ok(())
    /// # }
    /// ```
    pub fn user_dn(&self, username: &str) -> Result<String, SecurityError> {
        if username.is_empty() {
            return Err(SecurityError::Ldap {
                message: "username is empty".to_owned(),
            });
        }
        Ok(self
            .user_dn_template
            .replace(LDAP_USERNAME_PLACEHOLDER, username))
    }
}

/// Identity returned after a successful directory bind.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LdapIdentity {
    dn: String,
    username: String,
    roles: Vec<String>,
}

impl LdapIdentity {
    /// Creates an identity with directory DN, login name, and roles.
    #[must_use]
    pub fn new(
        dn: impl Into<String>,
        username: impl Into<String>,
        roles: impl IntoIterator<Item = impl Into<String>>,
    ) -> Self {
        Self {
            dn: dn.into(),
            username: username.into(),
            roles: roles.into_iter().map(Into::into).collect(),
        }
    }

    /// Bound distinguished name.
    #[must_use]
    pub fn dn(&self) -> &str {
        &self.dn
    }

    /// Login name used for the bind.
    #[must_use]
    pub fn username(&self) -> &str {
        &self.username
    }

    /// Roles granted after bind (app-owned mapping).
    #[must_use]
    pub fn roles(&self) -> &[String] {
        &self.roles
    }
}

/// Sync LDAP bind. Apps implement this with their LDAP client.
pub trait LdapBinder: Send + Sync {
    /// Binds as `username` / `password` and returns the directory identity.
    ///
    /// # Errors
    ///
    /// Returns [`SecurityError::Authentication`] or [`SecurityError::Ldap`] on failure.
    fn bind(&self, username: &str, password: &str) -> Result<LdapIdentity, SecurityError>;
}

/// Fixed-response binder for tests.
#[derive(Debug, Clone)]
pub struct MockLdapBinder {
    identity: LdapIdentity,
    expected_username: Option<String>,
    expected_password: Option<String>,
}

impl MockLdapBinder {
    /// Always returns `identity` (any non-empty password succeeds).
    #[must_use]
    pub const fn new(identity: LdapIdentity) -> Self {
        Self {
            identity,
            expected_username: None,
            expected_password: None,
        }
    }

    /// Requires exact username/password match before returning the identity.
    #[must_use]
    pub fn with_credentials(
        mut self,
        username: impl Into<String>,
        password: impl Into<String>,
    ) -> Self {
        self.expected_username = Some(username.into());
        self.expected_password = Some(password.into());
        self
    }
}

impl LdapBinder for MockLdapBinder {
    fn bind(&self, username: &str, password: &str) -> Result<LdapIdentity, SecurityError> {
        if password.is_empty() {
            return Err(SecurityError::Authentication {
                message: "empty password".to_owned(),
            });
        }
        if let (Some(expected_user), Some(expected_pass)) =
            (&self.expected_username, &self.expected_password)
            && (username != expected_user || password != expected_pass)
        {
            return Err(SecurityError::Authentication {
                message: "invalid ldap credentials".to_owned(),
            });
        }
        Ok(self.identity.clone())
    }
}

/// Builds an authenticated security token from a successful LDAP identity.
#[must_use]
pub fn token_from_ldap_identity(
    identity: &LdapIdentity,
    password_echo: impl Into<String>,
) -> UsernamePasswordToken {
    UsernamePasswordToken::authenticated(
        InMemoryUser::new(identity.username(), identity.roles().to_vec()),
        password_echo,
    )
}

/// Binds then maps to a [`UsernamePasswordToken`].
///
/// # Errors
///
/// Propagates binder failures.
pub fn authenticate_ldap_password(
    binder: &dyn LdapBinder,
    username: &str,
    password: &str,
) -> Result<UsernamePasswordToken, SecurityError> {
    let identity = binder.bind(username, password)?;
    Ok(token_from_ldap_identity(&identity, password))
}

/// Firewall [`Authenticator`] that expects `username:password` in the credentials string.
#[derive(Clone)]
pub struct LdapAuthenticator {
    binder: Arc<dyn LdapBinder>,
}

impl LdapAuthenticator {
    /// Wraps a binder for header / form credentials shaped as `username:password`.
    #[must_use]
    pub fn new(binder: impl LdapBinder + 'static) -> Self {
        Self {
            binder: Arc::new(binder),
        }
    }
}

impl Authenticator for LdapAuthenticator {
    fn authenticate(
        &self,
        credentials: Option<&str>,
    ) -> Result<UsernamePasswordToken, SecurityError> {
        let Some(raw) = credentials else {
            return Err(SecurityError::Authentication {
                message: "missing credentials".to_owned(),
            });
        };
        let (username, password) = split_user_password(raw)?;
        authenticate_ldap_password(self.binder.as_ref(), username, password)
    }
}

/// Splits `username:password` (first colon). Password may contain colons.
///
/// # Errors
///
/// Returns [`SecurityError::Ldap`] when the shape is invalid.
pub fn split_user_password(raw: &str) -> Result<(&str, &str), SecurityError> {
    let Some((username, password)) = raw.split_once(':') else {
        return Err(SecurityError::Ldap {
            message: "credentials must be username:password".to_owned(),
        });
    };
    if username.is_empty() {
        return Err(SecurityError::Ldap {
            message: "username is empty".to_owned(),
        });
    }
    Ok((username, password))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::user::{TokenInterface, UserInterface};

    fn sample_config() -> LdapBindConfig {
        LdapBindConfig::new(
            "ldaps://ldap.example.com",
            "dc=example,dc=com",
            "uid={username},ou=people,dc=example,dc=com",
        )
    }

    fn sample_identity() -> LdapIdentity {
        LdapIdentity::new(
            "uid=alice,ou=people,dc=example,dc=com",
            "alice",
            ["ROLE_USER"],
        )
    }

    #[test]
    fn user_dn_expands_username() {
        let config = sample_config();
        assert_eq!(config.uri(), "ldaps://ldap.example.com");
        assert_eq!(config.base_dn(), "dc=example,dc=com");
        assert_eq!(
            config.user_dn_template(),
            "uid={username},ou=people,dc=example,dc=com"
        );
        assert_eq!(
            config.user_dn("alice").expect("dn"),
            "uid=alice,ou=people,dc=example,dc=com"
        );
    }

    #[test]
    fn user_dn_rejects_empty_username() {
        let err = sample_config().user_dn("").expect_err("empty");
        assert!(matches!(err, SecurityError::Ldap { .. }));
    }

    #[test]
    fn mock_bind_and_token() {
        let binder = MockLdapBinder::new(sample_identity()).with_credentials("alice", "secret");
        let token = authenticate_ldap_password(&binder, "alice", "secret").expect("ok");
        assert!(token.is_authenticated());
        assert_eq!(
            token.user().map(UserInterface::user_identifier),
            Some("alice")
        );
        assert_eq!(token.credentials(), Some("secret"));
    }

    #[test]
    fn mock_without_expected_credentials_accepts_any_user() {
        let binder = MockLdapBinder::new(sample_identity());
        let token = authenticate_ldap_password(&binder, "anyone", "secret").expect("ok");
        assert_eq!(
            token.user().map(UserInterface::user_identifier),
            Some("alice")
        );
    }

    #[test]
    fn mock_rejects_wrong_username() {
        let binder = MockLdapBinder::new(sample_identity()).with_credentials("alice", "secret");
        let err = authenticate_ldap_password(&binder, "eve", "secret").expect_err("bad");
        assert!(matches!(err, SecurityError::Authentication { .. }));
    }

    #[test]
    fn mock_rejects_wrong_password() {
        let binder = MockLdapBinder::new(sample_identity()).with_credentials("alice", "secret");
        let err = authenticate_ldap_password(&binder, "alice", "nope").expect_err("bad");
        assert!(matches!(err, SecurityError::Authentication { .. }));
    }

    #[test]
    fn mock_rejects_empty_password() {
        let binder = MockLdapBinder::new(sample_identity());
        let err = binder.bind("alice", "").expect_err("empty");
        assert!(matches!(err, SecurityError::Authentication { .. }));
    }

    #[test]
    fn authenticator_parses_colon_credentials() {
        let auth = LdapAuthenticator::new(
            MockLdapBinder::new(LdapIdentity::new(
                "uid=bob,ou=people,dc=example,dc=com",
                "bob",
                ["ROLE_USER"],
            ))
            .with_credentials("bob", "p:ass"),
        );
        let token = auth.authenticate(Some("bob:p:ass")).expect("ok");
        assert_eq!(
            token.user().map(UserInterface::user_identifier),
            Some("bob")
        );
    }

    #[test]
    fn authenticator_rejects_missing_and_bad_shape() {
        let auth = LdapAuthenticator::new(MockLdapBinder::new(sample_identity()));
        assert!(matches!(
            auth.authenticate(None),
            Err(SecurityError::Authentication { .. })
        ));
        assert!(matches!(
            auth.authenticate(Some("nocolon")),
            Err(SecurityError::Ldap { .. })
        ));
        assert!(matches!(
            auth.authenticate(Some(":nopass")),
            Err(SecurityError::Ldap { .. })
        ));
    }

    #[test]
    fn identity_accessors() {
        let id = sample_identity();
        assert_eq!(id.dn(), "uid=alice,ou=people,dc=example,dc=com");
        assert_eq!(id.username(), "alice");
        assert_eq!(id.roles(), &["ROLE_USER".to_owned()]);
        let token = token_from_ldap_identity(&id, "");
        assert!(token.is_authenticated());
    }

    #[test]
    fn split_user_password_ok() {
        assert_eq!(
            split_user_password("u:p:extra").expect("split"),
            ("u", "p:extra")
        );
    }
}