diff options
| author | tv <tv@krebsco.de> | 2026-11-19 20:11:05 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-19 21:27:21 +0100 |
| commit | 9bf334bedbc4b0cc3ce32fcd3baef27d3072f194 (patch) | |
| tree | 6236db6799c2523ccdb899e6a42cc2153ce4eb08 /src/api | |
| parent | fc6c7101cdb42666c1f79df4206029c7cf7e61c6 (diff) | |
federated nodes: a party's keys log in as the party, for federation work
A party's people work with the federation by signing as the party, with
a key of its set; what they need of a node is to read and to put events,
which a node's users, its operators, may do with every power besides.
POST /auth/login/party takes a challenge (POST /auth/challenge) signed
with one of the keys that sign for the party (fold::State::signing_keys:
those its current version names, and its key set as it counts), for the
namespace continuum-login, armored or as the federation spells a
signature (keys::signer, keys::verifies_in: the federation's checks, but
for the namespace), and answers a token as the party. Logins by key are
counted per party as per user (challenge::take_with, shared with
logging in by a registry's keys).
The token names the key (Claims::party). Such a session does federation
work alone (api::access::federated): what --store-readers do, and
POST /events; anything else is 403. Every request checks that the key
still signs for the party: once a version or the party's key set drops
it, 401. A build from before reads no token naming a key, as its claims
deny fields they don't know, so never takes a party's session for a
user's; and only the node that issued it takes it, as every token in a
federated swarm.
Diffstat (limited to 'src/api')
| -rw-r--r-- | src/api/access.rs | 62 | ||||
| -rw-r--r-- | src/api/login.rs | 81 | ||||
| -rw-r--r-- | src/api/mod.rs | 93 |
3 files changed, 176 insertions, 60 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 diff --git a/src/api/login.rs b/src/api/login.rs index 0658a69..ffcf9b8 100644 --- a/src/api/login.rs +++ b/src/api/login.rs @@ -8,6 +8,9 @@ //! * `POST /auth/challenge` and `POST /auth/login/key`, without a password: a challenge //! to sign with one of the user's keys, then the same token for it, signed //! (`access::challenge`). +//! * `POST /auth/login/party`, without a password, on a node holding a federated store: a +//! token as a party, for a challenge signed with one of the keys that sign for it there, +//! which does federation work alone (`login_party`). //! * `POST /auth/refresh`, with a token: a new one for the same login, signed by this //! node, optionally narrower still (`limit`, within the token's). //! * `POST /auth/logout`: every token of the user, of every login, no longer taken @@ -279,6 +282,7 @@ fn new_login( session_exp, login_exp: session_exp, limit, + party: None, } } @@ -322,6 +326,82 @@ pub(crate) struct KeyLogin { limit: Option<Vec<CapRequest>>, } +/// A party's login by one of its keys (`POST /auth/login/party`). +#[cfg(feature = "federation")] +#[derive(Deserialize)] +pub(crate) struct PartyLogin { + party: String, + challenge: String, + /// Armored, as `ssh-keygen -Y sign -n continuum-login` writes it, or its blob in base64. + signature: String, +} + +/// `POST /auth/login/party`: on a node holding a federated store, a token as `party` for a +/// challenge signed with one of the keys that sign for it there, as its current version +/// and its key set say (`fold::State::signing_keys`), as `ssh-keygen -Y sign -n +/// continuum-login` signs, an ECDSA signature's `s` low (`401` if not, saying why): a +/// party's session, which does federation work alone, while that key signs for the +/// party (`api::access::federated`). `409` on a node holding none. +#[cfg(feature = "federation")] +pub(crate) async fn login_party( + State(state): State<AppState>, + Json(request): Json<PartyLogin>, +) -> Result<Response, ApiError> { + let (swarm, now) = issuing(&state).await?; + let key = state + .node_key + .as_ref() + .ok_or(ApiError(StatusCode::SERVICE_UNAVAILABLE, None))?; + let party = request.party.as_str(); + let keys = state + .holding + .with(|store| { + let read = store.state(); + read.signing_keys(party, read.current()) + }) + .ok_or_else(|| { + ApiError( + StatusCode::CONFLICT, + Some("no federated store here, whose parties' keys log in".to_owned()), + ) + })?; + let signer = { + let mut used = state.challenges.lock_or_recover(); + let mut signer = None; + challenge::take_with( + key, + &request.challenge, + &format!("party {party}"), + now, + &mut used, + state.key_logins_per_user, + |challenge| { + if keys.is_empty() { + return Err(format!("{party} is no party of the current version")); + } + let found = crate::federation::keys::signer( + &keys, + challenge::NAMESPACE, + &request.signature, + challenge.as_bytes(), + ) + .ok_or_else(|| format!("signed by none of the keys that sign for {party}"))?; + signer = Some(found.to_owned()); + Ok(()) + }, + ) + .map_err(|why| { + debug!(party = %party, reason = %why, "Refused a party's login"); + ApiError(StatusCode::UNAUTHORIZED, Some(why)) + })?; + signer + }; + let mut claims = new_login(&state, swarm, now, party, "party", None, None); + claims.party = signer; + info!(party = %party, login = "party", session = %claims.session, "A party logged in"); + answer(&state, &claims) +} + /// `POST /auth/login/key`: a token, as `POST /auth/login` gives one, for a challenge /// signed by one of the user's keys (`401` if not, saying why); `409` in a swarm without /// a registry, which holds no keys. @@ -745,6 +825,7 @@ pub(crate) async fn whoami( "session": token.claims.session, "sessionExpiresAt": token.claims.session_exp, "limit": token.claims.limit, + "party": token.claims.party, }) }); Json(json!({ diff --git a/src/api/mod.rs b/src/api/mod.rs index d91c434..c4265fb 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -44,7 +44,8 @@ use crate::state::AppState; /// Builds the complete HTTP router. Everything except the API description (`/swagger-ui`, /// `/openapi.yaml`), `/health`, `/ready` and logging in by key (`/auth/challenge`, -/// `/auth/login/key`, which prove the user themselves) is authenticated. +/// `/auth/login/key`, `/auth/login/party`, which prove the user themselves) is +/// authenticated. pub(crate) fn router(state: AppState, auth_state: AuthState, debug_api: bool) -> Router { let mut api: Router<AppState> = Router::new() .route("/store", get(store::get_store_root)) @@ -118,48 +119,54 @@ pub(crate) fn router(state: AppState, auth_state: AuthState, debug_api: bool) -> } // Inside authentication, which finds the user: what the user may do. - api.layer(middleware::from_fn_with_state( - state.clone(), - access::authorize, - )) - .layer(middleware::from_fn_with_state( - auth_state.clone(), - basic_auth, - )) - // Who has a password here (`/auth/keys`: a user's last key goes only then). - .layer(axum::Extension(auth_state)) - // A stream answered to a token ends with it (expired, revoked, its session ended). - .layer(middleware::from_fn_with_state( - state.clone(), - login::streams_end, - )) - // Outside authentication: a token, if the request brings one, names the user. - .layer(middleware::from_fn_with_state(state.clone(), login::bearer)) - // Outermost: pages of other origins, and their preflights, which bring no - // credentials. - .layer(middleware::from_fn_with_state(state.clone(), cors::cors)) - // The hand-written spec (`docs/openapi.yaml`), and the Swagger UI showing it. - .merge( - // Relative to the UI's page (`/swagger-ui/`), so it is found behind a proxy that - // serves the API under a path of its own too. - SwaggerUi::new("/swagger-ui").config(utoipa_swagger_ui::Config::from("../openapi.yaml")), - ) - .route("/openapi.yaml", get(openapi_spec)) - // Logging in by key: a challenge, and the token for it signed, without a password. - .route("/auth/challenge", axum::routing::post(login::challenge)) - .route("/auth/login/key", axum::routing::post(login::login_key)) - .route("/health", get(health::node_health)) - // To log in to the dashboard on, and the dashboard's page, without authentication - // (they hold nothing). - .route("/login", get(dashboard_login)) - .route("/admin/dashboard", get(dashboard_page)) - .route("/ready", get(health::ready)) - // Outermost, so authentication failures get the error body too. - .layer(middleware::from_fn(json_errors)) - .layer(middleware::from_fn(relative_redirects)) - // A line per request, as it is answered. - .layer(middleware::from_fn(request_log::log_requests)) - .with_state(state) + let router = api + .layer(middleware::from_fn_with_state( + state.clone(), + access::authorize, + )) + .layer(middleware::from_fn_with_state( + auth_state.clone(), + basic_auth, + )) + // Who has a password here (`/auth/keys`: a user's last key goes only then). + .layer(axum::Extension(auth_state)) + // A stream answered to a token ends with it (expired, revoked, its session ended). + .layer(middleware::from_fn_with_state( + state.clone(), + login::streams_end, + )) + // Outside authentication: a token, if the request brings one, names the user. + .layer(middleware::from_fn_with_state(state.clone(), login::bearer)) + // Outermost: pages of other origins, and their preflights, which bring no + // credentials. + .layer(middleware::from_fn_with_state(state.clone(), cors::cors)) + // The hand-written spec (`docs/openapi.yaml`), and the Swagger UI showing it. + .merge( + // Relative to the UI's page (`/swagger-ui/`), so it is found behind a proxy that + // serves the API under a path of its own too. + SwaggerUi::new("/swagger-ui") + .config(utoipa_swagger_ui::Config::from("../openapi.yaml")), + ) + .route("/openapi.yaml", get(openapi_spec)) + // Logging in by key: a challenge, and the token for it signed, without a password. + .route("/auth/challenge", axum::routing::post(login::challenge)) + .route("/auth/login/key", axum::routing::post(login::login_key)); + // A party's login by its key, on a node holding a federated store. + #[cfg(feature = "federation")] + let router = router.route("/auth/login/party", axum::routing::post(login::login_party)); + router + .route("/health", get(health::node_health)) + // To log in to the dashboard on, and the dashboard's page, without authentication + // (they hold nothing). + .route("/login", get(dashboard_login)) + .route("/admin/dashboard", get(dashboard_page)) + .route("/ready", get(health::ready)) + // Outermost, so authentication failures get the error body too. + .layer(middleware::from_fn(json_errors)) + .layer(middleware::from_fn(relative_redirects)) + // A line per request, as it is answered. + .layer(middleware::from_fn(request_log::log_requests)) + .with_state(state) } /// The HTTP router of a node that only relays (`--relay-only`): its figures and the log |
