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/lib.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index e054c42..3222369 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,6 +31,7 @@ mod rqtl; pub mod search; mod streaming; pub mod suggest; +pub mod tokens; pub mod wikidata; use error::ApiError; @@ -238,6 +239,20 @@ pub fn app() -> Router { if config::Config::load().test_token_enabled { api_router = api_router.routes(routes!(routes::dev::get_test_token)); } + // Administering the short API tokens. Always registered, and each + // handler refuses anything that did not arrive directly from this + // machine (see src/routes/tokens.rs): minting credentials must not be + // reachable from the network, so there is no switch to turn that off. + // A sub-router so the localhost guard is a layer over exactly these + // four routes: it must refuse a remote caller before anything of the + // request is parsed (see routes::tokens::local_only). + let token_router = OpenApiRouter::new() + .routes(routes!(routes::tokens::list_tokens)) + .routes(routes!(routes::tokens::mint_token)) + .routes(routes!(routes::tokens::revoke_token)) + .routes(routes!(routes::tokens::who_am_i)) + .route_layer(axum::middleware::from_fn(routes::tokens::local_only)); + api_router = api_router.merge(token_router); let (router, api) = api_router.split_for_parts(); router .route("/openapi.json", axum::routing::get(|| async { Json(api) })) -- cgit 1.4.1