tideway 0.7.17

A batteries-included Rust web framework built on Axum for building SaaS applications quickly
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
//! Stripe webhook handling.
//!
//! Handles webhook signature verification, event routing, and subscription state syncing.

use crate::error::Result;
use hmac::{Hmac, Mac};
use secrecy::{ExposeSecret, SecretString};
use sha2::Sha256;
use subtle::ConstantTimeEq;

use super::error::BillingError;
use super::plans::Plans;
use super::storage::BillingStore;
use super::subscription::{StripeSubscriptionData, SubscriptionManager, SubscriptionMetadata};

/// Webhook handler for Stripe events.
///
/// Handles webhook signature verification, idempotency, and event processing.
///
/// The webhook secret is stored using [`SecretString`] to prevent accidental
/// exposure in logs or debug output.
pub struct WebhookHandler<S: BillingStore> {
    store: S,
    webhook_secret: SecretString,
    plans: Plans,
}

impl<S: BillingStore + Clone> WebhookHandler<S> {
    /// Create a new webhook handler.
    ///
    /// The webhook secret is stored securely and won't be exposed in debug output.
    #[must_use]
    pub fn new(store: S, webhook_secret: impl Into<SecretString>, plans: Plans) -> Self {
        Self {
            store,
            webhook_secret: webhook_secret.into(),
            plans,
        }
    }

    /// Verify the webhook signature and parse the event.
    ///
    /// # Arguments
    /// * `payload` - The raw request body
    /// * `signature` - The `Stripe-Signature` header value
    ///
    /// # Errors
    /// Returns an error if signature verification fails or payload is invalid.
    pub fn verify_signature(&self, payload: &[u8], signature: &str) -> Result<WebhookEvent> {
        // Parse the signature header
        let sig_parts = parse_signature_header(signature)?;

        // Check timestamp is recent (within 5 minutes)
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0) as i64;

        let timestamp_diff = (now - sig_parts.timestamp).abs();
        if timestamp_diff > 300 {
            return Err(crate::error::TidewayError::BadRequest(
                "Webhook timestamp too old".to_string(),
            ));
        }

        // Compute expected signature
        let signed_payload = format!(
            "{}.{}",
            sig_parts.timestamp,
            String::from_utf8_lossy(payload)
        );
        let expected_sig = compute_signature(
            self.webhook_secret.expose_secret(),
            signed_payload.as_bytes(),
        )?;

        // Verify signature matches (constant-time comparison)
        let expected_bytes = hex::decode(&expected_sig)
            .map_err(|_| crate::error::TidewayError::Internal("Hex decode error".to_string()))?;
        let provided_bytes = hex::decode(&sig_parts.signature).map_err(|_| {
            crate::error::TidewayError::BadRequest("Invalid signature format".to_string())
        })?;

        if expected_bytes.ct_eq(&provided_bytes).unwrap_u8() != 1 {
            return Err(crate::error::TidewayError::BadRequest(
                "Invalid webhook signature".to_string(),
            ));
        }

        // Parse the JSON payload
        // Log detailed error internally but return generic message to prevent information leakage
        let event: WebhookEvent = serde_json::from_slice(payload).map_err(|e| {
            tracing::warn!(
                target: "tideway::billing::webhook",
                error = %e,
                "Failed to parse webhook payload"
            );
            BillingError::InvalidWebhookPayload {
                message: "malformed JSON payload".to_string(),
            }
        })?;

