From 586b0f63ef31fde987e00a305e91ee4e2136332e Mon Sep 17 00:00:00 2001 From: Pjotr Prins Date: Sat, 14 Nov 2026 14:20:54 +0000 Subject: tokens: short opaque API tokens in a single tokens.sqlite A second credential format beside gn-auth's JWT, for agents and scripts: `gn4ai_` + 56 body characters + 2 checksum characters = 64, from an alphabet with no look-alikes, so a typo is refused before the registry is consulted. Opaque by design -- nothing is stored in the token; every fact lives in one SQLite database (`tokens.sqlite`, GN4_API_TOKENS_DB, mode 0600, gitignored) keyed by the SHA-256 of the secret. Revoking is a row, takes effect on the next request, and a token in a log line reveals nothing; lose the registry and every short token stops working. Administration is local-only: GET/POST /tokens, DELETE /tokens/{id} and GET /tokens/who are behind a router layer that refuses any caller whose TCP peer is not 127.x.x.x/::1, and any request claiming to come through a proxy. A layer rather than a per-handler check, so nothing of a remote request is parsed before it is refused; there is no configuration switch, because minting credentials must never be reachable from the network. A short token reads public data only: every token-only endpoint, and `#[gnauth(secured)]` endpoints for public rows; a confidential dataset is refused with a 403 that names the JWT route, since gn-auth cannot validate an opaque token (the on-the-fly-bridge and registry-grant alternatives were both rejected for v1). scripts/gn4-token.py create|list|revoke|who drives the routes, so there is one implementation of the format and the revocation rules; the token is printed exactly once. Tests: the localhost guard over all four routes (remote peer, no peer, proxied header), and mint -> authenticate -> who -> revoke -> 401. --- src/config.rs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index ff0ab8c..b23d39b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -76,6 +76,11 @@ pub struct Config { /// Same GEMMA_WRAPPER_CMD config key GeneNetwork3 uses. Defaults to /// "gemma-wrapper", resolved via PATH. pub gemma_wrapper_cmd: String, + /// Where the short (`gn4ai_`) API tokens live: one SQLite database, + /// `GN4_API_TOKENS_DB`, default `tokens.sqlite` in the working + /// directory. See src/tokens.rs. The file is chmod 0600 on open -- + /// it is a secret store, and it must not be in the repository. + pub api_tokens_db: String, /// Directory gemma-wrapper uses to cache K matrices and association /// results between calls (its own --cache-dir switch). Defaults to /// a "gn4-gemma" directory under the system temp dir. @@ -274,6 +279,7 @@ impl Config { ); values.insert("XAPIAN_DB_PATH".to_string(), String::new()); values.insert("SEARCH_ENGINE".to_string(), "forward".to_string()); + values.insert("GN4_API_TOKENS_DB".to_string(), "tokens.sqlite".to_string()); values.insert("GEMMA_WRAPPER_CMD".to_string(), "gemma-wrapper".to_string()); values.insert( "GEMMA_CACHE_DIR".to_string(), @@ -443,6 +449,7 @@ impl Config { "GEMMA_PLOTTER", "XAPIAN_DB_PATH", "SEARCH_ENGINE", + "GN4_API_TOKENS_DB", "GEMMA_WRAPPER_CMD", "GEMMA_CACHE_DIR", "RAVANAN_CMD", @@ -507,6 +514,7 @@ impl Config { dev_token_client_secret: values["GN_AUTH_DEV_CLIENT_SECRET"].clone(), dev_token_username: values["GN_AUTH_DEV_USERNAME"].clone(), dev_token_password: values["GN_AUTH_DEV_PASSWORD"].clone(), + api_tokens_db: values["GN4_API_TOKENS_DB"].clone(), gemma_wrapper_cmd: values["GEMMA_WRAPPER_CMD"].clone(), gemma_cache_dir: values["GEMMA_CACHE_DIR"].clone(), ravanan_cmd: values["RAVANAN_CMD"].clone(), @@ -768,6 +776,7 @@ mod tests { "GN3_TIMEOUT_SECS", "XAPIAN_DB_PATH", "SEARCH_ENGINE", + "GN4_API_TOKENS_DB", "GEMMA_WRAPPER_CMD", "GEMMA_CACHE_DIR", "RAVANAN_CMD", -- cgit 1.4.1