oxify-model 0.1.0

Data models and types for OxiFY workflows, execution, and configuration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
//! Webhook types for event-driven workflow triggering
//!
//! Allows external systems to trigger workflows via HTTP callbacks

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use uuid::Uuid;

pub type WebhookId = Uuid;

/// Webhook configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub struct Webhook {
    /// Unique webhook identifier
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub id: WebhookId,

    /// Webhook name
    pub name: String,

    /// Optional description
    pub description: Option<String>,

    /// Workflow to trigger
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub workflow_id: Uuid,

    /// Webhook secret for HMAC verification
    #[serde(skip_serializing)]
    pub secret: String,

    /// Whether webhook is active
    pub enabled: bool,

    /// Event types to listen for (empty = all)
    pub event_types: Vec<String>,

    /// Custom headers to require
    pub required_headers: HashMap<String, String>,

    /// IP whitelist (empty = no restriction)
    pub ip_whitelist: Vec<String>,

    /// Maximum request body size (bytes)
    pub max_body_size: usize,

    /// Timeout for workflow execution (seconds)
    pub timeout_seconds: u32,

    /// Owner user ID
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub owner_id: Uuid,

    /// Creation timestamp
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub created_at: DateTime<Utc>,

    /// Last updated timestamp
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub updated_at: DateTime<Utc>,

    /// Last triggered timestamp
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub last_triggered_at: Option<DateTime<Utc>>,

    /// Total trigger count
    pub trigger_count: u64,

    /// Failed trigger count
    pub failed_count: u64,
}

impl Webhook {
    /// Create a new webhook
    pub fn new(name: String, workflow_id: Uuid, secret: String, owner_id: Uuid) -> Self {
        let now = Utc::now();
        Self {
            id: Uuid::new_v4(),
            name,
            description: None,
            workflow_id,
            secret,
            enabled: true,
            event_types: Vec::new(),
            required_headers: HashMap::new(),
            ip_whitelist: Vec::new(),
            max_body_size: 1024 * 1024, // 1MB default
            timeout_seconds: 300,       // 5 minutes default
            owner_id,
            created_at: now,
            updated_at: now,
            last_triggered_at: None,
            trigger_count: 0,
            failed_count: 0,
        }
    }

    /// Check if event type matches
    pub fn matches_event(&self, event_type: &str) -> bool {
        self.event_types.is_empty() || self.event_types.contains(&event_type.to_string())
    }

    /// Check if IP is allowed
    pub fn is_ip_allowed(&self, ip: &str) -> bool {
        self.ip_whitelist.is_empty() || self.ip_whitelist.contains(&ip.to_string())
    }

    /// Check required headers
    pub fn validates_headers(&self, headers: &HashMap<String, String>) -> bool {
        for (key, value) in &self.required_headers {
            if headers.get(key) != Some(value) {
                return false;
            }
        }
        true
    }

    /// Increment trigger count
    pub fn increment_trigger(&mut self) {
        self.trigger_count += 1;
        self.last_triggered_at = Some(Utc::now());
    }

    /// Increment failed count
    pub fn increment_failed(&mut self) {
        self.failed_count += 1;
    }

    /// Create safe view without secret
    pub fn to_safe_view(&self) -> WebhookView {
        WebhookView {
            id: self.id,
            name: self.name.clone(),
            description: self.description.clone(),
            workflow_id: self.workflow_id,
            enabled: self.enabled,
            event_types: self.event_types.clone(),
            owner_id: self.owner_id,
            created_at: self.created_at,
            updated_at: self.updated_at,
            last_triggered_at: self.last_triggered_at,
            trigger_count: self.trigger_count,
            failed_count: self.failed_count,
            success_rate: self.success_rate(),
        }
    }

    /// Calculate success rate
    pub fn success_rate(&self) -> f64 {
        if self.trigger_count == 0 {
            return 100.0;
        }
        let successful = self.trigger_count - self.failed_count;
        (successful as f64 / self.trigger_count as f64) * 100.0
    }
}

/// Safe webhook view (no secret)
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub struct WebhookView {
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub id: WebhookId,
    pub name: String,
    pub description: Option<String>,
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub workflow_id: Uuid,
    pub enabled: bool,
    pub event_types: Vec<String>,
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub owner_id: Uuid,
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub created_at: DateTime<Utc>,
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub updated_at: DateTime<Utc>,
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub last_triggered_at: Option<DateTime<Utc>>,
    pub trigger_count: u64,
    pub failed_count: u64,
    pub success_rate: f64,
}

/// Webhook event received
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub struct WebhookEvent {
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub id: Uuid,

    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub webhook_id: WebhookId,

    pub event_type: String,

    pub payload: serde_json::Value,

    pub headers: HashMap<String, String>,

    pub source_ip: String,

    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub received_at: DateTime<Utc>,

    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub processed_at: Option<DateTime<Utc>>,

    pub status: WebhookEventStatus,

    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub execution_id: Option<Uuid>,

    pub error_message: Option<String>,
}

