plexus-core 0.6.0

Core infrastructure for Plexus RPC: Activation trait, DynamicHub, schemas
Documentation
//! AUTH-2 — the grant gate in dispatch: default deny, every method.
//!
//! A [`DynamicHub`](super::DynamicHub) given a gate
//! ([`DynamicHub::with_gate`](super::DynamicHub::with_gate)) asks it before
//! routing every call: the method's grant (its schema's `grant`, else
//! `<activation>.<method>`), held through a role on the scope its target
//! resolves to (its schema's `target` param, read from the call's params),
//! within the session's ceiling. A hub without a gate never enters this
//! module and dispatches exactly as before.
//!
//! # Decision table (gate set)
//!
//! | call | anonymous | signed in |
//! |---|---|---|
//! | `public` method | runs (empty reach) | runs (its reach) |
//! | introspection: `<ns>.schema`, the hub's `hash` / `hashes` / `schema` | runs | runs |
//! | method with a `target`, value present | `Unauthenticated` | allowed iff a role held on a scope in the value's chain lists the grant, and every ceiling layer allows it there |
//! | method without a target (or the target param absent / null) | `Unauthenticated` | allowed iff the grant is held anywhere (within the ceiling) |
//! | method no schema describes (a nested child path) | `Unauthenticated` | as "without a target", grant = the dotted path |
//! | a signed-in grant (AUTH-5: `auth.whoami`, `auth.logout`, `auth.requires`) | `Unauthenticated` | runs (its reach), whatever the roles or ceiling |
//!
//! A target whose value is a list is checked element by element; every
//! element must be allowed.
//!
//! AUTH-13: before any of this, `params` must be an object (or absent):
//! the gate reads a target by name, and a bare value (`"MENGER-1"`, `[…]`)
//! names none — it would be decided as "held anywhere" while the method
//! deserialized it into its only parameter. A target that is neither a
//! scalar nor a list of scalars is refused too. Both are `InvalidParams`
//! (`error_kind: invalid_params`), whoever calls. Introspection stays open here: what it answers
//! is the caller's view (AUTH-4, [`super::view`]) — `_info` and every
//! `schema` answered per caller; this gate decides calls, not what is listed.
//!
//! # How a method receives its [`Reach`]
//!
//! The gate's answer — where the grant is held — rides into the method as
//! [`Reach`]: the dispatch runs the routed call inside a task-local scope,
//! and `#[plexus_macros::method]` hands a method that declares a `reach:
//! Reach` parameter (stripped from the schema, like `auth`) the value of
//! [`current_reach`]. Outside a gated dispatch, [`current_reach`] is
//! [`Reach::everywhere`] — what a hub without a gate has always meant. The
//! reach is live: [`Reach::allows`] / [`Reach::current`] ask the gate again
//! as it stands, which is how a stream checks each event (IDENTITY-37).

use plexus_auth_core::grants::{Gate, Grant, Reach, Refusal};
use serde_json::Value;

use super::auth::AuthContext;
use super::plexus::{AuthzDenyReason, PlexusError};
use super::scope_gate::MethodGateInfo;

tokio::task_local! {
    static CURRENT_REACH: Reach;
}

/// The reach of the call being dispatched: what the gate answered for its
/// grant. [`Reach::everywhere`] when no gate decided this call (a hub
/// without a gate, or a call that did not come through a gated dispatch).
///
/// `#[plexus_macros::method]` calls this for a method that declares a
/// `reach: Reach` parameter, before the method's stream is built — read it
/// there, not from inside a stream, which runs outside the dispatch's scope.
pub fn current_reach() -> Reach {
    CURRENT_REACH.try_with(Reach::clone).unwrap_or_else(|_| Reach::everywhere())
}

/// Run `fut` with `reach` as the [`current_reach`].
pub async fn with_reach<F: std::future::Future>(reach: Reach, fut: F) -> F::Output {
    CURRENT_REACH.scope(reach, fut).await
}

