bext-realtime 0.2.0

Realtime pub/sub for bext — WebSocket and SSE with optional Redis relay
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
//! Topic-level authorization policies and rules for the realtime pub/sub hub.
//!
//! Defines [`Policy`] variants (public, authenticated, role-based, user-specific)
//! and [`AuthRule`]s that map topic patterns to subscribe/publish policies.

use serde::{Deserialize, Serialize};

use crate::topic::TopicMatcher;

/// Authorization policy for a topic action (subscribe or publish).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", content = "value", rename_all = "snake_case")]
pub enum Policy {
    /// Anyone can perform the action.
    Public,
    /// Only authenticated users.
    Authenticated,
    /// Only users with a specific role.
    Role(String),
    /// Only a specific user.
    UserId(String),
}

/// A rule mapping a topic pattern to subscribe/publish policies.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthRule {
    /// Topic pattern (supports wildcards `*` and `#`).
    pub pattern: String,
    /// Policy for subscribing to this topic.
    pub subscribe_policy: Policy,
    /// Policy for publishing to this topic.
    pub publish_policy: Policy,
}

/// Caller-provided identity context for authorization checks.
#[derive(Debug, Clone, Default)]
pub struct AuthContext {
    /// The user's unique ID, if authenticated.
    pub user_id: Option<String>,
    /// Roles the user holds.
    pub roles: Vec<String>,
    /// Whether the user is authenticated at all.
    pub is_authenticated: bool,
}

impl AuthContext {
    /// Create an unauthenticated context.
    pub fn anonymous() -> Self {
        Self::default()
    }

    /// Create an authenticated context with the given user ID and roles.
    pub fn authenticated(user_id: impl Into<String>, roles: Vec<String>) -> Self {
        Self {
            user_id: Some(user_id.into()),
            roles,
            is_authenticated: true,
        }
    }
}

/// Topic authorization engine.
///
/// Holds a set of rules that map topic patterns to access policies.
/// When checking access, the *first matching rule* wins. If no rule matches,
/// a configurable default policy applies (defaults to `Public` for subscribe,
/// `Authenticated` for publish).
#[derive(Debug, Clone)]
pub struct TopicAuth {
    rules: Vec<AuthRule>,
    default_subscribe: Policy,
    default_publish: Policy,
}

impl TopicAuth {
    /// Create with default rules for the standard topic namespaces.
    ///
    /// Default rules:
    /// - `system/#` — subscribe: Authenticated, publish: never (internal only)
    /// - `plugin/#` — subscribe: Public, publish: Authenticated
    /// - `custom/#` — subscribe: Public, publish: Authenticated
    pub fn with_defaults() -> Self {
        Self {
            rules: vec![
                AuthRule {
                    pattern: "system/#".to_string(),
                    subscribe_policy: Policy::Authenticated,
                    // We encode "internal only" by requiring a special role
                    // that no external user should have.
                    publish_policy: Policy::Role("__system_internal__".to_string()),
                },
                AuthRule {
                    pattern: "plugin/#".to_string(),
                    subscribe_policy: Policy::Public,
                    publish_policy: Policy::Authenticated,
                },
                AuthRule {
                    pattern: "custom/#".to_string(),
                    subscribe_policy: Policy::Public,
                    publish_policy: Policy::Authenticated,
                },
            ],
            default_subscribe: Policy::Public,
            default_publish: Policy::Authenticated,
        }
    }

    /// Create with no rules (everything falls through to defaults).
    pub fn new() -> Self {
        Self {
            rules: Vec::new(),
            default_subscribe: Policy::Public,
            default_publish: Policy::Authenticated,
        }
    }

    /// Create with custom rules.
    pub fn with_rules(rules: Vec<AuthRule>) -> Self {
        Self {
            rules,
            default_subscribe: Policy::Public,
            default_publish: Policy::Authenticated,
        }
    }

    /// Override the default subscribe policy.
    pub fn set_default_subscribe(&mut self, policy: Policy) {
        self.default_subscribe = policy;
    }

    /// Override the default publish policy.
    pub fn set_default_publish(&mut self, policy: Policy) {
        self.default_publish = policy;
    }

    /// Add a rule.
    pub fn add_rule(&mut self, rule: AuthRule) {
        self.rules.push(rule);
    }

    /// Check if the given context is allowed to subscribe to `topic`.
    pub fn check_subscribe(&self, topic: &str, ctx: &AuthContext) -> bool {
        let policy = self.find_subscribe_policy(topic);
        Self::evaluate(policy, ctx)
    }