/// Webhook event processing status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub enum WebhookEventStatus {
    Pending,
    Processing,
    Completed,
    Failed,
    Rejected,
}

impl std::fmt::Display for WebhookEventStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            WebhookEventStatus::Pending => write!(f, "PENDING"),
            WebhookEventStatus::Processing => write!(f, "PROCESSING"),
            WebhookEventStatus::Completed => write!(f, "COMPLETED"),
            WebhookEventStatus::Failed => write!(f, "FAILED"),
            WebhookEventStatus::Rejected => write!(f, "REJECTED"),
        }
    }
}

/// Request to create a webhook
#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub struct CreateWebhookRequest {
    pub name: String,
    pub description: Option<String>,
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub workflow_id: Uuid,
    pub event_types: Vec<String>,
    pub required_headers: Option<HashMap<String, String>>,
    pub ip_whitelist: Option<Vec<String>>,
    pub max_body_size: Option<usize>,
    pub timeout_seconds: Option<u32>,
}

/// Request to update a webhook
#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub struct UpdateWebhookRequest {
    pub name: Option<String>,
    pub description: Option<String>,
    pub enabled: Option<bool>,
    pub event_types: Option<Vec<String>>,
    pub required_headers: Option<HashMap<String, String>>,
    pub ip_whitelist: Option<Vec<String>>,
    pub max_body_size: Option<usize>,
    pub timeout_seconds: Option<u32>,
}

/// Webhook trigger request
#[derive(Debug, Serialize, Deserialize)]
pub struct WebhookTriggerRequest {
    pub event_type: String,
    pub payload: serde_json::Value,
}

/// Response after webhook registration
#[derive(Debug, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
pub struct WebhookRegistrationResponse {
    #[cfg_attr(feature = "openapi", schema(value_type = String))]
    pub webhook_id: WebhookId,
    pub webhook_url: String,
    pub secret: String,
    pub created_at: DateTime<Utc>,
}

/// Helper to generate webhook secret
pub fn generate_webhook_secret() -> String {
    use rand::Rng;
    const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    const SECRET_LEN: usize = 32;

    let mut rng = rand::rng();
    (0..SECRET_LEN)
        .map(|_| {
            let idx = rng.random_range(0..CHARSET.len());
            CHARSET[idx] as char
        })
        .collect()
}

/// Helper to verify HMAC signature
pub fn verify_webhook_signature(secret: &str, payload: &[u8], signature: &str) -> bool {
    use hmac::{Hmac, Mac};
    use sha2::Sha256;

    type HmacSha256 = Hmac<Sha256>;

    let mut mac = match HmacSha256::new_from_slice(secret.as_bytes()) {
        Ok(m) => m,
        Err(_) => return false,
    };

    mac.update(payload);

    let expected = format!("sha256={}", hex::encode(mac.finalize().into_bytes()));

    expected == signature
}

