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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
//! Read-path per-user identity and the platform's read-scope contract.
//!
//! Since platform #2922 the role-scoped read routes (audit / decisions /
//! overrides) answer from the identity the CALLER presents, not from the tenant
//! credential alone. The tenant credential in `Authorization` says which
//! organization is asking; it does not say WHO. A caller that presents no
//! per-user identity to an enterprise stack is not "a caller who sees
//! everything" and is not "a caller who sees nothing by coincidence" — it is a
//! caller the platform cannot scope, and every scoped read it makes returns
//! zero rows by construction.
//!
//! This module carries the whole surface:
//!
//! - the per-user identity itself ([`AxonFlowConfig::user_token`] for a
//! client-wide identity, the `*_as` read methods for a per-call one, and
//! [`AxonFlowClient::as_user`] for a process acting on behalf of several
//! people), stamped as the `X-User-Token` header from exactly ONE site —
//! `AxonFlowClient::dispatch`, which every request goes through. There is no
//! per-method header plumbing, deliberately: the platform reads the header
//! once in its own proxy middleware (`platform/agent/proxy.go`
//! `proxyAuthMiddleware`), not per route, so a per-method sprinkle here would
//! be a second, drifting copy of a decision the platform makes in one place.
//!
//! - the response side of the same contract: `X-Axonflow-Read-Scope`, which the
//! platform stamps on every scoped read (`platform/orchestrator/read_scope.go`
//! `applyReadScopeHeader`) to say which of the three scopes the answer was
//! computed under. Without it, a 404 from explain and an empty list from
//! `list_decisions` are indistinguishable from "the row is not there", which
//! is how a governed read comes to report a confident, vacuous nothing.
//!
//! [`AxonFlowConfig::user_token`]: crate::AxonFlowConfig::user_token
//! [`AxonFlowClient::as_user`]: crate::AxonFlowClient::as_user
use fmt;
/// The request header carrying the per-user identity.
///
/// This constant is the SDK's only spelling of it. The header is set in exactly
/// one place (`AxonFlowClient::dispatch`); if you find yourself setting it in a
/// method, the method is the wrong altitude.
pub const HEADER_USER_TOKEN: &str = "X-User-Token";
/// The response header the platform stamps on scoped reads.
pub const HEADER_READ_SCOPE: &str = "X-Axonflow-Read-Scope";
/// The scope the platform computed a role-scoped read under, taken from the
/// `X-Axonflow-Read-Scope` response header.
///
/// Three named variants are the platform's closed set. Two states are NOT in it
/// and are deliberately distinct from each other and from the three:
///
/// - [`ReadScope::Absent`] — the response carried no such header. That is what a
/// pre-#2922 platform, a non-scoped route, or a proxy that dropped the header
/// looks like. It means "not stated", never "none": treating an absent header
/// as a scope of `none` would turn every older stack's perfectly good read
/// into a refusal.
///
/// - [`ReadScope::Other`] — a scope a newer platform names and this build does
/// not recognise. Its VALUE is preserved rather than folded into one of the
/// three, so a caller can see what it was — trimmed and lower-cased, like the
/// three named ones, because the same normalisation has to apply to every
/// value or the recognised set would depend on a proxy's header casing. It
/// never triggers a refusal:
/// this header is the platform's account of a decision it has ALREADY made and
/// applied, so an unrecognised value is a reporting gap on our side, not a
/// licence to invent an outcome.
/// A role-scoped read whose answer was decided by the caller's identity scope
/// rather than by the data.
///
/// Carried by [`AxonFlowError::ReadScope`], which exists because "no rows" and
/// "no identity" are the same bytes on the wire. The platform distinguishes them
/// in the `X-Axonflow-Read-Scope` header; this is that distinction made visible,
/// so a read that could not have succeeded reports a cause instead of a
/// confident nothing.
///
/// Two shapes, told apart by [`ReadScopeRefusal::identity_missing`]:
///
/// - [`ReadScope::None`] — no identity was RESOLVED; the read returned zero rows
/// by construction and says nothing about what exists.
/// - [`ReadScope::OwnRows`] — an identity WAS resolved, and the row is not among
/// the ones attributed to it. That does NOT mean the row exists and belongs to
/// somebody else: the platform answers "not attributed to you" and "not there
/// at all" with the identical 404, deliberately, so that a miss cannot be used
/// to probe for another user's rows. This reports the scope, not a claim about
/// what exists.
///
/// The presented token is never included in the message: it is safe to log,
/// which is the point of putting the diagnosis in a type rather than in a string
/// the caller assembles from the credential.
///
/// [`AxonFlowError::ReadScope`]: crate::AxonFlowError::ReadScope
/// The typed refusal for a scoped read that came back with nothing, or `None`
/// when the scope does not explain the result.
///
/// `None` for [`ReadScope::Tenant`] (the caller could see the whole tenant and it
/// still was not there — a genuine miss), for [`ReadScope::Absent`] (the platform
/// did not state a scope; see [`ReadScope`] for why absent is not none), and for
/// [`ReadScope::Other`] (a newer platform's; reporting a cause we cannot actually
/// read would be a confident wrong diagnosis).
/// The typed refusal for a scoped read that came back EMPTY under a scope that
/// could not have returned a row; `None` in every other case.
///
/// One helper rather than a check at each read, because "the page is empty and
/// the scope is none" is one rule and the reads that need it decode their body
/// on more than one path each. A rule copied per return site is a rule that ends
/// up applied on some of them.
///
/// The emptiness guard is as load-bearing as the scope guard: a non-empty page
/// is never turned into an error, whatever the header says. And only
/// [`ReadScope::None`] refuses — an own-rows or tenant-wide read that
/// legitimately found nothing is a real answer, and replacing it with an error
/// would swap one wrong report for another.
/// The diagnosis for a per-user identity that cannot be an HTTP header value.
///
/// Reported rather than dropped. Dropping it is the worst of the three outcomes
/// available: the read then goes out unidentified, the platform answers with a
/// scoped-empty page, and the SDK tells the caller "no identity was presented"
/// — which is true of the wire and false of what they did. A caller who set a
/// token and gets told they set none has been sent to look in the wrong place.
///
/// The token is NEVER in the message. The offending byte's index and CLASS are,
/// because those are what you need to find it in your own minting code, and
/// neither is a fragment of the credential. The byte's VALUE is withheld too.
///
/// The rejected set is narrower than "not printable ASCII", and getting that
/// wrong points the caller at an innocent byte. A header value admits obs-text,
/// so every byte from 0x80 up is LEGAL: `café-token` is sent as-is, and so is a
/// token with an emoji in it — measured against `HeaderValue::from_str`, not
/// inferred from the RFC. What it refuses is exactly the C0 controls except
/// tab, plus DEL. A diagnostic that treated non-ASCII as the offender would,
/// for a token like `café…\n…`, report the position of the `é` and send someone
/// to fix the one character that was fine.
pub
/// Whether two URLs are the same origin: scheme, host AND port.
///
/// Subdomains are NOT trusted, deliberately: this header is an identity
/// assertion, not a session cookie, and "close enough" is not a property an
/// identity should have.
pub