cedros-login-server 0.0.45

Authentication server for cedros-login with email/password, Google OAuth, and Solana wallet sign-in
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
//! Tests for the outbox worker

use super::*;
use crate::repositories::{InMemoryOutboxRepository, OutboxEventType};
use crate::services::{Email, EmailService, LogEmailService, LogNotificationService};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
use tokio::time::sleep;

#[derive(Clone)]
struct CountingEmailService {
    active: Arc<AtomicUsize>,
    max_active: Arc<AtomicUsize>,
    delay: Duration,
}

#[async_trait::async_trait]
impl EmailService for CountingEmailService {
    async fn send(&self, _email: Email) -> Result<(), AppError> {
        let current = self.active.fetch_add(1, Ordering::SeqCst) + 1;
        self.max_active.fetch_max(current, Ordering::SeqCst);
        sleep(self.delay).await;
        self.active.fetch_sub(1, Ordering::SeqCst);
        Ok(())
    }
}

#[tokio::test]
async fn test_process_email_verification_event() {
    let outbox_repo = Arc::new(InMemoryOutboxRepository::new());
    let email_service = Arc::new(LogEmailService::new());
    let notification_service = Arc::new(LogNotificationService::new());
    let token_cipher = TokenCipher::new("test-secret");

    let worker = OutboxWorker::new(
        outbox_repo.clone(),
        email_service.clone(),
        notification_service,
        OutboxWorkerConfig::default(),
        "https://example.com".to_string(),
        token_cipher.clone(),
    );

    let token_enc = token_cipher.encrypt("abc").unwrap();
    let event = crate::repositories::OutboxEvent::new(
        OutboxEventType::EmailVerification,
        serde_json::json!({
            "to": "test@example.com",
            "user_name": "Test User",
            "token_enc": token_enc,
            "expires_in_hours": 24
        }),
    );
    outbox_repo.create(event.clone()).await.unwrap();

    worker.process_event(&event).await.unwrap();

    let emails = email_service.get_sent_emails().await;
    assert_eq!(emails.len(), 1);
    assert_eq!(emails[0].to, "test@example.com");
}

#[tokio::test]
async fn test_process_notification_event() {
    let outbox_repo = Arc::new(InMemoryOutboxRepository::new());
    let email_service = Arc::new(LogEmailService::new());
    let notification_service = Arc::new(LogNotificationService::new());

    let worker = OutboxWorker::new(
        outbox_repo.clone(),
        email_service,
        notification_service.clone(),
        OutboxWorkerConfig::default(),
        "https://example.com".to_string(),
        TokenCipher::new("test-secret"),
    );

    let event = crate::repositories::OutboxEvent::new(
        OutboxEventType::NotifyLoginThreshold,
        serde_json::json!({
            "severity": "warn",
            "title": "Login Threshold Reached",
            "body": "5 failed login attempts for user@example.com",
            "metadata": {"ip": "192.168.1.1"}
        }),
    );
    outbox_repo.create(event.clone()).await.unwrap();

    worker.process_event(&event).await.unwrap();

    let notifications = notification_service.get_notifications().await;
    assert_eq!(notifications.len(), 1);
    assert_eq!(notifications[0].title, "Login Threshold Reached");
}

#[tokio::test]
async fn test_process_security_alert_event() {
    let outbox_repo = Arc::new(InMemoryOutboxRepository::new());
    let email_service = Arc::new(LogEmailService::new());
    let notification_service = Arc::new(LogNotificationService::new());

    let worker = OutboxWorker::new(
        outbox_repo.clone(),
        email_service.clone(),
        notification_service,
        OutboxWorkerConfig::default(),
        "https://example.com".to_string(),
        TokenCipher::new("test-secret"),
    );

    let event = crate::repositories::OutboxEvent::new(
        OutboxEventType::EmailSecurityAlert,
        serde_json::json!({
            "to": "user@example.com",
            "user_name": "Test User",
            "login_time": "December 13, 2025 at 14:30 UTC",
            "ip_address": "192.168.1.1",
            "device": "Mac",
            "browser": "Chrome",
            "action_url": "https://example.com/account/security"
        }),
    );
    outbox_repo.create(event.clone()).await.unwrap();

    worker.process_event(&event).await.unwrap();

    let emails = email_service.get_sent_emails().await;
    assert_eq!(emails.len(), 1);
    assert_eq!(emails[0].to, "user@example.com");
    assert_eq!(emails[0].subject, "New sign-in to your account");
    assert!(emails[0].html_body.contains("Test User"));
    assert!(emails[0].html_body.contains("Mac"));
    assert!(emails[0].html_body.contains("Chrome"));
}

