allowthem-core 0.0.9

Core types, database, and auth logic for allowthem
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
//! HTTP webhook email delivery.
//!
//! POSTs the rendered email plus structured template data to a
//! customer-supplied URL. Optionally HMAC-signs the body using the shared
//! [`crate::webhook_sig::sign_payload`] helper (plan §2.7).
//!
//! There is **no retry** in this sender. The integrator's endpoint must be
//! reliable, or they should configure a real SMTP path instead. See plan §2.7.

use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;

use serde::Serialize;

use crate::email::{EmailMessage, EmailSender, EmailTemplate};
use crate::email_render::{EmailBranding, render};
use crate::error::AuthError;
use crate::webhook_sig::sign_payload;

/// Configuration for [`WebhookEmailSender`].
#[derive(Debug, Clone)]
pub struct WebhookEmailConfig {
    /// URL to POST email notifications to.
    pub webhook_url: String,
    /// HMAC-SHA256 signing secret. When `None`, no `X-Allowthem-Signature`
    /// header is sent.
    pub signing_secret: Option<Vec<u8>>,
    /// Per-request timeout. Defaults to 10 seconds.
    pub timeout: Duration,
}

impl Default for WebhookEmailConfig {
    fn default() -> Self {
        Self {
            webhook_url: String::new(),
            signing_secret: None,
            timeout: Duration::from_secs(10),
        }
    }
}

/// Email sender that POSTs rendered emails to a customer webhook endpoint.
pub struct WebhookEmailSender {
    client: reqwest::Client,
    config: Arc<WebhookEmailConfig>,
    branding: Arc<EmailBranding>,
}

impl WebhookEmailSender {
    /// Build a sender. Returns `Err` if the underlying HTTP client cannot be
    /// constructed (rare; typically only a TLS init failure).
    pub fn new(config: WebhookEmailConfig, branding: EmailBranding) -> Result<Self, AuthError> {
        let client = reqwest::Client::builder()
            .timeout(config.timeout)
            .build()
            .map_err(|e| AuthError::Email(e.to_string()))?;
        Ok(Self {
            client,
            config: Arc::new(config),
            branding: Arc::new(branding),
        })
    }
}

// ─── Payload types ──────────────────────────────────────────────────────────

#[derive(Serialize)]
struct WebhookPayload<'a> {
    to: &'a str,
    subject: &'a str,
    template_type: &'static str,
    template_data: TemplateData<'a>,
    rendered: RenderedRef<'a>,
}

#[derive(Serialize)]
#[serde(untagged)]
enum TemplateData<'a> {
    EmailVerification {
        url: &'a str,
        username: &'a str,
    },
    PasswordReset {
        url: &'a str,
        username: &'a str,
    },
    MfaRecovery {
        codes: &'a [String],
        username: &'a str,
    },
    Invitation {
        url: &'a str,
        invited_by: &'a str,
    },
}

#[derive(Serialize)]
struct RenderedRef<'a> {
    html: &'a str,
    text: &'a str,
}

fn template_type(t: &EmailTemplate) -> &'static str {
    match t {
        EmailTemplate::EmailVerification { .. } => "email_verification",
        EmailTemplate::PasswordReset { .. } => "password_reset",
        EmailTemplate::MfaRecovery { .. } => "mfa_recovery",
        EmailTemplate::Invitation { .. } => "invitation",
    }
}

fn template_data(t: &EmailTemplate) -> TemplateData<'_> {
    match t {
        EmailTemplate::EmailVerification { url, username } => {
            TemplateData::EmailVerification { url, username }
        }
        EmailTemplate::PasswordReset { url, username } => {
            TemplateData::PasswordReset { url, username }
        }
        EmailTemplate::MfaRecovery { codes, username } => {
            TemplateData::MfaRecovery { codes, username }
        }
        EmailTemplate::Invitation { url, invited_by } => {
            TemplateData::Invitation { url, invited_by }
        }
    }
}

// ─── EmailSender impl ────────────────────────────────────────────────────────

