Skip to main content

pidge_client/auth/
token_store.rs

1//! Backend-agnostic facade over [`KeychainStore`] and [`FileStore`].
2//!
3//! Callers pass a [`TokenStorage`] enum value (looked up from the account's
4//! config entry) and this module dispatches to the right backend. Use this
5//! type everywhere except in narrow keychain/file-specific code paths (e.g.
6//! migrations that read from one backend and write to the other).
7
8use pidge_core::TokenStorage;
9
10use crate::auth::file_store::FileStore;
11use crate::auth::store::KeychainStore;
12use crate::auth::tokens::TokenSet;
13use crate::error::ClientError;
14
15pub struct TokenStore;
16
17impl TokenStore {
18    /// Load tokens for an email from the specified backend.
19    pub fn load(email: &str, storage: TokenStorage) -> Result<Option<TokenSet>, ClientError> {
20        match storage {
21            TokenStorage::Keychain => KeychainStore::load(email),
22            TokenStorage::File => FileStore::load(email),
23        }
24    }
25
26    /// Save tokens for an email to the specified backend, overwriting any existing entry.
27    pub fn save(email: &str, tokens: &TokenSet, storage: TokenStorage) -> Result<(), ClientError> {
28        match storage {
29            TokenStorage::Keychain => KeychainStore::save(email, tokens),
30            TokenStorage::File => FileStore::save(email, tokens),
31        }
32    }
33
34    /// Remove tokens for an email from the specified backend. No-op if no entry exists.
35    pub fn delete(email: &str, storage: TokenStorage) -> Result<(), ClientError> {
36        match storage {
37            TokenStorage::Keychain => KeychainStore::delete(email),
38            TokenStorage::File => FileStore::delete(email),
39        }
40    }
41}