zebra_rpc/server/
cookie.rsuse base64::{engine::general_purpose::URL_SAFE, Engine as _};
use color_eyre::Result;
use rand::RngCore;
use std::{
fs::{remove_file, File},
io::Write,
path::Path,
};
const FILE: &str = ".cookie";
#[derive(Clone, Debug)]
pub struct Cookie(String);
impl Cookie {
pub fn authenticate(&self, passwd: String) -> bool {
*passwd == self.0
}
}
impl Default for Cookie {
fn default() -> Self {
let mut bytes = [0u8; 32];
rand::thread_rng().fill_bytes(&mut bytes);
Self(URL_SAFE.encode(bytes))
}
}
pub fn write_to_disk(cookie: &Cookie, dir: &Path) -> Result<()> {
std::fs::create_dir_all(dir)?;
File::create(dir.join(FILE))?.write_all(format!("__cookie__:{}", cookie.0).as_bytes())?;
tracing::info!("RPC auth cookie written to disk");
Ok(())
}
pub fn remove_from_disk(dir: &Path) -> Result<()> {
remove_file(dir.join(FILE))?;
tracing::info!("RPC auth cookie removed from disk");
Ok(())
}