//! The one place that decides what an authenticated caller may do. //! //! Order everywhere: authenticate (401 / CSRF 403), resolve the resource, then //! the gate. A resource the caller cannot see answers 404 (it is not confirmed to //! exist); one they can see but may not use answers 403. Grants are read from the //! database on every call, so a revoke is effective on the next request. use super::routes::OperatorActor; use crate::auth::{AuditAction, AuditOutcome, AuditResource, Permission}; use crate::state::HubState; use axum::http::StatusCode; use std::collections::HashSet; use uuid::Uuid; fn now() -> i64 { chrono::Utc::now().timestamp() } fn audit( state: &HubState, actor: &OperatorActor, resource: AuditResource, allowed: bool, ) -> Result<(), StatusCode> { state .db .audit_event( &actor.principal, AuditAction::Authorization, resource, if allowed { AuditOutcome::Allowed } else { AuditOutcome::Denied }, actor.request_id, ) .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR) } fn internal(error: impl std::fmt::Display) -> StatusCode { tracing::error!("authorization lookup failed: {error}"); StatusCode::INTERNAL_SERVER_ERROR } /// Management actions are never delegated: anyone who is not a configured /// administrator gets 403 without any lookup, so this also reveals nothing about resources. pub(super) fn admin_only(state: &HubState, actor: &OperatorActor) -> Result<(), StatusCode> { let allowed = actor.subject.admin; audit(state, actor, AuditResource::Api, allowed)?; if allowed { Ok(()) } else { Err(StatusCode::FORBIDDEN) } } /// Use `permission` on one orbit. Not visible (no `orbit.read`) is 404, visible /// but not permitted is 403. A permission that cannot be granted is admin-only. pub(super) fn scoped( state: &HubState, actor: &OperatorActor, permission: Permission, orbit: Uuid, ) -> Result<(), StatusCode> { let resource = AuditResource::Orbit(orbit); if actor.subject.admin { return audit(state, actor, resource, true); } let held = |permission: Permission| -> Result { if permission.grantable() { { state .db .has_permission( actor.subject.user_id.as_deref(), &actor.subject.groups, permission.as_str(), &orbit.to_string(), now(), ) .map_err(internal) } } else { Ok(false) } }; if !held(Permission::OrbitRead)? { audit(state, actor, resource, false)?; return Err(StatusCode::NOT_FOUND); } let allowed = held(permission)?; audit(state, actor, resource, allowed)?; if allowed { Ok(()) } else { Err(StatusCode::FORBIDDEN) } } /// Use one relation. Administrators always; anyone else only through a live grant on that very relation /// (not on its orbit, not on an older version of it). Everything else answers 404, so a relation's /// existence is not confirmed to someone who cannot use it. #[allow(dead_code)] // used by the refresh run (M6c); the matrix already exercises the list built on the same grants pub(super) fn relation( state: &HubState, actor: &OperatorActor, relation: Uuid, ) -> Result<(), StatusCode> { let resource = AuditResource::Relation(relation); if actor.subject.admin { return audit(state, actor, resource, true); } let granted = state .db .granted_relations( actor.subject.user_id.as_deref(), &actor.subject.groups, now(), ) .map_err(internal)?; let allowed = granted.iter().any(|id| id == &relation.to_string()); audit(state, actor, resource, allowed)?; if allowed { Ok(()) } else { Err(StatusCode::NOT_FOUND) } } /// The relations a caller may use: `None` for an administrator (all), otherwise their granted ids. /// A caller with none gets 403 rather than an empty list. pub(super) fn visible_relations( state: &HubState, actor: &OperatorActor, ) -> Result>, StatusCode> { if actor.subject.admin { audit(state, actor, AuditResource::Api, true)?; return Ok(None); } let granted = state .db .granted_relations( actor.subject.user_id.as_deref(), &actor.subject.groups, now(), ) .map_err(internal)?; audit(state, actor, AuditResource::Api, !granted.is_empty())?; if granted.is_empty() { Err(StatusCode::FORBIDDEN) } else { Ok(Some(granted)) } } /// Which orbits a list may show. pub(super) enum Visible { All, Only(HashSet), } impl Visible { pub(super) fn contains(&self, orbit: Uuid) -> bool { match self { Self::All => true, Self::Only(set) => set.contains(&orbit), } } /// SQL-friendly form: `None` = no restriction. pub(super) fn restriction(&self) -> Option> { match self { Self::All => None, Self::Only(set) => Some(set.iter().map(Uuid::to_string).collect()), } } } /// Orbits where the caller holds both `orbit.read` and `permission`. A caller /// with nothing to see gets 403 rather than an empty list. pub(super) fn visible( state: &HubState, actor: &OperatorActor, permission: Permission, ) -> Result { if actor.subject.admin { audit(state, actor, AuditResource::Api, true)?; return Ok(Visible::All); } let fetch = |permission: Permission| { if !permission.grantable() { return Ok(Default::default()); } state .db .permitted_orbits( actor.subject.user_id.as_deref(), &actor.subject.groups, permission.as_str(), now(), ) .map_err(internal) }; let read = fetch(Permission::OrbitRead)?; let wanted = if permission == Permission::OrbitRead { None } else { Some(fetch(permission)?) }; let parse = |set: &HashSet| -> HashSet { set.iter() .filter_map(|id| Uuid::parse_str(id).ok()) .collect() }; let result = match wanted { None if read.everywhere => Visible::All, None => Visible::Only(parse(&read.orbits)), Some(w) => match (read.everywhere, w.everywhere) { (true, true) => Visible::All, (true, false) => Visible::Only(parse(&w.orbits)), (false, true) => Visible::Only(parse(&read.orbits)), (false, false) => Visible::Only( parse(&read.orbits) .intersection(&parse(&w.orbits)) .copied() .collect(), ), }, }; let nothing = matches!(&result, Visible::Only(set) if set.is_empty()); audit(state, actor, AuditResource::Api, !nothing)?; if nothing { Err(StatusCode::FORBIDDEN) } else { Ok(result) } }