oximedia-workflow 0.2.0

Comprehensive workflow orchestration engine for OxiMedia
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
//! HTTP webhook trigger system for OxiMedia workflows.
//!
//! This module provides a self-contained webhook trigger registry that maps
//! incoming HTTP requests to workflow IDs. It is transport-agnostic: callers
//! construct a [`WebhookPayload`] from whatever HTTP framework they use and
//! pass it to [`WebhookTriggerRegistry::match_trigger`].
//!
//! # Signature verification
//!
//! When a [`WebhookTrigger`] is configured with a `secret_token`, the registry
//! validates the `X-Signature` request header against a deterministic XOR-based
//! MAC of the raw body. The MAC is **not** cryptographically secure (it is a
//! lightweight stub for integration testing); production deployments should use
//! the full HMAC-SHA256 implementation in [`crate::triggers`].
//!
//! # Example
//!
//! ```rust
//! use oximedia_workflow::webhook_trigger::{
//!     WebhookTrigger, WebhookTriggerRegistry, WebhookPayload,
//! };
//!
//! let mut registry = WebhookTriggerRegistry::new();
//! registry.add_trigger(WebhookTrigger {
//!     id: "t1".to_string(),
//!     path: "/webhooks/ingest".to_string(),
//!     workflow_id: "wf-ingest".to_string(),
//!     secret_token: None,
//! });
//!
//! let payload = WebhookPayload {
//!     method: "POST".to_string(),
//!     path: "/webhooks/ingest".to_string(),
//!     body: "{}".to_string(),
//!     headers: vec![],
//! };
//!
//! let trigger = registry.match_trigger(&payload);
//! assert!(trigger.is_some());
//! assert_eq!(trigger.unwrap().workflow_id, "wf-ingest");
//! ```

#![allow(dead_code)]

// ---------------------------------------------------------------------------
// WebhookTrigger
// ---------------------------------------------------------------------------

/// A registered webhook trigger that binds an HTTP path to a workflow.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WebhookTrigger {
    /// Unique identifier for this trigger.
    pub id: String,
    /// HTTP path that activates the trigger (e.g. `"/webhooks/ingest-ready"`).
    pub path: String,
    /// ID of the workflow to start when this trigger fires.
    pub workflow_id: String,
    /// Optional shared secret used to validate `X-Signature` headers.
    ///
    /// When set the header value must match `sha256=<xor_mac(body, secret)>`.
    pub secret_token: Option<String>,
}

impl WebhookTrigger {
    /// Create a new trigger without a secret.
    #[must_use]
    pub fn new(
        id: impl Into<String>,
        path: impl Into<String>,
        workflow_id: impl Into<String>,
    ) -> Self {
        Self {
            id: id.into(),
            path: path.into(),
            workflow_id: workflow_id.into(),
            secret_token: None,
        }
    }

    /// Attach a shared secret to this trigger.
    #[must_use]
    pub fn with_secret(mut self, secret: impl Into<String>) -> Self {
        self.secret_token = Some(secret.into());
        self
    }
}

// ---------------------------------------------------------------------------
// WebhookPayload
// ---------------------------------------------------------------------------

/// Represents an incoming HTTP request delivered to a webhook endpoint.
#[derive(Debug, Clone)]
pub struct WebhookPayload {
    /// HTTP method (e.g. `"POST"`).
    pub method: String,
    /// URL path of the request.
    pub path: String,
    /// Raw request body as a UTF-8 string.
    pub body: String,
    /// Request headers as `(name, value)` pairs.  Header names should be
    /// lower-cased for case-insensitive comparison.
    pub headers: Vec<(String, String)>,
}

impl WebhookPayload {
    /// Look up a header value by name (case-insensitive).
    #[must_use]
    pub fn header(&self, name: &str) -> Option<&str> {
        let lower = name.to_lowercase();
        self.headers
            .iter()
            .find(|(k, _)| k.to_lowercase() == lower)
            .map(|(_, v)| v.as_str())
    }
}

// ---------------------------------------------------------------------------
// WebhookTriggerRegistry
// ---------------------------------------------------------------------------

/// In-process registry that matches incoming [`WebhookPayload`]s to registered
/// [`WebhookTrigger`]s.
#[derive(Debug, Default)]
pub struct WebhookTriggerRegistry {
    /// All registered triggers, in insertion order.
    pub triggers: Vec<WebhookTrigger>,
}

