acton-htmx 1.0.0-beta.7

Opinionated Rust web framework for HTMX applications
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
//! CSRF Manager Agent
//!
//! Actor-based CSRF token management using acton-reactive.
//! Implements per-session token generation, validation, and rotation.
//!
//! This module provides a unified message API that works for both:
//! 1. **Web Handlers**: Using optional oneshot channels for synchronous responses
//! 2. **Agent-to-Agent**: Using reply_envelope for asynchronous responses
//!
//! CSRF tokens are:
//! - Cryptographically secure (32 bytes of randomness)
//! - Stored per-session (one active token per session)
//! - Automatically rotated on successful validation
//! - Validated against POST/PUT/DELETE/PATCH requests

use crate::agents::request_reply::{create_request_reply, send_response, ResponseChannel};
use crate::agents::default_agent_config;
use crate::auth::session::SessionId;
use acton_reactive::prelude::*;
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
use chrono::{DateTime, Duration, Utc};
use rand::Rng;
use std::collections::HashMap;
use tokio::sync::oneshot;

// Type alias for the ManagedAgent builder type
type CsrfAgentBuilder = ManagedAgent<Idle, CsrfManagerAgent>;

/// CSRF token string (base64url-encoded 32-byte random value)
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct CsrfToken(String);

impl CsrfToken {
    /// Generate a new cryptographically secure CSRF token
    #[must_use]
    pub fn generate() -> Self {
        let mut rng = rand::rng();
        let mut bytes = [0u8; 32];
        rng.fill(&mut bytes);
        Self(URL_SAFE_NO_PAD.encode(bytes))
    }

    /// Get the token as a string slice
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Create a token from a string (for validation)
    #[must_use]
    pub const fn from_string(s: String) -> Self {
        Self(s)
    }
}

impl std::fmt::Display for CsrfToken {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// CSRF token data stored per session
#[derive(Clone, Debug)]
struct CsrfTokenData {
    /// The actual token
    token: CsrfToken,
    /// When the token expires (24 hours by default)
    expires_at: DateTime<Utc>,
}

impl CsrfTokenData {
    /// Create new token data with default expiration (24 hours)
    #[must_use]
    fn new(token: CsrfToken) -> Self {
        let expires_at = Utc::now() + Duration::hours(24);
        Self { token, expires_at }
    }

    /// Check if the token has expired
    #[must_use]
    fn is_expired(&self) -> bool {
        Utc::now() > self.expires_at
    }
}

/// CSRF manager agent model
#[derive(Debug, Default, Clone)]
pub struct CsrfManagerAgent {
    /// Token storage per session
    tokens: HashMap<SessionId, CsrfTokenData>,
}

// ============================================================================
// Unified Message Types
// ============================================================================

/// Request to get or create a CSRF token for a session
///
/// This message works for both web handlers (with oneshot channel) and
/// agent-to-agent communication (via reply_envelope).
#[derive(Clone, Debug)]
pub struct GetOrCreateToken {
    /// The session ID to get/create token for
    pub session_id: SessionId,
    /// Optional response channel for web handlers
    pub response_tx: Option<ResponseChannel<CsrfToken>>,
}

impl GetOrCreateToken {
    /// Create a new get-or-create token request with response channel for web handlers
    #[must_use]
    pub fn new(session_id: SessionId) -> (Self, oneshot::Receiver<CsrfToken>) {
        let (response_tx, rx) = create_request_reply();
        let request = Self {
            session_id,
            response_tx: Some(response_tx),
        };
        (request, rx)
    }

    /// Create a new get-or-create token message for agent-to-agent communication
    #[must_use]
    pub const fn agent_message(session_id: SessionId) -> Self {
        Self {
            session_id,
            response_tx: None,
        }
    }
}

/// Request to validate a CSRF token
///
/// This message works for both web handlers (with oneshot channel) and
/// agent-to-agent communication (via reply_envelope).
#[derive(Clone, Debug)]
pub struct ValidateToken {
    /// The session ID to validate against
    pub session_id: SessionId,
    /// The token to validate
    pub token: CsrfToken,
    /// Optional response channel for web handlers
    pub response_tx: Option<ResponseChannel<bool>>,
}

impl ValidateToken {
    /// Create a new validate token request with response channel for web handlers
    #[must_use]
    pub fn new(session_id: SessionId, token: CsrfToken) -> (Self, oneshot::Receiver<bool>) {
        let (response_tx, rx) = create_request_reply();
        let request = Self {
            session_id,
            token,
            response_tx: Some(response_tx),
        };
        (request, rx)
    }

