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
use crate::middleware::{Middleware, MiddlewareFuture, MiddlewareResult};
use failure::Error;
use futures::{future, Future, Poll};
use tgbot::types::{Integer, Update};

/// Access control middleware
///
/// Helps to deny/allow updates from specific user/chat
pub struct AccessMiddleware<P> {
    policy: P,
}

impl<P> AccessMiddleware<P> {
    /// Creates a middleware with specified policy
    pub fn new(policy: P) -> Self {
        AccessMiddleware { policy }
    }
}

impl<C, P> Middleware<C> for AccessMiddleware<P>
where
    P: AccessPolicy<C>,
{
    fn before(&mut self, context: &mut C, update: &Update) -> MiddlewareFuture {
        MiddlewareFuture::new(self.policy.is_granted(context, &update).and_then(|result| {
            if result {
                Ok(MiddlewareResult::Continue)
            } else {
                Ok(MiddlewareResult::Stop)
            }
        }))
    }
}

/// An access policy
///
/// Decides whether update is allowed or not
pub trait AccessPolicy<C> {
    /// Return true if update is allowed and false otherwise
    fn is_granted(&mut self, context: &mut C, update: &Update) -> AccessPolicyFuture;
}

/// Access policy future
#[must_use = "futures do nothing unless polled"]
pub struct AccessPolicyFuture {
    inner: Box<Future<Item = bool, Error = Error> + Send>,
}

impl AccessPolicyFuture {
    /// Creates a future
    pub fn new<F>(f: F) -> Self
    where
        F: Future<Item = bool, Error = Error> + Send + 'static,
    {
        AccessPolicyFuture { inner: Box::new(f) }
    }
}

impl From<bool> for AccessPolicyFuture {
    fn from(flag: bool) -> AccessPolicyFuture {
        AccessPolicyFuture::new(future::ok(flag))
    }
}

impl Future for AccessPolicyFuture {
    type Item = bool;
    type Error = Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        self.inner.poll()
    }
}

/// An access rule - contains information about principal and grant
#[derive(Debug)]
pub struct AccessRule {
    principal: Principal,
    is_granted: bool,
}

impl AccessRule {
    /// Creates a new rule
    pub fn new<P: Into<Principal>>(principal: P, is_granted: bool) -> Self {
        AccessRule {
            principal: principal.into(),
            is_granted,
        }
    }

    /// Creates a new rule with granted access
    pub fn allow<P: Into<Principal>>(principal: P) -> Self {
        Self::new(principal, true)
    }

    /// Creates a new rule with forbidden access
    pub fn deny<P: Into<Principal>>(principal: P) -> Self {
        Self::new(principal, false)
    }

    /// Creates a new rule with granted access for all
    pub fn allow_all() -> Self {
        Self::allow(Principal::All)
    }

    /// Creates a new rule with forbidden access for all
    pub fn deny_all() -> Self {
        Self::deny(Principal::All)
    }

    /// Creates a new rule with granted access for user
    pub fn allow_user<P: Into<PrincipalUser>>(principal: P) -> Self {
        Self::allow(principal.into())
    }

    /// Creates a new rule with forbidden access for user
    pub fn deny_user<P: Into<PrincipalUser>>(principal: P) -> Self {
        Self::deny(principal.into())
    }

    /// Creates a new rule with granted access for chat
    pub fn allow_chat<P: Into<PrincipalChat>>(principal: P) -> Self {
        Self::allow(principal.into())
    }

    /// Creates a new rule with forbidden access for chat
    pub fn deny_chat<P: Into<PrincipalChat>>(principal: P) -> Self {
        Self::deny(principal.into())
    }

    /// Creates a new rule with granted access for chat user
    pub fn allow_chat_user<C, U>(chat: C, user: U) -> Self
    where
        C: Into<PrincipalChat>,
        U: Into<PrincipalUser>,
    {
        Self::allow((chat.into(), user.into()))
    }

    /// Creates a new rule with forbidden access for chat user
    pub fn deny_chat_user<C, U>(chat: C, user: U) -> Self
    where
        C: Into<PrincipalChat>,
        U: Into<PrincipalUser>,
    {
        Self::deny((chat.into(), user.into()))
    }

    /// Whether rule accepts an update
    pub fn accepts(&self, update: &Update) -> bool {
        self.principal.accepts(update)
    }
}

/// Principal helps to decide should rule accept an update or not
#[derive(Debug)]
pub enum Principal {
    /// Accepts all updates
    All,
    /// Accepts updates only from a specified user
    User(PrincipalUser),
    /// Accepts updates only from a specified chat
    Chat(PrincipalChat),
    /// Accepts updates only from a user in chat
    ChatUser(PrincipalChat, PrincipalUser),
}

impl From<PrincipalUser> for Principal {
    fn from(principal: PrincipalUser) -> Principal {
        Principal::User(principal)
    }
}

impl From<PrincipalChat> for Principal {
    fn from(principal: PrincipalChat) -> Principal {
        Principal::Chat(principal)
    }
}

