1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// from gst-gir-files (https://gitlab.freedesktop.org/gstreamer/gir-files-rs.git)
// DO NOT EDIT

use crate::{ffi, RTSPToken};
use glib::{
    prelude::*,
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::boxed::Box as Box_;

glib::wrapper! {
    /// The authentication structure.
    ///
    /// ## Signals
    ///
    ///
    /// #### `accept-certificate`
    ///  Emitted during the TLS handshake after the client certificate has
    /// been received. See also [`RTSPAuthExt::set_tls_authentication_mode()`][crate::prelude::RTSPAuthExt::set_tls_authentication_mode()].
    ///
    ///
    ///
    /// # Implements
    ///
    /// [`RTSPAuthExt`][trait@crate::prelude::RTSPAuthExt], [`trait@glib::ObjectExt`], [`RTSPAuthExtManual`][trait@crate::prelude::RTSPAuthExtManual]
    #[doc(alias = "GstRTSPAuth")]
    pub struct RTSPAuth(Object<ffi::GstRTSPAuth, ffi::GstRTSPAuthClass>);

    match fn {
        type_ => || ffi::gst_rtsp_auth_get_type(),
    }
}

impl RTSPAuth {
    pub const NONE: Option<&'static RTSPAuth> = None;

    /// Create a new [`RTSPAuth`][crate::RTSPAuth] instance.
    ///
    /// # Returns
    ///
    /// a new [`RTSPAuth`][crate::RTSPAuth]
    #[doc(alias = "gst_rtsp_auth_new")]
    pub fn new() -> RTSPAuth {
        assert_initialized_main_thread!();
        unsafe { from_glib_full(ffi::gst_rtsp_auth_new()) }
    }

    /// Check if `check` is allowed in the current context.
    /// ## `check`
    /// the item to check
    ///
    /// # Returns
    ///
    /// FALSE if check failed.
    #[doc(alias = "gst_rtsp_auth_check")]
    pub fn check(check: &str) -> Result<(), glib::error::BoolError> {
        assert_initialized_main_thread!();
        unsafe {
            glib::result_from_gboolean!(
                ffi::gst_rtsp_auth_check(check.to_glib_none().0),
                "Check failed"
            )
        }
    }

    /// Construct a Basic authorisation token from `user` and `pass`.
    /// ## `user`
    /// a userid
    /// ## `pass`
    /// a password
    ///
    /// # Returns
    ///
    /// the base64 encoding of the string `user`:`pass`.
    /// `g_free()` after usage.
    #[doc(alias = "gst_rtsp_auth_make_basic")]
    pub fn make_basic(user: &str, pass: &str) -> glib::GString {
        assert_initialized_main_thread!();
        unsafe {
            from_glib_full(ffi::gst_rtsp_auth_make_basic(
                user.to_glib_none().0,
                pass.to_glib_none().0,
            ))
        }
    }
}

impl Default for RTSPAuth {
    fn default() -> Self {
        Self::new()
    }
}

unsafe impl Send for RTSPAuth {}
unsafe impl Sync for RTSPAuth {}

mod sealed {
    pub trait Sealed {}
    impl<T: super::IsA<super::RTSPAuth>> Sealed for T {}
}

/// Trait containing all [`struct@RTSPAuth`] methods.
///
/// # Implementors
///
/// [`RTSPAuth`][struct@crate::RTSPAuth]
pub trait RTSPAuthExt: IsA<RTSPAuth> + sealed::Sealed + 'static {
    /// Add a basic token for the default authentication algorithm that
    /// enables the client with privileges listed in `token`.
    /// ## `basic`
    /// the basic token
    /// ## `token`
    /// authorisation token
    #[doc(alias = "gst_rtsp_auth_add_basic")]
    fn add_basic(&self, basic: &str, token: &RTSPToken) {
        unsafe {
            ffi::gst_rtsp_auth_add_basic(
                self.as_ref().to_glib_none().0,
                basic.to_glib_none().0,
                token.to_glib_none().0,
            );
        }
    }

    /// Add a digest `user` and `pass` for the default authentication algorithm that
    /// enables the client with privileges listed in `token`.
    /// ## `user`
    /// the digest user name
    /// ## `pass`
    /// the digest password
    /// ## `token`
    /// authorisation token
    #[doc(alias = "gst_rtsp_auth_add_digest")]
    fn add_digest(&self, user: &str, pass: &str, token: &RTSPToken) {
        unsafe {
            ffi::gst_rtsp_auth_add_digest(
                self.as_ref().to_glib_none().0,
                user.to_glib_none().0,
                pass.to_glib_none().0,
                token.to_glib_none().0,
            );
        }
    }

    /// Get the default token for `self`. This token will be used for unauthenticated
    /// users.
    ///
    /// # Returns
    ///
    /// the [`RTSPToken`][crate::RTSPToken] of `self`. `gst_rtsp_token_unref()` after
    /// usage.
    #[doc(alias = "gst_rtsp_auth_get_default_token")]
    #[doc(alias = "get_default_token")]
    fn default_token(&self) -> Option<RTSPToken> {
        unsafe {
            from_glib_full(ffi::gst_rtsp_auth_get_default_token(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    ///
    /// # Returns
    ///
    /// the `realm` of `self`
    #[cfg(feature = "v1_16")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
    #[doc(alias = "gst_rtsp_auth_get_realm")]
    #[doc(alias = "get_realm")]
    fn realm(&self) -> Option<glib::GString> {
        unsafe { from_glib_full(ffi::gst_rtsp_auth_get_realm(self.as_ref().to_glib_none().0)) }
    }

    /// Gets the supported authentication methods of `self`.
    ///
    /// # Returns
    ///
    /// The supported authentication methods
    #[doc(alias = "gst_rtsp_auth_get_supported_methods")]
    #[doc(alias = "get_supported_methods")]
    fn supported_methods(&self) -> gst_rtsp::RTSPAuthMethod {
        unsafe {
            from_glib(ffi::gst_rtsp_auth_get_supported_methods(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Get the [`gio::TlsAuthenticationMode`][crate::gio::TlsAuthenticationMode].
    ///
    /// # Returns
    ///
    /// the [`gio::TlsAuthenticationMode`][crate::gio::TlsAuthenticationMode].
    #[doc(alias = "gst_rtsp_auth_get_tls_authentication_mode")]
    #[doc(alias = "get_tls_authentication_mode")]
    fn tls_authentication_mode(&self) -> gio::TlsAuthenticationMode {
        unsafe {
            from_glib(ffi::gst_rtsp_auth_get_tls_authentication_mode(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Get the [`gio::TlsCertificate`][crate::gio::TlsCertificate] used for negotiating TLS `self`.
    ///
    /// # Returns
    ///
    /// the [`gio::TlsCertificate`][crate::gio::TlsCertificate] of `self`. `g_object_unref()` after
    /// usage.
    #[doc(alias = "gst_rtsp_auth_get_tls_certificate")]
    #[doc(alias = "get_tls_certificate")]
    fn tls_certificate(&self) -> Option<gio::TlsCertificate> {
        unsafe {
            from_glib_full(ffi::gst_rtsp_auth_get_tls_certificate(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Get the [`gio::TlsDatabase`][crate::gio::TlsDatabase] used for verifying client certificate.
    ///
    /// # Returns
    ///
    /// the [`gio::TlsDatabase`][crate::gio::TlsDatabase] of `self`. `g_object_unref()` after
    /// usage.
    #[doc(alias = "gst_rtsp_auth_get_tls_database")]
    #[doc(alias = "get_tls_database")]
    fn tls_database(&self) -> Option<gio::TlsDatabase> {
        unsafe {
            from_glib_full(ffi::gst_rtsp_auth_get_tls_database(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Parse the contents of the file at `path` and enable the privileges
    /// listed in `token` for the users it describes.
    ///
    /// The format of the file is expected to match the format described by
    /// <https://en.wikipedia.org/wiki/Digest_access_authentication`The_.htdigest_file`>,
    /// as output by the `htdigest` command.
    /// ## `path`
    /// Path to the htdigest file
    /// ## `token`
    /// authorisation token
    ///
    /// # Returns
    ///
    /// [`true`] if the file was successfully parsed, [`false`] otherwise.
    #[cfg(feature = "v1_16")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
    #[doc(alias = "gst_rtsp_auth_parse_htdigest")]
    fn parse_htdigest(&self, path: impl AsRef<std::path::Path>, token: &RTSPToken) -> bool {
        unsafe {
            from_glib(ffi::gst_rtsp_auth_parse_htdigest(
                self.as_ref().to_glib_none().0,
                path.as_ref().to_glib_none().0,
                token.to_glib_none().0,
            ))
        }
    }

    /// Removes `basic` authentication token.
    /// ## `basic`
    /// the basic token
    #[doc(alias = "gst_rtsp_auth_remove_basic")]
    fn remove_basic(&self, basic: &str) {
        unsafe {
            ffi::gst_rtsp_auth_remove_basic(self.as_ref().to_glib_none().0, basic.to_glib_none().0);
        }
    }

    /// Removes a digest user.
    /// ## `user`
    /// the digest user name
    #[doc(alias = "gst_rtsp_auth_remove_digest")]
    fn remove_digest(&self, user: &str) {
        unsafe {
            ffi::gst_rtsp_auth_remove_digest(self.as_ref().to_glib_none().0, user.to_glib_none().0);
        }
    }

    /// Set the `realm` of `self`
    /// ## `realm`
    /// The realm to set
    #[cfg(feature = "v1_16")]
    #[cfg_attr(docsrs, doc(cfg(feature = "v1_16")))]
    #[doc(alias = "gst_rtsp_auth_set_realm")]
    fn set_realm(&self, realm: Option<&str>) {
        unsafe {
            ffi::gst_rtsp_auth_set_realm(self.as_ref().to_glib_none().0, realm.to_glib_none().0);
        }
    }

    /// Sets the supported authentication `methods` for `self`.
    /// ## `methods`
    /// supported methods
    #[doc(alias = "gst_rtsp_auth_set_supported_methods")]
    fn set_supported_methods(&self, methods: gst_rtsp::RTSPAuthMethod) {
        unsafe {
            ffi::gst_rtsp_auth_set_supported_methods(
                self.as_ref().to_glib_none().0,
                methods.into_glib(),
            );
        }
    }

    /// The [`gio::TlsAuthenticationMode`][crate::gio::TlsAuthenticationMode] to set on the underlying GTlsServerConnection.
    /// When set to another value than [`gio::TlsAuthenticationMode::None`][crate::gio::TlsAuthenticationMode::None],
    /// [`accept-certificate`][struct@crate::RTSPAuth#accept-certificate] signal will be emitted and must be handled.
    /// ## `mode`
    /// a [`gio::TlsAuthenticationMode`][crate::gio::TlsAuthenticationMode]
    #[doc(alias = "gst_rtsp_auth_set_tls_authentication_mode")]
    fn set_tls_authentication_mode(&self, mode: gio::TlsAuthenticationMode) {
        unsafe {
            ffi::gst_rtsp_auth_set_tls_authentication_mode(
                self.as_ref().to_glib_none().0,
                mode.into_glib(),
            );
        }
    }

    /// Set the TLS certificate for the auth. Client connections will only
    /// be accepted when TLS is negotiated.
    /// ## `cert`
    /// a [`gio::TlsCertificate`][crate::gio::TlsCertificate]
    #[doc(alias = "gst_rtsp_auth_set_tls_certificate")]
    fn set_tls_certificate(&self, cert: Option<&impl IsA<gio::TlsCertificate>>) {
        unsafe {
            ffi::gst_rtsp_auth_set_tls_certificate(
                self.as_ref().to_glib_none().0,
                cert.map(|p| p.as_ref()).to_glib_none().0,
            );
        }
    }

    /// Sets the certificate database that is used to verify peer certificates.
    /// If set to [`None`] (the default), then peer certificate validation will always
    /// set the [`gio::TlsCertificateFlags::UNKNOWN_CA`][crate::gio::TlsCertificateFlags::UNKNOWN_CA] error.
    /// ## `database`
    /// a [`gio::TlsDatabase`][crate::gio::TlsDatabase]
    #[doc(alias = "gst_rtsp_auth_set_tls_database")]
    fn set_tls_database(&self, database: Option<&impl IsA<gio::TlsDatabase>>) {
        unsafe {
            ffi::gst_rtsp_auth_set_tls_database(
                self.as_ref().to_glib_none().0,
                database.map(|p| p.as_ref()).to_glib_none().0,
            );
        }
    }

    /// Emitted during the TLS handshake after the client certificate has
    /// been received. See also [`set_tls_authentication_mode()`][Self::set_tls_authentication_mode()].
    /// ## `connection`
    /// a [`gio::TlsConnection`][crate::gio::TlsConnection]
    /// ## `peer_cert`
    /// the peer's [`gio::TlsCertificate`][crate::gio::TlsCertificate]
    /// ## `errors`
    /// the problems with `peer_cert`.
    ///
    /// # Returns
    ///
    /// [`true`] to accept `peer_cert` (which will also
    /// immediately end the signal emission). [`false`] to allow the signal
    /// emission to continue, which will cause the handshake to fail if
    /// no one else overrides it.
    #[doc(alias = "accept-certificate")]
    fn connect_accept_certificate<
        F: Fn(&Self, &gio::TlsConnection, &gio::TlsCertificate, gio::TlsCertificateFlags) -> bool
            + Send
            + Sync
            + 'static,
    >(
        &self,
        f: F,
    ) -> SignalHandlerId {
        unsafe extern "C" fn accept_certificate_trampoline<
            P: IsA<RTSPAuth>,
            F: Fn(&P, &gio::TlsConnection, &gio::TlsCertificate, gio::TlsCertificateFlags) -> bool
                + Send
                + Sync
                + 'static,
        >(
            this: *mut ffi::GstRTSPAuth,
            connection: *mut gio::ffi::GTlsConnection,
            peer_cert: *mut gio::ffi::GTlsCertificate,
            errors: gio::ffi::GTlsCertificateFlags,
            f: glib::ffi::gpointer,
        ) -> glib::ffi::gboolean {
            let f: &F = &*(f as *const F);
            f(
                RTSPAuth::from_glib_borrow(this).unsafe_cast_ref(),
                &from_glib_borrow(connection),
                &from_glib_borrow(peer_cert),
                from_glib(errors),
            )
            .into_glib()
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"accept-certificate\0".as_ptr() as *const _,
                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
                    accept_certificate_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl<O: IsA<RTSPAuth>> RTSPAuthExt for O {}