tangled-cli 0.1.0

CLI for interacting with Tangled, an AT Protocol-based git collaboration platform
//! ServiceAuth token minting.

use anyhow::Result;
use tangled_api::lexicon::com::atproto::server;

use super::auth::PdsAuth;

/// Mints a ServiceAuth token on the PDS for the service at `service_base`
/// (a knot or spindle base URL); the audience is `did:web:<host>` and the
/// token is bound to the XRPC method `lxm` (knots reject unbound tokens).
pub async fn mint(
    pds_base: &str,
    auth: &PdsAuth,
    service_base: &str,
    lxm: &str,
) -> Result<String> {
    let trimmed = service_base.trim_end_matches('/');
    let host = trimmed
        .strip_prefix("https://")
        .or_else(|| trimmed.strip_prefix("http://"))
        .unwrap_or(trimmed);
    let params = server::get_service_auth::Params {
        aud: format!("did:web:{host}"),
        exp: Some(chrono::Utc::now().timestamp() + 60),
        lxm: Some(lxm.to_string()),
    };
    let rb = server::get_service_auth(super::http(), pds_base, &params);
    let out: server::get_service_auth::Output = auth.send(rb).await?;
    Ok(out.token)
}