diff options
| -rw-r--r-- | docs/access.md | 3 | ||||
| -rw-r--r-- | src/access/token.rs | 43 | ||||
| -rw-r--r-- | src/api/login.rs | 6 |
3 files changed, 50 insertions, 2 deletions
diff --git a/docs/access.md b/docs/access.md index 26026e9..71725e2 100644 --- a/docs/access.md +++ b/docs/access.md @@ -118,7 +118,8 @@ curl -H "Authorization: Bearer ct1.…" http://other-node:3000/store * **Who signs it**: the node it logged in on, with its node key. Every node takes a token signed by a member of the swarm (the member list gives the keys: a node ID is - its key), and none signed by another or since forgotten. + its key), and none signed by another or since forgotten; in a swarm holding a store + ([`store.md`](store.md), Members), only one it signed itself. * **What it says**: who, how it proved it, when it ends, and optionally less than the user may do (`limit`, capabilities as `POST /admin/users/{name}` takes them): never what the user may do. Every node judges a request by the registry it holds then, so diff --git a/src/access/token.rs b/src/access/token.rs index 2aefcd7..54dd7f8 100644 --- a/src/access/token.rs +++ b/src/access/token.rs @@ -98,6 +98,9 @@ pub(crate) enum Refused { Revoked, #[error("its session was ended (logged out of)")] Ended, + #[error("issued by another node, which a swarm holding a store takes none of")] + #[cfg(feature = "federation")] + ElsewhereIssued, } /// The claims of `token` if this node takes it: well formed, for `swarm`, signed by a @@ -147,6 +150,21 @@ pub(crate) fn verify( Ok(claims) } +/// `claims`, of a token `verify` took, if this node, `local`, takes it: in a swarm holding a +/// store, only one it issued itself (`docs/store.md`, Members), as a node there is +/// whoever its operator lets in, and lets in nobody's but its own. +#[cfg(feature = "federation")] +pub(crate) fn issued_here( + doc: &impl ReadDoc, + local: &str, + claims: Claims, +) -> Result<Claims, Refused> { + match super::policy::of(doc) { + super::policy::Policy::Store(_) if claims.issuer != local => Err(Refused::ElsewhereIssued), + _ => Ok(claims), + } +} + /// Whether a token of `claims`, taken once (`verify`), still is by what `doc` says now: /// its issuer a member and not banned, its user registered, enabled, its tokens not /// revoked since and its session not ended. What a stream opened with it is judged by @@ -270,6 +288,31 @@ mod tests { serde_json::from_slice(&BASE64URL.decode(payload).unwrap()).unwrap() } + /// In a swarm holding a store, a node takes only the tokens it issued. + #[cfg(feature = "federation")] + #[test] + fn a_store_node_takes_its_own_tokens_only() { + let key = NodeKey::from_seed([1; 32]); + let other = NodeKey::from_seed([2; 32]); + let mut doc = crate::document::events::started("federation", b"a root").unwrap(); + member(&mut doc, &key); + member(&mut doc, &other); + let token = issue(&key, &claims(&key)); + let claims = verify(&doc, &token, "s", 1_500).unwrap(); + assert_eq!( + issued_here(&doc, &key.id(), claims.clone()), + Ok(claims.clone()) + ); + assert_eq!( + issued_here(&doc, &other.id(), claims.clone()), + Err(Refused::ElsewhereIssued) + ); + assert_eq!( + issued_here(&Document::new(), &other.id(), claims.clone()), + Ok(claims) + ); + } + #[test] fn only_members_issue_and_nobody_forges() { let key = NodeKey::from_seed([1; 32]); diff --git a/src/api/login.rs b/src/api/login.rs index c5cbe8d..0658a69 100644 --- a/src/api/login.rs +++ b/src/api/login.rs @@ -112,7 +112,11 @@ pub(crate) async fn bearer( let now = state.clock.now_millis().as_u64(); let verified = { let replica = state.doc.read().await; - token::verify(&replica.doc, &text, &swarm, now) + let verified = token::verify(&replica.doc, &text, &swarm, now); + #[cfg(feature = "federation")] + let verified = + verified.and_then(|claims| token::issued_here(&replica.doc, state.local_id(), claims)); + verified } .and_then(|claims| { // Ended here where the document can't record it (no registry). |
