Skip to main content

subc_client_rs/
route_authorize.rs

1pub use subc_protocol::scope::{
2    RouteAuthorizeExpected, RouteAuthorizeOutcome, RouteAuthorizeResponse, RouteAuthorizeScope,
3};
4use subc_protocol::{scope::ROUTE_AUTHORIZE_OP, session::ModuleControlRequestFromModule};
5
6use crate::{ModuleControlReply, ModuleHandle, Principal, ScopeCallError};
7
8impl ModuleHandle {
9    /// Ask whether `principal` may still open `target` using the stored scope
10    /// and its expected claim (`route.authorize`). Requires a registered module
11    /// connection: the daemon permits only its configured federated owner with
12    /// `route-authorize/v1` in that module's registration.
13    ///
14    /// Judgments about the route, including refusal outcomes such as
15    /// `NotAuthorized`, are values in the `Ok` response. Daemon Error frames
16    /// instead refuse the call itself and return [`ScopeCallError::Refused`],
17    /// preserving their code and message. In particular,
18    /// `route_authorize_not_permitted` means the asking module may not use this
19    /// operation, not that the route is unauthorized. Transport, timeout,
20    /// unsupported operation, and malformed or unexpected replies are also call
21    /// errors. Without `route.authorize` in HELLO_ACK's `subc_ops`, nothing is sent.
22    ///
23    /// This is a point-in-time judgment, not a reservation: a later route open
24    /// may still be refused.
25    pub async fn route_authorize(
26        &self,
27        principal: Principal,
28        scope: RouteAuthorizeScope,
29        target: String,
30        expected: RouteAuthorizeExpected,
31    ) -> Result<RouteAuthorizeResponse, ScopeCallError> {
32        if !self.shared.supports_route_authorize {
33            return Err(ScopeCallError::NotSupported {
34                op: ROUTE_AUTHORIZE_OP,
35            });
36        }
37        let request = ModuleControlRequestFromModule::RouteAuthorize {
38            principal,
39            scope,
40            target,
41            expected,
42        };
43        match self.scope_call(ROUTE_AUTHORIZE_OP, &request).await? {
44            ModuleControlReply::RouteAuthorize(response) => Ok(response),
45            other => Err(ScopeCallError::Protocol(format!(
46                "unexpected {ROUTE_AUTHORIZE_OP} response: {other:?}"
47            ))),
48        }
49    }
50}