/// What the gate decided for a call that may proceed: run it under this
/// reach, or (introspection) without one.
pub(crate) enum Admit {
    Reach(Reach),
    Open,
}

impl From<Refusal> for PlexusError {
    fn from(r: Refusal) -> Self {
        match r {
            Refusal::Unauthenticated => {
                PlexusError::Unauthenticated("sign in first: this method is not public".into())
            }
            Refusal::MissingGrant { grant, scope, roles, held_on } => PlexusError::Forbidden {
                reason: AuthzDenyReason::MissingGrant { grant, scope, roles, held_on },
            },
            Refusal::Ended { reason } => PlexusError::Unauthenticated(reason),
        }
    }
}

/// AUTH-11: the identity a call acts as — `auth` while its sign-in stands
/// ([`Gate::alive`]); once it has ended (signed out, expired, revoked),
/// nobody: the call is decided and dispatched as anonymous, and the second
/// value says why. A socket that signed out sees the anonymous view and
/// reaches only the `public` methods — not `auth.grant`.
pub(crate) fn standing<'a>(gate: &Gate, auth: Option<&'a AuthContext>) -> (Option<&'a AuthContext>, Option<String>) {
    match auth.map(|a| gate.alive(a)) {
        Some(Err(reason)) => (None, Some(reason)),
        _ => (auth, None),
    }
}

/// AUTH-11: an anonymous refusal of a sign-in that ended says so, rather
/// than "sign in first".
pub(crate) fn say_ended(e: PlexusError, ended: Option<&str>) -> PlexusError {
    match (e, ended) {
        (PlexusError::Unauthenticated(_), Some(reason)) => PlexusError::Unauthenticated(reason.to_string()),
        (e, _) => e,
    }
}

/// The target values a call names: `None` when the param is absent or
/// null; one string per element when it is a list. AUTH-13: a value the
/// gate cannot read as a scope — an object, or a list holding anything but
/// scalars — is refused, never skipped (skipped, it would be decided as
/// "no target": held anywhere).
fn target_values(params: &Value, param: &str) -> Result<Option<Vec<String>>, PlexusError> {
    let Some(v) = params.get(param) else { return Ok(None) };
    let one = |v: &Value| match v {
        Value::String(s) => Some(s.clone()),
        Value::Number(n) => Some(n.to_string()),
        Value::Bool(b) => Some(b.to_string()),
        _ => None,
    };
    let unreadable = || {
        PlexusError::InvalidParams(format!("`{param}` names what this call acts on: a string, or a list of strings"))
    };
    match v {
        Value::Null => Ok(None),
        Value::Array(items) => items.iter().map(|i| one(i).ok_or_else(unreadable)).collect::<Result<_, _>>().map(Some),
        other => one(other).map(|s| Some(vec![s])).ok_or_else(unreadable),
    }
}

/// AUTH-13: under a gate, a call's params are an object (or absent). A
/// bare value names no target, so the gate would decide it as "held
/// anywhere" — while a method of one parameter would take it as that
/// parameter.
fn params_shape(params: &Value) -> Result<(), PlexusError> {
    match params {
        Value::Object(_) | Value::Null => Ok(()),
        _ => Err(PlexusError::InvalidParams(
            "params must be an object naming each parameter, e.g. {\"id\": \"…\"}: a gated method takes no bare value".into(),
        )),
    }
}

