1pub use crate::uri::SASLMechanism;
2use crate::{
3 types::{AMQPValue, FieldTable, generation::gen_field_table},
4 uri::AMQPUserInfo,
5};
6
7#[derive(Clone, Debug, PartialEq, Eq)]
9pub struct Credentials {
10 username: String,
11 password: String,
12}
13
14impl Credentials {
15 pub fn new(username: String, password: String) -> Self {
17 Self { username, password }
18 }
19
20 pub fn username(&self) -> &str {
22 &self.username
23 }
24
25 pub fn password(&self) -> &str {
27 &self.password
28 }
29
30 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 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 + 15 + 5 + self.username().len() + 5 + 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 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}