systemprompt_security/authz/types/
request.rs1use 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#[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 #[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 #[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 #[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}