about summary refs log tree commit diff
path: root/src/auth.rs
diff options
context:
space:
mode:
authorPjotr Prins <pjotr.public01@thebird.nl>2026-11-14 15:10:28 +0000
committerPjotr Prins <pjotr.public01@thebird.nl>2026-11-14 15:10:28 +0000
commit6daec6b5d03a80af3e2496b6bf666340b3d718fd (patch)
tree842d614ba0a5cdd72473ea0d96eb061fd8fef0f5 /src/auth.rs
parent586b0f63ef31fde987e00a305e91ee4e2136332e (diff)
downloadgenenetwork4-6daec6b5d03a80af3e2496b6bf666340b3d718fd.tar.gz
log: name the person and the credential, and resolve handles from the registry
The access log said who (`user`) but not with what: with short tokens a
request can be made by a shared credential, so "which person" and "which
credential" are now separate fields -- `scheme` ("jwt" | "opaque") and,
for an opaque token, `token` (its registry handle, e.g.
`agent-ravanan-3`). Never the token or its hash, and the path still
carries no query string.

The line also gains `ts` (RFC 3339 UTC), so a redirected stderr file is
self-describing rather than relying on journald's prefix. Hand-rolled
civil-from-days rather than a new crate: one format, and no Cargo.lock
drift.

scripts/gn4-log.py reads such a log and shows it as a table with the
handle joined against tokens.sqlite: the subject as the registry has it
now (flagging a handle whose subject has been repointed), and the token's
status today -- [revoked], [expired], [unknown] -- which is how "an agent
was working at 14:02" is told apart from "an agent was working at 14:02
with a credential revoked since". It reads the registry read-only, passes
non-JSON lines through in table mode (the process log interleaves
backtraces), and offers --json and --summary for jq and for counting.

CallerSlot now carries the whole Caller (scheme, id, subject) rather than
just the subject string.
Diffstat (limited to 'src/auth.rs')
-rw-r--r--src/auth.rs34
1 files changed, 26 insertions, 8 deletions
diff --git a/src/auth.rs b/src/auth.rs
index a2036e1..23ef582 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -62,18 +62,36 @@ pub const USER_HEADER: &str = "x-gn4-user";
 /// [`crate::lib::tag_caller`] to put on the response, since the extractor
 /// runs inside the handler and the middleware outside it.
 #[derive(Debug, Clone, Default)]
-pub struct CallerSlot(std::sync::Arc<std::sync::Mutex<Option<String>>>);
+pub struct CallerSlot(std::sync::Arc<std::sync::Mutex<Option<Caller>>>);
+
+/// Who a request ran as, and with which credential. The subject is a
+/// gn-auth user id; `id` is the registry handle of an opaque token
+/// (`agent-ravanan-3`) and `None` for a JWT. Both go to the access log,
+/// because "which person, with which credential" is two different
+/// questions -- a shared token and a personal one can carry the same
+/// subject, and a token's handle is what `scripts/gn4-log.py` resolves
+/// against the registry.
+#[derive(Debug, Clone)]
+pub struct Caller {
+    pub scheme: Scheme,
+    pub id: Option<String>,
+    pub subject: String,
+}
 
 impl CallerSlot {
-    /// Record the subject of the verified token.
-    pub fn set(&self, subject: &str) {
+    /// Record the identity of the verified credential.
+    pub fn set(&self, scheme: Scheme, id: Option<&str>, subject: &str) {
         if let Ok(mut slot) = self.0.lock() {
-            *slot = Some(subject.to_string());
+            *slot = Some(Caller {
+                scheme,
+                id: id.map(str::to_string),
+                subject: subject.to_string(),
+            });
         }
     }
 
-    /// The subject, if a handler got as far as verifying a token.
-    pub fn get(&self) -> Option<String> {
+    /// The caller, if a handler got as far as verifying a token.
+    pub fn get(&self) -> Option<Caller> {
         self.0.lock().ok().and_then(|slot| slot.clone())
     }
 }
@@ -161,7 +179,7 @@ impl<S: Send + Sync> FromRequestParts<S> for AuthToken {
                 .map_err(|e| unauthorized(&format!("token registry error: {e}")))?
                 .ok_or_else(|| unauthorized("That token is not valid, has been revoked or has expired."))?;
             if let Some(slot) = parts.extensions.get::<CallerSlot>() {
-                slot.set(&record.subject);
+                slot.set(Scheme::Opaque, Some(&record.id), &record.subject);
             }
             return Ok(AuthToken {
                 scheme: Scheme::Opaque,
@@ -176,7 +194,7 @@ impl<S: Send + Sync> FromRequestParts<S> for AuthToken {
             verify(token, &config.auth_server_url).map_err(|e| unauthorized(&e.to_string()))?;
         // For the response header the proxy logs (see `CallerSlot`).
         if let Some(slot) = parts.extensions.get::<CallerSlot>() {
-            slot.set(&claims.sub);
+            slot.set(Scheme::Jwt, None, &claims.sub);
         }
         Ok(AuthToken {
             scheme: Scheme::Jwt,