about summary refs log tree commit diff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorPjotr Prins <pjotr.public01@thebird.nl>2026-10-05 07:34:26 +0000
committerPjotr <wrk@bosa.lan>2026-10-05 07:34:26 +0000
commit649ff6395bdca06530ceabd3e216c66851dc60d6 (patch)
tree2bbe7008a8258b11ada120315fedff208d230206 /src/lib.rs
parent693e7ee89f8618c1abbf8af5c0db23b071700803 (diff)
downloadgenenetwork4-649ff6395bdca06530ceabd3e216c66851dc60d6.tar.gz
Rename /dev/token to /get-test-token; require a token everywhere
Rename: POST /dev/token -> POST /get-test-token (src/routes/dev.rs,
TestToken replacing DevToken), matching tests/get_test_token.rs and
test/python/test_get_test_token.py (renamed from
tests/dev_token.rs/test_dev_token.py). Config::dev_token_enabled() and
the GN_AUTH_DEV_* config keys keep their names -- they're about the
dev-token feature generically, not tied to this one route's path.

Every endpoint now requires a valid token, not just the
dataset_probeset*/traits* family: species(_by_name/_datasets),
groups(_by_name/_strains/_traits*/_datasets) and dataset_by_name all
gain an AuthToken parameter. /, /version, /openapi.json and
/get-test-token itself are the only exceptions -- a health check, a
version check, the spec describing all this, and the endpoint that
hands out a token in the first place (which would be circular if it
needed one).

Rather than repeating security(("bearer_token" = [])) on every
#[utoipa::path], ApiDoc now declares it once as the document's default
security(...), and the three exceptions besides /get-test-token (/,
/version) override it with their own empty security() -- standard
OpenAPI semantics (an operation's own security, even empty, overrides
the document default; an absent one inherits it). error::Unauthorized,
a new #[derive(utoipa::IntoResponses)] type wrapping ApiError, is the
shared 401 every gated handler now references bare in its own
responses(...) list instead of writing out the same
(status = 401, description = "...", body = ApiError) tuple by hand.
Verified the resolved security per operation in a live /openapi.json.

tests/common::auth_token() (Rust) and the new
test/python/auth_helpers.auth_token() (Python, deduplicating what four
files previously each defined locally) now try this server's own
POST /get-test-token first, falling back to GN4_TEST_AUTH_TOKEN --
every previously-ungated test file gains a token check and switches to
get_with_token; the whole suite, both languages, now runs with zero
manually-provisioned tokens given GENENETWORK4_DEV_MODE=true
GENENETWORK4_BIND=127.0.0.1 for the test run. ServerTestCase gains
EXTRA_ENV so each Python test class can start its own server with dev
mode on. Verified both configured (auto-fetch) and unconfigured
(GN4_TEST_AUTH_TOKEN fallback, and full skip when neither is set, e.g.
the Guix build sandbox) paths.

README.md, docs/gn-auth.md and RELEASE_NOTES.md updated throughout for
the rename and the new blanket token requirement.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XDDpXvZBQat9uSgDeEMJ8d
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs20
1 files changed, 12 insertions, 8 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 84a143a..b1f004c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -21,12 +21,13 @@ use error::ApiError;
     title = "GeneNetwork4 API",
     description = "GeneNetwork4 REST API",
     license(name = "AGPL-3.0-or-later")
-), modifiers(&SecurityAddon))]
+), security(("bearer_token" = [])), modifiers(&SecurityAddon))]
 struct ApiDoc;
 
-/// Registers the "bearer_token" scheme used by endpoints that require a
-/// gn-auth token (see `security(...)` on their `#[utoipa::path]`, e.g.
-/// `routes::datasets::dataset_probeset_info`).
+/// Registers the "bearer_token" scheme `ApiDoc`'s own `security(...)`
+/// (above) requires by default on every operation -- every endpoint
+/// needs a token unless it opts out with its own empty `security()`
+/// (`hello_world`, `version`, `routes::dev::get_test_token`: see each).
 struct SecurityAddon;
 
 impl utoipa::Modify for SecurityAddon {
@@ -75,7 +76,7 @@ pub fn app() -> Router {
     // dev-mode server bound to loopback only. See
     // Config::dev_token_enabled and src/routes/dev.rs.
     if config::Config::load().dev_token_enabled() {
-        api_router = api_router.routes(routes!(routes::dev::dev_token));
+        api_router = api_router.routes(routes!(routes::dev::get_test_token));
     }
     let (router, api) = api_router.split_for_parts();
     router
@@ -93,11 +94,14 @@ fn request_id_string(request_id: &RequestId) -> Option<String> {
 }
 
 // hello_world and version predate the domain-module split and don't
-// belong to a data domain; kept here as top-level utility routes.
+// belong to a data domain; kept here as top-level utility routes. Both
+// opt out of ApiDoc's default security requirement with an empty
+// security() -- neither takes an AuthToken, so documenting them as
+// needing one would be wrong, not just inconsistent.
 #[utoipa::path(get, path = "/", summary = "Greeting; confirms the server is up", responses(
     (status = 200, description = "Greeting", body = String),
     (status = 500, description = "Internal error", body = ApiError)
-))]
+), security())]
 async fn hello_world() -> &'static str {
     "Hello, world!"
 }
@@ -115,7 +119,7 @@ struct VersionInfo {
 #[utoipa::path(get, path = "/version", summary = "Build information: version, git commit, commit date", responses(
     (status = 200, description = "Build information", body = VersionInfo),
     (status = 500, description = "Internal error", body = ApiError)
-))]
+), security())]
 async fn version() -> Json<VersionInfo> {
     Json(VersionInfo {
         name: "GeneNetwork4",