    /// Create a new validate token message for agent-to-agent communication
    #[must_use]
    pub const fn agent_message(session_id: SessionId, token: CsrfToken) -> Self {
        Self {
            session_id,
            token,
            response_tx: None,
        }
    }
}

/// Request to delete a CSRF token (on session cleanup)
#[derive(Clone, Debug)]
pub struct DeleteToken {
    /// The session ID to delete token for
    pub session_id: SessionId,
}

impl DeleteToken {
    /// Create a new delete token request (fire-and-forget)
    #[must_use]
    pub const fn new(session_id: SessionId) -> Self {
        Self { session_id }
    }
}

/// Message to cleanup expired tokens
#[derive(Clone, Debug)]
pub struct CleanupExpired;

impl CsrfManagerAgent {
    /// Spawn CSRF manager agent
    ///
    /// # Errors
    ///
    /// Returns error if agent initialization fails
    pub async fn spawn(runtime: &mut AgentRuntime) -> anyhow::Result<AgentHandle> {
        let config = default_agent_config("csrf_manager")?;
        let builder = runtime.new_agent_with_config::<Self>(config).await;
        Self::configure_handlers(builder).await
    }

    /// Configure all message handlers for the CSRF manager
    async fn configure_handlers(mut builder: CsrfAgentBuilder) -> anyhow::Result<AgentHandle> {
        builder
            // Unified handler for GetOrCreateToken (works for both web and agent-to-agent)
            .mutate_on::<GetOrCreateToken>(|agent, envelope| {
                let session_id = envelope.message().session_id.clone();
                let response_tx = envelope.message().response_tx.clone();
                let reply_envelope = envelope.reply_envelope();

                let token = Self::get_or_create_token_internal(&mut agent.model, &session_id);

                AgentReply::from_async(async move {
                    // Web handler response if channel provided
                    if let Some(tx) = response_tx {
                        let _ = send_response(tx, token.clone()).await;
                    }
                    // Agent-to-agent response via envelope (always sent)
                    let _: () = reply_envelope.send(token).await;
                })
            })
            // Unified handler for ValidateToken (works for both web and agent-to-agent)
            .mutate_on::<ValidateToken>(|agent, envelope| {
                let session_id = envelope.message().session_id.clone();
                let token = envelope.message().token.clone();
                let response_tx = envelope.message().response_tx.clone();
                let reply_envelope = envelope.reply_envelope();

                let valid = Self::validate_and_rotate_token(&mut agent.model, &session_id, &token);

                AgentReply::from_async(async move {
                    // Web handler response if channel provided
                    if let Some(tx) = response_tx {
                        let _ = send_response(tx, valid).await;
                    }
                    // Agent-to-agent response via envelope (always sent)
                    let _: () = reply_envelope.send(valid).await;
                })
            })
            // Handler for DeleteToken (fire-and-forget)
            .mutate_on::<DeleteToken>(|agent, envelope| {
                let session_id = envelope.message().session_id.clone();
                agent.model.tokens.remove(&session_id);
                AgentReply::immediate()
            })
            // Handler for CleanupExpired
            .mutate_on::<CleanupExpired>(|agent, _envelope| {
                agent.model.tokens.retain(|_session_id, data| !data.is_expired());
                tracing::debug!(
                    "Cleaned up expired CSRF tokens, {} tokens remaining",
                    agent.model.tokens.len()
                );
                AgentReply::immediate()
            });

        Ok(builder.start().await)
    }

    /// Pure function: Get or create a CSRF token
    fn get_or_create_token_internal(model: &mut Self, session_id: &SessionId) -> CsrfToken {
        if let Some(data) = model.tokens.get(session_id) {
            if !data.is_expired() {
                return data.token.clone();
            }
        }

        // Create new token
        let new_token = CsrfToken::generate();
        model
            .tokens
            .insert(session_id.clone(), CsrfTokenData::new(new_token.clone()));
        new_token
    }

    /// Pure function: Validate token and rotate on success
    fn validate_and_rotate_token(
        model: &mut Self,
        session_id: &SessionId,
        token: &CsrfToken,
    ) -> bool {
        let valid = model
            .tokens
            .get(session_id)
            .filter(|data| !data.is_expired() && &data.token == token)
            .is_some();

        if valid {
            let new_token = CsrfToken::generate();
            model
                .tokens
                .insert(session_id.clone(), CsrfTokenData::new(new_token));
        }

        valid
    }
}

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

