1use chrono::{DateTime, Utc};
9use rusqlite::Connection;
10use std::path::Path;
11use std::sync::{Arc, Mutex};
12use thiserror::Error;
13
14#[path = "mandate_store_next/mod.rs"]
15mod mandate_store_next;
16
17#[derive(Debug, Clone, PartialEq, Eq)]
19pub struct AuthzReceipt {
20 pub mandate_id: String,
21 pub use_id: String,
22 pub use_count: u32,
23 pub consumed_at: DateTime<Utc>,
24 pub tool_call_id: String,
25 pub was_new: bool,
28}
29
30#[derive(Debug, Error, PartialEq, Eq)]
32pub enum AuthzError {
33 #[error("Mandate not found: {mandate_id}")]
34 MandateNotFound { mandate_id: String },
35
36 #[error("Mandate already used (single_use=true)")]
37 AlreadyUsed,
38
39 #[error("Max uses exceeded: {current} > {max}")]
40 MaxUsesExceeded { max: u32, current: u32 },
41
42 #[error("Nonce replay detected: {nonce}")]
43 NonceReplay { nonce: String },
44
45 #[error("Mandate metadata conflict for {mandate_id}: stored {field} differs")]
46 MandateConflict { mandate_id: String, field: String },
47
48 #[error("Invalid mandate constraints: single_use=true with max_uses={max_uses}")]
49 InvalidConstraints { max_uses: u32 },
50
51 #[error("Mandate revoked at {revoked_at}")]
52 Revoked { revoked_at: DateTime<Utc> },
53
54 #[error("Database error: {0}")]
55 Database(String),
56}
57
58impl From<rusqlite::Error> for AuthzError {
59 fn from(e: rusqlite::Error) -> Self {
60 AuthzError::Database(e.to_string())
61 }
62}
63
64#[derive(Debug, Clone)]
66pub struct MandateMetadata {
67 pub mandate_id: String,
68 pub mandate_kind: String,
69 pub audience: String,
70 pub issuer: String,
71 pub expires_at: Option<DateTime<Utc>>,
72 pub single_use: bool,
73 pub max_uses: Option<u32>,
74 pub canonical_digest: String,
75 pub key_id: String,
76}
77
78#[derive(Debug, Clone)]
80pub struct ConsumeParams<'a> {
81 pub mandate_id: &'a str,
82 pub tool_call_id: &'a str,
83 pub nonce: Option<&'a str>,
84 pub audience: &'a str,
85 pub issuer: &'a str,
86 pub tool_name: &'a str,
87 pub operation_class: &'a str,
88 pub source_run_id: Option<&'a str>,
89}
90
91#[derive(Clone)]
93pub struct MandateStore {
94 conn: Arc<Mutex<Connection>>,
95}
96
97impl MandateStore {
98 pub fn open(path: &Path) -> Result<Self, AuthzError> {
100 mandate_store_next::schema::open_impl(path)
101 }
102
103 pub fn memory() -> Result<Self, AuthzError> {
105 mandate_store_next::schema::memory_impl()
106 }
107
108 pub fn from_connection(conn: Connection) -> Result<Self, AuthzError> {
110 mandate_store_next::schema::from_connection_impl(conn)
111 }
112
113 pub fn upsert_mandate(&self, meta: &MandateMetadata) -> Result<(), AuthzError> {
115 mandate_store_next::upsert::upsert_mandate_impl(self, meta)
116 }
117
118 pub fn consume_mandate(&self, params: &ConsumeParams<'_>) -> Result<AuthzReceipt, AuthzError> {
120 mandate_store_next::txn::consume_mandate_in_txn_impl(self, params)
121 }
122
123 fn consume_mandate_inner(
124 &self,
125 conn: &Connection,
126 params: &ConsumeParams<'_>,
127 ) -> Result<AuthzReceipt, AuthzError> {
128 mandate_store_next::consume::consume_mandate_inner_impl(conn, params)
129 }
130
131 pub fn get_use_count(&self, mandate_id: &str) -> Result<Option<u32>, AuthzError> {
133 mandate_store_next::stats::get_use_count_impl(self, mandate_id)
134 }
135
136 pub fn count_uses(&self, mandate_id: &str) -> Result<u32, AuthzError> {
138 mandate_store_next::stats::count_uses_impl(self, mandate_id)
139 }
140
141 pub fn nonce_exists(
143 &self,
144 audience: &str,
145 issuer: &str,
146 nonce: &str,
147 ) -> Result<bool, AuthzError> {
148 mandate_store_next::stats::nonce_exists_impl(self, audience, issuer, nonce)
149 }
150
151 pub fn upsert_revocation(&self, r: &RevocationRecord) -> Result<(), AuthzError> {
159 mandate_store_next::revocation::upsert_revocation_impl(self, r)
160 }
161
162 pub fn get_revoked_at(&self, mandate_id: &str) -> Result<Option<DateTime<Utc>>, AuthzError> {
164 mandate_store_next::revocation::get_revoked_at_impl(self, mandate_id)
165 }
166
167 pub fn is_revoked(&self, mandate_id: &str) -> Result<bool, AuthzError> {
169 mandate_store_next::revocation::is_revoked_impl(self, mandate_id)
170 }
171}
172
173#[derive(Debug, Clone)]
175pub struct RevocationRecord {
176 pub mandate_id: String,
177 pub revoked_at: DateTime<Utc>,
178 pub reason: Option<String>,
179 pub revoked_by: Option<String>,
180 pub source: Option<String>,
181 pub event_id: Option<String>,
182}
183
184pub fn compute_use_id(mandate_id: &str, tool_call_id: &str, use_count: u32) -> String {
190 mandate_store_next::stats::compute_use_id_impl(mandate_id, tool_call_id, use_count)
191}
192
193#[cfg(test)]
194#[path = "mandate_store_next/tests.rs"]
195mod tests;