feat: persistent sessions and OIDC sign-in (auth M2 + M3)

Sessions and users (M2)
- users and sessions tables in SQLite; only a digest of the session ID is stored
- idle 30 min / absolute 8 h deadlines, UPDATE-only touch, per-user revoke by generation
- emergency operator is a users row (issuer local:emergency); its sessions are
  bound to the operator hash fingerprint, so unsetting or rotating it ends them
- CSRF derived from the session ID; GET /api/v1/me

OIDC login (M3)
- openidconnect 4.1: authorization code + PKCE S256, static redirect URI, no redirects
- one-time login transactions bound to the browser by a Lax correlation cookie
- identity is (issuer, subject); admin only from configured subjects/groups;
  a signed-in user without a grant gets 403 on management APIs
- OIDC sessions are bound to the OIDC configuration fingerprint
- SSO button, no-access page and fixed error codes in the web UI
- tests against a mock IdP with signed and malformed tokens; rehearsed against
  Keycloak 26.1 in a real browser
- rsa 0.9 advisory (RUSTSEC-2023-0071) excepted with a written reason: runtime
  code only verifies signatures with public keys

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-14 13:43:09 +07:00
parent a9ebded5ba
commit 618681fa7f
23 changed files with 3695 additions and 139 deletions

View File

@@ -237,7 +237,7 @@ struct ReconstituteStartResponse {
pub(super) struct OperatorActor {
pub(super) name: String,
browser: bool,
principal: Principal,
pub(super) principal: Principal,
request_id: Uuid,
}
@@ -263,16 +263,18 @@ pub(super) fn require_admin_mutation(
authorize_operator(state, peer, headers, true)
}
fn authorize_operator(
pub(super) fn authorize_operator(
state: &HubState,
peer: SocketAddr,
headers: &HeaderMap,
mutation: bool,
) -> Result<OperatorActor, StatusCode> {
let request_id = Uuid::new_v4();
let attempted_actor = state.sessions.active(headers)
.map(|session| Principal::Emergency(session.actor)).unwrap_or(Principal::Anonymous);
let result = authorize_operator_inner(state, peer, headers, mutation, request_id);
// Resolve the session once: each lookup is a DB query and may slide the idle window.
let session = state.sessions.active(headers);
let attempted_actor = session.as_ref()
.map(|session| session.principal()).unwrap_or(Principal::Anonymous);
let result = authorize_operator_inner(state, peer, headers, mutation, request_id, session);
let actor = result.as_ref().map(|actor| &actor.principal).unwrap_or(&attempted_actor);
state.db.audit_event(actor, AuditAction::Authorization, AuditResource::Api,
if result.is_ok() { AuditOutcome::Allowed } else { AuditOutcome::Denied }, request_id)
@@ -282,16 +284,21 @@ fn authorize_operator(
fn authorize_operator_inner(
state: &HubState, peer: SocketAddr, headers: &HeaderMap, mutation: bool, request_id: Uuid,
session: Option<crate::admin::BrowserSession>,
) -> Result<OperatorActor, StatusCode> {
if state.admin.token.is_some() && state.admin.authorize(peer, headers).is_ok() {
return Ok(OperatorActor { name: "api-token".into(), browser: false,
principal: Principal::Service("legacy-admin-token"), request_id });
}
if let Some(session) = state.sessions.active(headers) {
if let Some(session) = session {
// Signed in is not the same as allowed: a user without a grant gets 403.
if !session.admin {
return Err(StatusCode::FORBIDDEN);
}
if mutation {
super::session::require_csrf(headers, &session.csrf.to_string())?;
}
return Ok(OperatorActor { principal: Principal::Emergency(session.actor.clone()),
return Ok(OperatorActor { principal: session.principal(),
name: session.actor, browser: true, request_id });
}
if !state.sessions.enabled() {
@@ -327,6 +334,7 @@ pub fn router(state: HubState) -> Router {
.merge(super::jobs::routes())
.merge(super::catalog::routes())
.merge(super::session::routes())
.merge(super::oidc::routes())
.merge(super::restore::routes())
.with_state(state);