    #[test]
    fn test_csrf_token_generation() {
        let token1 = CsrfToken::generate();
        let token2 = CsrfToken::generate();

        // Tokens should be unique
        assert_ne!(token1, token2);

        // Tokens should be base64url encoded (44 chars for 32 bytes)
        assert_eq!(token1.as_str().len(), 43); // 32 bytes = 43 base64url chars without padding
    }

    #[test]
    fn test_csrf_token_display() {
        let token = CsrfToken::generate();
        let as_string = format!("{token}");
        assert_eq!(as_string, token.as_str());
    }

    #[test]
    fn test_csrf_token_from_string() {
        let original = "test_token_value";
        let token = CsrfToken::from_string(original.to_string());
        assert_eq!(token.as_str(), original);
    }

    #[test]
    fn test_csrf_token_data_creation() {
        let token = CsrfToken::generate();
        let data = CsrfTokenData::new(token.clone());

        assert_eq!(data.token, token);
        assert!(!data.is_expired());
        assert!(data.expires_at > Utc::now());
    }

    #[test]
    fn test_csrf_token_data_expiration() {
        let token = CsrfToken::generate();
        let mut data = CsrfTokenData::new(token);

        // Manually set expiration to the past
        data.expires_at = Utc::now() - Duration::hours(1);

        assert!(data.is_expired());
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn test_csrf_manager_spawn() {
        let mut runtime = ActonApp::launch();
        let result = CsrfManagerAgent::spawn(&mut runtime).await;
        assert!(result.is_ok());
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn test_get_or_create_token() {
        let mut runtime = ActonApp::launch();
        let handle = CsrfManagerAgent::spawn(&mut runtime).await.unwrap();

        let session_id = SessionId::generate();
        let (request, rx) = GetOrCreateToken::new(session_id.clone());

        handle.send(request).await;

        let token1 = rx.await.expect("Failed to receive token");

        // Request again - should get the same token
        let (request2, rx2) = GetOrCreateToken::new(session_id);
        handle.send(request2).await;

        let token2 = rx2.await.expect("Failed to receive token");

        assert_eq!(token1, token2);
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn test_validate_token_success() {
        let mut runtime = ActonApp::launch();
        let handle = CsrfManagerAgent::spawn(&mut runtime).await.unwrap();

        let session_id = SessionId::generate();

        // Get a token
        let (request, rx) = GetOrCreateToken::new(session_id.clone());
        handle.send(request).await;
        let token = rx.await.expect("Failed to receive token");

        // Validate it
        let (validate_request, validate_rx) =
            ValidateToken::new(session_id.clone(), token.clone());
        handle.send(validate_request).await;
        let valid = validate_rx.await.expect("Failed to receive validation result");

        assert!(valid);

        // After validation, token should be rotated - old token should be invalid
        let (validate_request2, validate_rx2) = ValidateToken::new(session_id, token);
        handle.send(validate_request2).await;
        let valid2 = validate_rx2
            .await
            .expect("Failed to receive validation result");

        assert!(!valid2);
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn test_validate_token_failure() {
        let mut runtime = ActonApp::launch();
        let handle = CsrfManagerAgent::spawn(&mut runtime).await.unwrap();

        let session_id = SessionId::generate();

        // Get a token
        let (request, rx) = GetOrCreateToken::new(session_id.clone());
        handle.send(request).await;
        let _token = rx.await.expect("Failed to receive token");

        // Try to validate with wrong token
        let wrong_token = CsrfToken::generate();
        let (validate_request, validate_rx) = ValidateToken::new(session_id, wrong_token);
        handle.send(validate_request).await;
        let valid = validate_rx.await.expect("Failed to receive validation result");

        assert!(!valid);
    }

    #[tokio::test(flavor = "multi_thread")]
    async fn test_delete_token() {
        let mut runtime = ActonApp::launch();
        let handle = CsrfManagerAgent::spawn(&mut runtime).await.unwrap();

        let session_id = SessionId::generate();

        // Get a token
        let (request, rx) = GetOrCreateToken::new(session_id.clone());
        handle.send(request).await;
        let token = rx.await.expect("Failed to receive token");

        // Delete the token
        let delete_request = DeleteToken::new(session_id.clone());
        handle.send(delete_request).await;

        // Try to validate - should fail
        let (validate_request, validate_rx) = ValidateToken::new(session_id, token);
        handle.send(validate_request).await;
        let valid = validate_rx.await.expect("Failed to receive validation result");

        assert!(!valid);
    }
}