Skip to main content

security/
authorization.rs

1use bitflags::bitflags;
2
3use crate::bridge;
4use crate::error::Result;
5
6bitflags! {
7    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8    pub struct AuthorizationOptions: u32 {
9        const DEFAULTS = 0;
10        const INTERACTION_ALLOWED = 1 << 0;
11        const EXTEND_RIGHTS = 1 << 1;
12        const PARTIAL_RIGHTS = 1 << 2;
13        const DESTROY_RIGHTS = 1 << 3;
14        const PREAUTHORIZE = 1 << 4;
15        const SKIP_INTERNAL_AUTH = 1 << 9;
16        const NO_DATA = 1 << 20;
17    }
18}
19
20#[derive(Debug)]
21pub struct Authorization {
22    handle: bridge::Handle,
23}
24
25impl Authorization {
26    pub fn new() -> Result<Self> {
27        Self::with_options(AuthorizationOptions::DEFAULTS)
28    }
29
30    pub fn with_options(options: AuthorizationOptions) -> Result<Self> {
31        let mut status = 0;
32        let mut error = std::ptr::null_mut();
33        let raw = unsafe {
34            bridge::security_authorization_create(options.bits(), &mut status, &mut error)
35        };
36        bridge::required_handle("security_authorization_create", raw, status, error)
37            .map(|handle| Self { handle })
38    }
39
40    pub fn external_form(&self) -> Result<Vec<u8>> {
41        let mut status = 0;
42        let mut error = std::ptr::null_mut();
43        let raw = unsafe {
44            bridge::security_authorization_make_external_form(
45                self.handle.as_ptr(),
46                &mut status,
47                &mut error,
48            )
49        };
50        bridge::required_data("security_authorization_make_external_form", raw, status, error)
51    }
52
53    pub fn from_external_form(external_form: &[u8]) -> Result<Self> {
54        let mut status = 0;
55        let mut error = std::ptr::null_mut();
56        let raw = unsafe {
57            bridge::security_authorization_create_from_external_form(
58                external_form.as_ptr().cast(),
59                bridge::len_to_isize(external_form.len())?,
60                &mut status,
61                &mut error,
62            )
63        };
64        bridge::required_handle(
65            "security_authorization_create_from_external_form",
66            raw,
67            status,
68            error,
69        )
70        .map(|handle| Self { handle })
71    }
72}