1use std::{fmt, str::FromStr};
2
3use crate::{Error, Result};
4
5/// Authentication mechanisms
6///
7/// See <https://dbus.freedesktop.org/doc/dbus-specification.html#auth-mechanisms>
8#[derive(Clone, Copy, Debug, PartialEq, Eq)]
9pub enum AuthMechanism {
10 /// This is the recommended authentication mechanism on platforms where credentials can be
11 /// transferred out-of-band, in particular Unix platforms that can perform credentials-passing
12 /// over the `unix:` transport.
13 External,
14
15 /// This mechanism is designed to establish that a client has the ability to read a private
16 /// file owned by the user being authenticated.
17 Cookie,
18
19 /// Does not perform any authentication at all, and should not be accepted by message buses.
20 /// However, it might sometimes be useful for non-message-bus uses of D-Bus.
21 Anonymous,
22}
23
24impl fmt::Display for AuthMechanism {
25 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26 let mech: &'static str = match self {
27 AuthMechanism::External => "EXTERNAL",
28 AuthMechanism::Cookie => "DBUS_COOKIE_SHA1",
29 AuthMechanism::Anonymous => "ANONYMOUS",
30 };
31 write!(f, "{mech}")
32 }
33}
34
35impl FromStr for AuthMechanism {
36 type Err = Error;
37
38 fn from_str(s: &str) -> Result<Self> {
39 match s {
40 "EXTERNAL" => Ok(AuthMechanism::External),
41 "DBUS_COOKIE_SHA1" => Ok(AuthMechanism::Cookie),
42 "ANONYMOUS" => Ok(AuthMechanism::Anonymous),
43 _ => Err(Error::Handshake(format!("Unknown mechanism: {s}"))),
44 }
45 }
46}
47