1use std::{fmt, str::FromStr};
23use crate::{Error, Result};
45/// 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.
13External,
1415/// This mechanism is designed to establish that a client has the ability to read a private
16 /// file owned by the user being authenticated.
17Cookie,
1819/// 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.
21Anonymous,
22}
2324impl fmt::Display for AuthMechanism {
25fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26let mech = match self {
27 AuthMechanism::External => "EXTERNAL",
28 AuthMechanism::Cookie => "DBUS_COOKIE_SHA1",
29 AuthMechanism::Anonymous => "ANONYMOUS",
30 };
31write!(f, "{mech}")
32 }
33}
3435impl FromStr for AuthMechanism {
36type Err = Error;
3738fn from_str(s: &str) -> Result<Self> {
39match 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}