Attach Blossom auth token in clients

Co-authored-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co>
Signed-off-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co>
This commit is contained in:
npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757
2026-07-01 15:38:49 -04:00
parent 9f2a11b338
commit 5cc7e4493e
20 changed files with 221 additions and 21 deletions

View File

@@ -116,6 +116,8 @@ pub struct BuzzClient {
auth_tag: Option<Tag>,
/// Raw JSON of the auth tag for the `x-auth-tag` HTTP header.
auth_tag_json: Option<String>,
/// API token attached as `X-Auth-Token` for Blossom media uploads.
api_token: Option<String>,
}
impl BuzzClient {
@@ -124,6 +126,7 @@ impl BuzzClient {
keys: Keys,
auth_tag: Option<Tag>,
auth_tag_json: Option<String>,
api_token: Option<String>,
) -> Result<Self, CliError> {
let http = reqwest::Client::builder()
.timeout(Duration::from_secs(10))
@@ -136,6 +139,11 @@ impl BuzzClient {
keys,
auth_tag,
auth_tag_json,
api_token: api_token
.as_deref()
.map(str::trim)
.filter(|token| !token.is_empty())
.map(str::to_string),
})
}
@@ -375,13 +383,16 @@ impl BuzzClient {
Duration::from_secs(120)
};
let url = format!("{}/media/upload", self.relay_url);
let req = self
let mut req = self
.http
.put(&url)
.timeout(upload_timeout)
.header("Authorization", &auth_header)
.header("Content-Type", &mime)
.header("X-SHA-256", &sha256);
if let Some(token) = self.api_token.as_deref() {
req = req.header("X-Auth-Token", token);
}
let resp = self.with_auth_tag(req).body(bytes).send().await?;
if !resp.status().is_success() {

View File

@@ -842,7 +842,7 @@ mod tests {
let keys =
nostr::Keys::parse("0000000000000000000000000000000000000000000000000000000000000001")
.expect("valid test key");
BuzzClient::new("ws://localhost:3000".to_string(), keys, None, None)
BuzzClient::new("ws://localhost:3000".to_string(), keys, None, None, None)
.expect("client construction should not fail")
}

View File

@@ -786,7 +786,7 @@ mod tests {
// common quick-check inputs.
fn test_client(keys: nostr::Keys) -> BuzzClient {
BuzzClient::new("http://127.0.0.1:9".into(), keys, None, None).unwrap()
BuzzClient::new("http://127.0.0.1:9".into(), keys, None, None, None).unwrap()
}
#[test]

View File

@@ -56,6 +56,7 @@ Configuration (flags override env vars):
BUZZ_RELAY_URL Relay base URL [default: http://localhost:3000]
BUZZ_PRIVATE_KEY Nostr private key (hex or nsec) [required]
BUZZ_AUTH_TAG NIP-OA auth tag JSON [optional]
BUZZ_API_TOKEN API token for Blossom media upload [optional]
The 'pack' subcommand runs locally and does not require a relay connection.
@@ -75,6 +76,10 @@ struct Cli {
#[arg(long, env = "BUZZ_AUTH_TAG")]
auth_tag: Option<String>,
/// API token attached as X-Auth-Token for Blossom media uploads.
#[arg(long, env = "BUZZ_API_TOKEN")]
api_token: Option<String>,
/// Output format: 'json' (default, full fields) or 'compact' (reduced fields).
#[arg(long, value_enum, default_value = "json")]
format: OutputFormat,
@@ -1420,7 +1425,9 @@ async fn run(cli: Cli) -> Result<(), CliError> {
}
// Auth: private key is required for all relay operations.
// The keypair IS the identity — no tokens, no other auth.
// The keypair is the identity for Nostr/WebSocket auth. `BUZZ_API_TOKEN`,
// when present, is only attached to Blossom media uploads as an additional
// relay-side scope credential.
let private_key_str = cli.private_key.ok_or_else(|| {
CliError::Auth("BUZZ_PRIVATE_KEY is required (use --private-key or set env var)".into())
})?;
@@ -1443,7 +1450,13 @@ async fn run(cli: Cli) -> Result<(), CliError> {
_ => (None, None),
};
let client = BuzzClient::new(relay_url, keys, auth_tag, auth_tag_json)?;
let api_token = cli
.api_token
.as_deref()
.map(str::trim)
.filter(|token| !token.is_empty())
.map(str::to_string);
let client = BuzzClient::new(relay_url, keys, auth_tag, auth_tag_json, api_token)?;
match cli.command {
Cmd::Messages(sub) => commands::messages::dispatch(sub, &client, &cli.format).await,
@@ -1523,6 +1536,20 @@ mod tests {
);
}
#[test]
fn api_token_flag_is_available() {
let cmd = Cli::command();
let api_token = cmd
.get_arguments()
.find(|arg| arg.get_id() == "api_token")
.expect("global api_token argument should exist");
assert_eq!(api_token.get_long(), Some("api-token"));
assert!(api_token
.get_env()
.is_some_and(|env| env == "BUZZ_API_TOKEN"));
}
#[test]
fn subcommand_names_are_stable() {
fn names(cmd: &clap::Command, group: &str) -> Vec<String> {