impl From<(PrincipalChat, PrincipalUser)> for Principal {
    fn from(principal: (PrincipalChat, PrincipalUser)) -> Principal {
        Principal::ChatUser(principal.0, principal.1)
    }
}

impl Principal {
    /// Creates a principal for user
    pub fn user<P: Into<PrincipalUser>>(principal: P) -> Self {
        Principal::User(principal.into())
    }

    /// Creates a principal for chat
    pub fn chat<P: Into<PrincipalChat>>(principal: P) -> Self {
        Principal::Chat(principal.into())
    }
}

/// Represents a user
#[derive(Debug)]
pub enum PrincipalUser {
    /// Accepts updates only from a user with specified ID
    Id(Integer),
    /// Accepts updates only from a user with specified @username
    Username(String),
}

impl From<Integer> for PrincipalUser {
    fn from(user_id: Integer) -> PrincipalUser {
        PrincipalUser::Id(user_id)
    }
}

impl From<String> for PrincipalUser {
    fn from(username: String) -> PrincipalUser {
        PrincipalUser::Username(username)
    }
}

impl<'a> From<&'a str> for PrincipalUser {
    fn from(username: &'a str) -> PrincipalUser {
        PrincipalUser::Username(String::from(username))
    }
}

impl PrincipalUser {
    fn accepts(&self, update: &Update) -> bool {
        match self {
            PrincipalUser::Id(user_id) => update.get_user().map(|u| u.id == *user_id),
            PrincipalUser::Username(ref username) => update.get_user().and_then(|u| {
                if let Some(ref x) = u.username {
                    Some(x == username)
                } else {
                    None
                }
            }),
        }
        .unwrap_or(false)
    }
}

/// Represents a chat
#[derive(Debug)]
pub enum PrincipalChat {
    /// Accepts updates only from a chat with specified ID
    Id(Integer),
    /// Accepts updates only from a chat with specified @username
    Username(String),
}

impl From<Integer> for PrincipalChat {
    fn from(chat_id: Integer) -> PrincipalChat {
        PrincipalChat::Id(chat_id)
    }
}

impl From<String> for PrincipalChat {
    fn from(username: String) -> PrincipalChat {
        PrincipalChat::Username(username)
    }
}

impl<'a> From<&'a str> for PrincipalChat {
    fn from(username: &'a str) -> PrincipalChat {
        PrincipalChat::Username(String::from(username))
    }
}

impl PrincipalChat {
    fn accepts(&self, update: &Update) -> bool {
        match self {
            PrincipalChat::Id(chat_id) => update.get_chat_id().map(|x| x == *chat_id),
            PrincipalChat::Username(ref chat_username) => update.get_chat_username().map(|x| x == chat_username),
        }
        .unwrap_or(false)
    }
}

impl Principal {
    fn accepts(&self, update: &Update) -> bool {
        match self {
            Principal::User(principal) => principal.accepts(&update),
            Principal::Chat(principal) => principal.accepts(&update),
            Principal::ChatUser(chat_principal, user_principal) =>
                chat_principal.accepts(&update) && user_principal.accepts(&update),
            Principal::All => true,
        }
    }
}

/// In-memory access policy
///
/// Stores all rules in a Vec
///
/// If there are no rules found for update, is_granted() will return false
/// You can use `Rule::allow(Principal::All)` as last rule in order to change this behaviour
#[derive(Default)]
pub struct InMemoryAccessPolicy {
    rules: Vec<AccessRule>,
}

impl InMemoryAccessPolicy {
    /// Creates a new policy
    pub fn new(rules: Vec<AccessRule>) -> Self {
        InMemoryAccessPolicy { rules }
    }

    /// Adds a rule to the end of the list
    pub fn push_rule(mut self, rule: AccessRule) -> Self {
        self.rules.push(rule);
        self
    }
}

