feat: per-orbit access grants and an Access console (auth M5)

Authorization
- one gate for every human endpoint: authenticate, resolve the resource, then
  admin_only / scoped / visible; require_admin* removed so each handler must choose
- grants (user or IdP group; global or one orbit; optional end date) read live on
  every request; only orbit.read, snapshot.read, capture.run and snapshot.download
  can be granted, everything else stays with configured administrators
- an orbit without orbit.read is 404, a visible one without the permission is 403,
  a caller with nothing to see gets 403 on lists; job history is filtered before LIMIT
- jobs and audit record the stable principal id, not the IdP display name
- restore targets and restore history are admin-only until self-service (M6)
- sessions keep the groups verified at sign-in

Administration
- /api/v1/access grants and users (enable, disable, sign out everywhere), validated
  and audited by id; admins cannot disable themselves or the emergency operator
- Access console with the shared Select and a themed DatePicker; non-administrators
  only see Vault and Activity and never call administrator endpoints
- the Window/Radar view is stored per account (/api/v1/me/preferences)
- UI tolerates older hubs and mocks (missing admin/grants) and a console that fails
  to render no longer blanks the app

Tests: a matrix generated from the endpoint contract exercises every human route
against real resources in two orbits for each kind of caller.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-14 14:45:41 +07:00
parent 54abd02250
commit d12472ba02
29 changed files with 2284 additions and 169 deletions

View File

