use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Confirmation {
Confirmed,
Phrase(String),
Asked(super::asked::Approval),
}
#[must_use]
pub fn argument_digest(argument: Option<&str>) -> [u8; 32] {
let mut hasher = blake3::Hasher::new();
match argument {
Some(value) => {
hasher.update(b"some\0");
hasher.update(value.as_bytes());
}
None => {
hasher.update(b"none\0");
}
}
*hasher.finalize().as_bytes()
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ConfirmationQuestion {
pub id: [u8; 32],
pub device_label: String,
#[serde(default)]
pub agent: String,
pub workspace: String,
pub title: String,
pub action_label: String,
pub resource_label: String,
pub arguments: Vec<(String, String)>,
pub risk: String,
pub kind: String,
pub undoable: bool,
#[serde(default)]
pub ask: Option<PersonAsk>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConfirmationReply {
AllowOnce,
Deny,
Replied(String),
}
pub const ASK_PROVIDER: &str = "tilo.person";
pub const ASK_RESOURCE: &str = "person";
pub const ASK: &str = "ask";
pub const MAX_ASK_CHARS: usize = 2_000;
pub const MAX_ASK_CHOICES: usize = 8;
pub const MAX_CHOICE_CHARS: usize = 80;
pub const ASK_WITH_CHOICES: &str = "choices: a question is answered only with one of its \
choices, at least one. To hear your person in their own words, ask in the conversation \
and read their reply there. For a password, a key or another secret, use the vault's \
`ask` (Ask your person for a secret).";
#[must_use]
pub fn ask_target() -> super::RemoteActionTarget {
super::RemoteActionTarget {
provider: ASK_PROVIDER.to_owned(),
address: super::ActionAddress::new(ASK_RESOURCE, ASK),
}
}
#[must_use]
pub fn is_ask(target: &super::RemoteActionTarget) -> bool {
target.provider == ASK_PROVIDER
&& target.address.resource == ASK_RESOURCE
&& target.address.action == ASK
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct PersonAsk {
pub question: String,
pub choices: Vec<String>,
}
impl PersonAsk {
pub fn checked(mut self) -> Result<Self, String> {
let question = self.question.trim().to_owned();
self.question = question;
if self.question.is_empty() || self.question.chars().count() > MAX_ASK_CHARS {
return Err(format!("question: 1 to {MAX_ASK_CHARS} characters"));
}
if self.choices.is_empty() {
return Err(ASK_WITH_CHOICES.to_owned());
}
if self.choices.len() > MAX_ASK_CHOICES {
return Err(format!("choices: at most {MAX_ASK_CHOICES}"));
}
for choice in &mut self.choices {
*choice = choice.trim().to_owned();
}
if self.choices.iter().any(|choice| {
choice.is_empty()
|| choice.chars().count() > MAX_CHOICE_CHARS
|| choice.chars().any(char::is_control)
}) {
return Err(format!(
"choices: each 1 to {MAX_CHOICE_CHARS} characters on one line"
));
}
let mut seen = std::collections::HashSet::new();
if !self.choices.iter().all(|choice| seen.insert(choice)) {
return Err("choices: each once".to_owned());
}
Ok(self)
}
#[must_use]
pub fn answered_by(&self, reply: &str) -> bool {
let reply = reply.trim();
self.choices.iter().any(|choice| choice == reply)
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PersonAnswer {
Answered { choice: String },
Declined,
}
#[must_use]
pub(crate) fn unanswered(
correlation: u64,
error: super::ActionCallError,
) -> super::RemoteActionMessage {
super::RemoteActionMessage::ConfirmationResponse {
correlation,
result: Err(error),
}
}