diff options
| author | Pjotr Prins <pjotr.public01@thebird.nl> | 2026-10-04 15:00:58 +0200 |
|---|---|---|
| committer | Pjotr Prins <pjotr.public01@thebird.nl> | 2026-10-04 15:00:58 +0200 |
| commit | dfbf105652140d52e50a96853f0a7ab91fec0776 (patch) | |
| tree | e31e62d0d891dce85be8a09d5094d9d8f06cf7e0 /docs | |
| parent | abb1fe51b1e8cf4079c462b3657e21376d389b99 (diff) | |
| parent | 859a86bf39df6eafa6988215cb84556fc9623ea0 (diff) | |
| download | genenetwork4-dfbf105652140d52e50a96853f0a7ab91fec0776.tar.gz | |
Merge branch 'gn-auth'
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/gn-auth.md | 102 |
1 files changed, 92 insertions, 10 deletions
diff --git a/docs/gn-auth.md b/docs/gn-auth.md index 42ff910..c33dc54 100644 --- a/docs/gn-auth.md +++ b/docs/gn-auth.md @@ -185,14 +185,96 @@ has many more (`/auth/role/...`, `/auth/user/...`, genenetwork4 doesn't need to talk to gn-auth for every request -- only for the two things above: verifying a presented token's signature (cache the JWKS, refresh periodically, don't refetch per-request), and --- for endpoints that should be scoped to a specific group/species -- -asking gn-auth what that token's holder is allowed to do on that -resource. Endpoints with no sensitive data behind them don't need to -call gn-auth at all. - -This document describes the mechanism; implementing it in Rust -- -likely a custom Axum extractor that verifies the JWT (e.g. via the -`jsonwebtoken` or `josekit` crate for RS256) and yields the resolved -user to handlers that opt in, so endpoints that don't need auth aren't -forced to go through it -- is still to be done. +-- for data that isn't public -- asking gn-auth what that token's +holder is allowed to do on a specific resource. Endpoints with no +sensitive data behind them don't need to call gn-auth at all. + +Whether data "is public" is answered by GeneNetwork's own schema, not +by asking gn-auth: GeneNetwork2 treats a dataset (`ProbeSetFreeze`, +`PublishFreeze`, `GenoFreeze`) as public exactly when +`public > 0 AND confidentiality < 1` (`wqflask/api/router.py`, used +consistently throughout GN2/GN3). `InbredSet`/group rows carry a +`public` column too, but GN2 never actually filters on it anywhere -- +it's informational only there -- so groups/species endpoints need no +gating at all, matching GN2's own behavior. For data that *is* public +by this test, still require a valid token (don't skip verification +entirely), but skip the resource-authorization call to gn-auth -- one +less network round trip for what is, in practice, most of the data. + +A dataset older than 5 years is treated as public too, regardless of +its `public`/`confidentiality` flags -- a confidentiality embargo isn't +meant to last forever. This isn't a GN2 convention, just a rule applied +here, based on `CreateTime`. + +### Resolving a probeset/trait to a gn-auth resource + +A group/`InbredSet` resource's id is resolved via +`GET /auth/resource/populations/resource-id/<speciesid>/<inbredsetid>` +(above). A probeset/trait doesn't have an equivalent "resolve this id +to a resource id" endpoint -- instead, call +`POST /auth/data/authorisation` directly with a `"Dataset::Trait"` +string (e.g. `{"traits": ["HC_M2_0606_P::1442370_at"]}`); gn-auth +resolves the resource itself, internally, via a join on +`linked_mrna_data.dataset_name` (`gn_auth/auth/authorisation/data/ +mrna.py`'s `resources_by_datasets_and_traits`). If the trait's dataset +was never linked to a gn-auth resource at all (the common case for +data nobody has ever restricted), gn-auth answers with a 404, which +genenetwork4 treats the same as "no privileges" -- not a request +failure. + +### Implemented, in `src/auth.rs` + +- `AuthToken`, an Axum extractor (`impl FromRequestParts`): verifies a + bearer token's RS256 signature and standard time claims (`exp`/`nbf`, + checked when present -- gn-auth itself treats them as optional, + confirmed against its own validator) against a cached, periodically + refreshed JWKS. Add it as a handler parameter to require a valid + token; omit it for endpoints that don't need one. +- `is_publicly_readable(public, confidentiality, create_time)`: the GN2 + threshold above, plus the 5-year embargo-age rule, as a plain + function a handler calls on its own fetched row. +- `authorise(auth_server_url, token, resource_id, privilege)`: the two + gn-auth calls (resource-id resolution is the caller's job -- + `GET /auth/resource/populations/resource-id/...` -- `authorise` + itself only wraps `POST /auth/resource/authorisation`), with the + per-(user, resource) answer cached briefly so repeated requests for + the same resource by the same user don't each cost a round trip. +- `authorise_trait(auth_server_url, token, dataset_name, trait_name, + privilege)`: the trait-keyed equivalent of `authorise`, wrapping + `POST /auth/data/authorisation` -- for data addressed by a + `(dataset, probeset)` pair rather than a resource id directly. Same + caching as `authorise`. +- `tests/auth.rs` runs all of this against a real gn-auth instance + (set `GN4_TEST_AUTH_TOKEN`/`GN4_TEST_RESOURCE_ID`; skips otherwise). + +### Wired into a route: `GET /datasets/{dataset}/probesets/{probeset}` + +This is the first (and so far only) endpoint gated this way: + +- An `AuthToken` is required unconditionally -- every caller needs a + valid token, even for public data. +- If the dataset is publicly readable (`is_publicly_readable`), the + probeset's metadata is returned with no further gn-auth call. +- Otherwise, `authorise_trait(..., "group:resource:view-resource")` is + called; a `false` result (including the "never linked, so no + privileges" 404 case above) is a 403, not a 404 -- the dataset and + probeset both exist, the caller just isn't allowed to see them. +- A failure to reach/parse gn-auth's answer is a 502, distinct from a + 403 (denied) or a 500 (this service's own database error) -- see + `tests/dataset_probeset_info.rs` for all of these cases exercised + against a real gn-auth instance. + +Which other endpoints should require a token, and which privilege each +should check, remains a separate decision, not yet made here. + +### Found wrong in gn-auth while building this + +Four bugs in gn-auth itself blocked this integration before they were +fixed (see gn-auth's own `RELEASE_NOTES.md` and its +`fix-fresh-deployment-bugs` branch): a JWKS cache that could stay empty +for hours after a fresh deployment's first token was issued, a dev +client whose secret was never hashed so it could never authenticate, +`public_resources()` crashing on every fresh deployment, and +`POST /auth/resource/authorisation` crashing (instead of degrading to +the anonymous view) on an invalid rather than missing bearer token. |
