axum-security 0.0.2

A security toolbox for the Axum library
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
//! Policy-based access control (PBAC).
//!
//! Define access policies as functions, closures, or custom types that implement
//! [`Policy<U>`]. Combine them with [`and`](PolicyExt::and), [`or`](PolicyExt::or),
//! and [`not`](PolicyExt::not). Apply them to routes with [`PolicyRouterExt::with_policy`].
//!
//! Requires an active session (from the `jwt`, `cookie`, or `basic-auth` feature).
//! Returns `401` if no session is present, `403` if the policy denies access.
//!
//! # Example
//!
//! ```rust,ignore
//! use axum::{Router, routing::get};
//! use axum_security::pbac::{PolicyExt, PolicyRouterExt};
//!
//! fn is_admin(user: &MyUser) -> bool { user.admin }
//! fn is_active(user: &MyUser) -> bool { !user.banned }
//!
//! let app = Router::new()
//!     .route("/admin", get(handler).with_policy(is_admin.and(is_active)));
//! ```

use std::{convert::Infallible, future::Future, marker::PhantomData, pin::Pin};

use axum::{
    Router,
    extract::Request,
    http::StatusCode,
    response::{IntoResponse, Response},
    routing::MethodRouter,
};
use tower::{Layer, Service};

use crate::session::Session;

/// A policy that decides whether a user is allowed access.
///
/// Return `Ok(true)` to allow, `Ok(false)` to deny (→ 403), or `Err(e)` for a custom
/// error response. Closures `Fn(&U) -> bool` implement this trait automatically.
pub trait Policy<U>: Send + Sync + 'static + Clone {
    /// The error type returned when the policy evaluation itself fails.
    type Error: IntoResponse + Send;

    /// Evaluate the policy for the given user.
    fn evaluate(&self, user: &U) -> impl Future<Output = Result<bool, Self::Error>> + Send;
}

impl<U, F> Policy<U> for F
where
    F: Fn(&U) -> bool + Send + Sync + 'static + Clone,
{
    type Error = Infallible;

    fn evaluate(&self, user: &U) -> impl Future<Output = Result<bool, Self::Error>> + Send {
        let result = (self)(user);
        async move { Ok(result) }
    }
}

/// Combinator: both policies must allow. Created by [`PolicyExt::and`].
#[derive(Clone)]
pub struct AllOf<P1, P2>(pub P1, pub P2);

impl<U, P1, P2> Policy<U> for AllOf<P1, P2>
where
    U: Send + Sync + 'static,
    P1: Policy<U>,
    P2: Policy<U>,
{
    type Error = Response;

    async fn evaluate(&self, user: &U) -> Result<bool, Self::Error> {
        let a = self
            .0
            .evaluate(user)
            .await
            .map_err(IntoResponse::into_response)?;
        if !a {
            return Ok(false);
        }
        self.1
            .evaluate(user)
            .await
            .map_err(IntoResponse::into_response)
    }
}

/// Combinator: at least one policy must allow. Created by [`PolicyExt::or`].
#[derive(Clone)]
pub struct AnyOf<P1, P2>(pub P1, pub P2);

impl<U, P1, P2> Policy<U> for AnyOf<P1, P2>
where
    U: Send + Sync + 'static,
    P1: Policy<U>,
    P2: Policy<U>,
{
    type Error = Response;

    async fn evaluate(&self, user: &U) -> Result<bool, Self::Error> {
        let a = self
            .0
            .evaluate(user)
            .await
            .map_err(IntoResponse::into_response)?;
        if a {
            return Ok(true);
        }
        self.1
            .evaluate(user)
            .await
            .map_err(IntoResponse::into_response)
    }
}

/// Combinator: inverts the policy. Created by [`PolicyExt::not`].
#[derive(Clone)]
pub struct Not<P>(pub P);

impl<U, P> Policy<U> for Not<P>
where
    U: Send + Sync + 'static,
    P: Policy<U>,
{
    type Error = P::Error;

    async fn evaluate(&self, user: &U) -> Result<bool, Self::Error> {
        Ok(!self.0.evaluate(user).await?)
    }
}