        Ok(event)
    }

    /// Process a verified webhook event.
    ///
    /// This method handles idempotency and routes to the appropriate handler.
    pub async fn handle_event(&self, event: WebhookEvent) -> Result<WebhookOutcome> {
        // Check idempotency
        if self.store.is_event_processed(&event.id).await? {
            return Ok(WebhookOutcome::AlreadyProcessed);
        }

        // Route to handler
        let outcome = match event.event_type.as_str() {
            "checkout.session.completed" => self.handle_checkout_completed(&event).await?,
            "customer.subscription.created" | "customer.subscription.updated" => {
                self.handle_subscription_updated(&event).await?
            }
            "customer.subscription.deleted" => self.handle_subscription_deleted(&event).await?,
            "invoice.paid" => self.handle_invoice_paid(&event).await?,
            "invoice.payment_failed" => self.handle_payment_failed(&event).await?,
            _ => WebhookOutcome::Ignored,
        };

        // Mark as processed (only for non-ignored events)
        if !matches!(outcome, WebhookOutcome::Ignored) {
            self.store.mark_event_processed(&event.id).await?;
        }

        Ok(outcome)
    }

    /// Handle checkout.session.completed event.
    async fn handle_checkout_completed(&self, event: &WebhookEvent) -> Result<WebhookOutcome> {
        // Extract subscription data from checkout session
        let session = event.data.object.as_object().ok_or_else(|| {
            crate::error::TidewayError::BadRequest("Invalid event data".to_string())
        })?;

        // Get subscription ID from the completed checkout
        let subscription_id = session.get("subscription").and_then(|v| v.as_str());

        if subscription_id.is_none() {
            // Not a subscription checkout (maybe one-time payment)
            return Ok(WebhookOutcome::Ignored);
        }

        // The subscription.created webhook will handle syncing the actual subscription
        Ok(WebhookOutcome::Processed)
    }

    /// Handle subscription created/updated events.
    async fn handle_subscription_updated(&self, event: &WebhookEvent) -> Result<WebhookOutcome> {
        let sub_data = self.parse_subscription_data(&event.data.object)?;

        // Update local state
        let sub_manager = SubscriptionManager::new(
            self.store.clone(),
            NullSubscriptionClient,
            self.plans.clone(),
        );
        sub_manager.sync_from_stripe(sub_data).await?;

        Ok(WebhookOutcome::Processed)
    }

    /// Handle subscription deleted event.
    async fn handle_subscription_deleted(&self, event: &WebhookEvent) -> Result<WebhookOutcome> {
        let subscription_id = event
            .data
            .object
            .get("id")
            .and_then(|v| v.as_str())
            .ok_or_else(|| {
                crate::error::TidewayError::BadRequest("Missing subscription ID".to_string())
            })?;

        let sub_manager = SubscriptionManager::new(
            self.store.clone(),
            NullSubscriptionClient,
            self.plans.clone(),
        );
        sub_manager.delete_subscription(subscription_id).await?;

        Ok(WebhookOutcome::Processed)
    }

    /// Handle invoice.paid event.
    async fn handle_invoice_paid(&self, event: &WebhookEvent) -> Result<WebhookOutcome> {
        // Invoice paid - subscription period updated
        // The subscription.updated webhook will handle the actual update
        // This is mainly for triggering any custom logic on successful payment

        let _subscription_id = event
            .data
            .object
            .get("subscription")
            .and_then(|v| v.as_str());

        // Could emit custom event here for app-specific logic
        Ok(WebhookOutcome::Processed)
    }

    /// Handle invoice.payment_failed event.
    async fn handle_payment_failed(&self, event: &WebhookEvent) -> Result<WebhookOutcome> {
        // Payment failed - the subscription.updated webhook will mark it past_due
        // This is mainly for triggering notifications

        let _subscription_id = event
            .data
            .object
            .get("subscription")
            .and_then(|v| v.as_str());

        // Could emit custom event here for app-specific notification logic
        Ok(WebhookOutcome::Processed)
    }

    /// Parse subscription data from Stripe webhook payload.
    fn parse_subscription_data(
        &self,
        object: &serde_json::Value,
    ) -> Result<StripeSubscriptionData> {
        let obj = object.as_object().ok_or_else(|| {
            crate::error::TidewayError::BadRequest("Invalid subscription data".to_string())
        })?;

        let id = obj
            .get("id")
            .and_then(|v| v.as_str())
            .ok_or_else(|| {
                crate::error::TidewayError::BadRequest("Missing subscription ID".to_string())
            })?
            .to_string();

        let customer_id = obj
            .get("customer")
            .and_then(|v| v.as_str())
            .ok_or_else(|| {
                crate::error::TidewayError::BadRequest("Missing customer ID".to_string())
            })?
            .to_string();

        let status = obj
            .get("status")
            .and_then(|v| v.as_str())
            .unwrap_or("active")
            .to_string();

        let current_period_start = obj
            .get("current_period_start")
            .and_then(|v| v.as_u64())
            .unwrap_or(0);

        let current_period_end = obj
            .get("current_period_end")
            .and_then(|v| v.as_u64())
            .unwrap_or(0);

        let trial_end = obj.get("trial_end").and_then(|v| v.as_u64());

        let cancel_at_period_end = obj
            .get("cancel_at_period_end")
            .and_then(|v| v.as_bool())
            .unwrap_or(false);

        // Parse items to get plan and seat info
        let (plan_id, base_item_id, seat_item_id, extra_seats) =
            self.parse_subscription_items(obj)?;

        // Get metadata
        let metadata = obj
            .get("metadata")
            .and_then(|v| v.as_object())
            .map(|m| SubscriptionMetadata {
                billable_id: m
                    .get("billable_id")
                    .and_then(|v| v.as_str())
                    .map(String::from),
                billable_type: m
                    .get("billable_type")
                    .and_then(|v| v.as_str())
                    .map(String::from),
            })
            .unwrap_or_default();

        Ok(StripeSubscriptionData {
            id,
            customer_id,
            plan_id,
            status,
            current_period_start,
            current_period_end,
            extra_seats,
            trial_end,
            cancel_at_period_end,
            base_item_id,
            seat_item_id,
            metadata,
        })
    }

    /// Parse subscription items to extract plan and seat information.
    fn parse_subscription_items(
        &self,
        obj: &serde_json::Map<String, serde_json::Value>,
    ) -> Result<(String, Option<String>, Option<String>, u32)> {
        let items = obj
            .get("items")
            .and_then(|v| v.get("data"))
            .and_then(|v| v.as_array())
            .ok_or_else(|| {
                crate::error::TidewayError::BadRequest("Missing subscription items".to_string())
            })?;

        let mut plan_id = String::new();
        let mut base_item_id = None;
        let mut seat_item_id = None;
        let mut extra_seats = 0u32;

        // Get all seat price IDs for lookup
        let seat_prices: std::collections::HashSet<_> = self
            .plans
            .iter()
            .filter_map(|(_, p)| p.extra_seat_price_id.clone())
            .collect();

        for item in items {
            let item_obj = item.as_object().ok_or_else(|| {
                crate::error::TidewayError::BadRequest("Invalid item".to_string())
            })?;

            let item_id = item_obj
                .get("id")
                .and_then(|v| v.as_str())
                .map(String::from);

            let price_id = item_obj
                .get("price")
                .and_then(|v| v.get("id"))
                .and_then(|v| v.as_str())
                .unwrap_or("");

            let quantity = item_obj
                .get("quantity")
                .and_then(|v| v.as_u64())
                .unwrap_or(1) as u32;

            // Check if this is a seat item
            if seat_prices.contains(price_id) {
                seat_item_id = item_id;
                extra_seats = quantity;
            } else {
                // This is the base plan
                base_item_id = item_id;

                // Find plan by price ID
                if let Some(plan) = self.plans.find_by_stripe_price(price_id) {
                    plan_id = plan.id.clone();
                }
            }
        }

        if plan_id.is_empty() {
            return Err(crate::error::TidewayError::BadRequest(
                "Could not determine plan from subscription".to_string(),
            ));
        }

        Ok((plan_id, base_item_id, seat_item_id, extra_seats))
    }
}

