Skip to main content

systemprompt_security/authz/types/
request.rs

1use std::borrow::Cow;
2use std::collections::BTreeMap;
3
4use serde::{Deserialize, Serialize};
5use systemprompt_identifiers::{Actor, McpToolName, ModelId, TraceId, UserId};
6
7use super::decision::DenyReason;
8use super::entity_ref::EntityRef;
9
10/// Open enforcement-site context attached to an [`AuthzRequest`].
11///
12/// Replaces the previous closed enum so tenants can add their own
13/// enforcement sites (skill execution, order submission, file egress, ...)
14/// without a core change.
15///
16/// `kind` is a dotted-namespaced literal. Core mints three:
17///
18/// - `"none"` — no context (server-attach RBAC, etc).
19/// - `"gateway.invocation"` — payload `{ "model": "..." }`.
20/// - `"mcp.tool_call"` — payload `{ "tool": "..." }`.
21///
22/// Tenants mint their own (e.g. `"acme.order_submission"`) and recognise
23/// them in their hook. Core never interprets `payload`.
24#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
25pub struct AuthzContext {
26    pub kind: Cow<'static, str>,
27    #[serde(default, skip_serializing_if = "serde_json::Value::is_null")]
28    pub payload: serde_json::Value,
29}
30
31impl Default for AuthzContext {
32    fn default() -> Self {
33        Self::none()
34    }
35}
36
37impl AuthzContext {
38    pub const NONE_KIND: &'static str = "none";
39    pub const GATEWAY_INVOCATION_KIND: &'static str = "gateway.invocation";
40    pub const MCP_TOOL_CALL_KIND: &'static str = "mcp.tool_call";
41
42    #[must_use]
43    pub const fn none() -> Self {
44        Self {
45            kind: Cow::Borrowed(Self::NONE_KIND),
46            payload: serde_json::Value::Null,
47        }
48    }
49
50    #[must_use]
51    pub fn gateway_invocation(model: &ModelId) -> Self {
52        Self {
53            kind: Cow::Borrowed(Self::GATEWAY_INVOCATION_KIND),
54            payload: serde_json::json!({ "model": model.as_str() }),
55        }
56    }
57
58    #[must_use]
59    pub fn mcp_tool_call(tool: &McpToolName) -> Self {
60        Self {
61            kind: Cow::Borrowed(Self::MCP_TOOL_CALL_KIND),
62            payload: serde_json::json!({ "tool": tool.as_str() }),
63        }
64    }
65
66    /// `kind` must be dotted-namespaced (e.g. `"acme.order_submission"`) so
67    /// kinds from independent extensions cannot collide.
68    #[must_use]
69    pub fn extension(kind: impl Into<Cow<'static, str>>, payload: serde_json::Value) -> Self {
70        Self {
71            kind: kind.into(),
72            payload,
73        }
74    }
75
76    #[must_use]
77    pub fn gateway_invocation_model(&self) -> Option<ModelId> {
78        if self.kind != Self::GATEWAY_INVOCATION_KIND {
79            return None;
80        }
81        self.payload
82            .get("model")
83            .and_then(|v| v.as_str())
84            .map(ModelId::new)
85    }
86
87    #[must_use]
88    pub fn mcp_tool_call_tool(&self) -> Option<McpToolName> {
89        if self.kind != Self::MCP_TOOL_CALL_KIND {
90            return None;
91        }
92        self.payload
93            .get("tool")
94            .and_then(|v| v.as_str())
95            .map(McpToolName::new)
96    }
97
98    #[must_use]
99    pub fn is_none(&self) -> bool {
100        self.kind == Self::NONE_KIND
101    }
102}
103
104#[derive(Debug, Clone, Serialize, Deserialize)]
105pub struct AuthzRequest {
106    pub entity: EntityRef,
107    pub user_id: UserId,
108    #[serde(default)]
109    pub roles: Vec<String>,
110    /// Opaque ABAC attribute bag forwarded from `JwtClaims.attributes`.
111    /// Tenants namespace keys (e.g. `"acme.desk"`, `"boeing.clearance"`);
112    /// core never interprets values.
113    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
114    pub attributes: BTreeMap<String, serde_json::Value>,
115    pub trace_id: TraceId,
116    #[serde(default)]
117    pub context: AuthzContext,
118    /// RFC 8693 delegation lineage forwarded from
119    /// `RequestContext.auth.act_chain`. Empty when no token-exchange chain
120    /// is present.
121    #[serde(default, skip_serializing_if = "Vec::is_empty")]
122    pub act_chain: Vec<Actor>,
123}
124
125#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
126#[serde(tag = "decision", rename_all = "lowercase")]
127pub enum AuthzDecision {
128    Allow,
129    Deny { reason: DenyReason, policy: String },
130}