moq_ext_access/policy/verify.rs
1//! Policies that decide on the token a peer presents.
2
3use std::fmt;
4
5use super::{Closed, Decision, Event, PeerRequest, Policy};
6use crate::PeerId;
7
8/// A check of the token a peer presents, which returns what the token authorizes.
9///
10/// A verifier is synchronous, since checking a signature is CPU work. Wrap
11/// it in [`Verify`] to use it as a [`Policy`].
12pub trait TokenVerifier<P: PeerId>: fmt::Debug + Send + Sync + 'static {
13 /// Verifies `token` for the request's peer and returns what it authorizes.
14 ///
15 /// The token is an argument of its own, so a token that does not come
16 /// from the session's setup can go through the same verifier.
17 fn verify(&self, token: &str, request: &PeerRequest<P>) -> Decision;
18}
19
20/// A policy that decides with a [`TokenVerifier`] when the peer presents a token.
21///
22/// With a token, the verifier decides. A token it rejects is a rejection,
23/// and never falls through to the fallback, so a peer cannot get the
24/// fallback's rights with a forged token. Without a token, the fallback
25/// policy decides. The default fallback, [`Closed`], makes the token
26/// required.
27///
28/// One case falls through: when a node decides again about a live session
29/// and the verifier rejects its token with [`moq_net::Error::Expired`], the
30/// fallback decides. A session whose token expires thus narrows to the
31/// fallback's rights, or closes under [`Closed`]. A new session with an
32/// expired token is rejected.
33///
34/// A node that dials a peer decides with no token, so the fallback decides
35/// every dial, and under [`Closed`] the node can dial nobody.
36///
37/// The token is the one [`SessionRequest::token`](crate::SessionRequest::token)
38/// returns.
39///
40/// # Examples
41///
42/// Admit peers with a valid token, and let others subscribe:
43///
44/// ```
45/// use moq_ext_access::{
46/// Grant,
47/// policy::{self, Decision, PeerRequest, Rejection, TokenVerifier, Verify},
48/// };
49///
50/// #[derive(Debug)]
51/// struct Password;
52///
53/// impl TokenVerifier<String> for Password {
54/// fn verify(&self, token: &str, _request: &PeerRequest<String>) -> Decision {
55/// match token {
56/// "secret" => Ok(Grant::everything().into()),
57/// _ => Err(Rejection::new("wrong password")),
58/// }
59/// }
60/// }
61///
62/// let policy = Verify::new(Password).with_fallback(policy::Open(Grant::subscribe_only()));
63/// # drop(policy);
64/// ```
65#[derive(Debug, Clone)]
66pub struct Verify<V, F = Closed> {
67 verifier: V,
68 fallback: F,
69}
70
71impl<V> Verify<V> {
72 /// Creates a policy that requires a token, which `verifier` checks.
73 #[must_use]
74 pub fn new(verifier: V) -> Self {
75 Self {
76 verifier,
77 fallback: Closed,
78 }
79 }
80}
81
82impl<V, F> Verify<V, F> {
83 /// Returns this policy with `fallback` deciding the requests without a token.
84 #[must_use]
85 pub fn with_fallback<G>(self, fallback: G) -> Verify<V, G> {
86 Verify {
87 verifier: self.verifier,
88 fallback,
89 }
90 }
91}
92
93impl<P, V, F> Policy<P> for Verify<V, F>
94where
95 P: PeerId,
96 V: TokenVerifier<P>,
97 F: Policy<P>,
98{
99 async fn decide(&self, request: &PeerRequest<P>) -> Decision {
100 let Some(token) = request.token() else {
101 return self.fallback.decide(request).await;
102 };
103 match self.verifier.verify(token, request) {
104 Err(rejection)
105 if request.event() != Event::Connect
106 && matches!(rejection.code(), moq_net::Error::Expired) =>
107 {
108 self.fallback.decide(request).await
109 }
110 decision => decision,
111 }
112 }
113}