use std::sync::Arc;
use crate::auth::policy::AuthPolicy;
use crate::auth::provider::{AuthProvider, UnlockRequest};
use crate::error::{AccountError, Result};
use crate::id::{AccountId, ProfileIx};
use crate::store::AccountStore;
use crate::unlocked::UnlockedAccount;
pub struct AccountSession {
store: Arc<AccountStore>,
account: AccountId,
default_profile_ix: ProfileIx,
}
impl AccountSession {
pub fn new(
store: Arc<AccountStore>,
account: AccountId,
default_profile_ix: ProfileIx,
) -> Self {
Self {
store,
account,
default_profile_ix,
}
}
pub fn enroll(
store: Arc<AccountStore>,
account: AccountId,
password: dig_session::Password,
entropy: &[u8; dig_session::ENTROPY_LEN],
default_profile_ix: ProfileIx,
) -> Result<UnlockedAccount> {
let seed = store
.enroll(&account, password, entropy)
.map_err(|why| AccountError::Keystore(why.to_string()))?;
Ok(UnlockedAccount::new(
account,
Arc::new(seed),
default_profile_ix,
))
}
pub fn enroll_from_recovery_phrase(
store: Arc<AccountStore>,
account: AccountId,
password: dig_session::Password,
phrase: &str,
default_profile_ix: ProfileIx,
) -> Result<UnlockedAccount> {
let seed = store
.enroll_from_recovery_phrase(&account, password, phrase)
.map_err(|why| AccountError::Keystore(why.to_string()))?;
Ok(UnlockedAccount::new(
account,
Arc::new(seed),
default_profile_ix,
))
}
pub fn account_id(&self) -> &AccountId {
&self.account
}
pub fn is_locked(&self) -> bool {
true
}
pub async fn unlock(
&self,
provider: &dyn AuthProvider,
policy: &dyn AuthPolicy,
) -> Result<UnlockedAccount> {
let factors = provider
.collect_factors(UnlockRequest::new(self.account.clone()))
.await?;
policy
.authorize(&factors)
.map_err(|why| AccountError::Auth(why.to_string()))?;
let seed = self
.store
.unlock(&self.account, factors.password)
.map_err(|why| AccountError::Keystore(why.to_string()))?;
Ok(UnlockedAccount::new(
self.account.clone(),
Arc::new(seed),
self.default_profile_ix,
))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::auth::factors::AuthFactors;
use crate::auth::policy::{AllOf, PasswordOnlyPolicy};
use crate::auth::provider::{SpendConfirmRequest, SpendDecision};
use crate::auth::second_factor::SecondFactor;
use dig_keystore::MemoryBackend;
use dig_session::{Password, ENTROPY_LEN};
const SEED: [u8; ENTROPY_LEN] = [0x9C; ENTROPY_LEN];
const PW: &str = "correct horse battery staple";
struct FixedProvider {
password: &'static str,
totp: Option<&'static str>,
}
#[async_trait::async_trait]
impl AuthProvider for FixedProvider {
async fn collect_factors(&self, _request: UnlockRequest) -> Result<AuthFactors> {
Ok(AuthFactors {
password: Password::new(self.password),
totp: self.totp.map(str::to_string),
passkey: None,
})
}
async fn confirm_spend(&self, _request: SpendConfirmRequest) -> Result<SpendDecision> {
Ok(SpendDecision::Approve)
}
}
struct FixedCodeFactor(&'static str);
impl SecondFactor for FixedCodeFactor {
fn name(&self) -> &str {
"TOTP"
}
fn verify(&self, factors: &AuthFactors) -> std::result::Result<(), String> {
match factors.totp.as_deref() {
Some(c) if c == self.0 => Ok(()),
_ => Err("bad code".into()),
}
}
}
fn enrolled_store() -> (Arc<AccountStore>, AccountId) {
let store = AccountStore::new(Arc::new(MemoryBackend::new()));
let id = AccountId::new("acct");
store.enroll(&id, Password::new(PW), &SEED).unwrap();
(Arc::new(store), id)
}
#[test]
fn a_locked_session_holds_no_seed() {
let (store, id) = enrolled_store();
let session = AccountSession::new(store, id.clone(), ProfileIx::ROOT);
assert!(session.is_locked());
assert_eq!(session.account_id(), &id);
}
#[test]
fn enroll_creates_and_returns_a_live_account_without_a_raw_seed() {
let store = Arc::new(AccountStore::new(Arc::new(MemoryBackend::new())));
let id = AccountId::new("fresh");
let acct = AccountSession::enroll(
store.clone(),
id.clone(),
Password::new(PW),
&SEED,
ProfileIx::ROOT,
)
.unwrap();
assert_eq!(acct.account_id(), &id);
assert!(matches!(
AccountSession::enroll(store, id, Password::new(PW), &SEED, ProfileIx::ROOT),
Err(AccountError::Keystore(_))
));
}
#[tokio::test]
async fn unlock_authenticates_and_yields_a_live_account() {
let (store, id) = enrolled_store();
let session = AccountSession::new(store, id.clone(), ProfileIx::ROOT);
let provider = FixedProvider {
password: PW,
totp: None,
};
let acct = session
.unlock(&provider, &PasswordOnlyPolicy)
.await
.unwrap();
assert_eq!(acct.account_id(), &id);
}
#[tokio::test]
async fn a_wrong_password_fails_the_unlock_closed() {
let (store, id) = enrolled_store();
let session = AccountSession::new(store, id, ProfileIx::ROOT);
let provider = FixedProvider {
password: "wrong",
totp: None,
};
let result = session.unlock(&provider, &PasswordOnlyPolicy).await;
assert!(matches!(result, Err(AccountError::Keystore(_))));
}
#[tokio::test]
async fn a_failing_policy_refuses_before_the_password() {
let (store, id) = enrolled_store();
let session = AccountSession::new(store, id, ProfileIx::ROOT);
let provider = FixedProvider {
password: PW,
totp: None,
};
let policy = AllOf::new(vec![Box::new(FixedCodeFactor("123456"))]);
let result = session.unlock(&provider, &policy).await;
assert!(matches!(result, Err(AccountError::Auth(_))));
}
}