impl EmailSender for WebhookEmailSender {
    fn send<'a>(
        &'a self,
        message: &'a EmailMessage,
    ) -> Pin<Box<dyn Future<Output = Result<(), AuthError>> + Send + 'a>> {
        Box::pin(async move {
            let rendered = render(&message.template, &self.branding);

            let payload = WebhookPayload {
                to: &message.to,
                subject: &message.subject,
                template_type: template_type(&message.template),
                template_data: template_data(&message.template),
                rendered: RenderedRef {
                    html: &rendered.html,
                    text: &rendered.text,
                },
            };

            let body = serde_json::to_vec(&payload).map_err(|e| AuthError::Email(e.to_string()))?;

            let mut req = self
                .client
                .post(&self.config.webhook_url)
                .header("Content-Type", "application/json")
                .header(
                    "X-Allowthem-Email-Template",
                    template_type(&message.template),
                );

            if let Some(secret) = &self.config.signing_secret {
                let ts = chrono::Utc::now().timestamp();
                let sig = sign_payload(secret, ts, &body);
                req = req.header("X-Allowthem-Signature", sig);
            }

            let resp = req
                .body(body)
                .send()
                .await
                .map_err(|e| AuthError::Email(e.to_string()))?;

            if resp.status().is_success() {
                Ok(())
            } else {
                Err(AuthError::Email(format!(
                    "webhook responded {}",
                    resp.status()
                )))
            }
        })
    }
}

#[cfg(test)]
mod tests {
    use wiremock::matchers::{header, method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    use crate::email::EmailTemplate;
    use crate::webhook_sig::verify_payload;

    use super::*;

    fn password_reset_msg() -> EmailMessage {
        EmailMessage {
            to: "user@example.com".to_owned(),
            subject: "Reset your password".to_owned(),
            template: EmailTemplate::PasswordReset {
                url: "https://app.example.com/reset?t=tok".to_owned(),
                username: "alice".to_owned(),
            },
        }
    }

    #[tokio::test]
    async fn posts_json_without_signature_when_no_secret() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/hook"))
            .and(header("Content-Type", "application/json"))
            .respond_with(ResponseTemplate::new(200))
            .expect(1)
            .mount(&server)
            .await;

        let sender = WebhookEmailSender::new(
            WebhookEmailConfig {
                webhook_url: format!("{}/hook", server.uri()),
                signing_secret: None,
                timeout: Duration::from_secs(5),
            },
            EmailBranding::default(),
        )
        .unwrap();

        sender.send(&password_reset_msg()).await.unwrap();

        // Assert no signature header was sent.
        let reqs = server.received_requests().await.unwrap();
        assert_eq!(reqs.len(), 1);
        assert!(!reqs[0].headers.contains_key("x-allowthem-signature"));

        // Assert body is a well-formed JSON with expected template_type.
        let body: serde_json::Value = serde_json::from_slice(&reqs[0].body).unwrap();
        assert_eq!(body["template_type"], "password_reset");
        assert_eq!(body["to"], "user@example.com");
        assert!(
            body["rendered"]["html"]
                .as_str()
                .unwrap()
                .contains("<!doctype html>")
        );
        assert!(body["rendered"]["text"].as_str().unwrap().len() > 0);
    }

    #[tokio::test]
    async fn posts_with_valid_signature_when_secret_provided() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/hook"))
            .respond_with(ResponseTemplate::new(200))
            .expect(1)
            .mount(&server)
            .await;

        let secret = b"webhook-secret".to_vec();
        let sender = WebhookEmailSender::new(
            WebhookEmailConfig {
                webhook_url: format!("{}/hook", server.uri()),
                signing_secret: Some(secret.clone()),
                timeout: Duration::from_secs(5),
            },
            EmailBranding::default(),
        )
        .unwrap();

        sender.send(&password_reset_msg()).await.unwrap();

        let reqs = server.received_requests().await.unwrap();
        let sig_header = reqs[0]
            .headers
            .get("x-allowthem-signature")
            .expect("signature header must be present")
            .to_str()
            .unwrap();

