gardesk/garwarp / 47ba17e

Browse files

derive caller from dbus header

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
47ba17eab2ddabedd3fe5ee5d69ee39088a6ed3a
Parents
a3b9db6
Tree
8091f4f

1 changed file

StatusFile+-
M garwarp/src/dbus.rs 94 13
garwarp/src/dbus.rsmodified
@@ -3,10 +3,12 @@ use std::collections::HashMap;
33
 use zbus::{
44
     blocking::Connection,
55
     interface,
6
+    message::Header,
67
     zvariant::{OwnedObjectPath, OwnedValue},
78
 };
89
 
9
-use crate::error::PortalResponseCode;
10
+use crate::error::{PortalError, map_portal_error};
11
+use crate::portal::derive_request_id_from_handle;
1012
 
1113
 pub const BACKEND_DBUS_NAME: &str = "org.freedesktop.impl.portal.desktop.garwarp";
1214
 pub const BACKEND_OBJECT_PATH: &str = "/org/freedesktop/portal/desktop";
@@ -14,7 +16,24 @@ const INTERFACE_VERSION: u32 = 1;
1416
 type PortalMethodReply = (u32, HashMap<String, OwnedValue>);
1517
 
1618
 fn failed_response() -> PortalMethodReply {
17
-    (PortalResponseCode::Failed as u32, HashMap::new())
19
+    failure_response(&PortalError::InternalFailure)
20
+}
21
+
22
+fn failure_response(error: &PortalError) -> PortalMethodReply {
23
+    let mapping = map_portal_error(error);
24
+    (mapping.code as u32, HashMap::new())
25
+}
26
+
27
+fn request_id_for_call(
28
+    request_handle: &OwnedObjectPath,
29
+    header: Header<'_>,
30
+) -> Result<String, PortalError> {
31
+    let sender = header
32
+        .sender()
33
+        .map(|sender| sender.as_str())
34
+        .ok_or(PortalError::UnauthorizedClient)?;
35
+
36
+    derive_request_id_from_handle(sender, request_handle.as_str())
1837
 }
1938
 
