cedros-login-server 0.0.18

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
//! Webhook callback implementation

use async_trait::async_trait;
use hmac::{Hmac, Mac};
use rand::Rng;
use reqwest::Client;
use serde_json::Value;
use sha2::Sha256;
use std::net::IpAddr;
use std::time::{SystemTime, UNIX_EPOCH};
use tracing::{debug, error, warn};

use crate::callback::{AuthCallback, AuthCallbackPayload};
use crate::errors::AppError;

use super::types::{
    AuthWebhookData, LogoutWebhookData, WebhookConfig, WebhookData, WebhookEvent, WebhookPayload,
};

type HmacSha256 = Hmac<Sha256>;

/// Webhook callback that sends signed HTTP POST requests
pub struct WebhookCallback {
    config: WebhookConfig,
    client: Client,
    secret_valid: bool,
}

impl WebhookCallback {
    /// Create a new webhook callback with the given configuration
    ///
    /// # Security Warning
    /// This method logs warnings but does not fail for weak secrets.
    /// Use `try_new()` for strict validation.
    #[deprecated(note = "Use try_new() for strict secret validation")]
    pub fn new(config: WebhookConfig) -> Self {
        // SEC-008: Validate secret and fail closed on send.
        let secret_valid = match config.validate_secret() {
            Ok(()) => true,
            Err(e) => {
                tracing::error!(
                    error = %e,
                    "SECURITY WARNING: Webhook secret validation failed. \
                    Webhooks may be vulnerable to forgery attacks. \
                    Use a strong secret generated with: openssl rand -base64 32"
                );
                false
            }
        };

        if !secret_valid {
            tracing::error!(
                "WebhookCallback created with invalid secret; delivery will fail closed. \
                Use WebhookCallback::try_new() to validate at construction."
            );
        }

        let client = Client::builder()
            .timeout(std::time::Duration::from_secs(config.timeout_secs))
            .build()
            .unwrap_or_else(|e| {
                tracing::error!(
                    error = %e,
                    "Failed to build webhook HTTP client; falling back to defaults"
                );
                Client::new()
            });

        Self {
            config,
            client,
            secret_valid,
        }
    }

    /// Try to create a new webhook callback, returning an error if it fails
    ///
    /// SEC-008: Validates that the webhook secret meets minimum security requirements.
    pub fn try_new(config: WebhookConfig) -> Result<Self, AppError> {
        // SEC-008: Validate secret before allowing webhook creation
        config.validate_secret().map_err(AppError::Config)?;

        let client = Client::builder()
            .timeout(std::time::Duration::from_secs(config.timeout_secs))
            .build()
            .map_err(|e| {
                AppError::Internal(anyhow::anyhow!("Failed to create HTTP client: {}", e))
            })?;

        Ok(Self {
            config,
            client,
            secret_valid: true,
        })
    }

    /// Send a webhook with retries
    async fn send_webhook(&self, payload: &WebhookPayload) -> Result<Value, AppError> {
        if !self.secret_valid {
            return Err(AppError::Config(
                "Webhook secret validation failed. Generate with: openssl rand -base64 32".into(),
            ));
        }
        let body = serde_json::to_string(payload).map_err(|e| AppError::Internal(e.into()))?;

        let timestamp = payload.timestamp.to_string();
        let signed_payload = format!("{}.{}", timestamp, body);
        let signature = self.sign_payload(&signed_payload);

        let mut last_error = None;

        // SEC-006: Max backoff cap prevents total retry time exceeding request timeout
        const MAX_BACKOFF_MS: u64 = 10_000; // 10 seconds max per retry

        for attempt in 0..=self.config.retry_attempts {
            if attempt > 0 {
                // Exponential backoff with jitter: base * 2^(attempt-1) +/- 25%
                let base_delay_ms = 100u64 * (1 << (attempt - 1));
                let jitter_range = base_delay_ms / 4; // +/- 25%
                let jitter =
                    rand::thread_rng().gen_range(0..=jitter_range * 2) as i64 - jitter_range as i64;
                // Cap delay at MAX_BACKOFF_MS to prevent excessive total retry time
                let delay_ms = (base_delay_ms as i64 + jitter)
                    .max(50)
                    .min(MAX_BACKOFF_MS as i64) as u64;
                let delay = std::time::Duration::from_millis(delay_ms);
                tokio::time::sleep(delay).await;
                debug!(
                    "Retrying webhook (attempt {}, delay {}ms)",
                    attempt + 1,
                    delay_ms
                );
            }

            match self.send_request(&body, &signature, &timestamp).await {
                Ok(response) => return Ok(response),
                Err(e) => {
                    warn!("Webhook attempt {} failed: {}", attempt + 1, e);
                    last_error = Some(e);
                }
            }
        }

        // All retries exhausted
        error!(
            "Webhook failed after {} attempts",
            self.config.retry_attempts + 1
        );
        Err(last_error.unwrap_or_else(|| AppError::Internal(anyhow::anyhow!("Webhook failed"))))
    }