/// Helper to create HMAC signature for testing
pub fn create_webhook_signature(secret: &str, payload: &[u8]) -> String {
    use hmac::{Hmac, Mac};
    use sha2::Sha256;

    type HmacSha256 = Hmac<Sha256>;

    let mut mac = HmacSha256::new_from_slice(secret.as_bytes()).expect("Invalid secret");
    mac.update(payload);

    format!("sha256={}", hex::encode(mac.finalize().into_bytes()))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_webhook_creation() {
        let workflow_id = Uuid::new_v4();
        let owner_id = Uuid::new_v4();
        let webhook = Webhook::new(
            "Test Webhook".to_string(),
            workflow_id,
            "test_secret".to_string(),
            owner_id,
        );

        assert_eq!(webhook.name, "Test Webhook");
        assert_eq!(webhook.workflow_id, workflow_id);
        assert_eq!(webhook.owner_id, owner_id);
        assert!(webhook.enabled);
        assert_eq!(webhook.trigger_count, 0);
        assert_eq!(webhook.failed_count, 0);
    }

    #[test]
    fn test_event_type_matching() {
        let mut webhook = Webhook::new(
            "Test".to_string(),
            Uuid::new_v4(),
            "secret".to_string(),
            Uuid::new_v4(),
        );

        // Empty event_types means match all
        assert!(webhook.matches_event("push"));
        assert!(webhook.matches_event("pull_request"));

        // Specific event types
        webhook.event_types = vec!["push".to_string(), "pull_request".to_string()];
        assert!(webhook.matches_event("push"));
        assert!(webhook.matches_event("pull_request"));
        assert!(!webhook.matches_event("release"));
    }

    #[test]
    fn test_ip_whitelist() {
        let mut webhook = Webhook::new(
            "Test".to_string(),
            Uuid::new_v4(),
            "secret".to_string(),
            Uuid::new_v4(),
        );

        // Empty whitelist means allow all
        assert!(webhook.is_ip_allowed("192.168.1.1"));
        assert!(webhook.is_ip_allowed("10.0.0.1"));

        // With whitelist
        webhook.ip_whitelist = vec!["192.168.1.1".to_string(), "10.0.0.0/8".to_string()];
        assert!(webhook.is_ip_allowed("192.168.1.1"));
        assert!(!webhook.is_ip_allowed("172.16.0.1"));
    }

    #[test]
    fn test_header_validation() {
        let mut webhook = Webhook::new(
            "Test".to_string(),
            Uuid::new_v4(),
            "secret".to_string(),
            Uuid::new_v4(),
        );

        // No required headers
        let headers = HashMap::new();
        assert!(webhook.validates_headers(&headers));

        // With required headers
        webhook
            .required_headers
            .insert("X-Custom-Header".to_string(), "expected-value".to_string());

        // Missing header
        assert!(!webhook.validates_headers(&headers));

        // Wrong value
        let mut headers = HashMap::new();
        headers.insert("X-Custom-Header".to_string(), "wrong-value".to_string());
        assert!(!webhook.validates_headers(&headers));

        // Correct value
        headers.insert("X-Custom-Header".to_string(), "expected-value".to_string());
        assert!(webhook.validates_headers(&headers));
    }

    #[test]
    fn test_trigger_counting() {
        let mut webhook = Webhook::new(
            "Test".to_string(),
            Uuid::new_v4(),
            "secret".to_string(),
            Uuid::new_v4(),
        );

        assert_eq!(webhook.trigger_count, 0);
        assert!(webhook.last_triggered_at.is_none());

        webhook.increment_trigger();
        assert_eq!(webhook.trigger_count, 1);
        assert!(webhook.last_triggered_at.is_some());

        webhook.increment_trigger();
        assert_eq!(webhook.trigger_count, 2);

        webhook.increment_failed();
        assert_eq!(webhook.failed_count, 1);
    }

    #[test]
    fn test_success_rate() {
        let mut webhook = Webhook::new(
            "Test".to_string(),
            Uuid::new_v4(),
            "secret".to_string(),
            Uuid::new_v4(),
        );

        // No triggers yet
        assert_eq!(webhook.success_rate(), 100.0);

        // All successful
        webhook.trigger_count = 10;
        webhook.failed_count = 0;
        assert_eq!(webhook.success_rate(), 100.0);

        // 50% success rate
        webhook.trigger_count = 10;
        webhook.failed_count = 5;
        assert_eq!(webhook.success_rate(), 50.0);

        // All failed
        webhook.trigger_count = 10;
        webhook.failed_count = 10;
        assert_eq!(webhook.success_rate(), 0.0);
    }

    #[test]
    fn test_safe_view() {
        let webhook = Webhook::new(
            "Test Webhook".to_string(),
            Uuid::new_v4(),
            "super_secret_value".to_string(),
            Uuid::new_v4(),
        );

        let view = webhook.to_safe_view();

        assert_eq!(view.id, webhook.id);
        assert_eq!(view.name, webhook.name);
        assert_eq!(view.workflow_id, webhook.workflow_id);
        // Note: secret is not in the view struct
    }

    #[test]
    fn test_webhook_secret_generation() {
        let secret1 = generate_webhook_secret();
        let secret2 = generate_webhook_secret();

        // Secrets should be 32 characters
        assert_eq!(secret1.len(), 32);
        assert_eq!(secret2.len(), 32);

        // Secrets should be different
        assert_ne!(secret1, secret2);

        // Should only contain alphanumeric characters
        assert!(secret1.chars().all(|c| c.is_ascii_alphanumeric()));
    }

    #[test]
    fn test_signature_verification() {
        let secret = "test_secret_key";
        let payload = b"test payload data";

        // Create signature
        let signature = create_webhook_signature(secret, payload);

        // Verify should pass
        assert!(verify_webhook_signature(secret, payload, &signature));

        // Wrong secret should fail
        assert!(!verify_webhook_signature(
            "wrong_secret",
            payload,
            &signature
        ));

        // Wrong payload should fail
        assert!(!verify_webhook_signature(
            secret,
            b"wrong payload",
            &signature
        ));

        // Wrong signature format should fail
        assert!(!verify_webhook_signature(
            secret,
            payload,
            "invalid_signature"
        ));
    }

    #[test]
    fn test_webhook_event_status_display() {
        assert_eq!(WebhookEventStatus::Pending.to_string(), "PENDING");
        assert_eq!(WebhookEventStatus::Processing.to_string(), "PROCESSING");
        assert_eq!(WebhookEventStatus::Completed.to_string(), "COMPLETED");
        assert_eq!(WebhookEventStatus::Failed.to_string(), "FAILED");
        assert_eq!(WebhookEventStatus::Rejected.to_string(), "REJECTED");
    }
}