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::{ffi, Credentials, IOStream};
6use glib::{
7 object::ObjectType as _,
8 prelude::*,
9 signal::{connect_raw, SignalHandlerId},
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 let f: &F = &*(f as *const F);
181 f(
182 &from_glib_borrow(this),
183 &glib::GString::from_glib_borrow(mechanism),
184 )
185 .into_glib()
186 }
187 unsafe {
188 let f: Box_<F> = Box_::new(f);
189 connect_raw(
190 self.as_ptr() as *mut _,
191 b"allow-mechanism\0".as_ptr() as *const _,
192 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
193 allow_mechanism_trampoline::<F> as *const (),
194 )),
195 Box_::into_raw(f),
196 )
197 }
198 }
199
200 /// Emitted to check if a peer that is successfully authenticated
201 /// is authorized.
202 /// ## `stream`
203 /// A #GIOStream for the #GDBusConnection.
204 /// ## `credentials`
205 /// Credentials received from the peer or [`None`].
206 ///
207 /// # Returns
208 ///
209 /// [`true`] if the peer is authorized, [`false`] if not.
210 #[doc(alias = "authorize-authenticated-peer")]
211 pub fn connect_authorize_authenticated_peer<
212 F: Fn(&Self, &IOStream, Option<&Credentials>) -> bool + 'static,
213 >(
214 &self,
215 f: F,
216 ) -> SignalHandlerId {
217 unsafe extern "C" fn authorize_authenticated_peer_trampoline<
218 F: Fn(&DBusAuthObserver, &IOStream, Option<&Credentials>) -> bool + 'static,
219 >(
220 this: *mut ffi::GDBusAuthObserver,
221 stream: *mut ffi::GIOStream,
222 credentials: *mut ffi::GCredentials,
223 f: glib::ffi::gpointer,
224 ) -> glib::ffi::gboolean {
225 let f: &F = &*(f as *const F);
226 f(
227 &from_glib_borrow(this),
228 &from_glib_borrow(stream),
229 Option::<Credentials>::from_glib_borrow(credentials)
230 .as_ref()
231 .as_ref(),
232 )
233 .into_glib()
234 }
235 unsafe {
236 let f: Box_<F> = Box_::new(f);
237 connect_raw(
238 self.as_ptr() as *mut _,
239 b"authorize-authenticated-peer\0".as_ptr() as *const _,
240 Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
241 authorize_authenticated_peer_trampoline::<F> as *const (),
242 )),
243 Box_::into_raw(f),
244 )
245 }
246 }
247}
248
249impl Default for DBusAuthObserver {
250 fn default() -> Self {
251 Self::new()
252 }
253}