/// Provides [`and`](PolicyExt::and), [`or`](PolicyExt::or), and [`not`](PolicyExt::not) combinators.
///
/// Automatically implemented for all [`Policy`] types.
pub trait PolicyExt<U>: Policy<U> + Sized {
    /// Require both `self` and `other` to allow access.
    fn and<P: Policy<U>>(self, other: P) -> AllOf<Self, P> {
        AllOf(self, other)
    }

    /// Allow access if either `self` or `other` allows.
    fn or<P: Policy<U>>(self, other: P) -> AnyOf<Self, P> {
        AnyOf(self, other)
    }

    /// Invert this policy (allow becomes deny and vice versa).
    fn not(self) -> Not<Self> {
        Not(self)
    }
}

impl<U, P: Policy<U>> PolicyExt<U> for P {}

/// A policy that always allows access.
#[derive(Clone, Copy)]
pub struct Allow;

impl<U: Send + Sync + 'static> Policy<U> for Allow {
    type Error = Infallible;

    async fn evaluate(&self, _user: &U) -> Result<bool, Self::Error> {
        Ok(true)
    }
}

/// A policy that always denies access.
#[derive(Clone, Copy)]
pub struct Deny;

impl<U: Send + Sync + 'static> Policy<U> for Deny {
    type Error = Infallible;

    async fn evaluate(&self, _user: &U) -> Result<bool, Self::Error> {
        Ok(false)
    }
}

/// Bridge from RBAC: a policy that checks if the user has a specific role.
///
/// Requires the `rbac` feature.
#[cfg(feature = "rbac")]
pub struct HasRole<R: crate::rbac::RBAC> {
    role: R,
}

#[cfg(feature = "rbac")]
impl<R: crate::rbac::RBAC> Clone for HasRole<R> {
    fn clone(&self) -> Self {
        HasRole { role: self.role }
    }
}

#[cfg(feature = "rbac")]
impl<R: crate::rbac::RBAC> HasRole<R> {
    pub fn new(role: R) -> Self {
        HasRole { role }
    }
}

#[cfg(feature = "rbac")]
impl<R: crate::rbac::RBAC> Policy<R::Resource> for HasRole<R> {
    type Error = Infallible;

    async fn evaluate(&self, user: &R::Resource) -> Result<bool, Self::Error> {
        Ok(R::extract_roles(user).into_iter().any(|r| *r == self.role))
    }
}

struct PolicyLayer<P, U> {
    policy: P,
    _marker: PhantomData<fn() -> U>,
}

impl<P: Clone, U> Clone for PolicyLayer<P, U> {
    fn clone(&self) -> Self {
        PolicyLayer {
            policy: self.policy.clone(),
            _marker: PhantomData,
        }
    }
}

impl<P, S, U> Layer<S> for PolicyLayer<P, U>
where
    P: Clone,
{
    type Service = PolicyService<P, S, U>;

    fn layer(&self, inner: S) -> PolicyService<P, S, U> {
        PolicyService {
            policy: self.policy.clone(),
            inner,
            _marker: PhantomData,
        }
    }
}

/// The [`Service`] created by `PolicyLayer`. You don't need to construct this directly.
pub struct PolicyService<P, S, U> {
    policy: P,
    inner: S,
    _marker: PhantomData<fn() -> U>,
}

impl<P: Clone, S: Clone, U> Clone for PolicyService<P, S, U> {
    fn clone(&self) -> Self {
        PolicyService {
            policy: self.policy.clone(),
            inner: self.inner.clone(),
            _marker: PhantomData,
        }
    }
}

type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;