/// Parsed webhook event.
#[derive(Debug, Clone, serde::Deserialize)]
pub struct WebhookEvent {
    /// Event ID.
    pub id: String,
    /// Event type (e.g., "checkout.session.completed").
    #[serde(rename = "type")]
    pub event_type: String,
    /// Event data.
    pub data: WebhookEventData,
    /// Timestamp when the event was created.
    pub created: u64,
}

/// Webhook event data.
#[derive(Debug, Clone, serde::Deserialize)]
pub struct WebhookEventData {
    /// The object that triggered the event.
    pub object: serde_json::Value,
}

/// Outcome of webhook processing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WebhookOutcome {
    /// Event was processed successfully.
    Processed,
    /// Event was ignored (not relevant).
    Ignored,
    /// Event was already processed (idempotency).
    AlreadyProcessed,
}

/// Parsed signature header parts.
struct SignatureParts {
    timestamp: i64,
    signature: String,
}

/// Parse the Stripe-Signature header.
fn parse_signature_header(header: &str) -> Result<SignatureParts> {
    let mut timestamp = None;
    let mut signature = None;

    for part in header.split(',') {
        let (key, value) = part.split_once('=').ok_or_else(|| {
            crate::error::TidewayError::BadRequest("Invalid signature header format".to_string())
        })?;

        match key.trim() {
            "t" => timestamp = value.parse().ok(),
            "v1" => signature = Some(value.to_string()),
            _ => {} // Ignore other versions
        }
    }

    Ok(SignatureParts {
        timestamp: timestamp.ok_or_else(|| {
            crate::error::TidewayError::BadRequest("Missing timestamp in signature".to_string())
        })?,
        signature: signature.ok_or_else(|| {
            crate::error::TidewayError::BadRequest("Missing v1 signature".to_string())
        })?,
    })
}