#[tokio::test]
async fn test_process_password_reset_event() {
    let outbox_repo = Arc::new(InMemoryOutboxRepository::new());
    let email_service = Arc::new(LogEmailService::new());
    let notification_service = Arc::new(LogNotificationService::new());
    let token_cipher = TokenCipher::new("test-secret");

    let worker = OutboxWorker::new(
        outbox_repo.clone(),
        email_service.clone(),
        notification_service,
        OutboxWorkerConfig::default(),
        "https://example.com".to_string(),
        token_cipher.clone(),
    );

    let token_enc = token_cipher.encrypt("abc123").unwrap();
    let event = crate::repositories::OutboxEvent::new(
        OutboxEventType::EmailPasswordReset,
        serde_json::json!({
            "to": "user@example.com",
            "user_name": "Test User",
            "token_enc": token_enc,
            "expires_in_minutes": 60
        }),
    );
    outbox_repo.create(event.clone()).await.unwrap();

    worker.process_event(&event).await.unwrap();

    let emails = email_service.get_sent_emails().await;
    assert_eq!(emails.len(), 1);
    assert_eq!(emails[0].to, "user@example.com");
    assert!(emails[0].html_body.contains("Reset"));
    assert!(emails[0].html_body.contains("60 minutes"));
}

#[tokio::test]
async fn test_process_invite_event() {
    let outbox_repo = Arc::new(InMemoryOutboxRepository::new());
    let email_service = Arc::new(LogEmailService::new());
    let notification_service = Arc::new(LogNotificationService::new());
    let token_cipher = TokenCipher::new("test-secret");

    let worker = OutboxWorker::new(
        outbox_repo.clone(),
        email_service.clone(),
        notification_service,
        OutboxWorkerConfig::default(),
        "https://example.com".to_string(),
        token_cipher.clone(),
    );

    let token_enc = token_cipher.encrypt("xyz").unwrap();
    let event = crate::repositories::OutboxEvent::new(
        OutboxEventType::EmailInvite,
        serde_json::json!({
            "to": "invitee@example.com",
            "org_name": "Acme Corp",
            "inviter_name": "John Doe",
            "role": "member",
            "token_enc": token_enc,
            "expires_in_days": 7
        }),
    );
    outbox_repo.create(event.clone()).await.unwrap();

    worker.process_event(&event).await.unwrap();

    let emails = email_service.get_sent_emails().await;
    assert_eq!(emails.len(), 1);
    assert_eq!(emails[0].to, "invitee@example.com");
    assert!(emails[0].subject.contains("Acme Corp"));
    assert!(emails[0].html_body.contains("John Doe"));
}

#[tokio::test]
async fn test_process_instant_link_event() {
    let outbox_repo = Arc::new(InMemoryOutboxRepository::new());
    let email_service = Arc::new(LogEmailService::new());
    let notification_service = Arc::new(LogNotificationService::new());
    let token_cipher = TokenCipher::new("test-secret");

    let worker = OutboxWorker::new(
        outbox_repo.clone(),
        email_service.clone(),
        notification_service,
        OutboxWorkerConfig::default(),
        "https://example.com".to_string(),
        token_cipher.clone(),
    );

    let token_enc = token_cipher.encrypt("abc").unwrap();
    let event = crate::repositories::OutboxEvent::new(
        OutboxEventType::EmailInstantLink,
        serde_json::json!({
            "to": "user@example.com",
            "user_name": "Test User",
            "token_enc": token_enc,
            "expires_in_minutes": 15
        }),
    );
    outbox_repo.create(event.clone()).await.unwrap();

    worker.process_event(&event).await.unwrap();

    let emails = email_service.get_sent_emails().await;
    assert_eq!(emails.len(), 1);
    assert_eq!(emails[0].to, "user@example.com");
    assert!(emails[0].subject.contains("sign-in"));
    assert!(emails[0].html_body.contains("15 minutes"));
}

#[tokio::test]
async fn test_process_event_marks_failed_on_max_attempts() {
    let outbox_repo = Arc::new(InMemoryOutboxRepository::new());
    let email_service = Arc::new(LogEmailService::new());
    let notification_service = Arc::new(LogNotificationService::new());

    let worker = OutboxWorker::new(
        outbox_repo.clone(),
        email_service,
        notification_service,
        OutboxWorkerConfig::default(),
        "https://example.com".to_string(),
        TokenCipher::new("test-secret"),
    );

    let mut event = crate::repositories::OutboxEvent::new(
        OutboxEventType::EmailVerification,
        serde_json::json!({}),
    );
    event.max_attempts = 1;
    let event_id = event.id;
    outbox_repo.create(event.clone()).await.unwrap();

    let result = worker.process_event(&event).await;
    assert!(result.is_err());

    let updated = outbox_repo.find_by_id(event_id).await.unwrap().unwrap();
    assert_eq!(updated.status, crate::repositories::OutboxStatus::Failed);
}

#[test]
fn test_sanitize_error_for_log() {
    // Email addresses should be redacted
    let error = "Failed to send email to user@example.com: connection refused";
    let sanitized = sanitize_error_for_log(error);
    assert!(!sanitized.contains("user@example.com"));
    assert!(sanitized.contains("[REDACTED]"));
    assert!(sanitized.contains("connection refused"));

    // Multiple emails should be redacted
    let error = "Error: foo@bar.com and baz@qux.org failed";
    let sanitized = sanitize_error_for_log(error);
    assert!(!sanitized.contains("foo@bar.com"));
    assert!(!sanitized.contains("baz@qux.org"));
    assert_eq!(sanitized.matches("[REDACTED]").count(), 2);

    // Long messages should be truncated
    let long_error = "x".repeat(200);
    let sanitized = sanitize_error_for_log(&long_error);
    assert!(sanitized.len() < 200);
    assert!(sanitized.contains("(truncated)"));

    // Multibyte content should truncate safely
    let multibyte = "🙂".repeat(200);
    let sanitized = sanitize_error_for_log(&multibyte);
    assert!(sanitized.contains("(truncated)"));

    // Normal errors should pass through
    let error = "Connection timeout after 30 seconds";
    let sanitized = sanitize_error_for_log(error);
    assert_eq!(sanitized, error);
}