impl WebhookTriggerRegistry {
    /// Create an empty registry.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Register a new trigger.  If a trigger with the same `id` already exists
    /// it is replaced.
    pub fn add_trigger(&mut self, trigger: WebhookTrigger) {
        if let Some(pos) = self.triggers.iter().position(|t| t.id == trigger.id) {
            self.triggers[pos] = trigger;
        } else {
            self.triggers.push(trigger);
        }
    }

    /// Remove the trigger with the given id, returning it if present.
    pub fn remove_trigger(&mut self, id: &str) -> Option<WebhookTrigger> {
        if let Some(pos) = self.triggers.iter().position(|t| t.id == id) {
            Some(self.triggers.remove(pos))
        } else {
            None
        }
    }

    /// Return the first trigger whose `path` exactly matches
    /// `payload.path`.
    ///
    /// If the matching trigger has a `secret_token`, the `X-Signature` header
    /// of the payload is also validated via [`validate_signature`].  A trigger
    /// whose signature check **fails** is skipped (the next matching trigger is
    /// tried instead).
    #[must_use]
    pub fn match_trigger(&self, payload: &WebhookPayload) -> Option<&WebhookTrigger> {
        for trigger in &self.triggers {
            if trigger.path != payload.path {
                continue;
            }
            // If a secret is configured, validate the signature header.
            if let Some(ref secret) = trigger.secret_token {
                if !validate_signature(payload, secret) {
                    continue;
                }
            }
            return Some(trigger);
        }
        None
    }

    /// Return the number of registered triggers.
    #[must_use]
    pub fn len(&self) -> usize {
        self.triggers.len()
    }

    /// Return `true` when no triggers are registered.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.triggers.is_empty()
    }
}

// ---------------------------------------------------------------------------
// XOR-based MAC (stub — not cryptographically secure)
// ---------------------------------------------------------------------------

/// Compute a lightweight 64-bit XOR-based MAC of `body` keyed with `secret`.
///
/// The algorithm folds each byte of `body` into a 64-bit accumulator using
/// XOR and a FNV-inspired mixing step, then XORs in bytes of the key cyclically.
/// This is **not** a secure MAC; it is a deterministic stub for use in tests
/// and local development.
#[must_use]
pub fn xor_mac(body: &str, secret: &str) -> u64 {
    let body_bytes = body.as_bytes();
    let key_bytes = secret.as_bytes();

    if body_bytes.is_empty() || key_bytes.is_empty() {
        return 0;
    }

    let mut acc: u64 = 0xcbf2_9ce4_8422_2325; // FNV-1a offset basis

    for (i, &b) in body_bytes.iter().enumerate() {
        let key_byte = key_bytes[i % key_bytes.len()];
        acc ^= u64::from(b) ^ u64::from(key_byte);
        // FNV-like mixing
        acc = acc.wrapping_mul(0x0000_0100_0000_01b3);
    }
    acc
}

/// Validate the `X-Signature` header of a [`WebhookPayload`] against a secret.
///
/// The expected header format is `sha256=<hex-encoded xor_mac>`.
///
/// Returns `true` when:
/// - The header is present and well-formed, **and**
/// - The computed MAC of `payload.body` with `secret` matches the header value.
///
/// Returns `false` in all other cases (missing header, bad format, wrong MAC).
#[must_use]
pub fn validate_signature(payload: &WebhookPayload, secret: &str) -> bool {
    let Some(sig_header) = payload.header("x-signature") else {
        return false;
    };

    let mac_hex = if let Some(hex) = sig_header.strip_prefix("sha256=") {
        hex
    } else {
        sig_header
    };

    let expected = xor_mac(&payload.body, secret);
    let expected_hex = format!("{expected:016x}");

    // Constant-time comparison (lengths equal → byte XOR fold).
    let a = expected_hex.as_bytes();
    let b = mac_hex.as_bytes();
    if a.len() != b.len() {
        return false;
    }
    let diff = a
        .iter()
        .zip(b.iter())
        .fold(0u8, |acc, (x, y)| acc | (x ^ y));
    diff == 0
}

// ===========================================================================
// SimpleWebhookTrigger — URL-centric fire-and-report API
// ===========================================================================

