Skip to main content

core_api/host/
permissions.rs

1//! OS authorization for one computer's Host and the services it runs; independent
2//! of any Space.
3//!
4//! Service shells declare requirements and own permission policy. Observations
5//! come from the relevant OS authorization context. A documented shared grant
6//! may be observed by the shell for its Core helper; an unrelated context needs
7//! its own observation. Always retain the actual observer's PID.
8//! Host reports one row per component and permission. Reading a report must never request authorization.
9//! A previous access probe is historical evidence, not a current OS setting.
10use serde::{Deserialize, Serialize};
11use std::collections::BTreeMap;
12
13pub type PlatformPermissionRequirements = BTreeMap<String, Vec<PermissionRequirement>>;
14
15/// Automation is authorized per target application, not as one global switch.
16#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
17#[serde(tag = "id", rename_all = "camelCase", deny_unknown_fields)]
18pub enum PermissionKey {
19    LocalNetwork {},
20    Bluetooth {},
21    Microphone {},
22    Reminders {},
23    Automation {
24        #[serde(rename = "targetBundleId")]
25        target_bundle_id: String,
26    },
27}
28
29#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
30#[serde(rename_all = "camelCase", deny_unknown_fields)]
31pub struct PermissionRequirement {
32    pub permission: PermissionKey,
33    /// Startup requirements fence the whole service's business runtime.
34    #[serde(default)]
35    pub enforcement: PermissionEnforcement,
36    /// Human-readable affected capability.
37    pub feature: String,
38    pub reason: String,
39}
40
41#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
42#[serde(rename_all = "camelCase")]
43pub enum PermissionEnforcement {
44    #[default]
45    Startup,
46    /// Checked by the target operation, without holding unrelated operations.
47    /// Used for Apple Events, whose target application need not be running.
48    Operation,
49}
50
51/// Service-level admission decision. Individual observations remain the actual
52/// executor's evidence, rather than a synthetic OS authorization state.
53#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
54#[serde(rename_all = "camelCase", deny_unknown_fields)]
55pub struct ServicePermissionState {
56    pub ready: bool,
57    pub reason: Option<String>,
58    pub blocked: Vec<PermissionKey>,
59}
60
61#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
62#[serde(rename_all = "camelCase")]
63pub enum PermissionState {
64    Unknown,
65    NotDetermined,
66    Granted,
67    /// This platform has no per-application consent for this resource. This says
68    /// nothing about device, account, session, sandbox or system-policy access.
69    NotRequired,
70    Denied,
71    Restricted,
72    Unsupported,
73}
74
75#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
76#[serde(rename_all = "camelCase")]
77pub enum PermissionEvidence {
78    /// A passive OS authorization query, not hardware availability.
79    System,
80    /// Platform semantics, e.g. Linux has no TCC consent for Bluetooth.
81    /// This is not evidence that an operation or device is available.
82    Platform,
83    /// A previous explicit request/probe; observedAtMs is mandatory.
84    Probe,
85    Unavailable,
86}
87
88#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
89#[serde(rename_all = "camelCase")]
90pub enum PermissionAction {
91    Request,
92    OpenSettings,
93}
94
95#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
96#[serde(rename_all = "camelCase", deny_unknown_fields)]
97pub struct PermissionObservation {
98    pub permission: PermissionKey,
99    /// PID of the process where this observation was made, not its OS grant owner.
100    pub process_id: u32,
101    pub state: PermissionState,
102    pub evidence: PermissionEvidence,
103    pub observed_at_ms: Option<i64>,
104    pub error: Option<String>,
105}
106
107impl PermissionObservation {
108    pub fn is_satisfied(&self) -> bool {
109        self.process_id != 0
110            && self.error.is_none()
111            && self.evidence != PermissionEvidence::Unavailable
112            && (self.evidence != PermissionEvidence::Probe || self.observed_at_ms.is_some())
113            && matches!(
114                self.state,
115                PermissionState::Granted | PermissionState::NotRequired
116            )
117    }
118}
119
120#[derive(Debug, Clone, Serialize, Deserialize)]
121#[serde(rename_all = "camelCase", deny_unknown_fields)]
122pub struct ServicePermissions {
123    /// Reporting service PID; individual observations may come from its sidecar.
124    pub process_id: u32,
125    pub permissions: Vec<PermissionObservation>,
126}
127
128#[derive(Debug, Clone, Serialize, Deserialize)]
129#[serde(rename_all = "camelCase", deny_unknown_fields)]
130pub struct HostPermission {
131    pub component_id: String,
132    pub component_name: String,
133    pub permission: PermissionKey,
134    pub enforcement: PermissionEnforcement,
135    pub feature: String,
136    pub reason: String,
137    /// None means that the actual executor could not be observed.
138    pub observation: Option<PermissionObservation>,
139    /// Collection failure, distinct from an observed OS denial.
140    pub error: Option<String>,
141    /// Mechanisms supported locally; remote callers can only read status.
142    pub supported_actions: Vec<PermissionAction>,
143}
144
145impl HostPermission {
146    pub fn is_satisfied(&self) -> bool {
147        self.error.is_none()
148            && self.observation.as_ref().is_some_and(|observation| {
149                observation.permission == self.permission && observation.is_satisfied()
150            })
151    }
152}
153
154#[derive(Debug, Clone, Serialize, Deserialize)]
155#[serde(rename_all = "camelCase", deny_unknown_fields)]
156pub struct PermissionDeclarationError {
157    pub component_id: String,
158    pub message: String,
159}
160
161#[derive(Debug, Clone, Serialize, Deserialize)]
162#[serde(rename_all = "camelCase", deny_unknown_fields)]
163pub struct HostPermissions {
164    pub host_id: String,
165    pub host_version: String,
166    pub platform: String,
167    pub observed_at_ms: i64,
168    pub permissions: Vec<HostPermission>,
169    /// Missing, malformed or unsupported declarations must remain visible.
170    /// An empty permission list is not proof that the inventory is complete.
171    pub declaration_errors: Vec<PermissionDeclarationError>,
172}
173
174/// Local-control mutation. A Hub may later relay `GET /v1/permissions`; it must
175/// not relay this request, and it does not store or decide the grant. The server
176/// must verify the local control credential and Host identity. This DTO has no
177/// caller-controlled isLocal flag or arbitrary Settings URL. The component
178/// selects an installed service (or "host"); the key selects its permission.
179/// The component is always explicit.
180#[derive(Debug, Clone, Serialize, Deserialize)]
181#[serde(rename_all = "camelCase", deny_unknown_fields)]
182pub struct HostPermissionRequest {
183    pub host_id: String,
184    pub component_id: String,
185    pub permission: PermissionKey,
186}
187
188pub const HOST_PERMISSIONS_PATH: &str = "/v1/permissions";
189pub const HOST_PERMISSION_REQUEST_PATH: &str = "/internal/permissions/request";
190pub const HOST_PERMISSION_SETTINGS_PATH: &str = "/internal/permissions/open-settings";