use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use time::OffsetDateTime;
use crate::ProviderId;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum FailureReason {
MissingToken,
InvalidToken,
AlreadyUsed,
Expired,
HostnameMismatch,
ActionMismatch,
ScoreTooLow,
Other(String),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct VerificationResult {
pub valid: bool,
pub provider: ProviderId,
pub score: Option<f64>,
pub action: Option<String>,
pub hostname: Option<String>,
pub challenge_time: Option<OffsetDateTime>,
pub reasons: Vec<FailureReason>,
pub metadata: BTreeMap<String, Value>,
}
impl VerificationResult {
#[must_use]
pub fn valid(provider: ProviderId) -> Self {
Self::new(true, provider)
}
#[must_use]
pub fn invalid(provider: ProviderId, reasons: Vec<FailureReason>) -> Self {
Self {
reasons,
..Self::new(false, provider)
}
}
fn new(valid: bool, provider: ProviderId) -> Self {
Self {
valid,
provider,
score: None,
action: None,
hostname: None,
challenge_time: None,
reasons: Vec::new(),
metadata: BTreeMap::new(),
}
}
}