pub fn extract_bearer_from_clipboard(s: &str) -> Option<String> {
let trimmed = s.trim();
if trimmed.is_empty() {
return None;
}
let lower = trimmed.to_ascii_lowercase();
if let Some(idx) = lower.find("bearer ") {
let after = &trimmed[idx + "bearer ".len()..];
let token = after
.split(|c: char| c.is_whitespace())
.next()
.unwrap_or("")
.trim_matches(|c: char| c == '"' || c == '\'');
if !token.is_empty() {
return Some(token.to_string());
}
}
let single = trimmed
.split(|c: char| c.is_whitespace())
.next()
.unwrap_or("");
if single.len() == trimmed.len() && !single.is_empty() {
return Some(single.to_string());
}
None
}
pub fn replace_bearer_in_curl(curl_text: &str, new_token: &str) -> Option<String> {
let lower = curl_text.to_ascii_lowercase();
let needle = "authorization: bearer ";
let idx = lower.find(needle)?;
let token_start = idx + needle.len();
let rest = &curl_text[token_start..];
let token_end_rel = rest.find(['\'', '"', '\n']).unwrap_or(rest.len());
let token_end = token_start + token_end_rel;
let mut out = String::with_capacity(curl_text.len() + new_token.len());
out.push_str(&curl_text[..token_start]);
out.push_str(new_token);
out.push_str(&curl_text[token_end..]);
Some(out)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn extract_raw() {
assert_eq!(
extract_bearer_from_clipboard("eyJabc.def.ghi"),
Some("eyJabc.def.ghi".to_string())
);
}
#[test]
fn extract_with_prefix() {
assert_eq!(
extract_bearer_from_clipboard("Bearer eyJabc.def.ghi"),
Some("eyJabc.def.ghi".to_string())
);
assert_eq!(
extract_bearer_from_clipboard("Authorization: Bearer eyJabc.def.ghi"),
Some("eyJabc.def.ghi".to_string())
);
assert_eq!(
extract_bearer_from_clipboard("authorization: bearer eyJabc.def.ghi\n"),
Some("eyJabc.def.ghi".to_string())
);
}
#[test]
fn replace_in_curl() {
let input = "curl 'https://x.com/' \\\n -H 'Authorization: Bearer oldtoken'";
let out = replace_bearer_in_curl(input, "newtoken").unwrap();
assert!(out.contains("Bearer newtoken"));
assert!(!out.contains("oldtoken"));
}
#[test]
fn replace_lower_case() {
let input = "curl 'https://x.com/' -H 'authorization: bearer abc.def.ghi'";
let out = replace_bearer_in_curl(input, "xxx").unwrap();
assert!(out.contains("bearer xxx"));
}
#[test]
fn no_bearer_returns_none() {
let input = "curl 'https://x.com/' -H 'X-Api-Key: foo'";
assert!(replace_bearer_in_curl(input, "xxx").is_none());
}
}