Skip to main content

security/
authorization.rs

1use bitflags::bitflags;
2use serde_json::Value;
3
4use crate::bridge;
5use crate::error::Result;
6
7bitflags! {
8    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9    /// Mirrors Authorization Services option bits used with `AuthorizationCreate`.
10    pub struct AuthorizationOptions: u32 {
11        /// Mirrors an Authorization Services option bit.
12        const DEFAULTS = 0;
13        /// Mirrors an Authorization Services option bit.
14        const INTERACTION_ALLOWED = 1 << 0;
15        /// Mirrors an Authorization Services option bit.
16        const EXTEND_RIGHTS = 1 << 1;
17        /// Mirrors an Authorization Services option bit.
18        const PARTIAL_RIGHTS = 1 << 2;
19        /// Mirrors an Authorization Services option bit.
20        const DESTROY_RIGHTS = 1 << 3;
21        /// Mirrors an Authorization Services option bit.
22        const PREAUTHORIZE = 1 << 4;
23        /// Mirrors an Authorization Services option bit.
24        const SKIP_INTERNAL_AUTH = 1 << 9;
25        /// Mirrors an Authorization Services option bit.
26        const NO_DATA = 1 << 20;
27    }
28}
29
30#[derive(Debug)]
31/// Wraps `AuthorizationRef`.
32pub struct Authorization {
33    handle: bridge::Handle,
34}
35
36impl Authorization {
37    /// Wraps the corresponding Authorization Services operation for `AuthorizationRef`.
38    pub fn new() -> Result<Self> {
39        Self::with_options(AuthorizationOptions::DEFAULTS)
40    }
41
42    /// Wraps the corresponding Authorization Services operation for `AuthorizationRef`.
43    pub fn with_options(options: AuthorizationOptions) -> Result<Self> {
44        let mut status = 0;
45        let mut error = std::ptr::null_mut();
46        let raw = unsafe {
47            bridge::security_authorization_create(options.bits(), &mut status, &mut error)
48        };
49        bridge::required_handle("security_authorization_create", raw, status, error)
50            .map(|handle| Self { handle })
51    }
52
53    /// Wraps the corresponding Authorization Services operation for `AuthorizationRef`.
54    pub fn external_form(&self) -> Result<Vec<u8>> {
55        let mut status = 0;
56        let mut error = std::ptr::null_mut();
57        let raw = unsafe {
58            bridge::security_authorization_make_external_form(
59                self.handle.as_ptr(),
60                &mut status,
61                &mut error,
62            )
63        };
64        bridge::required_data(
65            "security_authorization_make_external_form",
66            raw,
67            status,
68            error,
69        )
70    }
71
72    /// Wraps the corresponding Authorization Services operation for `AuthorizationRef`.
73    pub fn from_external_form(external_form: &[u8]) -> Result<Self> {
74        let mut status = 0;
75        let mut error = std::ptr::null_mut();
76        let raw = unsafe {
77            bridge::security_authorization_create_from_external_form(
78                external_form.as_ptr().cast(),
79                bridge::len_to_isize(external_form.len())?,
80                &mut status,
81                &mut error,
82            )
83        };
84        bridge::required_handle(
85            "security_authorization_create_from_external_form",
86            raw,
87            status,
88            error,
89        )
90        .map(|handle| Self { handle })
91    }
92
93    /// Wraps the corresponding Authorization Services operation for `AuthorizationRef`.
94    pub fn copy_info(&self, tag: Option<&str>) -> Result<Value> {
95        let tag = tag.map(bridge::cstring).transpose()?;
96        let mut status = 0;
97        let mut error = std::ptr::null_mut();
98        let raw = unsafe {
99            bridge::security_authorization_copy_info(
100                self.handle.as_ptr(),
101                tag.as_ref()
102                    .map_or(std::ptr::null(), |value| value.as_ptr()),
103                &mut status,
104                &mut error,
105            )
106        };
107        bridge::required_json("security_authorization_copy_info", raw, status, error)
108    }
109
110    /// Wraps the corresponding Authorization Services operation for `AuthorizationRef`.
111    pub fn copy_rights(&self, rights: &[&str], options: AuthorizationOptions) -> Result<Value> {
112        let rights_json = bridge::json_cstring(&rights)?;
113        let mut status = 0;
114        let mut error = std::ptr::null_mut();
115        let raw = unsafe {
116            bridge::security_authorization_copy_rights(
117                self.handle.as_ptr(),
118                rights_json.as_ptr(),
119                options.bits(),
120                &mut status,
121                &mut error,
122            )
123        };
124        bridge::required_json("security_authorization_copy_rights", raw, status, error)
125    }
126
127    /// Wraps the corresponding Authorization Services operation for `AuthorizationRef`.
128    pub fn copy_rights_async(
129        &self,
130        rights: &[&str],
131        options: AuthorizationOptions,
132    ) -> Result<Value> {
133        let rights_json = bridge::json_cstring(&rights)?;
134        let mut status = 0;
135        let mut error = std::ptr::null_mut();
136        let raw = unsafe {
137            bridge::security_authorization_copy_rights_async(
138                self.handle.as_ptr(),
139                rights_json.as_ptr(),
140                options.bits(),
141                &mut status,
142                &mut error,
143            )
144        };
145        bridge::required_json(
146            "security_authorization_copy_rights_async",
147            raw,
148            status,
149            error,
150        )
151    }
152}