/// A lightweight webhook trigger keyed only by destination URL.
///
/// Unlike [`WebhookTrigger`] (which is path-routed), `SimpleWebhookTrigger`
/// holds the full URL and exposes a single [`fire`](Self::fire) method that
/// returns a human-readable report string describing the event it would
/// dispatch.  No actual HTTP call is made; the implementation is deliberately
/// synchronous and allocation-free for use in tests and offline pipelines.
///
/// # Example
///
/// ```rust
/// use oximedia_workflow::webhook_trigger::SimpleWebhookTrigger;
///
/// let trigger = SimpleWebhookTrigger::new("https://example.com/hooks/ingest");
/// let report = trigger.fire("job.complete");
/// assert!(report.contains("job.complete"));
/// assert!(report.contains("example.com"));
/// ```
#[derive(Debug, Clone)]
pub struct SimpleWebhookTrigger {
    /// Destination URL for the webhook.
    pub url: String,
}

impl SimpleWebhookTrigger {
    /// Create a new simple webhook trigger for the given URL.
    #[must_use]
    pub fn new(url: impl Into<String>) -> Self {
        Self { url: url.into() }
    }

    /// Simulate firing a webhook event.
    ///
    /// Returns a report string: `"WEBHOOK {url} event={event}"`.
    /// No real HTTP request is made.
    #[must_use]
    pub fn fire(&self, event: &str) -> String {
        format!("WEBHOOK {} event={event}", self.url)
    }

    /// Return the target URL.
    #[must_use]
    pub fn url(&self) -> &str {
        &self.url
    }
}

