Skip to main content

ruststream_amqp/
config.rs

1//! Connection configuration: SASL profiles.
2
3use fe2o3_amqp::sasl_profile::SaslProfile;
4
5/// How the broker authenticates the connection, mapped onto the client's SASL profiles.
6///
7/// Constructed with the per-mechanism constructors and passed to
8/// [`AmqpBroker::sasl`](crate::AmqpBroker::sasl). A URL of the form `amqp://user:pass@host` also
9/// selects PLAIN implicitly; an explicit profile set here wins.
10///
11/// # Examples
12///
13/// ```
14/// use ruststream_amqp::Sasl;
15///
16/// let sasl = Sasl::plain("svc", "secret");
17/// # let _ = sasl;
18/// ```
19#[derive(Debug, Clone)]
20#[must_use]
21pub struct Sasl {
22    pub(crate) profile: SaslProfile,
23}
24
25impl Sasl {
26    /// SASL ANONYMOUS: no credentials, for brokers that allow unauthenticated connections.
27    ///
28    /// # Examples
29    ///
30    /// ```
31    /// use ruststream_amqp::Sasl;
32    /// let sasl = Sasl::anonymous();
33    /// # let _ = sasl;
34    /// ```
35    pub fn anonymous() -> Self {
36        Self {
37            profile: SaslProfile::Anonymous,
38        }
39    }
40
41    /// SASL PLAIN: username and password.
42    ///
43    /// # Examples
44    ///
45    /// ```
46    /// use ruststream_amqp::Sasl;
47    /// let sasl = Sasl::plain("svc", "secret");
48    /// # let _ = sasl;
49    /// ```
50    pub fn plain(username: impl Into<String>, password: impl Into<String>) -> Self {
51        Self {
52            profile: SaslProfile::Plain {
53                username: username.into(),
54                password: password.into(),
55            },
56        }
57    }
58
59    /// SASL EXTERNAL: authentication established outside SASL, typically a TLS client
60    /// certificate.
61    ///
62    /// # Examples
63    ///
64    /// ```
65    /// use ruststream_amqp::Sasl;
66    /// let sasl = Sasl::external();
67    /// # let _ = sasl;
68    /// ```
69    pub fn external() -> Self {
70        Self {
71            profile: SaslProfile::External,
72        }
73    }
74}