use vta_sdk::session::{SessionStore, TokenStatus};
pub use vta_sdk::session::SessionInfo;
const SERVICE_NAME: &str = "pnm-cli";
fn store() -> SessionStore {
SessionStore::new(
SERVICE_NAME,
crate::config::config_dir().expect("could not determine config directory"),
)
}
pub fn store_session(
keyring_key: &str,
did: &str,
private_key: &str,
vta_did: &str,
) -> Result<(), Box<dyn std::error::Error>> {
store().store_direct(keyring_key, did, private_key, vta_did)
}
pub fn store_pending_vta_binding(
keyring_key: &str,
did: &str,
private_key: &str,
) -> Result<(), Box<dyn std::error::Error>> {
store().store_pending_vta_binding(keyring_key, did, private_key)
}
pub fn bind_vta_did(keyring_key: &str, vta_did: &str) -> Result<(), Box<dyn std::error::Error>> {
store().bind_vta_did(keyring_key, vta_did)
}
pub fn has_pending_vta_binding(keyring_key: &str) -> bool {
store().has_pending_vta_binding(keyring_key)
}
pub fn logout(keyring_key: &str) {
store().logout(keyring_key);
println!("Logged out. Credentials and tokens removed.");
}
pub fn loaded_session(keyring_key: &str) -> Option<SessionInfo> {
store().loaded_session(keyring_key)
}
pub fn session_status(keyring_key: &str) -> Option<vta_sdk::session::SessionStatus> {
store().session_status(keyring_key)
}
pub fn status(keyring_key: &str) {
match store().session_status(keyring_key) {
Some(status) => {
println!("Client DID: {}", status.client_did);
println!(
"VTA DID: {}",
status.vta_did.as_deref().unwrap_or("(pending setup)")
);
match status.token_status {
TokenStatus::Valid { expires_in_secs } => {
println!("Token: valid (expires in {expires_in_secs}s)");
}
TokenStatus::Expired => {
println!("Token: expired");
}
TokenStatus::None => {
println!("Token: none (will authenticate on next request)");
}
}
}
None => {
println!("Not authenticated.");
println!("\nRun `pnm setup` to provision an admin identity for a VTA.");
}
}
}
pub async fn ensure_authenticated(
base_url: &str,
keyring_key: &str,
) -> Result<String, Box<dyn std::error::Error>> {
store().ensure_authenticated(base_url, keyring_key).await
}
pub async fn connect(
url_override: Option<&str>,
keyring_key: &str,
) -> Result<vta_sdk::client::VtaClient, Box<dyn std::error::Error>> {
store().connect(keyring_key, url_override).await
}