Skip to main content

faucet_source_rest/auth/
basic.rs

1//! HTTP Basic authentication.
2
3use base64::Engine;
4use faucet_core::FaucetError;
5use reqwest::header::{HeaderMap, HeaderValue};
6
7pub fn apply(headers: &mut HeaderMap, username: &str, password: &str) -> Result<(), FaucetError> {
8    let encoded =
9        base64::engine::general_purpose::STANDARD.encode(format!("{username}:{password}"));
10    let mut val = HeaderValue::from_str(&format!("Basic {encoded}"))
11        .map_err(|e| FaucetError::Auth(format!("invalid basic auth value: {e}")))?;
12    // Mark the credential sensitive so reqwest/hyper redact it from trace/debug
13    // logging (#78 LOW).
14    val.set_sensitive(true);
15    headers.insert("Authorization", val);
16    Ok(())
17}