gstreamer_rtsp_server/subclass/
rtsp_auth.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::{ffi, RTSPAuth, RTSPContext};
4use glib::{prelude::*, subclass::prelude::*, translate::*};
5use libc::c_char;
6
7pub trait RTSPAuthImpl: ObjectImpl + ObjectSubclass<Type: IsA<RTSPAuth>> + Send + Sync {
8    /// check the authentication of a client. The default implementation
9    ///  checks if the authentication in the header matches one of the basic
10    ///  authentication tokens. This function should set the authgroup field
11    ///  in the context.
12    fn authenticate(&self, ctx: &RTSPContext) -> bool {
13        self.parent_authenticate(ctx)
14    }
15
16    /// check if a resource can be accessed. this function should
17    ///  call authenticate to authenticate the client when needed. The method
18    ///  should also construct and send an appropriate response message on
19    ///  error.
20    fn check(&self, ctx: &RTSPContext, check: &glib::GString) -> bool {
21        self.parent_check(ctx, check)
22    }
23
24    fn generate_authenticate_header(&self, ctx: &RTSPContext) {
25        self.parent_generate_authenticate_header(ctx);
26    }
27}
28
29pub trait RTSPAuthImplExt: RTSPAuthImpl {
30    fn parent_authenticate(&self, ctx: &RTSPContext) -> bool {
31        unsafe {
32            let data = Self::type_data();
33            let parent_class = data.as_ref().parent_class() as *mut ffi::GstRTSPAuthClass;
34            (*parent_class)
35                .authenticate
36                .map(|f| {
37                    from_glib(f(
38                        self.obj().unsafe_cast_ref::<RTSPAuth>().to_glib_none().0,
39                        ctx.to_glib_none().0,
40                    ))
41                })
42                .unwrap_or(false)
43        }
44    }
45
46    fn parent_check(&self, ctx: &RTSPContext, check: &glib::GString) -> bool {
47        unsafe {
48            let data = Self::type_data();
49            let parent_class = data.as_ref().parent_class() as *mut ffi::GstRTSPAuthClass;
50            (*parent_class)
51                .check
52                .map(|f| {
53                    from_glib(f(
54                        self.obj().unsafe_cast_ref::<RTSPAuth>().to_glib_none().0,
55                        ctx.to_glib_none().0,
56                        check.to_glib_none().0,
57                    ))
58                })
59                .unwrap_or(false)
60        }
61    }
62
63    fn parent_generate_authenticate_header(&self, ctx: &RTSPContext) {
64        unsafe {
65            let data = Self::type_data();
66            let parent_class = data.as_ref().parent_class() as *mut ffi::GstRTSPAuthClass;
67            if let Some(f) = (*parent_class).generate_authenticate_header {
68                f(
69                    self.obj().unsafe_cast_ref::<RTSPAuth>().to_glib_none().0,
70                    ctx.to_glib_none().0,
71                )
72            }
73        }
74    }
75}
76
77impl<T: RTSPAuthImpl> RTSPAuthImplExt for T {}
78
79unsafe impl<T: RTSPAuthImpl> IsSubclassable<T> for RTSPAuth {
80    fn class_init(klass: &mut glib::Class<Self>) {
81        Self::parent_class_init::<T>(klass);
82        let klass = klass.as_mut();
83        klass.authenticate = Some(rtsp_auth_authenticate::<T>);
84        klass.check = Some(rtsp_auth_check::<T>);
85        klass.generate_authenticate_header = Some(rtsp_auth_generate_authenticate_header::<T>);
86    }
87}
88
89unsafe extern "C" fn rtsp_auth_authenticate<T: RTSPAuthImpl>(
90    ptr: *mut ffi::GstRTSPAuth,
91    ctx: *mut ffi::GstRTSPContext,
92) -> glib::ffi::gboolean {
93    let instance = &*(ptr as *mut T::Instance);
94    let imp = instance.imp();
95
96    imp.authenticate(&from_glib_borrow(ctx)).into_glib()
97}
98
99unsafe extern "C" fn rtsp_auth_check<T: RTSPAuthImpl>(
100    ptr: *mut ffi::GstRTSPAuth,
101    ctx: *mut ffi::GstRTSPContext,
102    check: *const c_char,
103) -> glib::ffi::gboolean {
104    let instance = &*(ptr as *mut T::Instance);
105    let imp = instance.imp();
106
107    imp.check(&from_glib_borrow(ctx), &from_glib_borrow(check))
108        .into_glib()
109}
110
111unsafe extern "C" fn rtsp_auth_generate_authenticate_header<T: RTSPAuthImpl>(
112    ptr: *mut ffi::GstRTSPAuth,
113    ctx: *mut ffi::GstRTSPContext,
114) {
115    let instance = &*(ptr as *mut T::Instance);
116    let imp = instance.imp();
117
118    imp.generate_authenticate_header(&from_glib_borrow(ctx));
119}