use std::collections::HashMap;
use std::io::Write;
use std::path::Path;
use datumesh_core::lexical;
use crate::config::ConfigError;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Principal {
pub id: String,
pub per_minute: Option<u32>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ApiKeys {
by_hash: HashMap<String, Principal>,
}
pub const KEY_PREFIX: &str = "dm_";
fn valid_principal(id: &str) -> bool {
!id.is_empty()
&& id.len() <= 64
&& id.bytes().all(|b| b.is_ascii_alphanumeric() || matches!(b, b'-' | b'_' | b'.' | b'@'))
}
impl ApiKeys {
pub fn parse(bytes: &[u8]) -> Result<ApiKeys, ConfigError> {
let text = std::str::from_utf8(bytes).map_err(|_| ConfigError::new("API keys", "not UTF-8"))?;
let mut by_hash = HashMap::new();
for (i, line) in text.lines().enumerate() {
let line = line.split('#').next().unwrap_or("").trim();
if line.is_empty() {
continue;
}
let bad = || ConfigError::new("API keys", format!("line {}: <sha256> <principal> [per minute]", i + 1));
let mut parts = line.split_whitespace();
let hash = parts.next().ok_or_else(bad)?;
let id = parts.next().ok_or_else(bad)?;
let per_minute = match parts.next() {
Some(n) => Some(n.parse::<u32>().map_err(|_| bad())?),
None => None,
};
if parts.next().is_some() || lexical::check_hex(hash, 64, "$").is_err() || !valid_principal(id) {
return Err(bad());
}
by_hash.insert(hash.to_string(), Principal { id: id.to_string(), per_minute });
}
Ok(ApiKeys { by_hash })
}
pub fn principal(&self, key: &str) -> Option<&Principal> {
if !key.starts_with(KEY_PREFIX) || key.len() > 256 {
return None;
}
self.by_hash.get(&datumesh_core::sha256_hex(key.as_bytes()))
}
pub fn insert_hash(&mut self, sha256: &str, principal: Principal) {
self.by_hash.insert(sha256.to_string(), principal);
}
pub fn len(&self) -> usize {
self.by_hash.len()
}
pub fn is_empty(&self) -> bool {
self.by_hash.is_empty()
}
}
pub fn new_key() -> Result<String, ConfigError> {
let mut b = [0u8; 32];
getrandom::fill(&mut b).map_err(|e| ConfigError::new("random", e.to_string()))?;
Ok(format!("{KEY_PREFIX}{}", lexical::to_hex(&b)))
}
pub fn create_key(keys_file: &Path, principal: &str, per_minute: Option<u32>, out: &Path) -> Result<(), ConfigError> {
if !valid_principal(principal) {
return Err(ConfigError::new("principal", "letters, digits, -, _, ., @; at most 64"));
}
if keys_file.exists() {
crate::config::read_secret_file(keys_file)?;
}
let key = new_key()?;
let mut opts = std::fs::OpenOptions::new();
opts.write(true).create_new(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
opts.mode(0o600);
}
let mut f = opts.open(out).map_err(|e| ConfigError::new(&out.display().to_string(), e.to_string()))?;
f.write_all(format!("{key}\n").as_bytes()).map_err(|e| ConfigError::new("key file", e.to_string()))?;
let mut opts = std::fs::OpenOptions::new();
opts.append(true).create(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
opts.mode(0o600);
}
let mut k = opts.open(keys_file).map_err(|e| ConfigError::new(&keys_file.display().to_string(), e.to_string()))?;
let line = match per_minute {
Some(n) => format!("{} {principal} {n}\n", datumesh_core::sha256_hex(key.as_bytes())),
None => format!("{} {principal}\n", datumesh_core::sha256_hex(key.as_bytes())),
};
k.write_all(line.as_bytes()).map_err(|e| ConfigError::new("keys file", e.to_string()))?;
Ok(())
}