aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/access/token.rs43
-rw-r--r--src/api/login.rs6
2 files changed, 48 insertions, 1 deletions
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).