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
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::{ffi, RTSPAuth, RTSPContext};
use glib::{prelude::*, subclass::prelude::*, translate::*};
use libc::c_char;

pub trait RTSPAuthImpl: RTSPAuthImplExt + ObjectImpl + Send + Sync {
    /// check the authentication of a client. The default implementation
    ///  checks if the authentication in the header matches one of the basic
    ///  authentication tokens. This function should set the authgroup field
    ///  in the context.
    fn authenticate(&self, ctx: &RTSPContext) -> bool {
        self.parent_authenticate(ctx)
    }

    /// check if a resource can be accessed. this function should
    ///  call authenticate to authenticate the client when needed. The method
    ///  should also construct and send an appropriate response message on
    ///  error.
    fn check(&self, ctx: &RTSPContext, check: &glib::GString) -> bool {
        self.parent_check(ctx, check)
    }

    fn generate_authenticate_header(&self, ctx: &RTSPContext) {
        self.parent_generate_authenticate_header(ctx);
    }
}

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

pub trait RTSPAuthImplExt: sealed::Sealed + ObjectSubclass {
    fn parent_authenticate(&self, ctx: &RTSPContext) -> bool {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GstRTSPAuthClass;
            (*parent_class)
                .authenticate
                .map(|f| {
                    from_glib(f(
                        self.obj().unsafe_cast_ref::<RTSPAuth>().to_glib_none().0,
                        ctx.to_glib_none().0,
                    ))
                })
                .unwrap_or(false)
        }
    }

    fn parent_check(&self, ctx: &RTSPContext, check: &glib::GString) -> bool {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GstRTSPAuthClass;
            (*parent_class)
                .check
                .map(|f| {
                    from_glib(f(
                        self.obj().unsafe_cast_ref::<RTSPAuth>().to_glib_none().0,
                        ctx.to_glib_none().0,
                        check.to_glib_none().0,
                    ))
                })
                .unwrap_or(false)
        }
    }

    fn parent_generate_authenticate_header(&self, ctx: &RTSPContext) {
        unsafe {
            let data = Self::type_data();
            let parent_class = data.as_ref().parent_class() as *mut ffi::GstRTSPAuthClass;
            if let Some(f) = (*parent_class).generate_authenticate_header {
                f(
                    self.obj().unsafe_cast_ref::<RTSPAuth>().to_glib_none().0,
                    ctx.to_glib_none().0,
                )
            }
        }
    }
}

impl<T: RTSPAuthImpl> RTSPAuthImplExt for T {}

unsafe impl<T: RTSPAuthImpl> IsSubclassable<T> for RTSPAuth {
    fn class_init(klass: &mut glib::Class<Self>) {
        Self::parent_class_init::<T>(klass);
        let klass = klass.as_mut();
        klass.authenticate = Some(rtsp_auth_authenticate::<T>);
        klass.check = Some(rtsp_auth_check::<T>);
        klass.generate_authenticate_header = Some(rtsp_auth_generate_authenticate_header::<T>);
    }
}

unsafe extern "C" fn rtsp_auth_authenticate<T: RTSPAuthImpl>(
    ptr: *mut ffi::GstRTSPAuth,
    ctx: *mut ffi::GstRTSPContext,
) -> glib::ffi::gboolean {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    imp.authenticate(&from_glib_borrow(ctx)).into_glib()
}

unsafe extern "C" fn rtsp_auth_check<T: RTSPAuthImpl>(
    ptr: *mut ffi::GstRTSPAuth,
    ctx: *mut ffi::GstRTSPContext,
    check: *const c_char,
) -> glib::ffi::gboolean {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    imp.check(&from_glib_borrow(ctx), &from_glib_borrow(check))
        .into_glib()
}

unsafe extern "C" fn rtsp_auth_generate_authenticate_header<T: RTSPAuthImpl>(
    ptr: *mut ffi::GstRTSPAuth,
    ctx: *mut ffi::GstRTSPContext,
) {
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.imp();

    imp.generate_authenticate_header(&from_glib_borrow(ctx));
}