#[path = "webhook/mod.rs"]
mod webhook;
#[cfg(feature = "webhook")]
pub use webhook::WebhookManagementServiceImpl;
#[cfg(feature = "webhook")]
use super::webhook_event_builder::WebhookEventBuilder;
use crate::domain::models::{Task, Webhook};
use crate::domain::models::{WebhookEvent, WebhookEventType};
#[cfg(feature = "webhook")]
use crate::domain::models::WebhookStatus;
#[cfg(feature = "webhook")]
use crate::application::dto::scrape_request::ScrapeRequestDto;
#[cfg(feature = "webhook")]
use crate::domain::repositories::webhook_event_repository::WebhookEventRepository;
#[cfg(feature = "webhook")]
use crate::domain::services::webhook_sender::WebhookSender;
use crate::infrastructure::security::constant_time_eq_str;
use anyhow::Result;
#[cfg(feature = "webhook")]
use anyhow::anyhow;
use async_trait::async_trait;
use chrono::Utc;
use standardwebhooks::Webhook as StandardWebhook;
#[cfg(feature = "webhook")]
use log::{error, info};
#[cfg(feature = "webhook")]
use std::sync::Arc;
#[async_trait]
pub trait WebhookService: Send + Sync {
async fn send_webhook(&self, event: &WebhookEvent) -> Result<u16>;
async fn trigger_completion(&self, task: &Task) -> Result<()>;
async fn trigger_failure(&self, task: &Task, error_msg: String) -> Result<()>;
}
#[cfg(feature = "webhook")]
pub struct WebhookServiceImpl {
webhook_sender: Arc<dyn WebhookSender>,
secret: String,
repository: Arc<dyn WebhookEventRepository>,
}
#[cfg(feature = "webhook")]
impl WebhookServiceImpl {
pub fn new(
webhook_sender: Arc<dyn WebhookSender>,
secret: String,
repository: Arc<dyn WebhookEventRepository>,
) -> Self {
Self {
webhook_sender,
secret,
repository,
}
}
fn generate_signature(&self, msg_id: &str, payload: &str, timestamp: i64) -> Option<String> {
let wh = match StandardWebhook::from_bytes(self.secret.as_bytes().to_vec()) {
Ok(w) => w,
Err(e) => {
log::error!("Failed to create standardwebhook: {:?}", e);
return None;
}
};
wh.sign(msg_id, timestamp, payload.as_bytes())
.map_err(|e| {
log::error!("Failed to sign webhook payload: {:?}", e);
e
})
.ok()
}
fn extract_webhook_url(&self, task: &Task) -> Option<String> {
if let Ok(req) = serde_json::from_value::<ScrapeRequestDto>(task.payload.clone()) {
return req.webhook;
}
task.payload
.get("webhook")
.and_then(|v| v.as_str())
.map(|s| s.to_string())
}
fn get_event_type(&self, task: &Task) -> WebhookEventType {
match task.task_type.as_str() {
"scrape" => WebhookEventType::ScrapeCompleted,
"crawl" => WebhookEventType::CrawlCompleted,
_ => WebhookEventType::Custom("extract.completed".to_string()),
}
}
fn get_failed_event_type(&self, task: &Task) -> WebhookEventType {
match task.task_type.as_str() {
"scrape" => WebhookEventType::ScrapeFailed,
"crawl" => WebhookEventType::CrawlFailed,
_ => WebhookEventType::Custom("extract.failed".to_string()),
}
}
}
#[cfg(feature = "webhook")]
#[async_trait]
impl WebhookService for WebhookServiceImpl {
async fn send_webhook(&self, event: &WebhookEvent) -> Result<u16> {
let timestamp = chrono::Utc::now().timestamp();
let payload_str = serde_json::to_string(&event.payload)?;
let signature = self
.generate_signature(&event.id.to_string(), &payload_str, timestamp)
.ok_or_else(|| {
anyhow::anyhow!(
"Failed to generate webhook signature for event {}",
event.id
)
})?;
let mut headers = std::collections::HashMap::new();
headers.insert("Content-Type".to_string(), "application/json".to_string());
headers.insert(
standardwebhooks::HEADER_WEBHOOK_SIGNATURE.to_string(),
signature,
);
headers.insert(
standardwebhooks::HEADER_WEBHOOK_TIMESTAMP.to_string(),
timestamp.to_string(),
);
headers.insert(
standardwebhooks::HEADER_WEBHOOK_ID.to_string(),
event.id.to_string(),
);
let status = self
.webhook_sender
.send_with_status(&event.webhook_url, &event.payload, Some(&headers))
.await?;
info!(
"Webhook sent successfully for event {} (status {})",
event.id, status
);
Ok(status)
}
async fn trigger_completion(&self, task: &Task) -> Result<()> {
let webhook_url = match self.extract_webhook_url(task) {
Some(url) => url,
None => {
info!("No webhook URL found for task {}", task.id);
return Ok(());
}
};
let event_type = self.get_event_type(task);
self.send_task_webhook(task, webhook_url, event_type, None)
.await
}
async fn trigger_failure(&self, task: &Task, error_msg: String) -> Result<()> {
let webhook_url = match self.extract_webhook_url(task) {
Some(url) => url,
None => {
info!("No webhook URL found for task {}", task.id);
return Ok(());
}
};
let event_type = self.get_failed_event_type(task);
self.send_task_webhook(task, webhook_url, event_type, Some(error_msg))
.await
}
}
#[cfg(feature = "webhook")]
impl WebhookServiceImpl {
async fn send_task_webhook(
&self,
task: &Task,
webhook_url: String,
event_type: WebhookEventType,
error_msg: Option<String>,
) -> Result<()> {
info!(
"Triggering webhook {:?} for task {} (url: {})",
event_type, task.id, webhook_url
);
let payload = WebhookEventBuilder::build_task_payload(task, error_msg.as_deref());
let mut event =
WebhookEventBuilder::build_task_event(task, event_type, payload, webhook_url);
event.status = WebhookStatus::Processing;
if let Err(e) = self.repository.create(&event).await {
error!("Failed to create webhook event for task {}: {}", task.id, e);
return Err(anyhow!("Failed to create webhook event: {}", e));
}
match self.send_webhook(&event).await {
Ok(status) => {
event.status = WebhookStatus::Delivered;
event.delivered_at = Some(chrono::Utc::now());
event.response_status = Some(status as i32);
if let Err(e) = self.repository.update(&event).await {
error!("Failed to mark webhook event {} delivered: {}", event.id, e);
}
Ok(())
}
Err(e) => {
error!("Failed to send webhook for task {}: {}", task.id, e);
event.status = WebhookStatus::Failed;
event.next_retry_at = Some(chrono::Utc::now() + chrono::Duration::seconds(5));
if let Err(update_err) = self.repository.update(&event).await {
error!(
"Failed to schedule retry for webhook event {}: {}",
event.id, update_err
);
}
Err(e)
}
}
}
}
#[async_trait]
pub trait WebhookManagementService: Send + Sync {
async fn register_webhook(
&self,
team_id: crate::domain::id::Id,
url: String,
) -> Result<Webhook>;
async fn trigger_webhook(
&self,
webhook_id: crate::domain::id::Id,
event_type: WebhookEventType,
payload: serde_json::Value,
) -> Result<()>;
async fn retry_failed(&self, limit: u64) -> Result<u64>;
async fn list_webhooks(&self, team_id: crate::domain::id::Id) -> Result<Vec<Webhook>>;
}
pub const WEBHOOK_AUTH_FAILED: &str = "webhook authentication failed";
const MAX_TIMESTAMP_AGE: i64 = 300;
fn validate_timestamp(timestamp: i64) -> bool {
let now = Utc::now().timestamp();
let diff = (now - timestamp).abs();
diff <= MAX_TIMESTAMP_AGE
}
fn generate_signature(secret: &str, msg_id: &str, payload: &[u8], timestamp: i64) -> String {
let wh = match StandardWebhook::from_bytes(secret.as_bytes().to_vec()) {
Ok(w) => w,
Err(e) => {
log::error!("Failed to create standardwebhook: {:?}", e);
return String::new();
}
};
wh.sign(msg_id, timestamp, payload).unwrap_or_default()
}
pub fn verify_webhook_signature(
secret: &str,
msg_id: &str,
payload: &str,
timestamp: i64,
signature: &str,
) -> bool {
verify_webhook_signature_bytes(secret, msg_id, payload.as_bytes(), timestamp, signature)
}
pub fn verify_webhook_signature_bytes(
secret: &str,
msg_id: &str,
payload: &[u8],
timestamp: i64,
signature: &str,
) -> bool {
if !validate_timestamp(timestamp) {
log::warn!("Webhook timestamp is outside valid window");
return false;
}
let expected_signature = generate_signature(secret, msg_id, payload, timestamp);
constant_time_eq_str(signature, &expected_signature)
}
pub fn verify_webhook_signature_from_parts(
secret: &str,
signature: &str,
timestamp_str: &str,
msg_id: &str,
body: &[u8],
) -> Result<(), &'static str> {
let timestamp: i64 = timestamp_str.parse().map_err(|_| WEBHOOK_AUTH_FAILED)?;
if !verify_webhook_signature_bytes(secret, msg_id, body, timestamp, signature) {
return Err(WEBHOOK_AUTH_FAILED);
}
Ok(())
}
#[cfg(all(test, feature = "webhook"))]
#[path = "tests/webhook_service_test.rs"]
mod tests;