use std::future::Future;
use dynosaur::dynosaur;
use iroh::EndpointId;
use n0_error::StackError;
use crate::parse::HttpProxyRequest;
#[derive(StackError)]
pub enum AuthError {
InvalidCredentials,
TokenExpired,
Forbidden,
BadRequest,
}
#[dynosaur(pub(crate) DynAuthHandler = dyn(box) AuthHandler)]
pub trait AuthHandler: Send + Sync {
fn authorize<'a>(
&'a self,
remote_id: EndpointId,
req: &'a HttpProxyRequest,
) -> impl Future<Output = Result<(), AuthError>> + Send + 'a;
}
#[derive(Debug, Default)]
pub struct DenyAll;
impl AuthHandler for DenyAll {
async fn authorize<'a>(
&'a self,
_remote_id: EndpointId,
_req: &'a HttpProxyRequest,
) -> Result<(), AuthError> {
Err(AuthError::Forbidden)
}
}
#[derive(Debug, Default)]
pub struct AcceptAll;
impl AuthHandler for AcceptAll {
async fn authorize<'a>(
&'a self,
_remote_id: EndpointId,
_req: &'a HttpProxyRequest,
) -> Result<(), AuthError> {
Ok(())
}
}