use crate::auth::Session;
use crate::error::StorageError;
use super::CredentialStore;
pub const SERVICE_NAME: &str = "eero-api";
pub const ACCOUNT_NAME: &str = "auth-tokens";
const BACKEND_NAME: &str = "keyring";
#[derive(Debug, Clone)]
pub struct KeyringStore {
service: String,
account: String,
}
impl KeyringStore {
#[must_use]
pub fn new() -> Self {
Self::with_entry(SERVICE_NAME, ACCOUNT_NAME)
}
#[must_use]
pub fn with_entry(service: impl Into<String>, account: impl Into<String>) -> Self {
Self {
service: service.into(),
account: account.into(),
}
}
fn entry(&self) -> Result<keyring::Entry, StorageError> {
keyring::Entry::new(&self.service, &self.account).map_err(|err| backend_error(&err))
}
}
impl Default for KeyringStore {
fn default() -> Self {
Self::new()
}
}
impl CredentialStore for KeyringStore {
fn load(&self) -> Result<Session, StorageError> {
let entry = self.entry()?;
match entry.get_password() {
Ok(json) => {
let (session, migrated) = Session::from_json_migrating(&json)?;
if migrated {
super::migrate_and_verify(self, &session, "keyring");
}
Ok(session)
}
Err(keyring::Error::NoEntry) => Ok(Session::empty()),
Err(err) => Err(backend_error(&err)),
}
}
fn save(&self, session: &Session) -> Result<(), StorageError> {
let entry = self.entry()?;
let json = session.to_json()?;
entry.set_password(&json).map_err(|err| backend_error(&err))
}
fn clear(&self) -> Result<(), StorageError> {
let entry = self.entry()?;
match entry.delete_credential() {
Ok(()) | Err(keyring::Error::NoEntry) => Ok(()),
Err(err) => Err(backend_error(&err)),
}
}
}
fn backend_error(err: &keyring::Error) -> StorageError {
StorageError::Backend {
backend: BACKEND_NAME.to_owned(),
message: err.to_string(),
}
}
#[cfg(test)]
mod tests {
use super::{ACCOUNT_NAME, BACKEND_NAME, CredentialStore, KeyringStore, SERVICE_NAME};
use crate::auth::Session;
use crate::error::StorageError;
#[test]
fn service_and_account_match_the_shared_eero_api_entry() {
assert_eq!(SERVICE_NAME, "eero-api");
assert_eq!(ACCOUNT_NAME, "auth-tokens");
}
#[test]
fn new_uses_the_shared_service_and_account() {
let store = KeyringStore::new();
assert_eq!(store.service, SERVICE_NAME);
assert_eq!(store.account, ACCOUNT_NAME);
}
#[test]
fn default_is_equivalent_to_new() {
let store = KeyringStore::default();
assert_eq!(store.service, SERVICE_NAME);
assert_eq!(store.account, ACCOUNT_NAME);
}
#[test]
fn with_entry_uses_the_given_service_and_account() {
let store = KeyringStore::with_entry("custom-service", "custom-account");
assert_eq!(store.service, "custom-service");
assert_eq!(store.account, "custom-account");
}
#[test]
fn debug_output_contains_no_credential() {
let store = KeyringStore::with_entry("svc", "acct");
let debug = format!("{store:?}");
assert!(debug.contains("svc"));
assert!(debug.contains("acct"));
}
#[test]
fn load_never_panics_and_never_fabricates_a_valid_session() {
let store = KeyringStore::with_entry(
"rusteero-test-service-no-backend",
"rusteero-test-account-no-backend-load_never_panics_and_never_fabricates_a_valid_session",
);
match store.load() {
Ok(session) => assert!(!session.is_valid()),
Err(StorageError::Backend { backend, message }) => {
assert_eq!(backend, BACKEND_NAME);
assert!(!message.is_empty());
}
Err(other) => panic!("unexpected error variant: {other:?}"),
}
}
#[cfg(target_os = "linux")]
#[test]
fn load_without_a_dbus_session_bus_fails_cleanly_with_no_credential_in_the_message() {
let store = KeyringStore::with_entry(
"rusteero-test-service-no-backend",
"rusteero-test-account-no-backend-load_without_a_dbus_session_bus_fails_cleanly_with_no_credential_in_the_message",
);
match store.load() {
Err(StorageError::Backend { backend, message }) => {
assert_eq!(backend, BACKEND_NAME);
assert!(!message.is_empty());
}
other => panic!(
"expected StorageError::Backend on Linux with no D-Bus session bus, got {other:?}"
),
}
}
struct ClearEntryOnDrop<'a>(&'a KeyringStore);
impl Drop for ClearEntryOnDrop<'_> {
fn drop(&mut self) {
let _ = self.0.clear();
}
}
#[test]
fn save_never_leaves_a_credential_behind_on_any_platform() {
let store = KeyringStore::with_entry(
"rusteero-test-service-no-backend",
"rusteero-test-account-no-backend-save_never_leaves_a_credential_behind_on_any_platform",
);
let _cleanup = ClearEntryOnDrop(&store);
let session = Session::from_token("should-never-appear-in-any-error-message");
match store.save(&session) {
Ok(()) => {}
Err(StorageError::Backend { backend, message }) => {
assert_eq!(backend, BACKEND_NAME);
assert!(!message.contains("should-never-appear-in-any-error-message"));
}
Err(other) => panic!("unexpected error variant: {other:?}"),
}
}
#[cfg(target_os = "linux")]
#[test]
fn save_without_a_dbus_session_bus_fails_cleanly_with_no_credential_in_the_message() {
let store = KeyringStore::with_entry(
"rusteero-test-service-no-backend",
"rusteero-test-account-no-backend-save_without_a_dbus_session_bus_fails_cleanly_with_no_credential_in_the_message",
);
let session = Session::from_token("should-never-appear-in-any-error-message");
match store.save(&session) {
Err(StorageError::Backend { backend, message }) => {
assert_eq!(backend, BACKEND_NAME);
assert!(!message.contains("should-never-appear-in-any-error-message"));
}
other => panic!(
"expected StorageError::Backend on Linux with no D-Bus session bus, got {other:?}"
),
}
}
#[test]
fn clear_without_a_working_backend_fails_cleanly_or_is_a_no_op() {
let store = KeyringStore::with_entry(
"rusteero-test-service-no-backend",
"rusteero-test-account-no-backend-clear_without_a_working_backend_fails_cleanly_or_is_a_no_op",
);
match store.clear() {
Ok(()) => {}
Err(StorageError::Backend { backend, message }) => {
assert_eq!(backend, BACKEND_NAME);
assert!(!message.is_empty());
}
Err(other) => panic!("unexpected error variant: {other:?}"),
}
}
#[test]
#[ignore = "requires a real OS keyring backend (Secret Service/Keychain/Credential Manager)"]
fn keyring_round_trip_save_load_clear() {
let store = KeyringStore::with_entry("rusteero-test-service", "rusteero-test-account");
let session = Session::from_token("integration-test-token");
store
.save(&session)
.expect("save succeeds against a real backend");
let loaded = store.load().expect("load succeeds against a real backend");
assert!(loaded.is_valid());
store
.clear()
.expect("clear succeeds against a real backend");
let cleared = store.load().expect("load succeeds after clear");
assert!(!cleared.is_valid());
}
}