impl<C> AccessPolicy<C> for InMemoryAccessPolicy {
    fn is_granted(&mut self, _context: &mut C, update: &Update) -> AccessPolicyFuture {
        let mut result = false;
        for rule in &self.rules {
            if rule.accepts(&update) {
                result = rule.is_granted;
                log::info!("Found rule: {:?}", rule);
                break;
            }
        }
        result.into()
    }
}

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

    struct MockPolicy {
        result: bool,
    }

    impl AccessPolicy<()> for MockPolicy {
        fn is_granted(&mut self, _: &mut (), _update: &Update) -> AccessPolicyFuture {
            self.result.into()
        }
    }

    #[test]
    fn test_middleware() {
        let update: Update = from_str(
            r#"{
            "update_id": 1,
            "message": {
                "message_id": 1,
                "date": 0,
                "from": {"id": 1, "is_bot": false, "first_name": "test", "username": "username1"},
                "chat": {"id": 1, "type": "private", "first_name": "test", "username": "username1"},
                "text": "test middleware"
            }
        }"#,
        )
        .unwrap();
        for &result in &[true, false] {
            let policy = MockPolicy { result };
            let mut middleware = AccessMiddleware::new(policy);
            let middleware_result = middleware.before(&mut (), &update).wait().unwrap();
            if result {
                assert_eq!(middleware_result, MiddlewareResult::Continue);
            } else {
                assert_eq!(middleware_result, MiddlewareResult::Stop);
            }
        }
    }

    #[test]
    fn test_in_memory_policy() {
        macro_rules! check_access {
            ($rules:expr, $updates:expr) => {{
                for rules in $rules {
                    let mut policy = InMemoryAccessPolicy::new(rules);
                    for (flag, update) in $updates {
                        let update: Update = from_str(update).unwrap();
                        let is_granted = policy.is_granted(&mut (), &update).wait().unwrap();
                        assert_eq!(is_granted, *flag);
                    }
                }
            }};
        }

        check_access!(
            vec![
                vec![AccessRule::allow_user(1)],
                vec![AccessRule::allow_user("username1")],
                vec![AccessRule::deny_user(2), AccessRule::allow_all()],
                vec![AccessRule::deny_user("username2"), AccessRule::allow_all()],
            ],
            &[
                (
                    true,
                    r#"{
                        "update_id": 1,
                        "message": {
                            "message_id": 1,
                            "date": 0,
                            "from": {"id": 1, "is_bot": false, "first_name": "test", "username": "username1"},
                            "chat": {"id": 1, "type": "private", "first_name": "test", "username": "username1"},
                            "text": "test allowed for user #1"
                        }
                    }"#
                ),
                (
                    false,
                    r#"{
                        "update_id": 1,
                        "message": {
                            "message_id": 2,
                            "date": 1,
                            "from": {"id": 2, "is_bot": false, "first_name": "test", "username": "username2"},
                            "chat": {"id": 2, "type": "private", "first_name": "test", "username": "username2"},
                            "text": "test denied for user #2"
                        }
                    }"#
                )
            ]
        );

        check_access!(
            vec![
                vec![AccessRule::allow_chat(1)],
                vec![AccessRule::allow_chat("username1")],
                vec![AccessRule::deny_chat(2), AccessRule::allow_all()],
                vec![AccessRule::deny_chat("username2"), AccessRule::allow_all()],
            ],
            &[
                (
                    true,
                    r#"{
                        "update_id": 1,
                        "message": {
                            "message_id": 1,
                            "date": 0,
                            "from": {"id": 111, "is_bot": false, "first_name": "test"},
                            "chat": {"id": 1, "type": "supergroup", "title": "test", "username": "username1"},
                            "text": "test allowed for chat #1"
                        }
                    }"#
                ),
                (
                    false,
                    r#"{
                        "update_id": 1,
                        "message": {
                            "message_id": 2,
                            "date": 1,
                            "from": {"id": 111, "is_bot": false, "first_name": "test"},
                            "chat": {"id": 2, "type": "supergroup", "title": "test", "username": "username2"},
                            "text": "test denied for chat #2"
                        }
                    }"#
                )
            ]
        );

        check_access!(
            vec![
                vec![AccessRule::allow_chat_user(1, 2)],
                vec![AccessRule::allow_chat_user("username1", 2)],
                vec![AccessRule::allow_chat_user(1, "username2")],
                vec![AccessRule::allow_chat_user("username1", "username2")],
                vec![
                    AccessRule::deny_chat_user(1, 3),
                    AccessRule::deny_chat_user(4, 3),
                    AccessRule::allow_all()
                ],
                vec![
                    AccessRule::deny_chat_user("username1", "username3"),
                    AccessRule::deny_chat_user(4, 3),
                    AccessRule::allow_all()
                ],
            ],
            &[
                (
                    true,
                    r#"{
                        "update_id": 1,
                        "message": {
                            "message_id": 1,
                            "date": 0,
                            "from": {"id": 2, "is_bot": false, "first_name": "test", "username": "username2"},
                            "chat": {"id": 1, "type": "supergroup", "title": "test", "username": "username1"},
                            "text": "test allowed for user in chat"
                        }
                    }"#
                ),
                (
                    false,
                    r#"{
                        "update_id": 1,
                        "message": {
                            "message_id": 2,
                            "date": 1,
                            "from": {"id": 3, "is_bot": false, "first_name": "test", "username": "username3"},
                            "chat": {"id": 1, "type": "supergroup", "title": "test", "username": "username1"},
                            "text": "test denied for user in chat"
                        }
                    }"#
                ),
                (
                    false,
                    r#"{
                        "update_id": 1,
                        "message": {
                            "message_id": 2,
                            "date": 1,
                            "from": {"id": 3, "is_bot": false, "first_name": "test", "username": "username3"},
                            "chat": {"id": 4, "type": "supergroup", "title": "test", "username": "username4"},
                            "text": "test denied for chat and user"
                        }
                    }"#
                )
            ]
        );
    }
}