        let now = chrono::Utc::now().timestamp();
        verify_payload(&secret, &reqs[0].body, sig_header, now, 60).unwrap();
    }

    #[tokio::test]
    async fn non_2xx_response_returns_email_error() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .respond_with(ResponseTemplate::new(500))
            .mount(&server)
            .await;

        let sender = WebhookEmailSender::new(
            WebhookEmailConfig {
                webhook_url: format!("{}/hook", server.uri()),
                signing_secret: None,
                timeout: Duration::from_secs(5),
            },
            EmailBranding::default(),
        )
        .unwrap();

        let err = sender.send(&password_reset_msg()).await.unwrap_err();
        assert!(matches!(err, AuthError::Email(ref s) if s.contains("500")));
    }

    #[tokio::test]
    async fn transport_error_returns_email_error() {
        // Point at a port that refuses connections.
        let sender = WebhookEmailSender::new(
            WebhookEmailConfig {
                webhook_url: "http://127.0.0.1:1/hook".to_owned(),
                signing_secret: None,
                timeout: Duration::from_millis(500),
            },
            EmailBranding::default(),
        )
        .unwrap();

        let err = sender.send(&password_reset_msg()).await.unwrap_err();
        assert!(matches!(err, AuthError::Email(_)));
    }

    #[tokio::test]
    async fn timeout_returns_email_error() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .respond_with(ResponseTemplate::new(200).set_delay(Duration::from_secs(5)))
            .mount(&server)
            .await;

        let sender = WebhookEmailSender::new(
            WebhookEmailConfig {
                webhook_url: format!("{}/hook", server.uri()),
                signing_secret: None,
                timeout: Duration::from_millis(100),
            },
            EmailBranding::default(),
        )
        .unwrap();

        let err = sender.send(&password_reset_msg()).await.unwrap_err();
        assert!(matches!(err, AuthError::Email(_)));
    }

    #[tokio::test]
    async fn includes_template_header_and_full_payload_shape() {
        // The plan §2.7 wire contract: integrators rely on the
        // `X-Allowthem-Email-Template` header to dispatch by template
        // (without parsing the body) and on `template_data` to re-render
        // in their own engine. Pin both.
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .respond_with(ResponseTemplate::new(200))
            .expect(1)
            .mount(&server)
            .await;

        let sender = WebhookEmailSender::new(
            WebhookEmailConfig {
                webhook_url: format!("{}/hook", server.uri()),
                signing_secret: None,
                timeout: Duration::from_secs(5),
            },
            EmailBranding::default(),
        )
        .unwrap();

        sender.send(&password_reset_msg()).await.unwrap();

        let reqs = server.received_requests().await.unwrap();
        let req = &reqs[0];
        assert_eq!(
            req.headers
                .get("x-allowthem-email-template")
                .expect("X-Allowthem-Email-Template header must be present")
                .to_str()
                .unwrap(),
            "password_reset"
        );

        let body: serde_json::Value = serde_json::from_slice(&req.body).unwrap();
        // template_data carries the structured fields as the original
        // EmailTemplate variant — re-render fodder for integrators.
        assert_eq!(
            body["template_data"]["url"],
            "https://app.example.com/reset?t=tok"
        );
        assert_eq!(body["template_data"]["username"], "alice");
        assert_eq!(body["subject"], "Reset your password");
    }

    #[tokio::test]
    async fn branding_app_name_appears_in_rendered_html() {
        // Sender-level branding (§2.4) flows into the `rendered.html` body.
        // Webhook integrators that present the pre-rendered email keep the
        // configured app name without re-rendering.
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .respond_with(ResponseTemplate::new(200))
            .mount(&server)
            .await;

        let branding = EmailBranding {
            app_name: "Acme Inc".to_owned(),
            logo_url: None,
            footer_line: None,
        };
        let sender = WebhookEmailSender::new(
            WebhookEmailConfig {
                webhook_url: format!("{}/hook", server.uri()),
                signing_secret: None,
                timeout: Duration::from_secs(5),
            },
            branding,
        )
        .unwrap();

        sender.send(&password_reset_msg()).await.unwrap();

        let reqs = server.received_requests().await.unwrap();
        let body: serde_json::Value = serde_json::from_slice(&reqs[0].body).unwrap();
        let html = body["rendered"]["html"].as_str().unwrap();
        assert!(
            html.contains("Acme Inc"),
            "branding.app_name must appear in rendered.html: {html}"
        );
    }
}