use axum::http::{HeaderMap, StatusCode, Uri};
use camel_api::security_policy::AccessMode;
use camel_auth::AuthenticatedPrincipal;
use crate::source_host::WasmSourceKernelAuth;
pub(crate) enum EdgeAuthOutcome {
PassThrough,
Authenticated(Box<AuthenticatedPrincipal>),
}
pub(crate) async fn authenticate_edge(
plan_access: Option<&AccessMode>,
kernel: Option<&WasmSourceKernelAuth>,
headers: &HeaderMap,
uri: &Uri,
) -> Result<EdgeAuthOutcome, StatusCode> {
match plan_access {
None | Some(AccessMode::Public) => return Ok(EdgeAuthOutcome::PassThrough),
Some(_) => {}
}
let Some(kernel) = kernel else {
tracing::warn!("wasm source: non-Public route without auth wiring — denying");
return Err(StatusCode::UNAUTHORIZED);
};
let Some(extracted) =
camel_auth::extract_token_multi(headers, uri, &kernel.plan.credential_sources)
else {
tracing::warn!("wasm source: no credential found in any permitted source");
return Err(StatusCode::UNAUTHORIZED);
};
match camel_auth::kernel_authenticate(&kernel.plan, &kernel.providers, &extracted).await {
Ok(principal) => Ok(EdgeAuthOutcome::Authenticated(Box::new(principal))),
Err(e) => {
tracing::warn!(error = %e, "wasm source: request authentication failed");
Err(StatusCode::UNAUTHORIZED)
}
}
}