use std::{
borrow::Cow,
error::Error,
fmt::{self, Display, Formatter},
};
use http::{HeaderMap, HeaderValue, header::WWW_AUTHENTICATE};
use super::{
realm::{MAX_REALM_BYTES, RealmIssue, escape, is_visible_ascii, validate},
response::has_valid_bearer_challenge,
};
use crate::http::policy::{HttpPolicy, PolicyError, PolicyResponseIssue, sealed::Sealed};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BearerChallenge {
realm: Cow<'static, str>,
}
impl BearerChallenge {
pub fn new(realm: &str) -> Result<Self, BearerChallengeError> {
validate(realm).map_err(BearerChallengeError::from_issue)?;
Ok(Self {
realm: Cow::Owned(realm.to_owned()),
})
}
pub const fn from_static(realm: &'static str) -> Self {
let bytes = realm.as_bytes();
assert!(!bytes.is_empty(), "Bearer realm must not be empty");
assert!(
bytes.len() <= MAX_REALM_BYTES,
"Bearer realm exceeds its public header budget"
);
let mut index = 0;
while index < bytes.len() {
assert!(
is_visible_ascii(bytes[index]),
"Bearer realm must contain only visible ASCII"
);
index += 1;
}
Self {
realm: Cow::Borrowed(realm),
}
}
fn header_value(&self) -> Result<HeaderValue, PolicyError> {
format!("Bearer realm=\"{}\"", escape(&self.realm))
.parse()
.map_err(|_| PolicyError::new("Bearer challenge is not a header value"))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum BearerChallengeError {
EmptyRealm,
RealmTooLong {
actual_bytes: usize,
},
InvalidByte {
byte_index: usize,
byte: u8,
},
}
impl BearerChallengeError {
fn from_issue(issue: RealmIssue) -> Self {
match issue {
RealmIssue::Empty => Self::EmptyRealm,
RealmIssue::TooLong { actual_bytes } => Self::RealmTooLong { actual_bytes },
RealmIssue::InvalidByte { byte_index, byte } => Self::InvalidByte { byte_index, byte },
}
}
}
impl Display for BearerChallengeError {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::EmptyRealm => formatter.write_str("Bearer realm must not be empty"),
Self::RealmTooLong { actual_bytes } => write!(
formatter,
"Bearer realm is {actual_bytes} bytes; maximum is {MAX_REALM_BYTES}"
),
Self::InvalidByte { byte_index, byte } => write!(
formatter,
"Bearer realm has invalid byte {byte:#04x} at index {byte_index}"
),
}
}
}
impl Error for BearerChallengeError {}
#[derive(Debug, Clone, Copy, Default)]
pub struct BearerUnauthorized;
impl Sealed for BearerUnauthorized {
fn validate_response_headers(headers: &HeaderMap) -> Result<(), PolicyResponseIssue> {
if !headers.contains_key(WWW_AUTHENTICATE) || has_valid_bearer_challenge(headers) {
return Ok(());
}
Err(PolicyResponseIssue {
header: "www-authenticate",
expected: "a valid Bearer challenge",
})
}
}
impl HttpPolicy for BearerUnauthorized {
type Input = BearerChallenge;
const STATUS: u16 = 401;
const NAME: &'static str = "unauthorized";
const REQUIRED_HEADERS: &'static [&'static str] = &["www-authenticate"];
fn headers(input: Self::Input) -> Result<HeaderMap, PolicyError> {
let mut headers = HeaderMap::new();
headers.insert(WWW_AUTHENTICATE, input.header_value()?);
Ok(headers)
}
}