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        let shared = self.shared();
33        crate::live_upgrade::ScopeRequest::check_fence(&shared)?;
34        if !shared.supports_route_authorize {
35            return Err(ScopeCallError::NotSupported {
36                op: ROUTE_AUTHORIZE_OP,
37            });
38        }
39        let request = ModuleControlRequestFromModule::RouteAuthorize {
40            principal,
41            scope,
42            target,
43            expected,
44        };
45        match crate::live_upgrade::ScopeRequest::call(&shared, ROUTE_AUTHORIZE_OP, &request).await?
46        {
47            ModuleControlReply::RouteAuthorize(response) => Ok(response),
48            other => Err(ScopeCallError::Protocol(format!(
49                "unexpected {ROUTE_AUTHORIZE_OP} response: {other:?}"
50            ))),
51        }
52    }
53}