/// Compute HMAC-SHA256 signature.
fn compute_signature(secret: &str, payload: &[u8]) -> Result<String> {
    type HmacSha256 = Hmac<Sha256>;

    let mut mac = HmacSha256::new_from_slice(secret.as_bytes())
        .map_err(|_| crate::error::TidewayError::Internal("HMAC error".to_string()))?;

    mac.update(payload);
    let result = mac.finalize();

    Ok(hex::encode(result.into_bytes()))
}

/// Null subscription client for webhook handler (doesn't need to make API calls).
struct NullSubscriptionClient;

impl super::subscription::StripeSubscriptionClient for NullSubscriptionClient {
    async fn cancel_subscription(&self, _subscription_id: &str) -> Result<()> {
        Ok(())
    }

    async fn cancel_subscription_at_period_end(&self, _subscription_id: &str) -> Result<()> {
        Ok(())
    }

    async fn resume_subscription(&self, _subscription_id: &str) -> Result<()> {
        Ok(())
    }

    async fn get_subscription(&self, _subscription_id: &str) -> Result<StripeSubscriptionData> {
        Err(crate::error::TidewayError::Internal(
            "Not implemented".to_string(),
        ))
    }

    async fn update_subscription(
        &self,
        _subscription_id: &str,
        _update: super::subscription::UpdateSubscriptionRequest,
    ) -> Result<StripeSubscriptionData> {
        Err(crate::error::TidewayError::Internal(
            "Not implemented".to_string(),
        ))
    }

    async fn extend_trial(
        &self,
        _subscription_id: &str,
        _new_trial_end: u64,
    ) -> Result<StripeSubscriptionData> {
        Err(crate::error::TidewayError::Internal(
            "Not implemented".to_string(),
        ))
    }

    async fn pause_subscription(&self, _subscription_id: &str) -> Result<()> {
        Ok(())
    }