#[tokio::test]
async fn test_process_batch_runs_concurrently() {
    let outbox_repo = Arc::new(InMemoryOutboxRepository::new());
    let active = Arc::new(AtomicUsize::new(0));
    let max_active = Arc::new(AtomicUsize::new(0));
    let email_service = Arc::new(CountingEmailService {
        active: active.clone(),
        max_active: max_active.clone(),
        delay: Duration::from_millis(50),
    });
    let notification_service = Arc::new(LogNotificationService::new());

    let worker = OutboxWorker::new(
        outbox_repo.clone(),
        email_service,
        notification_service,
        OutboxWorkerConfig {
            batch_size: 4,
            ..OutboxWorkerConfig::default()
        },
        "https://example.com".to_string(),
        TokenCipher::new("test-secret"),
    );

    for _ in 0..4 {
        let event = crate::repositories::OutboxEvent::new(
            OutboxEventType::EmailVerification,
            serde_json::json!({
                "to": "test@example.com",
                "verification_url": "https://example.com/verify",
                "expires_in_hours": 24
            }),
        );
        outbox_repo.create(event).await.unwrap();
    }

    worker.process_batch().await.unwrap();

    assert!(max_active.load(Ordering::SeqCst) >= 2);
}

#[tokio::test]
async fn test_idempotency_skips_already_delivered_event() {
    let outbox_repo = Arc::new(InMemoryOutboxRepository::new());
    let email_service = Arc::new(LogEmailService::new());
    let notification_service = Arc::new(LogNotificationService::new());
    let token_cipher = TokenCipher::new("test-secret");

    let worker = OutboxWorker::new(
        outbox_repo.clone(),
        email_service.clone(),
        notification_service,
        OutboxWorkerConfig::default(),
        "https://example.com".to_string(),
        token_cipher.clone(),
    );

    // Create an event with a valid payload
    let token_enc = token_cipher.encrypt("abc").unwrap();
    let mut event = crate::repositories::OutboxEvent::new(
        OutboxEventType::EmailVerification,
        serde_json::json!({
            "to": "test@example.com",
            "user_name": "Test User",
            "token_enc": token_enc,
            "expires_in_hours": 24
        }),
    );

    // Simulate: event was already delivered (e.g., crash after delivery but before mark_done)
    event.delivered_at = Some(chrono::Utc::now());

    let event_id = event.id;
    outbox_repo.create(event.clone()).await.unwrap();

    // Process the event - should skip sending due to delivered_at being set
    worker.process_event(&event).await.unwrap();

    // Verify no email was sent (idempotency worked)
    let emails = email_service.get_sent_emails().await;
    assert_eq!(
        emails.len(),
        0,
        "Expected no email to be sent for already-delivered event"
    );

    // Verify event is marked as done
    let updated = outbox_repo.find_by_id(event_id).await.unwrap().unwrap();
    assert_eq!(updated.status, crate::repositories::OutboxStatus::Done);
}

#[tokio::test]
async fn test_mark_delivered_sets_timestamp() {
    let outbox_repo = Arc::new(InMemoryOutboxRepository::new());
    let email_service = Arc::new(LogEmailService::new());
    let notification_service = Arc::new(LogNotificationService::new());
    let token_cipher = TokenCipher::new("test-secret");

    let worker = OutboxWorker::new(
        outbox_repo.clone(),
        email_service.clone(),
        notification_service,
        OutboxWorkerConfig::default(),
        "https://example.com".to_string(),
        token_cipher.clone(),
    );

    let token_enc = token_cipher.encrypt("abc").unwrap();
    let event = crate::repositories::OutboxEvent::new(
        OutboxEventType::EmailVerification,
        serde_json::json!({
            "to": "test@example.com",
            "user_name": "Test User",
            "token_enc": token_enc,
            "expires_in_hours": 24
        }),
    );
    let event_id = event.id;
    outbox_repo.create(event.clone()).await.unwrap();

    // Before processing, delivered_at should be None
    let before = outbox_repo.find_by_id(event_id).await.unwrap().unwrap();
    assert!(before.delivered_at.is_none());

    // Process the event
    worker.process_event(&event).await.unwrap();

    // After processing, delivered_at should be set
    let after = outbox_repo.find_by_id(event_id).await.unwrap().unwrap();
    assert!(
        after.delivered_at.is_some(),
        "Expected delivered_at to be set after successful processing"
    );
    assert_eq!(after.status, crate::repositories::OutboxStatus::Done);
}