    /// Check if the given context is allowed to publish to `topic`.
    pub fn check_publish(&self, topic: &str, ctx: &AuthContext) -> bool {
        let policy = self.find_publish_policy(topic);
        Self::evaluate(policy, ctx)
    }

    /// Return the list of rules.
    pub fn rules(&self) -> &[AuthRule] {
        &self.rules
    }

    // ── Private ─────────────────────────────────────────────────────

    fn find_subscribe_policy(&self, topic: &str) -> &Policy {
        for rule in &self.rules {
            if TopicMatcher::matches(&rule.pattern, topic) {
                return &rule.subscribe_policy;
            }
        }
        &self.default_subscribe
    }

    fn find_publish_policy(&self, topic: &str) -> &Policy {
        for rule in &self.rules {
            if TopicMatcher::matches(&rule.pattern, topic) {
                return &rule.publish_policy;
            }
        }
        &self.default_publish
    }

    fn evaluate(policy: &Policy, ctx: &AuthContext) -> bool {
        match policy {
            Policy::Public => true,
            Policy::Authenticated => ctx.is_authenticated,
            Policy::Role(required_role) => ctx.roles.contains(required_role),
            Policy::UserId(required_id) => ctx.user_id.as_deref() == Some(required_id.as_str()),
        }
    }
}

