Skip to main content

localauthentication/
la_right_store.rs

1//! `LARightStore` wrappers.
2
3use crate::ffi;
4use crate::la_error::Result;
5use crate::la_persisted_right::LAPersistedRight;
6use crate::la_right::LARight;
7use crate::private::{bridge_ptr, bridge_unit, cstring, OwnedHandle};
8
9/// Managed wrapper around Apple's singleton `LARightStore`.
10#[derive(Debug)]
11pub struct LARightStore {
12    handle: OwnedHandle,
13}
14
15impl LARightStore {
16    /// Retain the framework singleton store.
17    ///
18    /// # Errors
19    ///
20    /// Returns an error if the API is unavailable or the Swift bridge rejects the request.
21    pub fn shared() -> Result<Self> {
22        let raw = bridge_ptr(|out, error_out| unsafe {
23            ffi::la_right_store::la_right_store_shared(out, error_out)
24        })?;
25        Ok(Self {
26            handle: OwnedHandle::new(raw, ffi::la_right_store::la_right_store_release),
27        })
28    }
29
30    /// Fetch a persisted right by identifier.
31    ///
32    /// # Errors
33    ///
34    /// Returns a mapped framework or bridge error if the identifier does not resolve.
35    pub fn right_for_identifier(&self, identifier: &str) -> Result<LAPersistedRight> {
36        let identifier = cstring(identifier)?;
37        Ok(LAPersistedRight::from_raw(bridge_ptr(
38            |out, error_out| unsafe {
39                ffi::la_right_store::la_right_store_right_for_identifier(
40                    self.handle.as_ptr(),
41                    identifier.as_ptr(),
42                    out,
43                    error_out,
44                )
45            },
46        )?))
47    }
48
49    /// Persist a right for later reuse.
50    ///
51    /// # Errors
52    ///
53    /// Returns a mapped framework or bridge error if persistence fails.
54    pub fn save_right(&self, right: &LARight, identifier: &str) -> Result<LAPersistedRight> {
55        let identifier = cstring(identifier)?;
56        Ok(LAPersistedRight::from_raw(bridge_ptr(
57            |out, error_out| unsafe {
58                ffi::la_right_store::la_right_store_save_right(
59                    self.handle.as_ptr(),
60                    right.as_ptr(),
61                    identifier.as_ptr(),
62                    out,
63                    error_out,
64                )
65            },
66        )?))
67    }
68
69    /// Persist a right together with secret data.
70    ///
71    /// # Errors
72    ///
73    /// Returns a mapped framework or bridge error if persistence fails.
74    pub fn save_right_with_secret(
75        &self,
76        right: &LARight,
77        identifier: &str,
78        secret: &[u8],
79    ) -> Result<LAPersistedRight> {
80        let identifier = cstring(identifier)?;
81        Ok(LAPersistedRight::from_raw(bridge_ptr(
82            |out, error_out| unsafe {
83                ffi::la_right_store::la_right_store_save_right_with_secret(
84                    self.handle.as_ptr(),
85                    right.as_ptr(),
86                    identifier.as_ptr(),
87                    secret.as_ptr(),
88                    secret.len(),
89                    out,
90                    error_out,
91                )
92            },
93        )?))
94    }
95
96    /// Remove a persisted right.
97    ///
98    /// # Errors
99    ///
100    /// Returns a mapped framework or bridge error if removal fails.
101    pub fn remove_right(&self, right: &LAPersistedRight) -> Result<()> {
102        bridge_unit(|error_out| unsafe {
103            ffi::la_right_store::la_right_store_remove_right(
104                self.handle.as_ptr(),
105                right.as_ptr(),
106                error_out,
107            )
108        })
109    }
110
111    /// Remove a persisted right by identifier.
112    ///
113    /// # Errors
114    ///
115    /// Returns a mapped framework or bridge error if removal fails.
116    pub fn remove_right_for_identifier(&self, identifier: &str) -> Result<()> {
117        let identifier = cstring(identifier)?;
118        bridge_unit(|error_out| unsafe {
119            ffi::la_right_store::la_right_store_remove_right_for_identifier(
120                self.handle.as_ptr(),
121                identifier.as_ptr(),
122                error_out,
123            )
124        })
125    }
126
127    /// Remove all rights owned by the current client.
128    ///
129    /// # Errors
130    ///
131    /// Returns a mapped framework or bridge error if removal fails.
132    pub fn remove_all_rights(&self) -> Result<()> {
133        bridge_unit(|error_out| unsafe {
134            ffi::la_right_store::la_right_store_remove_all_rights(self.handle.as_ptr(), error_out)
135        })
136    }
137}