use std::io;
use std::path::PathBuf;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use base64::Engine;
use sha2::{Digest, Sha256};
use crate::{secure_fs, ConfigPaths};
pub fn token_file(paths: &ConfigPaths) -> PathBuf {
paths.private_dir().join("mcp-http.token")
}
pub fn tls_dir(paths: &ConfigPaths) -> PathBuf {
paths.private_dir().join("mcp-tls")
}
pub fn self_signed_cert(paths: &ConfigPaths) -> PathBuf {
tls_dir(paths).join("cert.pem")
}
pub fn self_signed_key(paths: &ConfigPaths) -> PathBuf {
tls_dir(paths).join("key.pem")
}
pub fn generate_token() -> io::Result<String> {
let mut bytes = [0u8; 32];
getrandom::fill(&mut bytes).map_err(|e| io::Error::other(e.to_string()))?;
Ok(URL_SAFE_NO_PAD.encode(bytes))
}
pub fn read_token(paths: &ConfigPaths) -> Option<String> {
let text = std::fs::read_to_string(token_file(paths)).ok()?;
let text = text.trim();
(!text.is_empty()).then(|| text.to_string())
}
pub fn rotate_token(paths: &ConfigPaths) -> io::Result<String> {
let token = generate_token()?;
write_token(paths, &token)?;
Ok(token)
}
fn write_token(paths: &ConfigPaths, token: &str) -> io::Result<()> {
secure_fs::create_private_dir_all(&paths.private_dir())?;
secure_fs::write_private(&token_file(paths), format!("{token}\n").as_bytes())
}
pub fn ensure_token(paths: &ConfigPaths) -> io::Result<(String, bool)> {
match read_token(paths) {
Some(token) => Ok((token, false)),
None => rotate_token(paths).map(|t| (t, true)),
}
}
pub fn token_hint(token: &str) -> String {
let chars: Vec<char> = token.chars().collect();
if chars.len() < 12 {
return "*".repeat(chars.len().max(4));
}
let head: String = chars[..4].iter().collect();
let tail: String = chars[chars.len() - 4..].iter().collect();
format!("{head}...{tail}")
}
pub use crate::osc52::osc52;
pub fn fingerprint_of_pem(pem: &str) -> Option<String> {
let begin = "-----BEGIN CERTIFICATE-----";
let end = "-----END CERTIFICATE-----";
let start = pem.find(begin)? + begin.len();
let stop = start + pem[start..].find(end)?;
let body: String = pem[start..stop]
.chars()
.filter(|c| !c.is_whitespace())
.collect();
let der = base64::engine::general_purpose::STANDARD
.decode(body)
.ok()?;
let digest = Sha256::digest(&der);
Some(
digest
.iter()
.map(|b| format!("{b:02X}"))
.collect::<Vec<_>>()
.join(":"),
)
}
pub fn fingerprint_of_file(path: &std::path::Path) -> Option<String> {
fingerprint_of_pem(&std::fs::read_to_string(path).ok()?)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_token_is_created_once_and_rotated_on_request() {
let dir = tempfile::tempdir().unwrap();
let paths = ConfigPaths::for_dir(dir.path());
assert!(read_token(&paths).is_none());
let (first, created) = ensure_token(&paths).unwrap();
assert!(created);
assert_eq!(first.len(), 43, "32 bytes, unpadded base64url");
let (again, created) = ensure_token(&paths).unwrap();
assert!(!created);
assert_eq!(first, again);
let rotated = rotate_token(&paths).unwrap();
assert_ne!(rotated, first);
assert_eq!(read_token(&paths).unwrap(), rotated);
}
#[cfg(unix)]
#[test]
fn the_token_file_is_user_only() {
use std::os::unix::fs::PermissionsExt;
let dir = tempfile::tempdir().unwrap();
let paths = ConfigPaths::for_dir(dir.path());
ensure_token(&paths).unwrap();
let mode = std::fs::metadata(token_file(&paths))
.unwrap()
.permissions()
.mode();
assert_eq!(mode & 0o777, 0o600);
}
#[test]
fn the_hint_never_shows_the_middle() {
let hint = token_hint("abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG");
assert_eq!(hint, "abcd...DEFG");
assert!(!token_hint("short").contains("sh"));
}
#[test]
fn a_fingerprint_is_the_sha256_of_the_der() {
let pem = "-----BEGIN CERTIFICATE-----\nYWJj\n-----END CERTIFICATE-----\n";
assert_eq!(
fingerprint_of_pem(pem).unwrap(),
"BA:78:16:BF:8F:01:CF:EA:41:41:40:DE:5D:AE:22:23:B0:03:61:A3:96:17:7A:9C:B4:10:FF:61:F2:00:15:AD"
);
assert!(fingerprint_of_pem("no certificate here").is_none());
}
}