iroh_live_node/policy.rs
1//! Policies that decide who may hold a session with a node, and with which rights.
2//!
3//! The types are [`moq_ext_access::policy`]'s, with the peer fixed to an
4//! iroh [`EndpointId`]. Set a node's policy with
5//! [`NodeConfig::with_policy`](crate::NodeConfig::with_policy). The node asks
6//! it when it accepts a session, after the peer's MoQ SETUP, and before it
7//! dials a peer. It decides each session on its own.
8//!
9//! To decide in the application, as a person who answers a call does, use a
10//! [`Queue`] and answer each [`Pending`] it yields. Raise
11//! [`NodeConfig::decide_timeout`](crate::NodeConfig::decide_timeout) then,
12//! since the node rejects a session whose decision takes longer.
13//!
14//! To admit the peers that present a token, wrap a [`TokenVerifier`] in a
15//! [`Verify`]. The `jwt` feature adds `JwtVerifier`, for moq-relay's JWTs,
16//! and the `rcan` feature adds `RcanVerifier`, for capability chains bound to
17//! the peer's key. A dial carries no token, so it gets the verifier's
18//! fallback: with the default [`Closed`], every dial of the node fails with
19//! [`Error::Rejected`](crate::Error::Rejected). Give a node that dials a
20//! fallback, or check `request.direction()` in it.
21//!
22//! # Examples
23//!
24//! Admit only known peers, and let each publish under its own id:
25//!
26//! ```
27//! use std::collections::BTreeSet;
28//!
29//! use iroh::EndpointId;
30//! use iroh_live_node::{
31//! Grant, NodeConfig,
32//! moq_net::Prefix,
33//! policy::{self, PeerRequest, Rejection},
34//! };
35//!
36//! let known: BTreeSet<EndpointId> = BTreeSet::new();
37//! let config =
38//! NodeConfig::default().with_policy(policy::from_fn(move |request: &PeerRequest| {
39//! let peer = request.peer();
40//! if !known.contains(peer) {
41//! return Err(Rejection::new("unknown peer"));
42//! }
43//! let own: Prefix = format!("live/{peer}").parse().expect("a prefix");
44//! Ok(Grant::publish_under(own).into())
45//! }));
46//! # drop(config);
47//! ```
48
49use iroh::EndpointId;
50#[cfg(feature = "jwt")]
51pub use moq_ext_access::policy::JwtVerifier;
52pub use moq_ext_access::policy::{
53 Authorization, Closed, Decision, DynPolicy, Event, Filter, FromFn, Open, Policy, Rejection,
54 TokenVerifier, Verify, from_fn,
55};
56#[cfg(feature = "rcan")]
57pub use moq_ext_access::policy::{
58 MAX_LINK_BYTES, MAX_LINKS, MoqCapability, RcanVerifier, encode_chain,
59};
60
61/// A peer's request for a session with this node, as a policy sees it.
62pub type PeerRequest = moq_ext_access::policy::PeerRequest<EndpointId>;
63
64/// A policy that hands each decision about a peer to the application.
65pub type Queue = moq_ext_access::policy::Queue<EndpointId>;
66
67/// A request about a peer, waiting in a [`Queue`] for the application's answer.
68pub type Pending = moq_ext_access::policy::Pending<EndpointId>;