use base64::{engine::general_purpose::STANDARD, Engine as _};
pub fn parse_basic(header_value: &str) -> Option<(String, String)> {
let encoded = header_value.strip_prefix("Basic ")?;
let decoded = STANDARD.decode(encoded.trim().as_bytes()).ok()?;
let s = std::str::from_utf8(&decoded).ok()?;
let (user, pass) = s.split_once(':')?;
Some((user.to_string(), pass.to_string()))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_well_formed_basic_header() {
let got = parse_basic("Basic YWxpY2U6aHVudGVyMg==").unwrap();
assert_eq!(got, ("alice".to_string(), "hunter2".to_string()));
}
#[test]
fn rejects_non_basic_scheme() {
assert!(parse_basic("Bearer xxx").is_none());
}
#[test]
fn rejects_missing_colon() {
let b64 = STANDARD.encode("no_colon");
assert!(parse_basic(&format!("Basic {}", b64)).is_none());
}
}