udb 0.3.7

Universal Data Broker — a Rust gRPC broker over multiple databases (Postgres, MySQL, SQLite, MongoDB, ClickHouse, Cassandra, MSSQL, Redis, Qdrant, S3, Neo4j, …) with per-tenant RLS, 2PC, sagas, and CDC.
Documentation
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
//! Server-side sessions: creation/refresh/revocation, listing, logout, CSRF
//! validation, and token (session / api-key / JWT) validation.

use super::*;

fn validate_session_response(
    rec: Option<SessionRecord>,
    now_unix: u64,
) -> authn_pb::ValidateTokenResponse {
    let Some(rec) = rec else {
        return authn_pb::ValidateTokenResponse {
            valid: false,
            ..Default::default()
        };
    };
    let principal = principal_from_session(&rec);
    authn_pb::ValidateTokenResponse {
        valid: true,
        user_id: rec.user_id.clone(),
        session_id: String::new(),
        account_kind: authn_entity_pb::AccountKind::Unspecified as i32,
        tenant_id: rec.tenant_id.clone(),
        roles: rec.roles.clone(),
        expires_at: timestamp_from_unix(rec.expires_at_unix),
        access_surface: "session".to_string(),
        device_id: rec.client_fingerprint.clone(),
        token_id: public_session_handle_from_hash(&rec.session_id_hash),
        session_type: authn_entity_pb::SessionType::ServerSide as i32,
        principal: Some(authn_principal_to_pb(
            &principal,
            rec.expires_at_unix as i64,
        )),
        project_id: rec.project_id.clone(),
        scopes: rec.scopes.clone(),
        attributes: [
            ("active".to_string(), rec.is_active(now_unix).to_string()),
            (
                "relationship_version".to_string(),
                rec.relationship_version.clone(),
            ),
        ]
        .into_iter()
        .collect(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn session_record() -> SessionRecord {
        SessionRecord {
            session_id_hash: "hmac-sha256:0123456789abcdef".to_string(),
            principal_id: "user-1".to_string(),
            user_id: "user-1".to_string(),
            service_identity: String::new(),
            tenant_id: "acme".to_string(),
            project_id: "billing".to_string(),
            scopes: vec!["data:read".to_string()],
            roles: vec!["reader".to_string()],
            relationship_version: "rv1".to_string(),
            created_at_unix: 10,
            updated_at_unix: 20,
            expires_at_unix: 300,
            revoked_at_unix: 0,
            client_fingerprint: "device".to_string(),
        }
    }

    #[test]
    fn validate_session_response_does_not_echo_raw_session_id_or_hash() {
        let rec = session_record();
        let response = validate_session_response(Some(rec.clone()), 20);

        assert!(response.valid);
        assert!(response.session_id.is_empty());
        assert!(response.token_id.starts_with("sesspub_"));
        assert_ne!(response.token_id, rec.session_id_hash);
        assert!(!response.token_id.contains("hmac-sha256"));
    }

    #[tokio::test]
    async fn list_sessions_denies_tenantless_non_admin_before_store_access() {
        let svc = AuthnServiceImpl::new(
            authn::AuthnConfig::default(),
            crate::runtime::security::SecurityConfig::default(),
        );
        let ctx = crate::runtime::service::method_security::test_claim_context(
            "reader-a",
            "",
            "",
            &["udb:authn:read"],
            &[],
        );
        let req = Request::new(authn_pb::ListSessionsRequest {
            user_id: "target-user".to_string(),
            active_only: true,
            page: None,
        });
        let err = crate::runtime::service::method_security::scope_claim_context_for_test(
            ctx,
            svc.list_sessions_impl(req),
        )
        .await
        .expect_err("tenantless non-admin must be denied before session listing");
        assert_eq!(err.code(), tonic::Code::PermissionDenied);
    }
}

fn validate_api_key_response(rec: Option<authn::ApiKeyRecord>) -> authn_pb::ValidateTokenResponse {
    let Some(rec) = rec else {
        return authn_pb::ValidateTokenResponse {
            valid: false,
            ..Default::default()
        };
    };
    let principal = principal_from_api_key(&rec);
    authn_pb::ValidateTokenResponse {
        valid: true,
        user_id: String::new(),
        session_id: String::new(),
        account_kind: authn_entity_pb::AccountKind::ServiceAccount as i32,
        tenant_id: rec.tenant_id.clone(),
        roles: Vec::new(),
        expires_at: timestamp_from_unix(rec.expires_at_unix),
        access_surface: "api_key".to_string(),
        device_id: String::new(),
        token_id: rec.key_prefix.clone(),
        session_type: authn_entity_pb::SessionType::ApiKey as i32,
        principal: Some(authn_principal_to_pb(
            &principal,
            rec.expires_at_unix as i64,
        )),
        project_id: rec.project_id.clone(),
        scopes: rec.scopes.clone(),
        attributes: Default::default(),
    }
}

impl AuthnServiceImpl {
    async fn authorize_list_sessions_target_user(&self, user_id: &str) -> Result<(), Status> {
        if !crate::runtime::service::method_security::claim_context_present() {
            return Ok(());
        }
        let ctx = crate::runtime::service::method_security::current_claim_context();
        if ctx.is_cross_tenant_admin() {
            return Ok(());
        }
        if !ctx.subject.trim().is_empty() && ctx.subject.trim() == user_id.trim() {
            return Ok(());
        }
        if ctx.tenant_id.trim().is_empty() {
            tracing::warn!(
                target: "udb.audit.authz",
                subject = %ctx.subject,
                target_user = %user_id,
                "DENY: list_sessions requires a tenant-bound bearer or cross-tenant admin"
            );
            return Err(Status::permission_denied(
                "operation requires a tenant-scoped bearer token or a cross-tenant admin role",
            ));
        }
        let target = self
            .users
            .get_user_by_id(user_id)
            .await
            .map_err(Status::internal)?
            .ok_or_else(|| Status::not_found("user not found"))?;
        if target.tenant_id.trim().is_empty() {
            tracing::warn!(
                target: "udb.audit.authz",
                subject = %ctx.subject,
                target_user = %user_id,
                "DENY: list_sessions target user has no tenant boundary"
            );
            return Err(Status::permission_denied(
                "target user must belong to the bearer token tenant",
            ));
        }
        crate::runtime::service::method_security::enforce_body_tenant_matches_claim(
            &ctx,
            &target.tenant_id,
            &target.project_id,
        )
    }

    /// Create a server-side login session for `user`, returning the raw session
    /// id (the refresh credential) and its absolute expiry.
    pub(super) async fn create_login_session(
        &self,
        user: &UserRecord,
        client_fingerprint: String,
        scopes: Vec<String>,
        roles: Vec<String>,
        now: u64,
    ) -> Result<(String, u64), Status> {
        if !self.config.sessions_usable() {
            return Err(Status::failed_precondition(
                "sessions disabled (set UDB_SESSION_ENABLED and UDB_SESSION_HASH_SECRET)",
            ));
        }
        let raw_session_id = format!("sess_{}", Uuid::new_v4().simple());
        let expires = now.saturating_add(self.config.session_ttl_secs);
        let rec = SessionRecord {
            session_id_hash: authn::hash_secret(&raw_session_id, &self.hash_key()),
            principal_id: user.user_id.clone(),
            user_id: user.user_id.clone(),
            service_identity: String::new(),
            tenant_id: user.tenant_id.clone(),
            project_id: user.project_id.clone(),
            // Block 1 (auth_fix.md, Decision E): the SessionRecord is the carrier —
            // seed the resolved role-derived grants here so the legacy session
            // refresh path (`:443`) and any consumer reads the real grants, not
            // an empty set. The caller resolves once and threads them in.
            scopes,
            roles,
            relationship_version: String::new(),
            created_at_unix: now,
            updated_at_unix: now,
            expires_at_unix: expires,
            revoked_at_unix: 0,
            client_fingerprint,
        };
        self.sessions.put(&rec).await.map_err(Status::internal)?;
        Ok((raw_session_id, expires))
    }

    pub(super) async fn create_session_impl(
        &self,
        request: Request<authn_pb::CreateSessionRequest>,
    ) -> Result<Response<authn_pb::CreateSessionResponse>, Status> {
        if !self.config.sessions_usable() {
            return Err(Status::failed_precondition(
                "sessions disabled (set UDB_SESSION_ENABLED and UDB_SESSION_HASH_SECRET)",
            ));
        }
        let req = request.into_inner();
        let p = req
            .principal
            .ok_or_else(|| Status::invalid_argument("principal is required"))?;
        let now = now_unix();
        let ttl = if req.ttl_seconds > 0 {
            req.ttl_seconds as u64
        } else {
            self.config.session_ttl_secs
        };
        let expires = now.saturating_add(ttl);
        let raw_session_id = format!("sess_{}", Uuid::new_v4().simple());
        let rec = SessionRecord {
            session_id_hash: authn::hash_secret(&raw_session_id, &self.hash_key()),
            principal_id: p.principal_id,
            user_id: p.user_id,
            service_identity: p.service_identity,
            tenant_id: p.tenant_id,
            project_id: p.project_id,
            scopes: p.scopes,
            roles: p.roles,
            relationship_version: String::new(),
            created_at_unix: now,
            updated_at_unix: now,
            expires_at_unix: expires,
            revoked_at_unix: 0,
            client_fingerprint: req.client_fingerprint,
        };
        self.sessions.put(&rec).await.map_err(Status::internal)?;
        Ok(Response::new(authn_pb::CreateSessionResponse {
            session_id: raw_session_id,
            expires_at_unix: expires as i64,
        }))
    }

    pub(super) async fn refresh_session_impl(
        &self,
        request: Request<authn_pb::RefreshSessionRequest>,
    ) -> Result<Response<authn_pb::RefreshSessionResponse>, Status> {
        let req = request.into_inner();
        let now = now_unix();
        let ttl = if req.ttl_seconds > 0 {
            req.ttl_seconds as u64
        } else {
            self.config.session_ttl_secs
        };
        match authn::refresh_session(
            self.sessions.as_ref(),
            &req.session_id,
            &self.hash_key(),
            now,
            ttl,
        )
        .await
        .map_err(Status::internal)?
        {
            Some(rec) => {
                // Phase L2: emit a session-refresh event (no raw token material —
                // a public handle derived from the session-id hash is used).
                let public_session_id = public_session_handle_from_hash(&authn::hash_secret(
                    &req.session_id,
                    &self.hash_key(),
                ));
                self.emit_event(
                    AuthEvent::new(
                        topics::SESSION_REFRESHED,
                        public_session_id.clone(),
                        rec.tenant_id.clone(),
                        serde_json::json!({
                            "session_public_id": public_session_id,
                            "user_id": rec.user_id.clone(),
                            "tenant_id": rec.tenant_id.clone(),
                            "project_id": rec.project_id.clone(),
                        }),
                    )
                    .with_correlation(format!("session-refresh:{}", rec.user_id)),
                )
                .await;
                Ok(Response::new(authn_pb::RefreshSessionResponse {
                    expires_at_unix: rec.expires_at_unix as i64,
                    active: true,
                }))
            }
            // A revoked / expired / unknown session cannot be refreshed. Return an
            // ERROR (not `Ok { active: false }`): a logged-out session must FAIL to
            // refresh, and `authn::refresh_session` already returned `None` without
            // extending it. Returning a success envelope let a caller treat the RPC
            // as "session still works" after logout (bug_report #3).
            None => Err(Status::unauthenticated(
                "session is not active (revoked, expired, or unknown)",
            )),
        }
    }

    pub(super) async fn revoke_session_impl(
        &self,
        request: Request<authn_pb::RevokeSessionRequest>,
    ) -> Result<Response<authn_pb::RevokeSessionResponse>, Status> {
        let req = request.into_inner();
        let now = now_unix();
        if req.all_for_principal && !req.principal_id.trim().is_empty() {
            let propagation_started = std::time::Instant::now();
            let n = self
                .sessions
                .revoke_all_for_principal(&req.principal_id, now)
                .await
                .map_err(Status::internal)?;
            self.metrics.observe_revocation_propagation_seconds(
                propagation_started.elapsed().as_secs_f64(),
            );
            return Ok(Response::new(authn_pb::RevokeSessionResponse {
                session_id: String::new(),
                revoked_at: None,
                operation_id: Uuid::new_v4().to_string(),
                revoked_count: n as i32,
            }));
        }
        let hash = authn::hash_secret(&req.session_id, &self.hash_key());
        let public_session_id = public_session_handle_from_hash(&hash);
        let ok = self
            .sessions
            .revoke(&hash, now)
            .await
            .map_err(Status::internal)?;
        if ok {
            // I2.3 — push the revoked session handle (which is the JWT `jti` for any
            // access token issued against this session) onto the durable cluster-wide
            // revocation deny list, so a still-unexpired JWT for this session stops
            // verifying on EVERY node via the fast denylist before its natural expiry
            // (the per-validate session lookup in `jwt_persisted_state_valid` already
            // covers it durably; this is the accelerator). Best-effort + tenant from
            // the validated claim. `0` expiry → the denylist's default TTL.
            let claim_tenant =
                crate::runtime::service::method_security::current_claim_context().tenant_id;
            let reason = if req.revoke_reason.trim().is_empty() {
                "session_revoked"
            } else {
                req.revoke_reason.trim()
            };
            self.revoke_token_jti(
                &req.session_id,
                "session",
                &claim_tenant,
                0,
                &req.principal_id,
                reason,
            )
            .await?;
            self.emit_event(AuthEvent::new(
                topics::SESSION_REVOKED,
                public_session_id.clone(),
                String::new(),
                serde_json::json!({
                    "session_public_id": public_session_id.clone(),
                    "revoke_reason": req.revoke_reason.clone(),
                    "revoked_by": req.principal_id.clone(),
                }),
            ))
            .await;
        }
        Ok(Response::new(authn_pb::RevokeSessionResponse {
            session_id: if ok { public_session_id } else { String::new() },
            revoked_at: None,
            operation_id: Uuid::new_v4().to_string(),
            revoked_count: i32::from(ok),
        }))
    }

    pub(super) async fn refresh_token_impl(
        &self,
        request: Request<authn_pb::RefreshTokenRequest>,
    ) -> Result<Response<authn_pb::RefreshTokenResponse>, Status> {
        let req = request.into_inner();
        let now = now_unix();
        // Phase 3 (I2.2): the primary refresh credential is a token-family token
        // (rt_<family>.<jti>). Rotate it atomically — reuse of a superseded value
        // revokes the whole family and emits a high-severity audit event.
        let presented = if !req.refresh_token.trim().is_empty() {
            req.refresh_token.clone()
        } else {
            req.session_id.clone()
        };
        if let Some(parts) = authn::token_family::parse_refresh_token(presented.trim()) {
            return self
                .refresh_with_family(&parts.family_id, &parts.jti, now)
                .await
                .map(Response::new);
        }
        // Legacy fallback: a server-side session id was presented as the refresh
        // credential (back-compat for session-only callers that predate the
        // token-family flow). No rotation token is returned in this path.
        let session_ref = presented;
        if session_ref.trim().is_empty() {
            return Err(Status::invalid_argument(
                "refresh_token or session_id is required",
            ));
        }
        // Sliding refresh: extend the backing session, then mint a fresh
        // short-lived access token bound to it.
        let Some(rec) = authn::refresh_session(
            self.sessions.as_ref(),
            &session_ref,
            &self.hash_key(),
            now,
            self.config.session_ttl_secs,
        )
        .await
        .map_err(Status::internal)?
        else {
            return Err(Status::unauthenticated("invalid credential"));
        };
        // Phase 3: a suspended/deactivated user must not keep minting access
        // tokens via a lingering session — gate refresh on live user status.
        if !self.user_is_active(&rec.user_id).await? {
            return Err(Status::permission_denied("user is not active"));
        }
        // Block 1 (auth_fix.md, Decision E): re-resolve on refresh so a role
        // revoked since login stops minting `udb:admin`. The `service_identity`
        // discriminator (NOT emptiness) decides the source: a service-principal
        // session carries scopes asserted at `CreateSession` that are not
        // role-derived, so keep its stored set; a user session takes the freshly
        // resolved grants verbatim (empty ⇒ empty drops admin on last-role
        // revocation, which a "fall back when empty" rule would mask).
        let (scopes, roles) = if rec.service_identity.trim().is_empty() {
            self.resolve_effective_grants(&rec.user_id, &rec.tenant_id, &rec.project_id)
        } else {
            (rec.scopes.clone(), rec.roles.clone())
        };
        let (access_token, access_exp) = self.issue_access_token(
            &rec.user_id,
            &rec.tenant_id,
            &rec.project_id,
            &scopes,
            &roles,
            &rec.service_identity,
            &session_ref,
            "refresh",
            now,
        );
        let access_token_expires_in = if access_exp > 0 {
            (access_exp - now as i64).max(0) as i32
        } else {
            self.config.session_ttl_secs as i32
        };
        Ok(Response::new(authn_pb::RefreshTokenResponse {
            access_token,
            access_token_expires_in,
            // Legacy session-id refresh path does not rotate a family token.
            refresh_token: String::new(),
            refresh_token_expires_in: 0,
        }))
    }

    /// Token-family refresh (I2.2): atomically rotate the family, mint a fresh
    /// access token + the next refresh token, and fail closed (revoking the
    /// family) on reuse of a superseded refresh token.
    async fn refresh_with_family(
        &self,
        family_id: &str,
        jti: &str,
        now: u64,
    ) -> Result<authn_pb::RefreshTokenResponse, Status> {
        use super::token_family::RotateOutcome;
        match self.rotate_refresh_family(family_id, jti, now).await? {
            RotateOutcome::Rotated {
                new_refresh_token,
                family,
            } => {
                // A suspended/deactivated user must not keep minting tokens via a
                // lingering refresh family — gate on live user status.
                if !self.user_is_active(&family.user_id).await? {
                    return Err(Status::permission_denied("user is not active"));
                }
                // Block 1 (auth_fix.md, Decision E): the token family stores no
                // grants (`TokenFamilyRow` has no scopes/roles columns), so
                // re-resolve from the warm snapshot — the `user_is_active` check
                // above already touched this user, so the read is free.
                let (scopes, roles) = self.resolve_effective_grants(
                    &family.user_id,
                    &family.tenant_id,
                    &family.project_id,
                );
                let (access_token, access_exp) = self.issue_access_token(
                    &family.user_id,
                    &family.tenant_id,
                    &family.project_id,
                    &scopes,
                    &roles,
                    "",
                    &format!("rtf_{family_id}"),
                    "refresh",
                    now,
                );
                let access_token_expires_in = if access_exp > 0 {
                    (access_exp - now as i64).max(0) as i32
                } else {
                    self.config.session_ttl_secs as i32
                };
                Ok(authn_pb::RefreshTokenResponse {
                    access_token,
                    access_token_expires_in,
                    refresh_token: new_refresh_token,
                    refresh_token_expires_in: self.config.session_ttl_secs as i32,
                })
            }
            // Reuse of a rotated-away token: the family is now revoked + audited
            // inside rotate_refresh_family. Fail closed.
            RotateOutcome::Reuse => Err(Status::unauthenticated("invalid credential")),
            RotateOutcome::NotFound => Err(Status::unauthenticated("invalid credential")),
        }
    }

    pub(super) async fn logout_impl(
        &self,
        request: Request<authn_pb::LogoutRequest>,
    ) -> Result<Response<authn_pb::LogoutResponse>, Status> {
        let req = request.into_inner();
        let now = now_unix();
        let count = if req.all_sessions {
            let propagation_started = std::time::Instant::now();
            let principal_id = req
                .context
                .as_ref()
                .map(|ctx| ctx.principal_id.clone())
                .unwrap_or_default();
            if principal_id.trim().is_empty() {
                return Err(Status::invalid_argument(
                    "context.principal_id is required for all_sessions logout",
                ));
            }
            // Kill every refresh-token family for the principal too — otherwise a
            // refresh token survives the all-sessions logout and keeps minting
            // access tokens.
            self.revoke_families_for_principal(&principal_id).await?;
            self.sessions
                .revoke_all_for_principal(&principal_id, now)
                .await
                .map_err(Status::internal)
                .map(|count| {
                    self.metrics.observe_revocation_propagation_seconds(
                        propagation_started.elapsed().as_secs_f64(),
                    );
                    count as i32
                })?
        } else {
            let hash = authn::hash_secret(&req.session_id, &self.hash_key());
            let revoked = self
                .sessions
                .revoke(&hash, now)
                .await
                .map_err(Status::internal)?;
            if revoked {
                // I2.3 — denylist the session handle (== JWT `jti`) so any still-valid
                // access token for this session is rejected cluster-wide on logout,
                // before its natural expiry. Best-effort accelerator over the durable
                // session lookup. Tenant comes from the validated claim.
                let claim_tenant =
                    crate::runtime::service::method_security::current_claim_context().tenant_id;
                self.revoke_token_jti(&req.session_id, "session", &claim_tenant, 0, "", "logout")
                    .await?;
                // The refresh-token family bound to this session must die with it,
                // or the logged-out session's refresh token could still mint access
                // tokens via `RefreshToken` (the family path only checks the user is
                // active, not whether the session was revoked).
                self.revoke_families_for_session(&req.session_id).await?;
            }
            i32::from(revoked)
        };
        Ok(Response::new(authn_pb::LogoutResponse {
            sessions_revoked: count,
        }))
    }

    pub(super) async fn validate_token_impl(
        &self,
        request: Request<authn_pb::ValidateTokenRequest>,
    ) -> Result<Response<authn_pb::ValidateTokenResponse>, Status> {
        let req = request.into_inner();
        let now = now_unix();
        let token_type = authn_entity_pb::TokenType::try_from(req.token_type).unwrap_or_default();
        let response = match token_type {
            authn_entity_pb::TokenType::Session => {
                let rec = authn::validate_session(
                    self.sessions.as_ref(),
                    &req.token,
                    &self.hash_key(),
                    now,
                    self.config.session_idle_ttl_secs,
                )
                .await
                .map_err(Status::internal)?;
                validate_session_response(rec, now)
            }
            authn_entity_pb::TokenType::ApiKey => {
                let rec = authn::validate_api_key(
                    self.api_keys.as_ref(),
                    &req.token,
                    &self.api_key_hash_key(),
                    now,
                )
                .await
                .map_err(Status::internal)?;
                validate_api_key_response(rec)
            }
            authn_entity_pb::TokenType::JwtAccess | authn_entity_pb::TokenType::JwtRefresh => {
                let claims = validate_bearer_token(&self.security, &req.token)
                    .map_err(|_| Status::unauthenticated("invalid credential"))?;
                let subject = claims.sub.clone().unwrap_or_default();
                // Phase 3: bind UDB-issued JWT validity to live persisted state
                // (user status + issuing-session revocation). See
                // `jwt_persisted_state_valid`.
                if !self.jwt_persisted_state_valid(&claims, now).await? {
                    self.metrics.record_auth_token_validation_failure();
                    return Ok(Response::new(authn_pb::ValidateTokenResponse {
                        valid: false,
                        ..Default::default()
                    }));
                }
                let principal = Principal {
                    principal_id: subject.clone(),
                    subject: subject.clone(),
                    user_id: subject.clone(),
                    service_identity: claims.service_identity.clone().unwrap_or_default(),
                    tenant_id: claims.tenant_id.clone().unwrap_or_default(),
                    project_id: claims.project_id.clone().unwrap_or_default(),
                    scopes: claims.resolved_scopes(),
                    roles: claims.roles.clone().unwrap_or_default(),
                    provider_id: String::new(),
                    auth_method: authn::AuthnMethod::Jwt.as_str().to_string(),
                };
                authn_pb::ValidateTokenResponse {
                    valid: true,
                    user_id: subject,
                    session_id: String::new(),
                    account_kind: authn_entity_pb::AccountKind::Unspecified as i32,
                    tenant_id: principal.tenant_id.clone(),
                    roles: principal.roles.clone(),
                    expires_at: None,
                    access_surface: "jwt".to_string(),
                    device_id: String::new(),
                    token_id: claims.jti.clone().unwrap_or_default(),
                    session_type: authn_entity_pb::SessionType::Jwt as i32,
                    principal: Some(authn_principal_to_pb(&principal, 0)),
                    project_id: principal.project_id.clone(),
                    scopes: principal.scopes.clone(),
                    attributes: Default::default(),
                }
            }
            _ => {
                return Err(Status::invalid_argument(
                    "supported token_type values are SESSION, API_KEY, JWT_ACCESS, and JWT_REFRESH",
                ));
            }
        };
        Ok(Response::new(response))
    }

    pub(super) async fn get_session_impl(
        &self,
        request: Request<authn_pb::GetSessionRequest>,
    ) -> Result<Response<authn_pb::GetSessionResponse>, Status> {
        let req = request.into_inner();
        let hash = authn::hash_secret(&req.session_id, &self.hash_key());
        let now = now_unix();
        let session = self
            .sessions
            .get(&hash)
            .await
            .map_err(Status::internal)?
            .map(|rec| session_record_to_pb(&rec, now));
        Ok(Response::new(authn_pb::GetSessionResponse { session }))
    }

    pub(super) async fn list_sessions_impl(
        &self,
        request: Request<authn_pb::ListSessionsRequest>,
    ) -> Result<Response<authn_pb::ListSessionsResponse>, Status> {
        let req = request.into_inner();
        if req.user_id.trim().is_empty() {
            return Err(Status::invalid_argument("user_id is required"));
        }
        // D3: bind the target user to the validated bearer claim before reading
        // sessions, mirroring the ListDevices target-user authorization.
        self.authorize_list_sessions_target_user(&req.user_id)
            .await?;
        let now = now_unix();
        let page = req.page.as_ref();
        let (limit, offset, _) = bounded_page_window(page);
        let (sessions, total) = self
            .sessions
            .list_for_principal_page(&req.user_id, req.active_only, now, limit, offset)
            .await
            .map_err(Status::internal)?;
        let sessions = sessions
            .iter()
            .map(|rec| session_record_to_pb(rec, now))
            .collect();
        Ok(Response::new(authn_pb::ListSessionsResponse {
            sessions,
            page: Some(bounded_page_response(total, page)),
        }))
    }

    pub(super) async fn validate_csrf_impl(
        &self,
        request: Request<authn_pb::ValidateCsrfRequest>,
    ) -> Result<Response<authn_pb::ValidateCsrfResponse>, Status> {
        let req = request.into_inner();
        if req.session_id.trim().is_empty() || req.csrf_token.trim().is_empty() {
            return Ok(Response::new(authn_pb::ValidateCsrfResponse {
                valid: false,
            }));
        }
        let now = now_unix();
        // Signed double-submit cookie pattern: the CSRF token is a keyed HMAC
        // bound to the session id (issued at login). Validate the session is
        // live, then constant-time compare the presented token to the expected
        // binding — a forged token can't be produced without the server secret.
        let session_live = authn::validate_session(
            self.sessions.as_ref(),
            &req.session_id,
            &self.hash_key(),
            now,
            self.config.session_idle_ttl_secs,
        )
        .await
        .map_err(Status::internal)?
        .is_some();
        let expected = self.csrf_token_for(&req.session_id);
        let token_ok = authn::constant_time_eq(&expected, &req.csrf_token);
        Ok(Response::new(authn_pb::ValidateCsrfResponse {
            valid: session_live && token_ok,
        }))
    }
}