// ===========================================================================
// Tests
// ===========================================================================

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

    // ------------------------------------------------------------------
    // xor_mac
    // ------------------------------------------------------------------

    #[test]
    fn xor_mac_empty_body_returns_zero() {
        assert_eq!(xor_mac("", "secret"), 0);
    }

    #[test]
    fn xor_mac_empty_secret_returns_zero() {
        assert_eq!(xor_mac("hello", ""), 0);
    }

    #[test]
    fn xor_mac_deterministic() {
        let a = xor_mac("payload body", "my-secret");
        let b = xor_mac("payload body", "my-secret");
        assert_eq!(a, b);
    }

    #[test]
    fn xor_mac_different_secrets_differ() {
        let a = xor_mac("same body", "secret-a");
        let b = xor_mac("same body", "secret-b");
        assert_ne!(a, b);
    }

    #[test]
    fn xor_mac_different_bodies_differ() {
        let a = xor_mac("body-a", "secret");
        let b = xor_mac("body-b", "secret");
        assert_ne!(a, b);
    }

    // ------------------------------------------------------------------
    // validate_signature
    // ------------------------------------------------------------------

    fn make_payload_with_sig(body: &str, secret: &str) -> WebhookPayload {
        let mac = xor_mac(body, secret);
        let header_val = format!("sha256={mac:016x}");
        WebhookPayload {
            method: "POST".to_string(),
            path: "/test".to_string(),
            body: body.to_string(),
            headers: vec![("x-signature".to_string(), header_val)],
        }
    }

    #[test]
    fn validate_signature_valid() {
        let payload = make_payload_with_sig("hello world", "my-secret");
        assert!(validate_signature(&payload, "my-secret"));
    }

    #[test]
    fn validate_signature_wrong_secret() {
        let payload = make_payload_with_sig("hello world", "correct-secret");
        assert!(!validate_signature(&payload, "wrong-secret"));
    }

    #[test]
    fn validate_signature_missing_header() {
        let payload = WebhookPayload {
            method: "POST".to_string(),
            path: "/test".to_string(),
            body: "hello".to_string(),
            headers: vec![],
        };
        assert!(!validate_signature(&payload, "secret"));
    }

    #[test]
    fn validate_signature_malformed_header() {
        let payload = WebhookPayload {
            method: "POST".to_string(),
            path: "/test".to_string(),
            body: "hello".to_string(),
            headers: vec![("x-signature".to_string(), "notahex!!".to_string())],
        };
        assert!(!validate_signature(&payload, "secret"));
    }

    // ------------------------------------------------------------------
    // WebhookPayload::header
    // ------------------------------------------------------------------

    #[test]
    fn header_lookup_case_insensitive() {
        let p = WebhookPayload {
            method: "POST".to_string(),
            path: "/".to_string(),
            body: String::new(),
            headers: vec![("Content-Type".to_string(), "application/json".to_string())],
        };
        assert_eq!(p.header("content-type"), Some("application/json"));
        assert_eq!(p.header("CONTENT-TYPE"), Some("application/json"));
        assert!(p.header("x-missing").is_none());
    }

    // ------------------------------------------------------------------
    // WebhookTriggerRegistry
    // ------------------------------------------------------------------

    #[test]
    fn registry_match_by_path() {
        let mut reg = WebhookTriggerRegistry::new();
        reg.add_trigger(WebhookTrigger::new("t1", "/hook/ingest", "wf-001"));
        reg.add_trigger(WebhookTrigger::new("t2", "/hook/export", "wf-002"));

        let payload = WebhookPayload {
            method: "POST".to_string(),
            path: "/hook/ingest".to_string(),
            body: "{}".to_string(),
            headers: vec![],
        };
        let m = reg.match_trigger(&payload);
        assert!(m.is_some());
        assert_eq!(m.unwrap().workflow_id, "wf-001");
    }

    #[test]
    fn registry_no_match_returns_none() {
        let mut reg = WebhookTriggerRegistry::new();
        reg.add_trigger(WebhookTrigger::new("t1", "/hook/known", "wf-001"));

        let payload = WebhookPayload {
            method: "POST".to_string(),
            path: "/hook/unknown".to_string(),
            body: "{}".to_string(),
            headers: vec![],
        };
        assert!(reg.match_trigger(&payload).is_none());
    }

    #[test]
    fn registry_secret_match_with_valid_signature() {
        let secret = "top-secret";
        let body = r#"{"event":"ingest_done"}"#;
        let mac = xor_mac(body, secret);
        let sig_header = format!("sha256={mac:016x}");

        let mut reg = WebhookTriggerRegistry::new();
        reg.add_trigger(WebhookTrigger::new("t1", "/secure", "wf-secure").with_secret(secret));

        let payload = WebhookPayload {
            method: "POST".to_string(),
            path: "/secure".to_string(),
            body: body.to_string(),
            headers: vec![("x-signature".to_string(), sig_header)],
        };

        let m = reg.match_trigger(&payload);
        assert!(m.is_some(), "should match with valid signature");
        assert_eq!(m.unwrap().id, "t1");
    }

    #[test]
    fn registry_secret_match_fails_with_wrong_signature() {
        let mut reg = WebhookTriggerRegistry::new();
        reg.add_trigger(
            WebhookTrigger::new("t1", "/secure", "wf-secure").with_secret("correct-secret"),
        );

        let payload = WebhookPayload {
            method: "POST".to_string(),
            path: "/secure".to_string(),
            body: "{}".to_string(),
            headers: vec![(
                "x-signature".to_string(),
                "sha256=deadbeef00000000".to_string(),
            )],
        };

        assert!(
            reg.match_trigger(&payload).is_none(),
            "should not match with wrong signature"
        );
    }

    #[test]
    fn registry_add_replaces_existing_id() {
        let mut reg = WebhookTriggerRegistry::new();
        reg.add_trigger(WebhookTrigger::new("t1", "/old-path", "wf-old"));
        reg.add_trigger(WebhookTrigger::new("t1", "/new-path", "wf-new"));

        assert_eq!(reg.len(), 1);
        assert_eq!(reg.triggers[0].path, "/new-path");
    }

    #[test]
    fn registry_remove_trigger() {
        let mut reg = WebhookTriggerRegistry::new();
        reg.add_trigger(WebhookTrigger::new("t1", "/hook", "wf-001"));
        reg.add_trigger(WebhookTrigger::new("t2", "/hook2", "wf-002"));
        assert_eq!(reg.len(), 2);

        let removed = reg.remove_trigger("t1");
        assert!(removed.is_some());
        assert_eq!(reg.len(), 1);

        let not_found = reg.remove_trigger("t999");
        assert!(not_found.is_none());
    }

    #[test]
    fn registry_is_empty() {
        let mut reg = WebhookTriggerRegistry::new();
        assert!(reg.is_empty());
        reg.add_trigger(WebhookTrigger::new("t1", "/x", "wf-x"));
        assert!(!reg.is_empty());
    }
}