@@ -28,6 +28,8 @@ pub struct StoredSession {
pub auth_method: String,
pub credential_fp: Option<String>,
pub admin: bool,
/// Groups verified at sign-in (JSON in the table).
pub groups: Vec<String>,
pub expires_at: i64,
}
@@ -60,6 +62,7 @@ pub(super) fn migrate(conn: &Connection) -> Result<()> {
)?;
// Added after the first M2 build; default 0 keeps old rows without admin rights.
super::ensure_column(conn, "sessions", "admin", "INTEGER NOT NULL DEFAULT 0")?;
super::ensure_column(conn, "sessions", "groups", "TEXT NOT NULL DEFAULT '[]'")?;
Ok(())
}
@@ -89,14 +92,15 @@ impl Db {
auth_method: &str,
credential_fp: Option<&str>,
admin: bool,
groups: &[String],
now: i64,
) -> Result<bool> {
let conn = self.conn.lock().unwrap();
conn.execute("DELETE FROM sessions WHERE expires_at <= ?1 OR last_seen + ?2 <= ?1", params![now, IDLE_SECS])?;
let inserted = conn.execute(
"INSERT INTO sessions(token_digest,user_id,generation,auth_method,credential_fp,auth_time,created_at,last_seen,expires_at,admin)
SELECT ?1, id, generation, ?3, ?4, ?5, ?5, ?5, ?6, ?7 FROM users WHERE id=?2 AND active=1",
params![session_digest(id), user_id, auth_method, credential_fp, now, now + ABSOLUTE_SECS, admin],
"INSERT INTO sessions(token_digest,user_id,generation,auth_method,credential_fp,auth_time,created_at,last_seen,expires_at,admin,groups)
SELECT ?1, id, generation, ?3, ?4, ?5, ?5, ?5, ?6, ?7, ?8 FROM users WHERE id=?2 AND active=1",
params![session_digest(id), user_id, auth_method, credential_fp, now, now + ABSOLUTE_SECS, admin, serde_json::to_string(groups)?],
)?;
Ok(inserted == 1)
}
@@ -109,7 +113,7 @@ impl Db {
let conn = self.conn.lock().unwrap();
let found: Option<(StoredSession, i64)> = conn
.query_row(
"SELECT s.user_id,u.display_name,s.auth_method,s.credential_fp,s.expires_at,s.last_seen,s.admin
"SELECT s.user_id,u.display_name,s.auth_method,s.credential_fp,s.expires_at,s.last_seen,s.admin,s.groups
FROM sessions s JOIN users u ON u.id=s.user_id
WHERE s.token_digest=?1 AND u.active=1 AND s.generation=u.generation
AND s.expires_at>?2 AND s.last_seen+?3>?2",
@@ -123,6 +127,7 @@ impl Db {
credential_fp: row.get(3)?,
expires_at: row.get(4)?,
admin: row.get(6)?,
groups: serde_json::from_str(&row.get::<_, String>(7)?).unwrap_or_default(),
},
row.get(5)?,
))
@@ -141,8 +146,6 @@ impl Db {
Ok(())
}
/// Used by the per-user revoke endpoint planned for M5 (admin).
#[allow(dead_code)]
/// Invalidate every session of a user, including any created concurrently
/// from the previous generation. Returns the number of rows removed.
pub fn revoke_user_sessions(&self, user_id: &str) -> Result<usize> {
@@ -180,13 +183,13 @@ mod tests {
async fn idle_and_absolute_deadlines_use_the_supplied_clock() {
let (_dir, db) = db().await;
let user = db.upsert_user("local:emergency", "ops", "ops", 1000).unwrap();
assert!(db.create_session("a", &user, "password", Some("fp"), true, 1000).unwrap());
assert!(db.create_session("a", &user, "password", Some("fp"), true, &[], 1000).unwrap());
assert!(db.lookup_session("a", 1000 + IDLE_SECS - 1).unwrap().is_some());
// sliding: the touch above moved last_seen, so a request just inside the
// new idle window is still fine, one beyond it is not
assert!(db.lookup_session("a", 1000 + 2 * IDLE_SECS - 2).unwrap().is_some());
assert!(db.lookup_session("a", 1000 + 3 * IDLE_SECS).unwrap().is_none());
assert!(db.create_session("b", &user, "password", None, true, 5000).unwrap());
assert!(db.create_session("b", &user, "password", None, true, &[], 5000).unwrap());
let mut now = 5000;
while now + IDLE_SECS - 10 < 5000 + ABSOLUTE_SECS {
now += IDLE_SECS - 10;
@@ -200,7 +203,7 @@ mod tests {
async fn only_a_digest_is_stored_and_csrf_is_not_the_key() {
let (_dir, db) = db().await;
let user = db.upsert_user("local:emergency", "ops", "ops", 1).unwrap();
db.create_session("raw-session-id", &user, "password", None, true, 1).unwrap();
db.create_session("raw-session-id", &user, "password", None, true, &[], 1).unwrap();
let conn = db.conn.lock().unwrap();
let key: String = conn.query_row("SELECT token_digest FROM sessions", [], |r| r.get(0)).unwrap();
assert_ne!(key, "raw-session-id");
@@ -216,7 +219,7 @@ mod tests {
async fn revoked_sessions_cannot_be_resurrected_by_a_late_touch() {
let (_dir, db) = db().await;
let user = db.upsert_user("local:emergency", "ops", "ops", 1000).unwrap();
db.create_session("a", &user, "password", None, true, 1000).unwrap();
db.create_session("a", &user, "password", None, true, &[], 1000).unwrap();
db.revoke_session("a").unwrap();
assert!(db.lookup_session("a", 1000 + 2 * TOUCH_AFTER_SECS).unwrap().is_none());
assert_eq!(db.session_rows(), 0);
@@ -228,14 +231,14 @@ mod tests {
let one = db.upsert_user("https://idp.example", "alice", "Alice", 10).unwrap();
let two = db.upsert_user("https://idp.example", "bob", "Bob", 10).unwrap();
for id in ["a1", "a2"] {
db.create_session(id, &one, "oidc", None, true, 10).unwrap();
db.create_session(id, &one, "oidc", None, true, &[], 10).unwrap();
}
db.create_session("b1", &two, "oidc", None, true, 10).unwrap();
db.create_session("b1", &two, "oidc", None, true, &[], 10).unwrap();
assert_eq!(db.revoke_user_sessions(&one).unwrap(), 2);
assert!(db.lookup_session("a1", 11).unwrap().is_none());
assert!(db.lookup_session("a2", 11).unwrap().is_none());
assert!(db.lookup_session("b1", 11).unwrap().is_some());
assert!(db.create_session("a3", &one, "oidc", None, true, 12).unwrap());
assert!(db.create_session("a3", &one, "oidc", None, true, &[], 12).unwrap());
assert!(db.lookup_session("a3", 13).unwrap().is_some());
}
@@ -243,11 +246,11 @@ mod tests {
async fn inactive_users_have_no_valid_or_new_sessions() {
let (_dir, db) = db().await;
let user = db.upsert_user("https://idp.example", "carol", "Carol", 10).unwrap();
db.create_session("c1", &user, "oidc", None, true, 10).unwrap();
db.create_session("c1", &user, "oidc", None, true, &[], 10).unwrap();
db.conn.lock().unwrap().execute("UPDATE users SET active=0 WHERE id=?1", params![user]).unwrap();
assert!(db.lookup_session("c1", 11).unwrap().is_none());
assert!(!db.create_session("c2", &user, "oidc", None, true, 11).unwrap());
assert!(!db.create_session("c3", "no-such-user", "oidc", None, true, 11).unwrap());
assert!(!db.create_session("c2", &user, "oidc", None, true, &[], 11).unwrap());
assert!(!db.create_session("c3", "no-such-user", "oidc", None, true, &[], 11).unwrap());
}
#[tokio::test]
@@ -270,8 +273,8 @@ mod tests {
let db = Db::connect(&path).await.unwrap();
db.migrate().await.unwrap();
let user = db.upsert_user("local:emergency", "ops", "ops", 1000).unwrap();
db.create_session("live", &user, "password", Some("fp"), true, 1000).unwrap();
db.create_session("old", &user, "password", Some("fp"), true, 1000).unwrap();
db.create_session("live", &user, "password", Some("fp"), true, &[], 1000).unwrap();
db.create_session("old", &user, "password", Some("fp"), true, &[], 1000).unwrap();
}
let db = Db::connect(&path).await.unwrap();
db.migrate().await.unwrap();