1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//! Dynamic, decision-returning operation-level authorization.
//!
//! Where [`requires_role`](crate::schema::QueryDefinition) answers the *static*
//! question "does this principal hold role X?", an [`Authorizer`] answers the
//! *dynamic* question "may **this** principal run **this** operation, given its
//! input?". It is the operation-level analogue of the
//! [`FieldAuthorizer`](crate::security::FieldAuthorizer) and the counterpart of the
//! [`RLSPolicy`](crate::security::RLSPolicy) plugin: a Policy Enforcement Point
//! where the engine *enforces* but the *decision* is delegated to an app-supplied
//! trait object (in-process rules, a DB query, or an external service).
//!
//! # Semantics
//!
//! - **Fail-closed**: any `Err` returned by [`Authorizer::authorize`] is treated as a hard deny —
//! the request fails with [`FraiseQLError::Authorization`] (HTTP 403 / `FORBIDDEN`). The
//! underlying error is *not* surfaced to the client (no information leak).
//! - **Anonymous requests**: [`AuthzRequest::principal`] is `None` on the unauthenticated entry
//! path. The authorizer is still consulted, so an app may explicitly allow public operations or
//! deny everything anonymous — the decision is the app's, not the engine's.
//! - **AND-composition**: the decision composes with the static `requires_role` gate as a logical
//! AND — an operation runs only if *both* the static gate and the authorizer allow it. The
//! `requires_role` gate keeps its enumeration-hiding "not found in schema" response; the
//! authorizer denies with an explicit 403.
//! - **Every level, by default**: a read that reaches a nested type — a GraphQL selection `users {
//! orders { … } }`, a REST embed `?select=orders(…)` — puts that level to the authorizer too,
//! once per request per path (never per row), with [`AuthzRequest::nesting`] set. The rule the
//! developer wrote for reading `Order` holds wherever `Order` is read. An authorizer that means
//! to gate operations only allows every request whose `nesting` is `Some`.
//!
//! # Matching a type or a name
//!
//! [`AuthzRequest::target_type`] is the type a request reads (or writes), on every call:
//! root and nested, GraphQL and REST. Match on it to hold every read of `Order`, whatever
//! the transport or the entry point. [`AuthzRequest::name`] is the root field at the
//! root. At a nested level it is the type's **canonical list query**: its first declared
//! SQL-backed list query, the spelling REST has always passed, or the type name when it
//! has none. A rule on `name` therefore catches a nested read only through that one query.
//!
//! # Wiring
//!
//! Register an implementation on [`RuntimeConfig`](crate::runtime::RuntimeConfig) via
//! [`with_authorizer`](crate::runtime::RuntimeConfig::with_authorizer), exactly parallel to
//! [`with_field_authorizer`](crate::runtime::RuntimeConfig::with_field_authorizer) and
//! [`with_rls_policy`](crate::runtime::RuntimeConfig::with_rls_policy).
use crate::;
/// The kind of GraphQL operation being authorized.
/// Where a nested level sits: the type it is reached through, and the path to it.
/// One operation or level to put to the [`Authorizer`] — what [`enforce_authz`] turns
/// into an [`AuthzRequest`].
/// An operation-level authorization request handed to an [`Authorizer`].
///
/// Carries the principal (or `None` for an anonymous request), the operation kind
/// and root field name, the type read, where a nested level sits, and the request
/// input — the inputs a static role check lacks.
/// The decision an [`Authorizer`] returns for a single operation.
/// A pluggable, decision-returning operation-level authorizer.
///
/// Implementations decide, per principal / per operation / per input, whether an
/// operation may execute. The engine enforces the decision; this trait supplies it.
/// Implementations must be `Send + Sync` to be shared across the async execution path.
///
/// # Example
///
/// ```
/// use fraiseql_core::security::{Authorizer, AuthzRequest, AuthzDecision, OperationKind};
/// use fraiseql_core::error::Result;
///
/// /// Allow reads for everyone; require an authenticated principal for writes.
/// struct WritesNeedAuth;
///
/// impl Authorizer for WritesNeedAuth {
/// fn authorize(&self, req: &AuthzRequest<'_>) -> Result<AuthzDecision> {
/// // Reads are public; writes (and any future operation kind) need a principal.
/// // `OperationKind` is `#[non_exhaustive]`, so avoid an exhaustive match here.
/// if matches!(req.operation, OperationKind::Query) || req.principal.is_some() {
/// Ok(AuthzDecision::Allow)
/// } else {
/// Ok(AuthzDecision::Deny { reason: "authentication required".to_string() })
/// }
/// }
/// }
/// ```
/// The fail-closed deny error: a generic 403 that never echoes the underlying policy
/// error (avoids leaking why, beyond the app-supplied `reason`).
/// The answer when a policy backend fails to decide: fail closed, and say so honestly.
///
/// A policy `Err` is not a refusal: nobody adjudicated the caller, the backend could not
/// be reached. Reporting it as 403 told operators an outage was a permissions problem
/// and told clients not to retry exactly when they should (#1374). It is a 503 with no
/// detail — the policy's own error is never surfaced (no information leak) — and the
/// operation never executes, exactly as for a deny. Shared by the operation and the
/// field authorizer, so the transports cannot disagree.
/// Run the configured [`Authorizer`] over one or more operations or levels, fail-closed.
///
/// A multi-root query yields one call per root; a nested level, one call of its own. Any
/// [`AuthzDecision::Deny`] returns [`FraiseQLError::Authorization`] (403), with the `Deny`'s
/// `reason` folded into the message. A policy `Err` returns [`policy_unavailable`] (503,
/// the error itself not surfaced). Either way the operation never executes.
///
/// This is the canonical enforcement entry point. It is `pub` so transports that do
/// not route through the core executor (e.g. the `WebSocket` subscription handler in
/// `fraiseql-server`) can enforce the same fail-closed contract without reconstructing
/// the (`#[non_exhaustive]`) [`AuthzRequest`] themselves.
///
/// # Errors
///
/// Returns [`FraiseQLError::Authorization`] on the first `Deny` decision, and
/// [`FraiseQLError::ServiceUnavailable`] on the first policy error.