systemprompt_security/authz/types/
request.rs1use std::borrow::Cow;
7use std::collections::BTreeMap;
8
9use serde::{Deserialize, Serialize};
10use systemprompt_identifiers::{
11 Actor, ContextId, McpToolName, ModelId, SessionId, TaskId, TraceId, UserId,
12};
13
14use super::decision::DenyReason;
15use super::entity_ref::EntityRef;
16
17#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
32pub struct AuthzContext {
33 pub kind: Cow<'static, str>,
34 #[serde(default, skip_serializing_if = "serde_json::Value::is_null")]
35 pub payload: serde_json::Value,
36}
37
38impl Default for AuthzContext {
39 fn default() -> Self {
40 Self::none()
41 }
42}
43
44impl AuthzContext {
45 pub const NONE_KIND: &'static str = "none";
46 pub const GATEWAY_INVOCATION_KIND: &'static str = "gateway.invocation";
47 pub const MCP_TOOL_CALL_KIND: &'static str = "mcp.tool_call";
48
49 #[must_use]
50 pub const fn none() -> Self {
51 Self {
52 kind: Cow::Borrowed(Self::NONE_KIND),
53 payload: serde_json::Value::Null,
54 }
55 }
56
57 #[must_use]
58 pub fn gateway_invocation(model: &ModelId) -> Self {
59 Self {
60 kind: Cow::Borrowed(Self::GATEWAY_INVOCATION_KIND),
61 payload: serde_json::json!({ "model": model.as_str() }),
62 }
63 }
64
65 #[must_use]
66 pub fn mcp_tool_call(tool: &McpToolName) -> Self {
67 Self {
68 kind: Cow::Borrowed(Self::MCP_TOOL_CALL_KIND),
69 payload: serde_json::json!({ "tool": tool.as_str() }),
70 }
71 }
72
73 #[must_use]
76 pub fn extension(kind: impl Into<Cow<'static, str>>, payload: serde_json::Value) -> Self {
77 Self {
78 kind: kind.into(),
79 payload,
80 }
81 }
82
83 #[must_use]
84 pub fn gateway_invocation_model(&self) -> Option<ModelId> {
85 if self.kind != Self::GATEWAY_INVOCATION_KIND {
86 return None;
87 }
88 self.payload
89 .get("model")
90 .and_then(|v| v.as_str())
91 .map(ModelId::new)
92 }
93
94 #[must_use]
95 pub fn mcp_tool_call_tool(&self) -> Option<McpToolName> {
96 if self.kind != Self::MCP_TOOL_CALL_KIND {
97 return None;
98 }
99 self.payload
100 .get("tool")
101 .and_then(|v| v.as_str())
102 .map(McpToolName::new)
103 }
104
105 #[must_use]
106 pub fn is_none(&self) -> bool {
107 self.kind == Self::NONE_KIND
108 }
109
110 pub const MARKETPLACE_FLOOR_KEY: &'static str = "marketplace.attribute_floor";
111
112 #[must_use]
119 pub fn with_marketplace_floor(&self, floor: &BTreeMap<String, serde_json::Value>) -> Self {
120 let mut payload = match self.payload.clone() {
121 serde_json::Value::Object(map) => map,
122 _ => serde_json::Map::new(),
123 };
124 let floor_value = floor
125 .iter()
126 .map(|(k, v)| (k.clone(), v.clone()))
127 .collect::<serde_json::Map<String, serde_json::Value>>();
128 payload.insert(
129 Self::MARKETPLACE_FLOOR_KEY.to_owned(),
130 serde_json::Value::Object(floor_value),
131 );
132 Self {
133 kind: self.kind.clone(),
134 payload: serde_json::Value::Object(payload),
135 }
136 }
137
138 #[must_use]
139 pub fn marketplace_floor(&self) -> Option<BTreeMap<String, serde_json::Value>> {
140 let obj = self.payload.get(Self::MARKETPLACE_FLOOR_KEY)?.as_object()?;
141 Some(
142 obj.iter()
143 .map(|(k, v)| (k.clone(), v.clone()))
144 .collect::<BTreeMap<String, serde_json::Value>>(),
145 )
146 }
147}
148
149#[derive(Debug, Clone, Serialize, Deserialize)]
150pub struct AuthzRequest {
151 pub entity: EntityRef,
152 pub user_id: UserId,
153 #[serde(default)]
154 pub roles: Vec<String>,
155 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
159 pub attributes: BTreeMap<String, serde_json::Value>,
160 pub trace_id: TraceId,
161 #[serde(default, skip_serializing_if = "Option::is_none")]
166 pub session_id: Option<SessionId>,
167 #[serde(default)]
168 pub context: AuthzContext,
169 #[serde(default, skip_serializing_if = "Option::is_none")]
174 pub context_id: Option<ContextId>,
175 #[serde(default, skip_serializing_if = "Option::is_none")]
179 pub task_id: Option<TaskId>,
180 #[serde(default, skip_serializing_if = "Vec::is_empty")]
184 pub act_chain: Vec<Actor>,
185}
186
187#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
188#[serde(tag = "decision", rename_all = "lowercase")]
189pub enum AuthzDecision {
190 Allow,
191 Deny { reason: DenyReason, policy: String },
192}