use crate::{Secret, SecretError};
mod chromium;
pub use chromium::{ChromiumBrand, ChromiumKey};
pub struct NextcloudCredential {
key: String,
}
impl NextcloudCredential {
pub fn new(server: &str, username: &str, account_id: &str) -> Result<Self, SecretError> {
if !server.starts_with("https://")
|| server.len() > 2048
|| server.contains(['?', '#', '@'])
|| username.is_empty()
|| username.len() > 256
|| username.contains(':')
|| account_id.is_empty()
|| account_id.len() > 128
|| !account_id
.bytes()
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_'))
|| server.chars().chain(username.chars()).any(char::is_control)
{
return Err(SecretError::Denied);
}
let separator = if server.ends_with('/') { "" } else { "/" };
Ok(Self {
key: format!("{username}:{server}{separator}:{account_id}"),
})
}
pub fn read_blocking(&self) -> Result<Option<Secret>, SecretError> {
crate::linux::read_nextcloud(&self.key)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn native_account_key_is_exact_and_rejects_unbound_queries() {
assert_eq!(
NextcloudCredential::new("https://cloud.test/nextcloud", "ada", "0")
.unwrap()
.key,
"ada:https://cloud.test/nextcloud/:0"
);
assert_eq!(
NextcloudCredential::new("https://cloud.test/", "ada", "7")
.unwrap()
.key,
"ada:https://cloud.test/:7"
);
for (server, user, id) in [
("http://cloud.test", "ada", "0"),
("https://cloud.test/#x", "ada", "0"),
("https://cloud.test", "", "0"),
("https://cloud.test", "ada", ""),
] {
assert!(NextcloudCredential::new(server, user, id).is_err());
}
}
}