use std::{ops::Deref, sync::Arc};
pub trait SecFetchAuthorizer {
fn authorize<B>(&self, request: &http::Request<B>) -> AuthorizationDecision;
}
#[doc(hidden)]
pub struct NoopAuthorizer;
impl SecFetchAuthorizer for NoopAuthorizer {
fn authorize<B>(&self, _: &http::Request<B>) -> AuthorizationDecision {
AuthorizationDecision::Continue
}
}
pub enum AuthorizationDecision {
Allowed,
Denied,
Continue,
}
impl<T, A> SecFetchAuthorizer for T
where
T: Deref<Target = A>,
A: SecFetchAuthorizer,
{
fn authorize<B>(&self, request: &http::Request<B>) -> AuthorizationDecision {
self.deref().authorize(request)
}
}
pub struct PathAuthorizer(Arc<[&'static str]>);
impl PathAuthorizer {
pub fn new(allowed_paths: impl Into<Arc<[&'static str]>>) -> Self {
Self(allowed_paths.into())
}
}
impl SecFetchAuthorizer for PathAuthorizer {
fn authorize<B>(&self, request: &http::Request<B>) -> AuthorizationDecision {
if self.0.contains(&request.uri().path()) {
return AuthorizationDecision::Allowed;
}
AuthorizationDecision::Continue
}
}