Skip to main content

limes_proto/
auth.rs

1use std::fmt;
2
3use zeroize::Zeroize;
4
5/// Credentials collected by a frontend and submitted to the backend.
6#[derive(Clone, PartialEq, Eq)]
7pub struct AuthRequest {
8    pub username: String,
9    pub password: String,
10    pub tty: Option<String>,
11}
12
13impl AuthRequest {
14    #[must_use]
15    pub fn new(username: impl Into<String>, password: impl Into<String>) -> Self {
16        Self {
17            username: username.into(),
18            password: password.into(),
19            tty: None,
20        }
21    }
22
23    pub fn clear_secret(&mut self) {
24        self.password.zeroize();
25        self.password.clear();
26    }
27}
28
29impl Drop for AuthRequest {
30    fn drop(&mut self) {
31        self.clear_secret();
32    }
33}
34
35impl fmt::Debug for AuthRequest {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        f.debug_struct("AuthRequest")
38            .field("username", &self.username)
39            .field("password", &"<redacted>")
40            .field("tty", &self.tty)
41            .finish()
42    }
43}
44
45/// Display-only authenticated identity; not authority to launch a session.
46#[derive(Debug, Clone, PartialEq, Eq)]
47pub struct AuthSuccess {
48    pub username: String,
49    pub uid: u32,
50    pub gid: u32,
51    pub home: Option<String>,
52    pub shell: Option<String>,
53}
54
55/// Authentication failure categories frontends can render differently.
56#[derive(Debug, Clone, PartialEq, Eq)]
57pub enum AuthFailure {
58    InvalidCredentials,
59    LockedOut,
60    BackendUnavailable(String),
61    Internal(String),
62}
63
64impl fmt::Display for AuthFailure {
65    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66        match self {
67            Self::InvalidCredentials => f.write_str("invalid credentials"),
68            Self::LockedOut => f.write_str("account locked out"),
69            Self::BackendUnavailable(reason) => {
70                write!(f, "authentication backend unavailable: {reason}")
71            }
72            Self::Internal(reason) => write!(f, "authentication error: {reason}"),
73        }
74    }
75}
76
77pub type AuthOutcome = Result<AuthSuccess, AuthFailure>;
78
79/// PAM conversation message kinds reported by authentication backends.
80#[derive(Debug, Clone, Copy, PartialEq, Eq)]
81pub enum PamMessageKind {
82    PromptEchoOn,
83    PromptEchoOff,
84    TextInfo,
85    Error,
86}
87
88#[cfg(test)]
89mod tests {
90    use super::*;
91
92    #[test]
93    fn auth_request_debug_redacts_password() {
94        let request = AuthRequest::new("alice", "secret");
95
96        let rendered = format!("{request:?}");
97
98        assert!(rendered.contains("alice"));
99        assert!(rendered.contains("<redacted>"));
100        assert!(!rendered.contains("secret"));
101    }
102
103    #[test]
104    fn clear_secret_removes_password_value() {
105        let mut request = AuthRequest::new("alice", "secret");
106
107        request.clear_secret();
108
109        assert!(request.password.is_empty());
110    }
111}