about summary refs log tree commit diff
path: root/src/auth.rs
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-10-13 06:06:37 +0000
committerPjotr <wrk@bosa.lan>2026-10-13 06:06:37 +0000
commit2551418eede25d0ffaebef67f9950f13e3551567 (patch)
tree1d173432582191401f69622925d7337d5ebe81c8 /src/auth.rs
parent2456d66fc2c14345ab0730ef0a4ae07458af8ec6 (diff)
downloadgenenetwork4-2551418eede25d0ffaebef67f9950f13e3551567.tar.gz
auth: add gnauth compile-time tags (secured/token_only/disabled)
Adds a gnauth-macros proc-macro crate and a private-field Authorized
capability type in src/auth.rs, so #[gnauth(secured)] is a real,
compile-time-checked guarantee that a handler performs the gn-auth
resource-authorisation round trip before sharing trait/probeset
values, rather than just a comment. #[gnauth(token_only)] marks
handlers that require a token but skip the round trip, and
#[gnauth(disabled)] marks the two handlers that need no token at all.

Also fixes groups.rs: group_trait_values, group_trait_info and
group_traits previously performed no gn-auth round trip at all for
published-trait data; they now resolve the group's PublishFreeze row
and call the same auth::check used elsewhere, denying by default when
no such row exists.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Diffstat (limited to 'src/auth.rs')
-rw-r--r--src/auth.rs63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/auth.rs b/src/auth.rs
index eb13186..d1a86c6 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -234,6 +234,69 @@ fn days_from_civil(y: i64, m: i64, d: i64) -> i64 {
     era * 146097 + doe - 719468
 }
 
+/// Proof that this request's data was cleared to go out: either it
+/// was already [`is_publicly_readable`], or gn-auth's resource-
+/// authorization round trip (`authorise_trait`/`authorise_dataset`,
+/// via [`check`]) said so. The field is private, so only this module
+/// can construct one -- a handler elsewhere can bind or match it
+/// (e.g. by having its `Found` variant carry `Authorized` alongside
+/// the data) but never fabricate one, which is what makes it an
+/// actual guarantee rather than a type-shaped comment. See
+/// `gnauth_macros::gnauth`, which checks at compile time that any
+/// handler tagged `#[gnauth(secured)]` actually mentions this type.
+#[derive(Debug, Clone, Copy)]
+pub struct Authorized(());
+
+/// Check whether `token`'s holder may view data gated by
+/// `dataset_name`'s own public/confidentiality/create_time -- either
+/// because it's [`is_publicly_readable`], or because gn-auth's
+/// resource-authorization round trip says so (per-trait when
+/// `trait_name` is `Some`, dataset-wide when `None`, e.g. for a
+/// listing). Returns the [`Authorized`] capability on success, so
+/// callers can't construct a "found and visible" result without one.
+///
+/// This is the one place [`is_publicly_readable`] and
+/// `authorise_trait`/`authorise_dataset` are combined; every gated
+/// handler (src/routes/traits.rs, src/routes/datasets.rs,
+/// src/routes/groups.rs) calls this instead of writing that branch
+/// out itself.
+pub fn check(
+    auth_server_url: &str,
+    token: &AuthToken,
+    dataset_name: &str,
+    trait_name: Option<&str>,
+    public: u8,
+    confidentiality: u8,
+    create_time: &str,
+) -> Result<Option<Authorized>, String> {
+    if is_publicly_readable(public, confidentiality, create_time) {
+        return Ok(Some(Authorized(())));
+    }
+    let authorised = match trait_name {
+        Some(trait_name) => {
+            authorise_trait(auth_server_url, token, dataset_name, trait_name, VIEW_RESOURCE_PRIVILEGE)?
+        }
+        None => authorise_dataset(auth_server_url, token, dataset_name, VIEW_RESOURCE_PRIVILEGE)?,
+    };
+    Ok(authorised.then_some(Authorized(())))
+}
+
+/// Boolean form of [`check`], for callers that filter a list of
+/// resources rather than reject a single request with 403 (e.g.
+/// publication_traits, which drops traits the caller may not view
+/// instead of failing the whole request).
+pub fn is_authorised(
+    auth_server_url: &str,
+    token: &AuthToken,
+    dataset_name: &str,
+    trait_name: Option<&str>,
+    public: u8,
+    confidentiality: u8,
+    create_time: &str,
+) -> Result<bool, String> {
+    Ok(check(auth_server_url, token, dataset_name, trait_name, public, confidentiality, create_time)?.is_some())
+}
+
 #[derive(Serialize)]
 struct AuthorisationRequest<'a> {
     #[serde(rename = "resource-ids")]