Skip to main content

rightkit_daemon/contract/
caller.rs

1use crate::peer::PeerIdentity;
2use std::collections::BTreeSet;
3
4/// A capability a device holds (from enrolment). A plain label compared for equality.
5pub type Scope = String;
6
7/// Who is making a request.
8#[derive(Debug, Clone, PartialEq, Eq)]
9pub enum Caller {
10    /// A local app on this machine. `app` is the label declared in `hello` (a claim, an allowlist
11    /// key); `peer` is what the OS verified about the other end.
12    LocalApp { app: String, peer: PeerIdentity },
13    /// A remote device identified by its id, with the scopes it was enrolled with.
14    Device { id: String, scopes: BTreeSet<Scope> },
15}
16
17impl Caller {
18    pub fn local_app(app: impl Into<String>, peer: PeerIdentity) -> Self {
19        Caller::LocalApp {
20            app: app.into(),
21            peer,
22        }
23    }
24
25    pub fn device<I, S>(id: impl Into<String>, scopes: I) -> Self
26    where
27        I: IntoIterator<Item = S>,
28        S: Into<Scope>,
29    {
30        Caller::Device {
31            id: id.into(),
32            scopes: scopes.into_iter().map(Into::into).collect(),
33        }
34    }
35
36    /// The OS-verified peer, for a local caller only.
37    pub fn peer(&self) -> Option<&PeerIdentity> {
38        match self {
39            Caller::LocalApp { peer, .. } => Some(peer),
40            Caller::Device { .. } => None,
41        }
42    }
43
44    /// The app label for a local caller, the device id for a device.
45    pub fn label(&self) -> &str {
46        match self {
47            Caller::LocalApp { app, .. } => app,
48            Caller::Device { id, .. } => id,
49        }
50    }
51}