diff options
Diffstat (limited to 'src/api/access.rs')
| -rw-r--r-- | src/api/access.rs | 62 |
1 files changed, 45 insertions, 17 deletions
diff --git a/src/api/access.rs b/src/api/access.rs index 47e24ba..339d9de 100644 --- a/src/api/access.rs +++ b/src/api/access.rs @@ -394,33 +394,61 @@ pub(crate) async fn authorize( } /// On a node holding a federated store, what its users who aren't its operators may do: a -/// user of `--store-readers` reads the federation (`federation_work`), and nothing else. -/// Its other users are its operators, who may do everything. The answer refusing the -/// request, where it is refused. +/// party's session (a token from a party's key, `login::login_party`) does federation work +/// (`federation_work`), putting events too, while the key it logged in with signs for +/// the party (`fold::State::signing_keys`); a user of `--store-readers` reads the +/// federation; and neither does anything else. Its other users are its operators, who may +/// do everything. The answer refusing the request, where it is refused. #[cfg(feature = "federation")] fn federated(state: &AppState, request: &Request<Body>, user: &str) -> Option<Response> { - if !state.store_readers.contains(user) { + let party_key = request + .extensions() + .get::<super::login::Token>() + .and_then(|token| token.claims.party.clone()); + if party_key.is_none() && !state.store_readers.contains(user) { return None; } - if state.holding.with(|_| ()).is_none() { - return Some(error_response( - StatusCode::FORBIDDEN, - format!("{user} reads a federated store here alone, and this node holds none"), - None, - )); + let signs = state.holding.with(|store| { + let read = store.state(); + party_key + .as_ref() + .is_none_or(|key| read.signing_keys(user, read.current()).contains(key)) + }); + let refused = |status: StatusCode, why: String| { + debug!(user = %user, method = %request.method(), path = %request.uri().path(), reason = %why, "Refused a request of a party or a reader"); + Some(error_response(status, why, None)) + }; + match signs { + None => { + return refused( + StatusCode::FORBIDDEN, + format!("{user} works with a federated store here alone, and this node holds none"), + ); + } + Some(false) => { + return refused( + StatusCode::UNAUTHORIZED, + format!("the key {user} logged in with signs for it no more: log in again"), + ); + } + Some(true) => {} } - if federation_work(request.method(), request.uri().path(), false) { + let puts = party_key.is_some(); + if federation_work(request.method(), request.uri().path(), puts) { return None; } - debug!(user = %user, method = %request.method(), path = %request.uri().path(), "Refused a reader's request that doesn't read the federation"); - Some(error_response( - StatusCode::FORBIDDEN, + let why = if puts { + format!( + "a session of {user}, a party, does federation work alone: reading and putting \ + events (docs/guides/federation/running-a-node.md, Its API)" + ) + } else { format!( "{user} reads the federation here, and does nothing else (--store-readers; \ docs/guides/federation/running-a-node.md, Its API)" - ), - None, - )) + ) + }; + refused(StatusCode::FORBIDDEN, why) } /// Whether a request is federation work: reading the events API (`GET` and `HEAD` of |