2039
 pub struct SessionNameGuard {
@@ -44,21 +63,29 @@ struct ScreenshotPortal;
4463
 impl ScreenshotPortal {
4564
     fn screenshot(
4665
         &self,
47
-        _handle: OwnedObjectPath,
66
+        handle: OwnedObjectPath,
4867
         _app_id: &str,
4968
         _parent_window: &str,
5069
         _options: HashMap<String, OwnedValue>,
70
+        #[zbus(header)] header: Header<'_>,
5171
     ) -> PortalMethodReply {
72
+        if let Err(error) = request_id_for_call(&handle, header) {
73
+            return failure_response(&error);
74
+        }
5275
         failed_response()
5376
     }
5477
 
5578
     fn pick_color(
5679
         &self,
57
-        _handle: OwnedObjectPath,
80
+        handle: OwnedObjectPath,
5881
         _app_id: &str,
5982
         _parent_window: &str,
6083
         _options: HashMap<String, OwnedValue>,
84
+        #[zbus(header)] header: Header<'_>,
6185
     ) -> PortalMethodReply {
86
+        if let Err(error) = request_id_for_call(&handle, header) {
87
+            return failure_response(&error);
88
+        }
6289
         failed_response()
6390
     }
6491
 
@@ -75,34 +102,46 @@ struct FileChooserPortal;
75102
 impl FileChooserPortal {
76103
     fn open_file(
77104
         &self,
78
-        _handle: OwnedObjectPath,
105
+        handle: OwnedObjectPath,
79106
         _app_id: &str,
80107
         _parent_window: &str,
81108
         _title: &str,
82109
         _options: HashMap<String, OwnedValue>,
110
+        #[zbus(header)] header: Header<'_>,
83111
     ) -> PortalMethodReply {
112
+        if let Err(error) = request_id_for_call(&handle, header) {
113
+            return failure_response(&error);
114
+        }
84115
         failed_response()
85116
     }
86117
 
87118
     fn save_file(
88119
         &self,
89
-        _handle: OwnedObjectPath,
120
+        handle: OwnedObjectPath,
90121
         _app_id: &str,
91122
         _parent_window: &str,
92123
         _title: &str,
93124
         _options: HashMap<String, OwnedValue>,
125
+        #[zbus(header)] header: Header<'_>,
94126
     ) -> PortalMethodReply {
127
+        if let Err(error) = request_id_for_call(&handle, header) {
128
+            return failure_response(&error);
129
+        }
95130
         failed_response()
96131
     }
97132
 
98133
     fn save_files(
99134
         &self,
100
-        _handle: OwnedObjectPath,
135
+        handle: OwnedObjectPath,
101136
         _app_id: &str,
102137
         _parent_window: &str,
103138
         _title: &str,
104139
         _options: HashMap<String, OwnedValue>,
140
+        #[zbus(header)] header: Header<'_>,
105141
     ) -> PortalMethodReply {
142
+        if let Err(error) = request_id_for_call(&handle, header) {
143
+            return failure_response(&error);
144
+        }
106145
         failed_response()
107146
     }
108147
 
@@ -119,16 +158,29 @@ struct AppChooserPortal;
119158
 impl AppChooserPortal {
120159
     fn choose_application(
121160
         &self,
122
-        _handle: OwnedObjectPath,
161
+        handle: OwnedObjectPath,
123162
         _app_id: &str,
124163
         _parent_window: &str,
125164
         _choices: Vec<String>,
126165
         _options: HashMap<String, OwnedValue>,
166
+        #[zbus(header)] header: Header<'_>,
127167
     ) -> PortalMethodReply {
168
+        if let Err(error) = request_id_for_call(&handle, header) {
169
+            return failure_response(&error);
170
+        }
128171
         failed_response()
129172
     }
130173
 
131
-    fn update_choices(&self, _handle: OwnedObjectPath, _choices: Vec<String>) {}
174
+    fn update_choices(
175
+        &self,
176
+        handle: OwnedObjectPath,
177
+        _choices: Vec<String>,
178
+        #[zbus(header)] header: Header<'_>,
179
+    ) -> zbus::fdo::Result<()> {
180
+        request_id_for_call(&handle, header)
181
+            .map(|_| ())
182
+            .map_err(|error| zbus::fdo::Error::Failed(map_portal_error(&error).reason.to_string()))
183
+    }
132184
 
133185
     #[zbus(property)]
134186
     fn version(&self) -> u32 {
@@ -145,7 +197,10 @@ mod tests {
145197
         ScreenshotPortal,
146198
     };
147199
     use crate::error::PortalResponseCode;
148
-    use zbus::zvariant::{OwnedObjectPath, OwnedValue};
200
+    use zbus::{
201
+        message::{Header, Message},
202
+        zvariant::{OwnedObjectPath, OwnedValue},
203
+    };
149204
 
150205
     #[test]
151206
     fn backend_object_path_is_portal_desktop_path() {
@@ -167,12 +222,18 @@ mod tests {
167222
             "org.test.App",
168223
             "x11:0x2a",
169224
             options.clone(),
225
+            test_call_header(),
170226
         );
171227
         assert_eq!(response, PortalResponseCode::Failed as u32);
172228
         assert!(results.is_empty());
173229
 
174
-        let (response, results) =
175
-            ScreenshotPortal.pick_color(request_handle_path(), "org.test.App", "", options);
230
+        let (response, results) = ScreenshotPortal.pick_color(
231
+            request_handle_path(),
232
+            "org.test.App",
233
+            "",
234
+            options,
235
+            test_call_header(),
236
+        );
176237
         assert_eq!(response, PortalResponseCode::Failed as u32);
177238
         assert!(results.is_empty());
178239
     }
@@ -186,6 +247,7 @@ mod tests {
186247
             "",
187248
             "Open",
188249
             options.clone(),
250
+            test_call_header(),
189251
         );
190252
         assert_eq!(response, PortalResponseCode::Failed as u32);
191253
         assert!(results.is_empty());
@@ -196,6 +258,7 @@ mod tests {
196258
             "",
197259
             "Save",
198260
             options.clone(),
261
+            test_call_header(),
199262
         );
200263
         assert_eq!(response, PortalResponseCode::Failed as u32);
201264
         assert!(results.is_empty());
@@ -206,6 +269,7 @@ mod tests {
206269
             "",
207270
             "Save",
208271
             options,
272
+            test_call_header(),
209273
         );
210274
         assert_eq!(response, PortalResponseCode::Failed as u32);
211275
         assert!(results.is_empty());
@@ -221,15 +285,32 @@ mod tests {
221285
             "",
222286
             choices.clone(),
223287
             options,
288
+            test_call_header(),
224289
         );
225290
         assert_eq!(response, PortalResponseCode::Failed as u32);
226291
         assert!(results.is_empty());
227292
 
228
-        AppChooserPortal.update_choices(request_handle_path(), choices);
293
+        let error = AppChooserPortal
294
+            .update_choices(request_handle_path(), choices, test_call_header())
295
+            .expect_err("update choices should reject missing sender metadata");
296
+        match error {
297
+            zbus::fdo::Error::Failed(reason) => assert_eq!(reason, "unauthorized_client"),
298
+            other => panic!("unexpected update choices error: {other:?}"),
299
+        }
229300
     }
230301
 
231302
     fn request_handle_path() -> OwnedObjectPath {
232303
         OwnedObjectPath::try_from("/org/freedesktop/portal/desktop/request/1_42/token_1")
233304
             .expect("valid request object path")
234305
     }
306
+
307
+    fn test_call_header() -> Header<'static> {
308
+        let call = Message::method_call(BACKEND_OBJECT_PATH, "TestCall")
309
+            .expect("test call builder")
310
+            .interface("org.freedesktop.impl.portal.Screenshot")
311
+            .expect("test interface")
312
+            .build(&())
313
+            .expect("test message");
314
+        Box::leak(Box::new(call)).header()
315
+    }
235316
 }