impl Default for TopicAuth {
    fn default() -> Self {
        Self::with_defaults()
    }
}

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

    fn anonymous() -> AuthContext {
        AuthContext::anonymous()
    }

    fn authenticated_user(user_id: &str) -> AuthContext {
        AuthContext::authenticated(user_id, vec![])
    }

    fn user_with_role(user_id: &str, role: &str) -> AuthContext {
        AuthContext::authenticated(user_id, vec![role.to_string()])
    }

    fn user_with_roles(user_id: &str, roles: Vec<&str>) -> AuthContext {
        AuthContext::authenticated(user_id, roles.into_iter().map(String::from).collect())
    }

    // ── Policy::Public ──────────────────────────────────────────────

    #[test]
    fn public_allows_anonymous() {
        let auth = TopicAuth::with_rules(vec![AuthRule {
            pattern: "test/#".to_string(),
            subscribe_policy: Policy::Public,
            publish_policy: Policy::Public,
        }]);

        assert!(auth.check_subscribe("test/foo", &anonymous()));
        assert!(auth.check_publish("test/foo", &anonymous()));
    }

    #[test]
    fn public_allows_authenticated() {
        let auth = TopicAuth::with_rules(vec![AuthRule {
            pattern: "test/#".to_string(),
            subscribe_policy: Policy::Public,
            publish_policy: Policy::Public,
        }]);

        let ctx = authenticated_user("alice");
        assert!(auth.check_subscribe("test/foo", &ctx));
        assert!(auth.check_publish("test/foo", &ctx));
    }

    // ── Policy::Authenticated ───────────────────────────────────────

    #[test]
    fn authenticated_denies_anonymous() {
        let auth = TopicAuth::with_rules(vec![AuthRule {
            pattern: "test/#".to_string(),
            subscribe_policy: Policy::Authenticated,
            publish_policy: Policy::Authenticated,
        }]);

        assert!(!auth.check_subscribe("test/foo", &anonymous()));
        assert!(!auth.check_publish("test/foo", &anonymous()));
    }

    #[test]
    fn authenticated_allows_logged_in() {
        let auth = TopicAuth::with_rules(vec![AuthRule {
            pattern: "test/#".to_string(),
            subscribe_policy: Policy::Authenticated,
            publish_policy: Policy::Authenticated,
        }]);

        let ctx = authenticated_user("bob");
        assert!(auth.check_subscribe("test/foo", &ctx));
        assert!(auth.check_publish("test/foo", &ctx));
    }

    // ── Policy::Role ────────────────────────────────────────────────

    #[test]
    fn role_denies_wrong_role() {
        let auth = TopicAuth::with_rules(vec![AuthRule {
            pattern: "admin/#".to_string(),
            subscribe_policy: Policy::Role("admin".to_string()),
            publish_policy: Policy::Role("admin".to_string()),
        }]);

        let ctx = user_with_role("alice", "viewer");
        assert!(!auth.check_subscribe("admin/settings", &ctx));
        assert!(!auth.check_publish("admin/settings", &ctx));
    }

    #[test]
    fn role_allows_correct_role() {
        let auth = TopicAuth::with_rules(vec![AuthRule {
            pattern: "admin/#".to_string(),
            subscribe_policy: Policy::Role("admin".to_string()),
            publish_policy: Policy::Role("admin".to_string()),
        }]);

        let ctx = user_with_role("alice", "admin");
        assert!(auth.check_subscribe("admin/settings", &ctx));
        assert!(auth.check_publish("admin/settings", &ctx));
    }

    #[test]
    fn role_denies_anonymous() {
        let auth = TopicAuth::with_rules(vec![AuthRule {
            pattern: "admin/#".to_string(),
            subscribe_policy: Policy::Role("admin".to_string()),
            publish_policy: Policy::Role("admin".to_string()),
        }]);

        assert!(!auth.check_subscribe("admin/settings", &anonymous()));
    }

    #[test]
    fn role_check_with_multiple_roles() {
        let auth = TopicAuth::with_rules(vec![AuthRule {
            pattern: "ops/#".to_string(),
            subscribe_policy: Policy::Role("operator".to_string()),
            publish_policy: Policy::Role("operator".to_string()),
        }]);

        let ctx = user_with_roles("bob", vec!["viewer", "operator"]);
        assert!(auth.check_subscribe("ops/deploy", &ctx));
    }

    // ── Policy::UserId ──────────────────────────────────────────────

    #[test]
    fn userid_allows_matching_user() {
        let auth = TopicAuth::with_rules(vec![AuthRule {
            pattern: "user/alice/#".to_string(),
            subscribe_policy: Policy::UserId("alice".to_string()),
            publish_policy: Policy::UserId("alice".to_string()),
        }]);

        let ctx = authenticated_user("alice");
        assert!(auth.check_subscribe("user/alice/inbox", &ctx));
        assert!(auth.check_publish("user/alice/inbox", &ctx));
    }

    #[test]
    fn userid_denies_different_user() {
        let auth = TopicAuth::with_rules(vec![AuthRule {
            pattern: "user/alice/#".to_string(),
            subscribe_policy: Policy::UserId("alice".to_string()),
            publish_policy: Policy::UserId("alice".to_string()),
        }]);

        let ctx = authenticated_user("bob");
        assert!(!auth.check_subscribe("user/alice/inbox", &ctx));
        assert!(!auth.check_publish("user/alice/inbox", &ctx));
    }

    #[test]
    fn userid_denies_anonymous() {
        let auth = TopicAuth::with_rules(vec![AuthRule {
            pattern: "user/alice/#".to_string(),
            subscribe_policy: Policy::UserId("alice".to_string()),
            publish_policy: Policy::UserId("alice".to_string()),
        }]);

        assert!(!auth.check_subscribe("user/alice/inbox", &anonymous()));
    }

    // ── Default rules ───────────────────────────────────────────────

    #[test]
    fn default_system_subscribe_requires_auth() {
        let auth = TopicAuth::with_defaults();
        assert!(!auth.check_subscribe("system/deploy", &anonymous()));
        assert!(auth.check_subscribe("system/deploy", &authenticated_user("u")));
    }

    #[test]
    fn default_system_publish_internal_only() {
        let auth = TopicAuth::with_defaults();
        // Even authenticated users can't publish to system topics
        assert!(!auth.check_publish("system/deploy", &authenticated_user("u")));
        assert!(!auth.check_publish("system/health", &user_with_role("u", "admin")));
        // Only the special internal role can
        assert!(auth.check_publish(
            "system/deploy",
            &user_with_role("internal", "__system_internal__")
        ));
    }

    #[test]
    fn default_plugin_subscribe_public() {
        let auth = TopicAuth::with_defaults();
        assert!(auth.check_subscribe("plugin/analytics/events", &anonymous()));
    }

    #[test]
    fn default_plugin_publish_requires_auth() {
        let auth = TopicAuth::with_defaults();
        assert!(!auth.check_publish("plugin/analytics/events", &anonymous()));
        assert!(auth.check_publish("plugin/analytics/events", &authenticated_user("u")));
    }

    #[test]
    fn default_custom_subscribe_public() {
        let auth = TopicAuth::with_defaults();
        assert!(auth.check_subscribe("custom/chat", &anonymous()));
    }

    #[test]
    fn default_custom_publish_requires_auth() {
        let auth = TopicAuth::with_defaults();
        assert!(!auth.check_publish("custom/chat", &anonymous()));
        assert!(auth.check_publish("custom/chat", &authenticated_user("u")));
    }

    // ── Fallthrough to defaults ─────────────────────────────────────

    #[test]
    fn unknown_topic_uses_default_subscribe_public() {
        let auth = TopicAuth::with_defaults();
        assert!(auth.check_subscribe("unknown/topic", &anonymous()));
    }

    #[test]
    fn unknown_topic_uses_default_publish_authenticated() {
        let auth = TopicAuth::with_defaults();
        assert!(!auth.check_publish("unknown/topic", &anonymous()));
        assert!(auth.check_publish("unknown/topic", &authenticated_user("u")));
    }

    // ── First matching rule wins ────────────────────────────────────

    #[test]
    fn first_matching_rule_wins() {
        let auth = TopicAuth::with_rules(vec![
            AuthRule {
                pattern: "data/secret".to_string(),
                subscribe_policy: Policy::Role("admin".to_string()),
                publish_policy: Policy::Role("admin".to_string()),
            },
            AuthRule {
                pattern: "data/#".to_string(),
                subscribe_policy: Policy::Public,
                publish_policy: Policy::Public,
            },
        ]);

        // "data/secret" matches the first rule (admin only)
        assert!(!auth.check_subscribe("data/secret", &anonymous()));
        assert!(auth.check_subscribe("data/secret", &user_with_role("u", "admin")));

        // "data/other" matches the second rule (public)
        assert!(auth.check_subscribe("data/other", &anonymous()));
    }

    // ── Mixed subscribe/publish policies ────────────────────────────

    #[test]
    fn asymmetric_policies() {
        let auth = TopicAuth::with_rules(vec![AuthRule {
            pattern: "broadcast/#".to_string(),
            subscribe_policy: Policy::Public,
            publish_policy: Policy::Role("broadcaster".to_string()),
        }]);

        let viewer = anonymous();
        let broadcaster = user_with_role("alice", "broadcaster");

        // Anyone can subscribe
        assert!(auth.check_subscribe("broadcast/news", &viewer));
        assert!(auth.check_subscribe("broadcast/news", &broadcaster));

        // Only broadcasters can publish
        assert!(!auth.check_publish("broadcast/news", &viewer));
        assert!(auth.check_publish("broadcast/news", &broadcaster));
    }

    // ── add_rule / set_defaults ─────────────────────────────────────

    #[test]
    fn add_rule_extends_rules() {
        let mut auth = TopicAuth::new();
        auth.add_rule(AuthRule {
            pattern: "secret/#".to_string(),
            subscribe_policy: Policy::Authenticated,
            publish_policy: Policy::Authenticated,
        });

        assert!(!auth.check_subscribe("secret/data", &anonymous()));
        assert!(auth.check_subscribe("secret/data", &authenticated_user("u")));
    }

    #[test]
    fn set_default_subscribe_changes_fallthrough() {
        let mut auth = TopicAuth::new();
        auth.set_default_subscribe(Policy::Authenticated);

        assert!(!auth.check_subscribe("anything", &anonymous()));
        assert!(auth.check_subscribe("anything", &authenticated_user("u")));
    }

    #[test]
    fn set_default_publish_changes_fallthrough() {
        let mut auth = TopicAuth::new();
        auth.set_default_publish(Policy::Public);

        assert!(auth.check_publish("anything", &anonymous()));
    }

    // ── Policy serialization ────────────────────────────────────────

    #[test]
    fn policy_roundtrip() {
        let policies = vec![
            Policy::Public,
            Policy::Authenticated,
            Policy::Role("admin".to_string()),
            Policy::UserId("alice".to_string()),
        ];

        for p in policies {
            let json_str = serde_json::to_string(&p).unwrap();
            let deserialized: Policy = serde_json::from_str(&json_str).unwrap();
            assert_eq!(p, deserialized);
        }
    }

    #[test]
    fn auth_rule_roundtrip() {
        let rule = AuthRule {
            pattern: "test/#".to_string(),
            subscribe_policy: Policy::Public,
            publish_policy: Policy::Role("admin".to_string()),
        };
        let json_str = serde_json::to_string(&rule).unwrap();
        let deserialized: AuthRule = serde_json::from_str(&json_str).unwrap();
        assert_eq!(rule.pattern, deserialized.pattern);
    }

    // ── AuthContext constructors ─────────────────────────────────────

    #[test]
    fn anonymous_context() {
        let ctx = AuthContext::anonymous();
        assert!(ctx.user_id.is_none());
        assert!(ctx.roles.is_empty());
        assert!(!ctx.is_authenticated);
    }

    #[test]
    fn authenticated_context() {
        let ctx = AuthContext::authenticated("alice", vec!["admin".to_string()]);
        assert_eq!(ctx.user_id, Some("alice".to_string()));
        assert_eq!(ctx.roles, vec!["admin"]);
        assert!(ctx.is_authenticated);
    }
}