use crate::secret::{SecretBytes, SecretResolver, SecuritySecretMaterial, SecuritySecretRef};
use crate::secret_keyring::{
FileSecretKeyring, KeyProtection, SecretAccessResult, WINDOWS_DPAPI_USER_SECRET_KEYRING_FORMAT,
};
use std::fmt;
use std::path::PathBuf;
#[derive(Clone)]
pub struct WindowsDpapiSecretKeyring {
inner: FileSecretKeyring,
}
impl WindowsDpapiSecretKeyring {
pub fn open(root: impl Into<PathBuf>) -> SecretAccessResult<Self> {
FileSecretKeyring::open_with(root.into(), KeyProtection::WindowsDpapiUser)
.map(|inner| Self { inner })
}
pub fn install_initial(&self, material: &SecuritySecretMaterial) -> SecretAccessResult<()> {
self.inner.install_initial(material)
}
pub fn rotate(
&self,
next: &SecuritySecretMaterial,
now_ms: u64,
) -> SecretAccessResult<Option<String>> {
self.inner.rotate(next, now_ms)
}
pub fn revoke(&self, key_id: &str) -> SecretAccessResult<()> {
self.inner.revoke(key_id)
}
pub fn resolve_active(&self, now_ms: u64) -> SecretAccessResult<SecuritySecretMaterial> {
self.inner.resolve_active(now_ms)
}
pub fn resolve_for_validation(
&self,
key_id: &str,
now_ms: u64,
) -> SecretAccessResult<SecuritySecretMaterial> {
self.inner.resolve_for_validation(key_id, now_ms)
}
pub fn recover(&self, now_ms: u64) -> SecretAccessResult<String> {
self.inner.recover(now_ms)
}
}
impl SecretResolver for WindowsDpapiSecretKeyring {
fn resolve(&self, reference: &SecuritySecretRef) -> crate::SecurityResult<SecretBytes> {
self.inner.resolve(reference)
}
}
impl fmt::Debug for WindowsDpapiSecretKeyring {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("WindowsDpapiSecretKeyring")
.field("scope", &"current-user/current-machine")
.finish_non_exhaustive()
}
}
impl fmt::Display for WindowsDpapiSecretKeyring {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(WINDOWS_DPAPI_USER_SECRET_KEYRING_FORMAT)
}
}