    /// Send a single webhook request
    async fn send_request(
        &self,
        body: &str,
        signature: &str,
        timestamp: &str,
    ) -> Result<Value, AppError> {
        self.validate_destination().await?;

        let response_fut = self
            .client
            .post(&self.config.url)
            .header("Content-Type", "application/json")
            .header("X-Cedros-Signature", signature)
            .header("X-Cedros-Timestamp", timestamp)
            .body(body.to_string())
            .send();

        let response = tokio::time::timeout(
            std::time::Duration::from_secs(self.config.timeout_secs),
            response_fut,
        )
        .await
        .map_err(|_| {
            AppError::Internal(anyhow::anyhow!(
                "Webhook request timed out after {}s",
                self.config.timeout_secs
            ))
        })?
        .map_err(|e| AppError::Internal(e.into()))?;

        if !response.status().is_success() {
            let status = response.status();
            let error_body = tokio::time::timeout(
                std::time::Duration::from_secs(self.config.timeout_secs),
                response.text(),
            )
            .await
            .ok()
            .and_then(|r| r.ok())
            .unwrap_or_default();
            return Err(AppError::Internal(anyhow::anyhow!(
                "Webhook returned {}: {}",
                status,
                error_body
            )));
        }

        // Try to parse response body as JSON, default to empty object
        let response_body = tokio::time::timeout(
            std::time::Duration::from_secs(self.config.timeout_secs),
            response.text(),
        )
        .await
        .ok()
        .and_then(|r| r.ok())
        .unwrap_or_default();
        let callback_data: Value = serde_json::from_str(&response_body)
            .unwrap_or_else(|_| Value::Object(serde_json::Map::new()));

        Ok(callback_data)
    }

    async fn validate_destination(&self) -> Result<(), AppError> {
        const DNS_LOOKUP_TIMEOUT_SECS: u64 = 2;

        let url = url::Url::parse(&self.config.url)
            .map_err(|e| AppError::Internal(anyhow::anyhow!("Invalid webhook URL: {}", e)))?;

        if url.scheme() != "http" && url.scheme() != "https" {
            return Err(AppError::Internal(anyhow::anyhow!(
                "Webhook URL must use http or https scheme"
            )));
        }

        let host = url
            .host()
            .ok_or_else(|| AppError::Internal(anyhow::anyhow!("Webhook URL must have a host")))?;

        match host {
            url::Host::Ipv4(ip) => {
                if is_private_ip(IpAddr::V4(ip)) {
                    return Err(AppError::Internal(anyhow::anyhow!(
                        "Webhook URL cannot target private IP addresses"
                    )));
                }
            }
            url::Host::Ipv6(ip) => {
                if is_private_ip(IpAddr::V6(ip)) {
                    return Err(AppError::Internal(anyhow::anyhow!(
                        "Webhook URL cannot target private IP addresses"
                    )));
                }
            }
            url::Host::Domain(domain) => {
                if domain == "localhost"
                    || domain.ends_with(".local")
                    || domain.ends_with(".internal")
                {
                    return Err(AppError::Internal(anyhow::anyhow!(
                        "Webhook URL cannot target internal hostnames"
                    )));
                }

                let port = url.port_or_known_default().unwrap_or(443);
                let addrs = tokio::time::timeout(
                    std::time::Duration::from_secs(DNS_LOOKUP_TIMEOUT_SECS),
                    tokio::net::lookup_host((domain, port)),
                )
                .await
                .map_err(|_| AppError::Internal(anyhow::anyhow!("Webhook DNS lookup timed out")))?
                .map_err(|e| {
                    AppError::Internal(anyhow::anyhow!("Failed to resolve webhook URL: {}", e))
                })?;
                for addr in addrs {
                    if is_private_ip(addr.ip()) {
                        return Err(AppError::Internal(anyhow::anyhow!(
                            "Webhook URL resolves to private IP addresses"
                        )));
                    }
                }
            }
        }

        Ok(())
    }

    /// Sign the payload with HMAC-SHA256
    ///
    /// Note: HMAC-SHA256 accepts keys of any size, so new_from_slice only fails
    /// if the algorithm is invalid (which it isn't for Sha256). This unwrap is safe.
    pub fn sign_payload(&self, payload: &str) -> String {
        // SAFETY: HMAC-SHA256 accepts keys of any length, this cannot fail
        let mut mac = HmacSha256::new_from_slice(self.config.secret.as_bytes())
            .expect("HMAC-SHA256 accepts keys of any size");
        mac.update(payload.as_bytes());
        let result = mac.finalize();
        hex::encode(result.into_bytes())
    }

