about summary refs log tree commit diff
path: root/scripts
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-11-06 17:17:39 +0000
committerPjotr <wrk@bosa.lan>2026-11-06 17:17:39 +0000
commitc39dd5440e29b7d6abcc602009c863a2c11ae1f7 (patch)
tree41841d87f9c19d448c3b77edb866128ce853e6f2 /scripts
parent5210ca7139b8670d8620c4924351605ae0550d5d (diff)
downloadgenenetwork4-c39dd5440e29b7d6abcc602009c863a2c11ae1f7.tar.gz
gn-auth-stub: --expires-in, document the public-data-only token
A stub token had a fixed 24h lifetime, and nothing said what an
ungranted one can reach. Both matter for the case that is not a test
run: handing a caller or an agent a long-lived token that may only read
public data. An ungranted stub token already is exactly that -- the
restriction lives in the authorisation lookup genenetwork4 consults for
anything not public, not in the token's scope -- so only the lifetime
was missing.

* scripts/gn-auth-stub.py (duration): new helper, "30d"/"24h"/"60m"/s.
(sign): take expires_in instead of a hard-coded 86400.
(main): --expires-in, defaulting to 24h as before; docstring says what
an ungranted token can reach and why the key's fixity makes a month-long
token safe.
* README.md (Tests): the public-data-only token, what stays visible
(dataset metadata), the --expires-in example and the iss/port coupling.
Diffstat (limited to 'scripts')
-rwxr-xr-xscripts/gn-auth-stub.py39
1 files changed, 36 insertions, 3 deletions
diff --git a/scripts/gn-auth-stub.py b/scripts/gn-auth-stub.py
index 55aa7dd..15d6a18 100755
--- a/scripts/gn-auth-stub.py
+++ b/scripts/gn-auth-stub.py
@@ -29,6 +29,17 @@ What it answers, and why each is needed:
   resource id to the fixture token, for the owner-side tests
   (`GN4_TEST_RESOURCE_ID` must name the same id).
 
+Left ungranted, a token from this stub is a *public-data-only* token:
+genenetwork4 decides what data may go out from the data's own
+public/confidentiality/create_time columns and, for anything not public,
+from this authorisation lookup (src/auth.rs's `check`). No privileges
+means public datasets read normally and every non-public one is a 403 --
+the restriction lives in the lookup, not in the token's `scope`, which
+genenetwork4 does not consult for data access. `--expires-in` sets how
+long such a token lasts (`30d`, `24h`, plain seconds; default 24h); the
+stub's key is fixed and never rotates, so the token stays verifiable for
+as long as anything serving `test/data/test_jwks.json` is up.
+
 `iss` has to match genenetwork4's `AUTH_SERVER_URL` exactly (it is
 passed to `Validation::set_issuer` verbatim), including the trailing
 slash the default `http://127.0.0.1:8081/` carries -- that is what
@@ -54,7 +65,19 @@ def b64u(raw):
     return base64.urlsafe_b64encode(raw).rstrip(b"=").decode()
 
 
-def sign(issuer, kid, subject, scope):
+def duration(text):
+    """A duration as seconds: "86400", "30d", "24h", "60m" or "45s"."""
+    units = {"s": 1, "m": 60, "h": 3600, "d": 86400}
+    unit = text[-1] if text[-1].isalpha() else None
+    try:
+        return int(float(text[:-1] if unit else text) * units[unit or "s"])
+    except (KeyError, ValueError) as error:
+        raise argparse.ArgumentTypeError(
+            f"not a duration: {text!r} (try 30d, 24h, 60m or plain seconds)"
+        ) from error
+
+
+def sign(issuer, kid, subject, scope, expires_in):
     """An RS256 token matching test_jwks.json, in gn-auth's claim shape."""
     now = int(time.time())
     header = {"alg": "RS256", "typ": "JWT", "kid": kid}
@@ -63,7 +86,7 @@ def sign(issuer, kid, subject, scope):
         "scope": scope,
         "iss": issuer,
         "iat": now,
-        "exp": now + 86400,
+        "exp": now + expires_in,
     }
     signing_input = ".".join(
         b64u(json.dumps(part, separators=(",", ":")).encode())
@@ -169,6 +192,16 @@ def main():
         help="scope claim to mint",
     )
     parser.add_argument(
+        "--expires-in",
+        type=duration,
+        default="24h",
+        metavar="DURATION",
+        help="how long the token stays valid, e.g. 30d, 24h, 60m or seconds "
+        "(default 24h). The stub's key is fixed and never rotates, so the "
+        "token stays verifiable for as long as any JWKS serving "
+        "test_jwks.json is up.",
+    )
+    parser.add_argument(
         "--grant-resource-id",
         action="append",
         default=[],
@@ -187,7 +220,7 @@ def main():
     issuer = args.issuer or f"http://127.0.0.1:{args.port}/"
     jwks_document = open(JWKS_FILE).read()
     kid = json.loads(jwks_document)["jwks"][0]["kid"]
-    token = sign(issuer, kid, args.subject, args.scope)
+    token = sign(issuer, kid, args.subject, args.scope, args.expires_in)
 
     # Bind before writing or printing anything: a second instance started
     # while one is already listening fails here, and must leave the first