pub struct SecretStore;Expand description
Cross-platform secret store backed by the host OS keychain.
Stateless by design — it holds no cached secrets. Every call round-trips to the OS. That makes concurrent usage safe and avoids any in-process leak surface beyond the immediate call’s return value.
Implementations§
Source§impl SecretStore
impl SecretStore
pub fn new() -> Self
Sourcepub fn put(&self, r: &SecretRef, value: &str) -> Result<(), SecretError>
pub fn put(&self, r: &SecretRef, value: &str) -> Result<(), SecretError>
Store a UTF-8 secret under (service, key). Replaces any existing
value at the same ref.
On macOS, writes via /usr/bin/security add-generic-password -U -A
so the resulting item has a permissive ACL — readable by any
binary the user runs. This is necessary because the legacy
keychain’s default ACL binds an item to the calling binary’s
code-signing hash, which changes on every cargo rebuild and
silently revokes read access from later versions of the same
CLI tool. (/usr/bin/security is Apple-signed with full
keychain entitlements — the same path reads, status checks,
and deletes use, and the same path users invoke manually.)
Trade-off: the value transits argv during the spawn (visible to
ps from the same user for ~milliseconds). Acceptable for the
“single-user developer machine” threat model; any process that
can see argv on this machine can also read the keychain
directly via security. On other platforms, behavior is
unchanged (keyring crate’s native backend).
Sourcepub fn put_json<T: Serialize>(
&self,
r: &SecretRef,
value: &T,
) -> Result<(), SecretError>
pub fn put_json<T: Serialize>( &self, r: &SecretRef, value: &T, ) -> Result<(), SecretError>
Store a structured value serialized as JSON.
Sourcepub fn get(&self, r: &SecretRef) -> Result<String, SecretError>
pub fn get(&self, r: &SecretRef) -> Result<String, SecretError>
Read a UTF-8 secret. Returns NotFound if no entry exists.
On macOS, reads through /usr/bin/security first so repeated
helper rebuilds do not churn Keychain prompts against each
binary’s CDHash. Backend/authorization failures are returned
directly instead of falling back to an in-process read path that
can trigger a second prompt.
Sourcepub fn get_json<T: for<'de> Deserialize<'de>>(
&self,
r: &SecretRef,
) -> Result<T, SecretError>
pub fn get_json<T: for<'de> Deserialize<'de>>( &self, r: &SecretRef, ) -> Result<T, SecretError>
Read a structured value previously stored via put_json.
Sourcepub fn delete(&self, r: &SecretRef) -> Result<(), SecretError>
pub fn delete(&self, r: &SecretRef) -> Result<(), SecretError>
Delete an entry. Returns Ok even if the entry didn’t exist — idempotent from the caller’s perspective.
On macOS, deletes through /usr/bin/security first so the
Apple-signed helper, not the rebuilt caller binary, owns
Keychain authorization.
Sourcepub fn status(&self, r: &SecretRef) -> Result<SecretStatus, SecretError>
pub fn status(&self, r: &SecretRef) -> Result<SecretStatus, SecretError>
Existence check without returning the value. Safe to log.
On macOS, checks status through /usr/bin/security first for
the same CDHash-stable authorization behavior as get.
Sourcepub fn is_available(&self) -> bool
pub fn is_available(&self) -> bool
Probe whether the OS secret store is reachable.
Opens an Entry for an internal-only sentinel and attempts to read
it. Returns true iff the backend responds with either a value or
NoEntry — both mean the store is reachable; PlatformFailure /
NoStorageAccess mean it isn’t.
§Side effects
- On macOS with a locked keychain, this may trigger a user unlock prompt. Call only when the caller is ready to handle that UX.
- On Linux it opens a DBus connection to Secret Service.
- Performance: one round-trip to the OS store. Not cached.
Sourcepub fn availability(&self) -> AvailabilityCheck
pub fn availability(&self) -> AvailabilityCheck
Detailed availability probe. Same round-trip as is_available,
but distinguishes “no backend at all” from a specific platform
failure so the FFI surface can emit a reason matching the
pattern used by the other v0.4 capability probes
(accountsList, calendarList, etc.).
Trait Implementations§
Source§impl Clone for SecretStore
impl Clone for SecretStore
Source§fn clone(&self) -> SecretStore
fn clone(&self) -> SecretStore
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more