    /// Get current Unix timestamp
    fn current_timestamp() -> u64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            // SAFETY: SystemTime::now() is always after UNIX_EPOCH on any reasonable system
            .expect("System clock is before Unix epoch")
            .as_secs()
    }
}

fn is_private_ip(ip: IpAddr) -> bool {
    match ip {
        IpAddr::V4(v4) => {
            let octets = v4.octets();
            octets[0] == 10
                || (octets[0] == 172 && (16..=31).contains(&octets[1]))
                || (octets[0] == 192 && octets[1] == 168)
                || octets[0] == 127
                || (octets[0] == 169 && octets[1] == 254)
                || octets[0] == 0
                || (octets[0] == 100 && (octets[1] & 0b1100_0000) == 64)
                || (octets[0] == 192 && octets[1] == 0 && octets[2] == 0)
                || (octets[0] == 198 && (octets[1] == 18 || octets[1] == 19))
                || octets[0] >= 224
        }
        IpAddr::V6(v6) => {
            v6.is_loopback()
                || v6.is_unspecified()
                || v6.is_multicast()
                || v6.segments()[0] & 0xfe00 == 0xfc00
                || v6.segments()[0] & 0xffc0 == 0xfe80
        }
    }
}

#[async_trait]
impl AuthCallback for WebhookCallback {
    async fn on_authenticated(&self, payload: &AuthCallbackPayload) -> Result<Value, AppError> {
        let webhook_payload = WebhookPayload {
            event: WebhookEvent::UserAuthenticated,
            timestamp: Self::current_timestamp(),
            data: WebhookData::Auth(AuthWebhookData {
                user_id: payload.user.id.to_string(),
                email: payload.user.email.clone(),
                name: payload.user.name.clone(),
                wallet_address: payload.user.wallet_address.clone(),
                auth_method: payload.method.as_str().to_string(),
                is_new_user: payload.is_new_user,
                session_id: payload.session_id.clone(),
                ip_address: payload.ip_address.clone(),
                user_agent: payload.user_agent.clone(),
            }),
        };

        self.send_webhook(&webhook_payload).await
    }

    async fn on_registered(&self, payload: &AuthCallbackPayload) -> Result<Value, AppError> {
        let webhook_payload = WebhookPayload {
            event: WebhookEvent::UserRegistered,
            timestamp: Self::current_timestamp(),
            data: WebhookData::Auth(AuthWebhookData {
                user_id: payload.user.id.to_string(),
                email: payload.user.email.clone(),
                name: payload.user.name.clone(),
                wallet_address: payload.user.wallet_address.clone(),
                auth_method: payload.method.as_str().to_string(),
                is_new_user: payload.is_new_user,
                session_id: payload.session_id.clone(),
                ip_address: payload.ip_address.clone(),
                user_agent: payload.user_agent.clone(),
            }),
        };

        self.send_webhook(&webhook_payload).await
    }

    async fn on_logout(&self, user_id: &str) -> Result<(), AppError> {
        let webhook_payload = WebhookPayload {
            event: WebhookEvent::UserLogout,
            timestamp: Self::current_timestamp(),
            data: WebhookData::Logout(LogoutWebhookData {
                user_id: user_id.to_string(),
            }),
        };

        self.send_webhook(&webhook_payload).await?;
        Ok(())
    }
}

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

    fn config_with_url(url: &str) -> WebhookConfig {
        WebhookConfig {
            url: url.to_string(),
            secret: "a".repeat(32),
            timeout_secs: 1,
            retry_attempts: 0,
        }
    }

    #[tokio::test]
    async fn test_validate_destination_rejects_private_ip() {
        let callback = WebhookCallback::try_new(config_with_url("http://127.0.0.1:8080")).unwrap();
        assert!(callback.validate_destination().await.is_err());
    }

    #[tokio::test]
    async fn test_validate_destination_accepts_public_ip() {
        let callback =
            WebhookCallback::try_new(config_with_url("https://8.8.8.8/webhook")).unwrap();
        assert!(callback.validate_destination().await.is_ok());
    }

    #[tokio::test]
    #[allow(deprecated)]
    async fn test_new_fails_closed_on_invalid_secret() {
        let mut config = config_with_url("https://example.com/webhook");
        config.secret = "short".to_string();
        let callback = WebhookCallback::new(config);
        let err = callback
            .on_logout("user-123")
            .await
            .unwrap_err()
            .to_string();
        assert!(err.contains("Webhook secret validation failed"));
    }
}