http_types/auth/
authentication_scheme.rs

1use std::fmt::{self, Display};
2use std::str::FromStr;
3
4use crate::bail_status as bail;
5
6/// HTTP Mutual Authentication Algorithms
7#[derive(Debug, PartialEq, Eq, Clone, Copy)]
8#[non_exhaustive]
9pub enum AuthenticationScheme {
10    /// [RFC7617](https://tools.ietf.org/html/rfc7617) Basic auth
11    Basic,
12    /// [RFC6750](https://tools.ietf.org/html/rfc6750) Bearer auth
13    Bearer,
14    /// [RFC7616](https://tools.ietf.org/html/rfc7616) Digest auth
15    Digest,
16    /// [RFC7486](https://tools.ietf.org/html/rfc7486) HTTP Origin-Bound Authentication (HOBA)
17    Hoba,
18    /// [RFC8120](https://tools.ietf.org/html/rfc8120) Mutual auth
19    Mutual,
20    /// [RFC4559](https://tools.ietf.org/html/rfc4559) Negotiate auth
21    Negotiate,
22    /// [RFC5849](https://tools.ietf.org/html/rfc5849) OAuth
23    OAuth,
24    /// [RFC7804](https://tools.ietf.org/html/rfc7804) SCRAM SHA1 auth
25    ScramSha1,
26    /// [RFC7804](https://tools.ietf.org/html/rfc7804) SCRAM SHA256 auth
27    ScramSha256,
28    /// [RFC8292](https://tools.ietf.org/html/rfc8292) Vapid auth
29    Vapid,
30}
31
32impl Display for AuthenticationScheme {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            Self::Basic => write!(f, "Basic"),
36            Self::Bearer => write!(f, "Bearer"),
37            Self::Digest => write!(f, "Digest"),
38            Self::Hoba => write!(f, "HOBA"),
39            Self::Mutual => write!(f, "Mutual"),
40            Self::Negotiate => write!(f, "Negotiate"),
41            Self::OAuth => write!(f, "OAuth"),
42            Self::ScramSha1 => write!(f, "SCRAM-SHA-1"),
43            Self::ScramSha256 => write!(f, "SCRAM-SHA-256"),
44            Self::Vapid => write!(f, "vapid"),
45        }
46    }
47}
48
49impl FromStr for AuthenticationScheme {
50    type Err = crate::Error;
51
52    fn from_str(s: &str) -> Result<Self, Self::Err> {
53        // NOTE(yosh): matching here is lowercase as specified by RFC2617#section-1.2
54        // > [...] case-insensitive token to identify the authentication scheme [...]
55        // https://tools.ietf.org/html/rfc2617#section-1.2
56        match s.to_lowercase().as_str() {
57            "basic" => Ok(Self::Basic),
58            "bearer" => Ok(Self::Bearer),
59            "digest" => Ok(Self::Digest),
60            "hoba" => Ok(Self::Hoba),
61            "mutual" => Ok(Self::Mutual),
62            "negotiate" => Ok(Self::Negotiate),
63            "oauth" => Ok(Self::OAuth),
64            "scram-sha-1" => Ok(Self::ScramSha1),
65            "scram-sha-256" => Ok(Self::ScramSha256),
66            "vapid" => Ok(Self::Vapid),
67            s => bail!(400, "`{}` is not a recognized authentication scheme", s),
68        }
69    }
70}