micromegas/servers/write_audience.rs
1//! Resolving the authenticated write audience at the HTTP edge.
2//!
3//! `rust/public` is the only crate that sees both `AuthContext` (`micromegas-auth`) and the
4//! ingestion service (`micromegas-ingestion`), so this is where an `Option<&Extension<AuthContext>>`
5//! -- the request extension every ingestion route carries -- turns into a
6//! [`micromegas_ingestion::write_audience::WriteAudience`]. `WriteAudience` is single-state: a
7//! credential carrying no bound audience, or one that fails `WriteAudience::new` validation,
8//! resolves to the deployment default here rather than to a distinct unstamped state.
9
10use axum::Extension;
11use micromegas_auth::types::AuthContext;
12use micromegas_ingestion::write_audience::WriteAudience;
13use micromegas_tracing::prelude::*;
14
15/// Resolves the write audience for one request: a credential carrying a bound audience gets
16/// stamped with it (or, if that label fails [`WriteAudience::new`] validation, with
17/// `default_audience` instead, `warn!`-logged); one without a bound audience -- or with no auth
18/// provider at all, `ctx: None` -- resolves straight to `default_audience`. The result is always
19/// a real audience: there is no unstamped state left to resolve to.
20pub fn resolve_write_audience(
21 ctx: Option<&Extension<AuthContext>>,
22 default_audience: &WriteAudience,
23) -> WriteAudience {
24 let Some(bound) = ctx.and_then(|Extension(c)| c.bound_audience.as_deref()) else {
25 return default_audience.clone();
26 };
27 match WriteAudience::new(bound) {
28 Ok(w) => w,
29 Err(e) => {
30 warn!(
31 "bound_audience failed WriteAudience validation, using the deployment default: {e:#}"
32 );
33 default_audience.clone()
34 }
35 }
36}