amq_protocol/
auth.rs

1pub use crate::uri::SASLMechanism;
2use crate::{
3    types::{AMQPValue, FieldTable, generation::gen_field_table},
4    uri::AMQPUserInfo,
5};
6
7/// Structure holding the username and password for authentication
8#[derive(Clone, Debug, PartialEq, Eq)]
9pub struct Credentials {
10    username: String,
11    password: String,
12}
13
14impl Credentials {
15    /// Create a new Credentials instance with the given username and password
16    pub fn new(username: String, password: String) -> Self {
17        Self { username, password }
18    }
19
20    /// Get the username
21    pub fn username(&self) -> &str {
22        &self.username
23    }
24
25    /// Get the password
26    pub fn password(&self) -> &str {
27        &self.password
28    }
29
30    /// Get the SASL authentication String for the given SASL mechanism
31    pub fn sasl_auth_string(&self, mechanism: SASLMechanism) -> String {
32        match mechanism {
33            SASLMechanism::AMQPlain => self.amqplain_auth_string(),
34            SASLMechanism::External => String::default(),
35            SASLMechanism::Plain => format!("\0{}\0{}", self.username(), self.password()),
36            SASLMechanism::RabbitCrDemo => self.username.clone(),
37        }
38    }
39
40    /// Get the answer we need to give to the server for the RabbitCrDemo mehanism
41    pub fn rabbit_cr_demo_answer(&self) -> String {
42        format!("My password is {}", self.password)
43    }
44
45    fn amqplain_auth_string(&self) -> String {
46        let needed_len = 4 /* FieldTable length */ + 15 /* LOGIN + PASSWORD + 2 * 1 (length) */ + 5 /* type + length */ + self.username().len() + 5 /* type + length */ + self.password().len();
47        let mut buf = vec![0; needed_len];
48        let mut table = FieldTable::default();
49        table.insert(
50            "LOGIN".into(),
51            AMQPValue::LongString(self.username().into()),
52        );
53        table.insert(
54            "PASSWORD".into(),
55            AMQPValue::LongString(self.password().into()),
56        );
57        gen_field_table(&table)((&mut buf[..]).into())
58            .expect("miscalculated AMQPLAIN string length");
59        // skip the FieldTable length
60        String::from_utf8_lossy(&buf.as_slice()[4..]).to_string()
61    }
62}
63
64impl Default for Credentials {
65    fn default() -> Self {
66        Self::new("guest".into(), "guest".into())
67    }
68}
69
70impl From<AMQPUserInfo> for Credentials {
71    fn from(user_info: AMQPUserInfo) -> Self {
72        Self {
73            username: user_info.username,
74            password: user_info.password,
75        }
76    }
77}
78
79#[cfg(test)]
80mod test {
81    use super::*;
82
83    #[test]
84    fn test_amqplain() {
85        assert_eq!(
86            Credentials::default().amqplain_auth_string(),
87            "\u{5}LOGINS\u{0}\u{0}\u{0}\u{5}guest\u{8}PASSWORDS\u{0}\u{0}\u{0}\u{5}guest"
88                .to_string()
89        );
90    }
91}