gio/auto/dbus_auth_observer.rs
1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{Credentials, IOStream, ffi};
6use glib::{
7 object::ObjectType as _,
8 prelude::*,
9 signal::{SignalHandlerId, connect_raw},
10 translate::*,
11};
12use std::boxed::Box as Box_;
13
14glib::wrapper! {
15 /// `GDBusAuthObserver` provides a mechanism for participating
16 /// in how a [`DBusServer`][crate::DBusServer] (or a [`DBusConnection`][crate::DBusConnection])
17 /// authenticates remote peers.
18 ///
19 /// Simply instantiate a `GDBusAuthObserver` and connect to the
20 /// signals you are interested in. Note that new signals may be added
21 /// in the future.
22 ///
23 /// ## Controlling Authentication Mechanisms
24 ///
25 /// By default, a `GDBusServer` or server-side `GDBusConnection` will allow
26 /// any authentication mechanism to be used. If you only want to allow D-Bus
27 /// connections with the `EXTERNAL` mechanism, which makes use of credentials
28 /// passing and is the recommended mechanism for modern Unix platforms such
29 /// as Linux and the BSD family, you would use a signal handler like this:
30 ///
31 /// **⚠️ The following code is in c ⚠️**
32 ///
33 /// ```c
34 /// static gboolean
35 /// on_allow_mechanism (GDBusAuthObserver *observer,
36 /// const gchar *mechanism,
37 /// gpointer user_data)
38 /// {
39 /// if (g_strcmp0 (mechanism, "EXTERNAL") == 0)
40 /// {
41 /// return TRUE;
42 /// }
43 ///
44 /// return FALSE;
45 /// }
46 /// ```
47 ///
48 /// ## Controlling Authorization
49 ///
50 /// By default, a `GDBusServer` or server-side `GDBusConnection` will accept
51 /// connections from any successfully authenticated user (but not from
52 /// anonymous connections using the `ANONYMOUS` mechanism). If you only
53 /// want to allow D-Bus connections from processes owned by the same uid
54 /// as the server, since GLib 2.68, you should use the
55 /// `G_DBUS_SERVER_FLAGS_AUTHENTICATION_REQUIRE_SAME_USER` flag. It’s equivalent
56 /// to the following signal handler:
57 ///
58 /// **⚠️ The following code is in c ⚠️**
59 ///
60 /// ```c
61 /// static gboolean
62 /// on_authorize_authenticated_peer (GDBusAuthObserver *observer,
63 /// GIOStream *stream,
64 /// GCredentials *credentials,
65 /// gpointer user_data)
66 /// {
67 /// gboolean authorized;
68 ///
69 /// authorized = FALSE;
70 /// if (credentials != NULL)
71 /// {
72 /// GCredentials *own_credentials;
73 /// own_credentials = g_credentials_new ();
74 /// if (g_credentials_is_same_user (credentials, own_credentials, NULL))
75 /// authorized = TRUE;
76 /// g_object_unref (own_credentials);
77 /// }
78 ///
79 /// return authorized;
80 /// }
81 /// ```
82 ///
83 /// ## Signals
84 ///
85 ///
86 /// #### `allow-mechanism`
87 /// Emitted to check if @mechanism is allowed to be used.
88 ///
89 ///
90 ///
91 ///
92 /// #### `authorize-authenticated-peer`
93 /// Emitted to check if a peer that is successfully authenticated
94 /// is authorized.
95 ///
96 ///
97 ///
98 /// # Implements
99 ///
100 /// [`trait@glib::ObjectExt`]
101 #[doc(alias = "GDBusAuthObserver")]
102 pub struct DBusAuthObserver(Object<ffi::GDBusAuthObserver>);
103
104 match fn {
105 type_ => || ffi::g_dbus_auth_observer_get_type(),
106 }
107}
108
109impl DBusAuthObserver {
110 /// Creates a new #GDBusAuthObserver object.
111 ///
112 /// # Returns
113 ///
114 /// A #GDBusAuthObserver. Free with g_object_unref().
115 #[doc(alias = "g_dbus_auth_observer_new")]
116 pub fn new() -> DBusAuthObserver {
117 unsafe { from_glib_full(ffi::g_dbus_auth_observer_new()) }
118 }
119
120 /// Emits the #GDBusAuthObserver::allow-mechanism signal on @self.
121 /// ## `mechanism`
122 /// The name of the mechanism, e.g. `DBUS_COOKIE_SHA1`.
123 ///
124 /// # Returns
125 ///
126 /// [`true`] if @mechanism can be used to authenticate the other peer, [`false`] if not.
127 #[doc(alias = "g_dbus_auth_observer_allow_mechanism")]
128 pub fn allow_mechanism(&self, mechanism: &str) -> bool {
129 unsafe {
130 from_glib(ffi::g_dbus_auth_observer_allow_mechanism(
131 self.to_glib_none().0,
132 mechanism.to_glib_none().0,
133 ))
134 }
135 }
136
137 /// Emits the #GDBusAuthObserver::authorize-authenticated-peer signal on @self.
138 /// ## `stream`
139 /// A #GIOStream for the #GDBusConnection.
140 /// ## `credentials`
141 /// Credentials received from the peer or [`None`].
142 ///
143 /// # Returns
144 ///
145 /// [`true`] if the peer is authorized, [`false`] if not.
146 #[doc(alias = "g_dbus_auth_observer_authorize_authenticated_peer")]
147 pub fn authorize_authenticated_peer(
148 &self,
149 stream: &impl IsA<IOStream>,
150 credentials: Option<&Credentials>,
151 ) -> bool {
152 unsafe {
153 from_glib(ffi::g_dbus_auth_observer_authorize_authenticated_peer(
154 self.to_glib_none().0,
155 stream.as_ref().to_glib_none().0,
156 credentials.to_glib_none().0,
157 ))
158 }
159 }
160
161 /// Emitted to check if @mechanism is allowed to be used.
162 /// ## `mechanism`
163 /// The name of the mechanism, e.g. `DBUS_COOKIE_SHA1`.
164 ///
165 /// # Returns
166 ///
167 /// [`true`] if @mechanism can be used to authenticate the other peer, [`false`] if not.
168 #[doc(alias = "allow-mechanism")]
169 pub fn connect_allow_mechanism<F: Fn(&Self, &str) -> bool + 'static>(
170 &self,
171 f: F,
172 ) -> SignalHandlerId {
173 unsafe extern "C" fn allow_mechanism_trampoline<
174 F: Fn(&DBusAuthObserver, &str) -> bool + 'static,
175 >(
176 this: *mut ffi::GDBusAuthObserver,
177 mechanism: *mut std::ffi::c_char,
178 f: glib::ffi::gpointer,
179 ) -> glib::ffi::gboolean {
180 unsafe {
181 let f: &F = &*(f as *const F);
182 f(
183 &from_glib_borrow(this),
184 &glib::GString::from_glib_borrow(mechanism),
185 )
186 .into_glib()
187 }
188 }
189 unsafe {
190 let f: Box_<F> = Box_::new(f);
191 connect_raw(
192 self.as_ptr() as *mut _,
193 c"allow-mechanism".as_ptr() as *const _,
194 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
195 allow_mechanism_trampoline::<F> as *const (),
196 )),
197 Box_::into_raw(f),
198 )
199 }
200 }
201
202 /// Emitted to check if a peer that is successfully authenticated
203 /// is authorized.
204 /// ## `stream`
205 /// A #GIOStream for the #GDBusConnection.
206 /// ## `credentials`
207 /// Credentials received from the peer or [`None`].
208 ///
209 /// # Returns
210 ///
211 /// [`true`] if the peer is authorized, [`false`] if not.
212 #[doc(alias = "authorize-authenticated-peer")]
213 pub fn connect_authorize_authenticated_peer<
214 F: Fn(&Self, &IOStream, Option<&Credentials>) -> bool + 'static,
215 >(
216 &self,
217 f: F,
218 ) -> SignalHandlerId {
219 unsafe extern "C" fn authorize_authenticated_peer_trampoline<
220 F: Fn(&DBusAuthObserver, &IOStream, Option<&Credentials>) -> bool + 'static,
221 >(
222 this: *mut ffi::GDBusAuthObserver,
223 stream: *mut ffi::GIOStream,
224 credentials: *mut ffi::GCredentials,
225 f: glib::ffi::gpointer,
226 ) -> glib::ffi::gboolean {
227 unsafe {
228 let f: &F = &*(f as *const F);
229 f(
230 &from_glib_borrow(this),
231 &from_glib_borrow(stream),
232 Option::<Credentials>::from_glib_borrow(credentials)
233 .as_ref()
234 .as_ref(),
235 )
236 .into_glib()
237 }
238 }
239 unsafe {
240 let f: Box_<F> = Box_::new(f);
241 connect_raw(
242 self.as_ptr() as *mut _,
243 c"authorize-authenticated-peer".as_ptr() as *const _,
244 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
245 authorize_authenticated_peer_trampoline::<F> as *const (),
246 )),
247 Box_::into_raw(f),
248 )
249 }
250 }
251}
252
253impl Default for DBusAuthObserver {
254 fn default() -> Self {
255 Self::new()
256 }
257}