use super::eligibility::Ineligibility;
use super::vault::UseAnswer;
pub const SECRET_REFUSED: &str = "secret_refused";
pub const SECRETS_UNAVAILABLE: &str = "secrets_unavailable";
pub const SECRET_ACCESS_PENDING: &str = "access_pending";
pub const SECRET_ACCESS_UNAVAILABLE: &str = "access_unavailable";
pub const SECRET_ACCESS_PENDING_MESSAGE: &str = "Waiting for approval to use a secret. The request has gone to the user. End your turn now; you will be resumed with the answer, and then you should run this same call again.";
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum LabelOutcome {
Proceed,
FileConsent {
request_id: String,
statement_b64url: String,
},
Refuse {
code: &'static str,
message: String,
},
}
impl LabelOutcome {
pub fn proceeds(&self) -> bool {
matches!(self, LabelOutcome::Proceed)
}
pub fn code(&self) -> Option<&'static str> {
match self {
LabelOutcome::Proceed => None,
LabelOutcome::FileConsent { .. } => Some(SECRET_ACCESS_PENDING),
LabelOutcome::Refuse { code, .. } => Some(code),
}
}
}
pub fn refuse(code: &'static str, label: &str, detail: &str) -> LabelOutcome {
let message = match code {
SECRET_REFUSED => format!(
"Secret label {label} was refused: this call's scope may not use secrets ({detail})."
),
SECRETS_UNAVAILABLE => format!(
"Secret label {label} could not be resolved: the vault is not available ({detail}). No command was started."
),
_ => format!("Secret label {label} was refused ({code}): {detail}."),
};
LabelOutcome::Refuse { code, message }
}
pub fn refuse_ineligible(label: &str, reason: Ineligibility) -> LabelOutcome {
let detail = match reason {
Ineligibility::NoScope => "the call carries no daemon stamp",
Ineligibility::Standalone => "the call is not on a stamped route",
Ineligibility::PluginOnlyRoute => "the call is on a plugin-only route",
Ineligibility::OtherOwner => "the call's scope is not a prefrontal-core scope",
Ineligibility::FlowRead => "the call is a read-only or flow-read call",
Ineligibility::EphemeralOutsideCodemode => "the call's stamp is not a command lifetime",
Ineligibility::WorkerRunSession => "the call's run belongs to a worker, not a head",
Ineligibility::NotACommandScope => "the call's stamp kind is not a command's",
Ineligibility::RunNotBound => "the codemode run is not bound to a live head route",
Ineligibility::HeadScopeNotAlfonso => "the run's head scope is not a prefrontal-core head",
};
refuse(SECRET_REFUSED, label, detail)
}
pub fn outcome_for(label: &str, resolution: &UseAnswer) -> LabelOutcome {
match resolution {
UseAnswer::Released(_) | UseAnswer::NotFound => LabelOutcome::Proceed,
UseAnswer::ApprovalRequired(request) => LabelOutcome::FileConsent {
request_id: request.request_id.clone(),
statement_b64url: request.statement_b64url.clone(),
},
UseAnswer::Unavailable => refuse(
SECRETS_UNAVAILABLE,
label,
"no vault is connected in this build",
),
}
}
pub fn outcome_for_second_use(label: &str, resolution: &UseAnswer) -> LabelOutcome {
match resolution {
UseAnswer::ApprovalRequired(_) => refuse(
SECRETS_UNAVAILABLE,
label,
"the vault is still waiting after the consent answer",
),
other => outcome_for(label, other),
}
}
pub fn first_failure(outcomes: &[LabelOutcome]) -> Option<&LabelOutcome> {
outcomes.iter().find(|outcome| !outcome.proceeds())
}