Skip to content

Commit

Permalink
--basic-auth-file and WEBSOCAT_BASIC_AUTH
Browse files Browse the repository at this point in the history
Resolves #240.
  • Loading branch information
vi committed May 9, 2024
1 parent 8010e5b commit e289e9f
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extern crate http_bytes;
use http_bytes::http;

use std::net::{IpAddr, SocketAddr};
use std::path::PathBuf;

use structopt::StructOpt;

Expand Down Expand Up @@ -522,10 +523,15 @@ struct Opt {
#[structopt(long = "--no-async-stdio")]
pub noasyncstdio: bool,

/// Add `Authorization: Basic` HTTP request header with this base64-encoded parameter
/// Add `Authorization: Basic` HTTP request header with this base64-encoded parameter.
/// Also available as `WEBSOCAT_BASIC_AUTH` environment variable
#[structopt(long = "--basic-auth")]
pub basic_auth: Option<String>,

/// Add `Authorization: Basic` HTTP request header base64-encoded content of the specified file
#[structopt(long = "--basic-auth-file")]
pub basic_auth_file: Option<PathBuf>,

/// [A] Wait for reading to finish before closing foreachmsg:'s peer
#[structopt(long = "--foreachmsg-wait-read")]
pub foreachmsg_wait_reads: bool,
Expand Down Expand Up @@ -1014,7 +1020,26 @@ fn run() -> Result<()> {
opts.request_headers.push((http::header::USER_AGENT, http::header::HeaderValue::from_bytes(x.as_bytes()).unwrap()));
}

let mut basic_auth_content : Option<String> = None;

if let Some(ba) = cmd.basic_auth {
basic_auth_content = Some(ba);
}
if let Ok(ba) = std::env::var("WEBSOCAT_BASIC_AUTH") {
if basic_auth_content.is_some() {
return Err("Multiple request basic auth options specified simultaneously")?;
}
basic_auth_content = Some(ba);
}
if let Some(baf) = cmd.basic_auth_file {
if basic_auth_content.is_some() {
return Err("Multiple request basic auth options specified simultaneously")?;
}
let x = std::fs::read_to_string(&baf).map_err(|e|{error!("Failed to read `{:?}`", baf); e})?;
basic_auth_content = Some(x.trim().to_owned());
}

if let Some(ba) = basic_auth_content {
let x = base64::encode(&ba);
let q = format!("Basic {}", x);
opts.custom_headers.push(("Authorization".to_owned(), q.as_bytes().to_vec()));
Expand Down

0 comments on commit e289e9f

Please sign in to comment.