mod device_flow;
pub(crate) mod env;
mod profile;
mod refresh;
pub use profile::{load_store, save_store, AegisProfile};
use anyhow::Result;
pub async fn require_key() -> Result<String> {
if let Some(key) = env::aegis_key() {
return Ok(key);
}
let mut store = load_store()?;
let profile_name = store.active_profile.clone();
let profile = store
.profiles
.get(&profile_name)
.ok_or_else(|| anyhow::anyhow!("Not authenticated. Run 'aegis auth login'."))?
.clone();
if chrono::Utc::now() < profile.expires_at {
return Ok(profile.access_key.clone());
}
let refreshed = refresh::refresh_token(&profile).await?;
store.profiles.insert(profile_name, refreshed.clone());
save_store(&store)?;
Ok(refreshed.access_key)
}
pub use device_flow::run_device_flow;