vti_common/auth/backend.rs
1//! Canonical auth-flow backend trait.
2//!
3//! Five callers (VTA, VTC, did-hosting-control, did-hosting-server,
4//! webvh-witness) all run the same shape of `/auth/challenge`,
5//! `/auth/authenticate`, `/auth/refresh` flow with minor policy
6//! differences (TEE attestation, DID-method allowlist, per-DID
7//! rate limit) and different storage / error / role types.
8//!
9//! [`AuthBackend`] is the boundary that lets the canonical handlers
10//! in [`crate::auth::handlers`] run unchanged across all five
11//! services. Each service implements `AuthBackend` once; the
12//! per-route boilerplate collapses to "build the input, call the
13//! handler, map the response."
14//!
15//! ## Trait shape
16//!
17//! - Associated [`Store`](AuthBackend::Store) — the session storage
18//! primitives the handler needs. Implementors typically wrap their
19//! keyspace handle in a thin adapter that implements
20//! [`SessionStore`].
21//! - Associated [`Error`](AuthBackend::Error) — the backend's own
22//! error type. Must convert from [`AuthError`] so the canonical
23//! handler can raise the auth-specific failures and the route
24//! layer surfaces them via its existing `IntoResponse` plumbing.
25//! - Associated [`Role`](AuthBackend::Role) — the backend's role
26//! enum (vti-common's `Role`, did-hosting's `Role`, etc.). The
27//! handler treats it opaquely; it appears in the JWT minter
28//! contract and the audit log only.
29//! - Default-method policy hooks — [`validate_did`], [`attest_challenge`],
30//! [`max_pending_challenges_per_did`], [`audit`] — backends override
31//! only when they need non-default behaviour. Most backends override
32//! one or two; the rest inherit safe defaults.
33//!
34//! ## What stays out of the trait
35//!
36//! - Transport (REST vs. DIDComm) — the canonical handler takes a
37//! pre-extracted [`AuthInput`] struct; transport-specific unpacking
38//! stays in the route handler.
39//! - JWT structure — `JwtKeys` is the same across all callers; the
40//! handler holds a `&JwtKeys` reference and mints directly.
41//! - Wire-shape serialisation — canonical request / response types
42//! live in `vta_sdk::protocols::auth` and are shared with clients.
43
44use async_trait::async_trait;
45use serde::Serialize;
46use std::fmt::Debug;
47
48use crate::auth::session::Session;
49
50// ---------------------------------------------------------------------------
51// Canonical auth-flow errors
52// ---------------------------------------------------------------------------
53
54/// Auth-specific failures the canonical handlers can raise.
55///
56/// Each backend's `Error` associated type must implement
57/// `From<AuthError>` so the handler can return these variants and
58/// the route layer surfaces them via its existing `IntoResponse`
59/// plumbing (e.g. vti-common's `AppError::Unauthorized(_)` arm).
60#[derive(Debug, thiserror::Error)]
61pub enum AuthError {
62 /// DID is not in the backend's ACL, or the ACL entry is expired.
63 /// Returned as 403 Forbidden to avoid revealing whether the DID
64 /// exists in the ACL system at all (timing-side-channel
65 /// mitigation; the ACL check happens before any other gate).
66 #[error("forbidden")]
67 Forbidden,
68
69 /// The DID's method (e.g. `did:foo:...`) is not in the backend's
70 /// allowlist. Distinct from `Forbidden` so audit logs can
71 /// distinguish "wrong method" from "not in ACL". Surfaced to
72 /// callers as a generic 403 to avoid leaking the allowlist
73 /// contents.
74 #[error("did method rejected")]
75 DidMethodRejected,
76
77 /// Too many concurrent `ChallengeSent` sessions for this DID;
78 /// the per-DID rate limit (default 10) is exhausted. Returned
79 /// as 429 Too Many Requests so clients can back off.
80 #[error("too many pending challenges")]
81 PendingChallengeLimitReached,
82
83 /// The session referenced by the request does not exist or has
84 /// expired (TTL swept it). Returned as 401 Unauthorized; the
85 /// holder must restart the challenge flow.
86 #[error("session not found")]
87 SessionNotFound,
88
89 /// The session exists but was already authenticated (replay) or
90 /// is otherwise not in the state the request expected. Returned
91 /// as 401 Unauthorized; the holder must restart.
92 #[error("session replay or state mismatch")]
93 SessionStateMismatch,
94
95 /// The presented challenge does not match what was issued for
96 /// this session. Constant-time compared. Returned as 401
97 /// Unauthorized.
98 #[error("challenge mismatch")]
99 ChallengeMismatch,
100
101 /// The challenge is older than the backend's configured TTL.
102 /// Returned as 401 Unauthorized; the holder must request a
103 /// fresh challenge.
104 #[error("challenge expired")]
105 ChallengeExpired,
106
107 /// The signer DID extracted from the transport (DIDComm `from`,
108 /// SIOPv2 `iss`) does not match the DID the session was issued
109 /// to. Critical binding — without this check, any leaked
110 /// challenge could be redeemed by any signer. Returned as 401
111 /// Unauthorized.
112 #[error("signer DID does not match session DID")]
113 SignerMismatch,
114
115 /// The DIDComm envelope's `created_time` is outside the freshness
116 /// window. Replay defense for the DIDComm transport. Returned as
117 /// 401 Unauthorized.
118 #[error("message created_time outside freshness window")]
119 StaleMessage,
120
121 /// The refresh token was not found or already consumed. Atomic
122 /// claim semantics: at most one caller succeeds per token.
123 /// Returned as 401 Unauthorized; the holder must re-authenticate.
124 #[error("refresh token not found or consumed")]
125 RefreshTokenInvalid,
126
127 /// The refresh token's absolute expiry has passed. Returned as
128 /// 401 Unauthorized; the holder must re-authenticate.
129 #[error("refresh token expired")]
130 RefreshTokenExpired,
131
132 /// TEE attestation failed in a `TeeMode::Required` deployment.
133 /// Returned as 503 Service Unavailable (the operator's TEE is
134 /// broken; the caller did nothing wrong).
135 #[error("tee attestation failed: {0}")]
136 AttestationFailed(String),
137
138 /// Surface for any wrapped error from the backend's policy or
139 /// storage layer that doesn't fit the variants above. The
140 /// canonical handler does not introspect this; it surfaces
141 /// unchanged via the backend's `Error::from(AuthError::Internal)`.
142 #[error("internal: {0}")]
143 Internal(String),
144}
145
146// ---------------------------------------------------------------------------
147// SessionStore — the storage primitives the canonical handlers need
148// ---------------------------------------------------------------------------
149
150/// Storage operations the canonical handlers invoke.
151///
152/// Each backend wraps its own keyspace handle (vti-common's
153/// `KeyspaceHandle` enum, did-hosting's `KeyspaceHandle` struct,
154/// future cloud-store backends) in an adapter implementing this
155/// trait. The handler holds a `&S` and never touches the concrete
156/// storage type directly.
157///
158/// ## Why this trait, not a single `KeyspaceHandle` type
159///
160/// did-hosting and vti-common evolved separate keyspace
161/// abstractions before the auth-architecture consolidation. Merging
162/// them is out of scope for the auth work; the trait boundary
163/// keeps them independent while still sharing the auth-flow code.
164#[async_trait]
165pub trait SessionStore: Send + Sync + 'static {
166 /// Wrapped error type. Conversion to `AuthError::Internal` is
167 /// the handler's responsibility (via `?` and the backend's
168 /// `From<AuthError>` impl).
169 type Error: Debug + Send + Sync + 'static;
170
171 /// Persist a session under its `session_id`.
172 async fn store_session(&self, session: &Session) -> Result<(), Self::Error>;
173
174 /// Load a session by `session_id`. `Ok(None)` if missing or expired-and-swept.
175 async fn get_session(&self, session_id: &str) -> Result<Option<Session>, Self::Error>;
176
177 /// Delete a session and its refresh-token reverse-index.
178 async fn delete_session(&self, session_id: &str) -> Result<(), Self::Error>;
179
180 /// Persist the `refresh_token → session_id` reverse-index.
181 /// Implementors choose whether to hash the key (recommended;
182 /// vti-common does, did-hosting historically does not) — the
183 /// handler treats the token as an opaque bearer.
184 async fn store_refresh_index(
185 &self,
186 refresh_token: &str,
187 session_id: &str,
188 ) -> Result<(), Self::Error>;
189
190 /// Atomically claim-and-delete the `refresh_token → session_id`
191 /// reverse-index. Cross-replica safe (Redis GETDEL / DynamoDB
192 /// DeleteItem ReturnValues=ALL_OLD / fjall mutex). Exactly one
193 /// concurrent caller observes `Some` for any given token.
194 /// Used by `/auth/refresh` to close the rotation TOCTOU.
195 async fn take_session_id_by_refresh(
196 &self,
197 refresh_token: &str,
198 ) -> Result<Option<String>, Self::Error>;
199
200 /// Count `ChallengeSent` sessions for `did`. Backends with an
201 /// O(1) per-DID tracker (did-hosting) override the default
202 /// O(N) prefix-scan implementation by re-implementing this
203 /// method.
204 ///
205 /// Default implementation provided for backends that haven't
206 /// yet built a tracker — correct but slow under load. Override
207 /// before relying on per-DID rate limiting in production.
208 async fn count_pending_challenges(&self, did: &str) -> Result<usize, Self::Error>;
209}
210
211// ---------------------------------------------------------------------------
212// AuthBackend — per-service policy + glue
213// ---------------------------------------------------------------------------
214
215/// Pluggable backend for the canonical `/auth/*` handlers.
216///
217/// One implementation per service. Most methods have safe defaults;
218/// implementors override only the policy hooks their service
219/// actually exercises (TEE attestation, DID-method allowlist, etc.).
220#[async_trait]
221pub trait AuthBackend: Send + Sync + 'static {
222 /// Session storage adapter.
223 type Store: SessionStore;
224
225 /// Backend-local error type. Must convert from [`AuthError`]
226 /// so the canonical handler can raise auth-specific failures.
227 /// Must implement `IntoResponse` at the route boundary; the
228 /// trait does not bound that here (would force an axum
229 /// dependency on every backend), but the canonical handler
230 /// surfaces the error verbatim and the route layer renders
231 /// it via its existing path.
232 type Error: From<AuthError> + Debug + Send + Sync + 'static;
233
234 /// Backend's role type. The handler holds it opaquely between
235 /// ACL lookup and JWT minting.
236 ///
237 /// - `Display` so the handler can render it into the JWT
238 /// `role` claim (which is a plain string per the canonical
239 /// spec).
240 /// - `Serialize` so the audit hook can include it in
241 /// structured logs.
242 type Role: std::fmt::Display + Serialize + Clone + Send + Sync + 'static;
243
244 // -------- Plumbing --------
245
246 /// Session store handle. The handler invokes the
247 /// [`SessionStore`] methods through this.
248 fn sessions(&self) -> &Self::Store;
249
250 /// Mint an access token JWT for an authenticated session.
251 ///
252 /// The trait abstracts over the concrete JWT minter — VTA + VTC
253 /// use `vti_common::auth::jwt::JwtKeys`; did-hosting has its own
254 /// minter type with the same shape but a separate `AppError`
255 /// surface. Each backend implements this method using whatever
256 /// minter it holds; the canonical handler treats the return
257 /// value as an opaque base64url-encoded JWS.
258 async fn mint_access_token(
259 &self,
260 subject: &str,
261 session_id: &str,
262 role: &Self::Role,
263 contexts: &[String],
264 amr: &[String],
265 acr: &str,
266 tee_attested: bool,
267 ttl_secs: u64,
268 ) -> Result<String, Self::Error>;
269
270 // -------- Policy hooks --------
271
272 /// Resolve a DID to a role + context scope. Returning an error
273 /// (typically [`AuthError::Forbidden`]) rejects the request
274 /// before any other gate fires.
275 async fn check_acl(&self, did: &str) -> Result<RoleResolution<Self::Role>, Self::Error>;
276
277 /// Optional DID-method validation gate. Default: accept any
278 /// method (backends with no allowlist). VTA overrides in TEE
279 /// mode to enforce `allowed_did_methods`.
280 async fn validate_did(&self, _did: &str) -> Result<(), Self::Error> {
281 Ok(())
282 }
283
284 /// Optional TEE attestation hook. Returns the attestation
285 /// report (if produced) and whether attestation succeeded.
286 /// Default: no attestation (None, false). VTA overrides in
287 /// TEE mode; in `TeeMode::Required` a failure here must be
288 /// raised as [`AuthError::AttestationFailed`].
289 async fn attest_challenge(
290 &self,
291 _challenge_bytes: &[u8; 32],
292 ) -> Result<AttestationOutcome, Self::Error> {
293 Ok(AttestationOutcome::not_attested())
294 }
295
296 /// Cap on concurrent `ChallengeSent` sessions per DID. Default
297 /// 10. Setting to 0 disables per-DID rate limiting (still
298 /// IP-rate-limited at the tower-governor layer). Backends with
299 /// low-trust callers may want this higher; backends with
300 /// admin-only callers can keep it at 10.
301 fn max_pending_challenges_per_did(&self) -> usize {
302 10
303 }
304
305 /// Audit hook fired at the end of each handler. Default impl
306 /// emits via `tracing::info!(audit=true)`; backends with
307 /// structured audit pipelines (e.g. VTC's audit log with HMAC
308 /// actor hashing) can override.
309 fn audit(&self, event: AuthAuditEvent<'_>) {
310 match event {
311 AuthAuditEvent::ChallengeIssued { did, session_id } => {
312 tracing::info!(audit = true, %did, %session_id, "auth challenge issued");
313 }
314 AuthAuditEvent::Authenticated {
315 did, session_id, ..
316 } => {
317 tracing::info!(audit = true, %did, %session_id, "auth successful");
318 }
319 AuthAuditEvent::Refreshed {
320 did,
321 old_session_id,
322 new_session_id,
323 ..
324 } => {
325 tracing::info!(
326 audit = true,
327 %did,
328 %old_session_id,
329 %new_session_id,
330 "token refreshed",
331 );
332 }
333 }
334 }
335
336 // -------- Timings --------
337
338 /// Challenge TTL in seconds. Typical: 60.
339 fn challenge_ttl(&self) -> u64;
340
341 /// Access-token TTL in seconds. Typical: 900 (15 min).
342 fn access_token_ttl(&self) -> u64;
343
344 /// Access-token TTL in seconds for a stepped-up
345 /// (`acr=aal2`) session. Default: 1/3 of [`Self::access_token_ttl`]
346 /// floored to a minimum of 60 seconds — closes M2 from the
347 /// May 2026 security review, which observed that a leaked
348 /// `aal2` token has the same 15-minute window as a `aal1`
349 /// token despite the elevated privileges it grants.
350 ///
351 /// Backends can override to set their own ratio or to
352 /// disable the elevation (return `access_token_ttl()` for a
353 /// uniform TTL).
354 fn access_token_ttl_for_aal2(&self) -> u64 {
355 let base = self.access_token_ttl();
356 std::cmp::max(60, base / 3)
357 }
358
359 /// Refresh-token TTL in seconds. Typical: 86400 (24 h).
360 fn refresh_token_ttl(&self) -> u64;
361
362 /// DIDComm `created_time` freshness window in seconds. The
363 /// canonical handler rejects messages older than this against
364 /// `session.created_at` to bound replay risk. Default 60s.
365 fn didcomm_freshness_window(&self) -> u64 {
366 60
367 }
368}
369
370// ---------------------------------------------------------------------------
371// Supporting types
372// ---------------------------------------------------------------------------
373
374/// Result of an ACL lookup. The handler propagates this opaquely
375/// into the JWT minter and audit event.
376#[derive(Debug, Clone)]
377pub struct RoleResolution<R> {
378 pub role: R,
379 /// Context-scoped backends (VTA) populate this. Backends with
380 /// flat ACL (did-hosting) leave it empty.
381 pub contexts: Vec<String>,
382}
383
384impl<R> RoleResolution<R> {
385 pub fn new(role: R) -> Self {
386 Self {
387 role,
388 contexts: Vec::new(),
389 }
390 }
391
392 pub fn with_contexts(role: R, contexts: Vec<String>) -> Self {
393 Self { role, contexts }
394 }
395}
396
397/// Outcome of the optional TEE attestation hook.
398#[derive(Debug, Clone)]
399pub struct AttestationOutcome {
400 /// JSON-serialised attestation report, if produced. Echoed
401 /// back to the client in the challenge response under
402 /// `tee_attestation`. `None` for backends with no TEE.
403 pub report: Option<serde_json::Value>,
404 /// Whether attestation succeeded for *this* challenge. The
405 /// JWT's `tee_attested` claim is sourced from this bit; a TEE
406 /// binary in `TeeMode::Optional` that fails attestation must
407 /// set this to `false`.
408 pub attested: bool,
409}
410
411impl AttestationOutcome {
412 pub fn not_attested() -> Self {
413 Self {
414 report: None,
415 attested: false,
416 }
417 }
418
419 pub fn attested(report: serde_json::Value) -> Self {
420 Self {
421 report: Some(report),
422 attested: true,
423 }
424 }
425}
426
427/// Events the canonical handlers emit to the backend's audit
428/// sink. The default `AuthBackend::audit` impl forwards each
429/// variant to `tracing::info!(audit=true)` so backends without
430/// a structured audit log get useful output for free.
431#[derive(Debug)]
432pub enum AuthAuditEvent<'a> {
433 /// Fired after a successful `/auth/challenge`. The session
434 /// is in `ChallengeSent` state.
435 ChallengeIssued { did: &'a str, session_id: &'a str },
436 /// Fired after a successful `/auth/authenticate`. The
437 /// session is now in `Authenticated` state with `amr`/`acr`
438 /// populated.
439 Authenticated {
440 did: &'a str,
441 session_id: &'a str,
442 amr: &'a [String],
443 acr: &'a str,
444 },
445 /// Fired after a successful `/auth/refresh`. The old
446 /// session has been deleted; the new one is `Authenticated`
447 /// at the *preserved* `amr`/`acr` from the old session.
448 Refreshed {
449 did: &'a str,
450 old_session_id: &'a str,
451 new_session_id: &'a str,
452 amr: &'a [String],
453 acr: &'a str,
454 },
455}
456
457// ---------------------------------------------------------------------------
458// Pre-extracted inputs the canonical handlers take
459// ---------------------------------------------------------------------------
460
461/// Inputs to `/auth/challenge`.
462#[derive(Debug, Clone)]
463pub struct ChallengeInput {
464 /// Caller's DID. ACL-gated.
465 pub did: String,
466 /// Optional ephemeral session pubkey (Ed25519 multikey
467 /// base58btc with `z` prefix) for Data-Integrity-proof
468 /// binding on subsequent trust-task envelopes. `None` for
469 /// callers that sign with their DID's own key.
470 pub session_pubkey_b58btc: Option<String>,
471}
472
473/// Inputs to `/auth/authenticate` after the transport layer has
474/// verified the signer.
475///
476/// The transport layer (DIDComm `unpack_signed`, SIOPv2 JWS
477/// verification, etc.) extracts the signer and produces this
478/// struct; the canonical handler then validates against the
479/// session and mints tokens.
480#[derive(Debug, Clone)]
481pub struct AuthenticateInput {
482 pub session_id: String,
483 pub challenge: String,
484 /// Verified signer DID. The transport layer must produce
485 /// this from a cryptographic check (DIDComm authcrypt,
486 /// JWS signature, etc.) — *never* echo it from the request
487 /// body unchecked.
488 pub signer_did: String,
489 /// Optional message `created_time` for DIDComm freshness
490 /// checking. `None` for REST transports.
491 pub created_time: Option<u64>,
492 /// Optional ephemeral session pubkey to register against
493 /// this session at the auth transition. SIOPv2 callers
494 /// (did-hosting-control) carry one to support
495 /// Data-Integrity-proof binding on subsequent
496 /// trust-task envelopes; DIDComm transports normally leave
497 /// this `None`. The route layer is responsible for any
498 /// shape-validation (e.g. `z6Mk…` Ed25519 multikey prefix)
499 /// before passing it in.
500 pub session_pubkey_b58btc: Option<String>,
501}
502
503/// Inputs to `/auth/refresh` after the transport layer has
504/// verified the signer.
505#[derive(Debug, Clone)]
506pub struct RefreshInput {
507 pub refresh_token: String,
508 /// Verified signer DID (DIDComm transports). REST transports
509 /// can leave this `None`; the canonical handler treats `None`
510 /// as "skip signer-DID-matches-session-DID check" — only safe
511 /// when the transport offers no signer assertion (i.e. plain
512 /// REST refresh, where the token itself is the only credential).
513 pub signer_did: Option<String>,
514}