impl<P, S, U> Service<Request> for PolicyService<P, S, U>
where
    P: Policy<U> + 'static,
    U: Clone + Send + Sync + 'static,
    S: Service<Request, Response = Response> + Clone + Send + 'static,
    S::Error: Send,
    S::Future: Send,
{
    type Response = Response;
    type Error = S::Error;
    type Future = BoxFuture<Result<Response, S::Error>>;

    fn poll_ready(
        &mut self,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Result<(), Self::Error>> {
        self.inner.poll_ready(cx)
    }

    fn call(&mut self, mut req: Request) -> Self::Future {
        let policy = self.policy.clone();
        let mut inner = self.inner.clone();
        Box::pin(async move {
            crate::debug!("evaluating policy");

            let Some(session) = Session::<U>::from_extensions(req.extensions_mut()) else {
                crate::debug!("no session found, returning 401");
                return Ok(StatusCode::UNAUTHORIZED.into_response());
            };

            match policy.evaluate(&session).await {
                Ok(true) => {
                    session.insert_into(req.extensions_mut());
                    inner.call(req).await
                }
                Ok(false) => {
                    crate::debug!("policy denied access, returning 403");
                    session.insert_into(req.extensions_mut());
                    Ok(StatusCode::FORBIDDEN.into_response())
                }
                Err(e) => {
                    crate::debug!("policy returned error");
                    session.insert_into(req.extensions_mut());
                    Ok(e.into_response())
                }
            }
        })
    }
}

/// Extension trait for applying a [`Policy`] to a [`MethodRouter`] or [`Router`].
pub trait PolicyRouterExt {
    /// Apply a policy to this route. Denies with `403` if the policy returns `false`.
    fn with_policy<P, U>(self, policy: P) -> Self
    where
        P: Policy<U> + 'static,
        U: Clone + Send + Sync + 'static;
}

impl<S: Clone + 'static> PolicyRouterExt for MethodRouter<S, Infallible> {
    fn with_policy<P, U>(self, policy: P) -> Self
    where
        P: Policy<U> + 'static,
        U: Clone + Send + Sync + 'static,
    {
        self.layer(PolicyLayer {
            policy,
            _marker: PhantomData,
        })
    }
}

impl<S: Clone + Send + Sync + 'static> PolicyRouterExt for Router<S> {
    fn with_policy<P, U>(self, policy: P) -> Self
    where
        P: Policy<U> + 'static,
        U: Clone + Send + Sync + 'static,
    {
        self.layer(PolicyLayer {
            policy,
            _marker: PhantomData,
        })
    }
}

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

    #[derive(Clone)]
    struct User {
        admin: bool,
        banned: bool,
    }

    fn is_admin(u: &User) -> bool {
        u.admin
    }

    fn is_not_banned(u: &User) -> bool {
        !u.banned
    }

    #[tokio::test]
    async fn closure_policy_passes() {
        let user = User {
            admin: true,
            banned: false,
        };
        assert!(is_admin.evaluate(&user).await.unwrap());
    }

    #[tokio::test]
    async fn closure_policy_fails() {
        let user = User {
            admin: false,
            banned: false,
        };
        assert!(!is_admin.evaluate(&user).await.unwrap());
    }

    #[tokio::test]
    async fn allow_always_passes() {
        let user = User {
            admin: false,
            banned: true,
        };
        assert!(Allow.evaluate(&user).await.unwrap());
    }

    #[tokio::test]
    async fn deny_always_fails() {
        let user = User {
            admin: true,
            banned: false,
        };
        assert!(!Deny.evaluate(&user).await.unwrap());
    }

    #[tokio::test]
    async fn not_combinator() {
        let user = User {
            admin: true,
            banned: false,
        };
        let policy = is_admin.not();
        assert!(!policy.evaluate(&user).await.unwrap());
    }

    #[tokio::test]
    async fn all_of_both_pass() {
        let user = User {
            admin: true,
            banned: false,
        };
        let policy = is_admin.and(is_not_banned);
        assert!(policy.evaluate(&user).await.unwrap());
    }

    #[tokio::test]
    async fn all_of_one_fails() {
        let user = User {
            admin: true,
            banned: true,
        };
        let policy = is_admin.and(is_not_banned);
        assert!(!policy.evaluate(&user).await.unwrap());
    }

    #[tokio::test]
    async fn any_of_one_passes() {
        let user = User {
            admin: true,
            banned: true,
        };
        let policy = is_admin.or(is_not_banned);
        assert!(policy.evaluate(&user).await.unwrap());
    }

    #[tokio::test]
    async fn any_of_none_pass() {
        let user = User {
            admin: false,
            banned: true,
        };
        let policy = is_admin.or(is_not_banned);
        assert!(!policy.evaluate(&user).await.unwrap());
    }

    #[tokio::test]
    async fn chained_combinators() {
        let user = User {
            admin: true,
            banned: false,
        };
        let policy = is_admin.and(is_not_banned);
        assert!(policy.evaluate(&user).await.unwrap());

        let policy2 = (is_admin as fn(&User) -> bool).not().or(is_not_banned);
        assert!(policy2.evaluate(&user).await.unwrap());
    }
}