/// Decide one call. `full_path` is the dotted method as dispatched;
/// `hub_ns` the hub's own namespace.
pub(crate) fn decide(
    gate: &Gate,
    hub_ns: &str,
    full_path: &str,
    info: Option<&MethodGateInfo>,
    params: &Value,
    auth: Option<&AuthContext>,
    signed_in_grants: &std::collections::BTreeSet<String>,
) -> Result<Admit, PlexusError> {
    params_shape(params)?;
    let (ns, method) = full_path.split_once('.').unwrap_or((full_path, ""));
    let introspection = method == "schema"
        || method.ends_with(".schema")
        || (ns == hub_ns && matches!(method, "hash" | "hashes"));
    if introspection {
        return Ok(Admit::Open);
    }
    let grant = Grant::new(info.and_then(|i| i.grant.clone()).unwrap_or_else(|| full_path.to_string()));
    if info.is_some_and(|i| i.public) {
        return Ok(Admit::Reach(gate.reach_of_public(auth, &grant)));
    }
    // AUTH-5: what every signed-in caller reaches (`auth.whoami`,
    // `auth.logout`, `auth.requires`) — whatever its roles or ceiling, but
    // never anonymously.
    if signed_in_grants.contains(grant.as_str()) {
        if auth.and_then(|a| a.principal()).is_none() {
            return Err(Refusal::Unauthenticated.into());
        }
        return Ok(Admit::Reach(gate.reach_of_public(auth, &grant)));
    }
    let targets = match info.and_then(|i| i.target.as_deref()) {
        Some(param) => target_values(params, param)?,
        None => None,
    };
    match targets {
        None => Ok(Admit::Reach(gate.check(auth, &grant, None)?)),
        Some(values) if values.is_empty() => Ok(Admit::Reach(gate.check(auth, &grant, None)?)),
        Some(values) => {
            let mut reach = None;
            for v in &values {
                reach = Some(gate.check(auth, &grant, Some(v))?);
            }
            Ok(Admit::Reach(reach.expect("non-empty")))
        }
    }
}

/// The wire rendering of a refusal's facts, merged into the error stream
/// item (`{"type":"error", …}`) beside its `message` and `code`:
/// `error_kind` always for an auth refusal, and for a missing grant its
/// `grant`, `scope`, `roles` and `held_on`. Additive: a client that reads
/// only `message` sees what it always saw.
pub(crate) fn error_fields(e: &PlexusError) -> Option<serde_json::Map<String, Value>> {
    let mut m = serde_json::Map::new();
    match e {
        PlexusError::Unauthenticated(_) => {
            m.insert("error_kind".into(), "unauthenticated".into());
        }
        PlexusError::Forbidden { reason } => {
            m.insert("error_kind".into(), "forbidden".into());
            match reason {
                AuthzDenyReason::MissingGrant { grant, scope, roles, held_on } => {
                    m.insert("grant".into(), grant.as_str().into());
                    m.insert("scope".into(), scope.as_str().into());
                    m.insert("roles".into(), roles.iter().map(|r| Value::from(r.as_str())).collect());
                    m.insert("held_on".into(), held_on.iter().map(|s| Value::from(s.as_str())).collect());
                }
                AuthzDenyReason::MissingScope { scope } => {
                    m.insert("reason".into(), "missing_scope".into());
                    m.insert("scope".into(), scope.as_str().into());
                }
                AuthzDenyReason::TenantBoundary => {
                    m.insert("reason".into(), "tenant_boundary".into());
                }
                AuthzDenyReason::NotAccepted => {
                    m.insert("reason".into(), "not_accepted".into());
                }
            }
        }
        // AUTH-13: params the gate (or the method) cannot read.
        PlexusError::InvalidParams(_) => {
            m.insert("error_kind".into(), "invalid_params".into());
        }
        _ => return None,
    }
    Some(m)
}

/// A refusal as a client reads it back from an error stream item — the
/// typed view of [`error_fields`]' `forbidden` shape.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ForbiddenItem {
    pub grant: String,
    pub scope: String,
    #[serde(default)]
    pub roles: Vec<String>,
    #[serde(default)]
    pub held_on: Vec<String>,
    pub message: String,
}

impl ForbiddenItem {
    /// The refusal an error item carries, when it is a missing grant.
    pub fn from_item(item: &Value) -> Option<Self> {
        if item.get("type")?.as_str()? != "error" || item.get("error_kind")?.as_str()? != "forbidden" {
            return None;
        }
        item.get("grant")?;
        serde_json::from_value(item.clone()).ok()
    }
}