    async fn resume_paused_subscription(
        &self,
        _subscription_id: &str,
    ) -> Result<StripeSubscriptionData> {
        Err(crate::error::TidewayError::Internal(
            "Not implemented".to_string(),
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::billing::Plans;
    use crate::billing::storage::test::InMemoryBillingStore;

    fn create_test_plans() -> Plans {
        Plans::builder()
            .plan("starter")
            .stripe_price("price_starter")
            .extra_seat_price("price_seat")
            .included_seats(3)
            .done()
            .unwrap()
            .build()
            .unwrap()
    }

    fn create_test_webhook_secret() -> String {
        "whsec_test_secret".to_string()
    }

    fn create_test_signature(secret: &str, payload: &[u8], timestamp: i64) -> String {
        let signed_payload = format!("{}.{}", timestamp, String::from_utf8_lossy(payload));
        let sig = compute_signature(secret, signed_payload.as_bytes()).unwrap();
        format!("t={},v1={}", timestamp, sig)
    }

    #[test]
    fn test_parse_signature_header() {
        let header = "t=1234567890,v1=abc123def456";
        let parts = parse_signature_header(header).unwrap();
        assert_eq!(parts.timestamp, 1234567890);
        assert_eq!(parts.signature, "abc123def456");
    }

    #[test]
    fn test_parse_signature_header_invalid() {
        let result = parse_signature_header("invalid");
        assert!(result.is_err());
    }

    #[test]
    fn test_verify_signature_valid() {
        let store = InMemoryBillingStore::new();
        let plans = create_test_plans();
        let secret = create_test_webhook_secret();
        let signature_secret = secret.clone();
        let handler = WebhookHandler::new(store, secret, plans);

        let payload = r#"{"id":"evt_123","type":"test","data":{"object":{}},"created":1234567890}"#;
        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64;

        let signature = create_test_signature(&signature_secret, payload.as_bytes(), timestamp);

        let result = handler.verify_signature(payload.as_bytes(), &signature);
        assert!(result.is_ok());
    }

    #[test]
    fn test_verify_signature_invalid() {
        let store = InMemoryBillingStore::new();
        let plans = create_test_plans();
        let handler = WebhookHandler::new(store, "whsec_test", plans);

        let payload = r#"{"id":"evt_123","type":"test","data":{"object":{}},"created":1234567890}"#;
        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs() as i64;

        // Wrong signature
        let signature = format!("t={},v1=invalid_signature_hex", timestamp);
        let result = handler.verify_signature(payload.as_bytes(), &signature);
        assert!(result.is_err());
    }

    #[test]
    fn test_verify_signature_old_timestamp() {
        let store = InMemoryBillingStore::new();
        let plans = create_test_plans();
        let secret = create_test_webhook_secret();
        let signature_secret = secret.clone();
        let handler = WebhookHandler::new(store, secret, plans);

        let payload = r#"{"id":"evt_123","type":"test","data":{"object":{}},"created":1234567890}"#;
        let old_timestamp = 1000000000i64; // Very old

        let signature = create_test_signature(&signature_secret, payload.as_bytes(), old_timestamp);

        let result = handler.verify_signature(payload.as_bytes(), &signature);
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_handle_event_idempotency() {
        let store = InMemoryBillingStore::new();
        let plans = create_test_plans();
        let handler = WebhookHandler::new(store.clone(), "whsec_test", plans);

        let event = WebhookEvent {
            id: "evt_test_123".to_string(),
            event_type: "invoice.paid".to_string(),
            data: WebhookEventData {
                object: serde_json::json!({"subscription": "sub_123"}),
            },
            created: 1234567890,
        };

        // First call processes
        let result = handler.handle_event(event.clone()).await.unwrap();
        assert_eq!(result, WebhookOutcome::Processed);

        // Second call returns already processed
        let result = handler.handle_event(event).await.unwrap();
        assert_eq!(result, WebhookOutcome::AlreadyProcessed);
    }

    #[tokio::test]
    async fn test_handle_event_ignored() {
        let store = InMemoryBillingStore::new();
        let plans = create_test_plans();
        let handler = WebhookHandler::new(store, "whsec_test", plans);

        let event = WebhookEvent {
            id: "evt_unknown".to_string(),
            event_type: "unknown.event.type".to_string(),
            data: WebhookEventData {
                object: serde_json::json!({}),
            },
            created: 1234567890,
        };

        let result = handler.handle_event(event).await.unwrap();
        assert_eq!(result, WebhookOutcome::Ignored);
    }

    #[tokio::test]
    async fn test_handle_subscription_updated() {
        let store = InMemoryBillingStore::new();
        let plans = create_test_plans();
        let handler = WebhookHandler::new(store.clone(), "whsec_test", plans);

        let event = WebhookEvent {
            id: "evt_sub_updated".to_string(),
            event_type: "customer.subscription.updated".to_string(),
            data: WebhookEventData {
                object: serde_json::json!({
                    "id": "sub_123",
                    "customer": "cus_123",
                    "status": "active",
                    "current_period_start": 1700000000u64,
                    "current_period_end": 1702592000u64,
                    "items": {
                        "data": [
                            {
                                "id": "si_base",
                                "price": {"id": "price_starter"},
                                "quantity": 1
                            },
                            {
                                "id": "si_seat",
                                "price": {"id": "price_seat"},
                                "quantity": 2
                            }
                        ]
                    },
                    "metadata": {
                        "billable_id": "org_123",
                        "billable_type": "org"
                    }
                }),
            },
            created: 1234567890,
        };

        let result = handler.handle_event(event).await.unwrap();
        assert_eq!(result, WebhookOutcome::Processed);

        // Verify subscription was stored
        let sub = store.get_subscription("org_123").await.unwrap();
        assert!(sub.is_some());
        let sub = sub.unwrap();
        assert_eq!(sub.plan_id, "starter");
        assert_eq!(sub.extra_seats, 2);
    }
}