use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Authorization {
pub identifier: crate::order::Identifier,
pub status: AuthorizationStatus,
#[serde(skip_serializing_if = "Option::is_none")]
pub expires: Option<String>,
pub challenges: Vec<Challenge>,
#[serde(skip_serializing_if = "Option::is_none")]
pub wildcard: Option<bool>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub enum AuthorizationStatus {
Pending,
Valid,
Invalid,
Deactivated,
Expired,
Revoked,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Challenge {
#[serde(rename = "type")]
pub challenge_type: String,
pub url: String,
pub status: ChallengeStatus,
pub token: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub validated: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub enum ChallengeStatus {
Pending,
Processing,
Valid,
Invalid,
}
#[derive(Debug, Clone)]
pub struct Http01Challenge {
pub token: String,
pub key_authorization: String,
pub url: String,
}
impl Http01Challenge {
pub fn path(&self) -> String {
format!("/.well-known/acme-challenge/{}", self.token)
}
pub fn content(&self) -> &str {
&self.key_authorization
}
}
#[derive(Debug, Clone)]
pub struct Dns01Challenge {
pub token: String,
pub dns_value: String,
pub url: String,
}
impl Dns01Challenge {
pub fn record_name(&self, domain: &str) -> String {
format!("_acme-challenge.{}", domain)
}
pub fn record_value(&self) -> &str {
&self.dns_value
}
}
#[derive(Debug, Clone)]
pub struct TlsAlpn01Challenge {
pub token: String,
pub key_authorization: String,
pub url: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_http01_challenge_path() {
let challenge = Http01Challenge {
token: "test_token".to_string(),
key_authorization: "test_key_auth".to_string(),
url: "https://example.com/challenge".to_string(),
};
assert_eq!(challenge.path(), "/.well-known/acme-challenge/test_token");
assert_eq!(challenge.content(), "test_key_auth");
}
#[test]
fn test_dns01_challenge_record() {
let challenge = Dns01Challenge {
token: "test_token".to_string(),
dns_value: "test_dns_value".to_string(),
url: "https://example.com/challenge".to_string(),
};
assert_eq!(
challenge.record_name("example.com"),
"_acme-challenge.example.com"
);
assert_eq!(challenge.record_value(), "test_dns_value");
}
#[test]
fn test_challenge_status_serialization() {
let status = ChallengeStatus::Valid;
let json = serde_json::to_string(&status).unwrap();
assert_eq!(json, "\"valid\"");
}
}