laye 0.1.0

A framework-agnostic role and permission based access control 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
//! The [`AccessPolicy`] builder and [`AccessRule`] enum.

use std::sync::Arc;

use crate::{principal::Principal, result::LayeCheckResult};

/// A boxed closure used with [`AccessRule::Custom`].
///
/// The function receives `Option<&dyn Principal>` (the current principal, if any) and returns
/// `true` to allow access or `false` to deny it.
pub type CustomFn = Arc<dyn Fn(Option<&dyn Principal>) -> bool + Send + Sync>;

/// A single access condition tested against a [`Principal`].
///
/// Rules are the atomic building blocks of an [`AccessPolicy`]. Combine them with
/// [`AccessPolicy::require_all`] (AND) or [`AccessPolicy::require_any`] (OR).
#[derive(Clone)]
pub enum AccessRule {
    /// Passes when the principal has the named role.
    ///
    /// Returns [`Unauthorized`](LayeCheckResult::Unauthorized) when no principal is present,
    /// [`Forbidden`](LayeCheckResult::Forbidden) when the principal lacks the role.
    Role(String),

    /// Passes when the principal does *not* have the named role.
    ///
    /// Returns [`Unauthorized`](LayeCheckResult::Unauthorized) when no principal is present,
    /// [`Forbidden`](LayeCheckResult::Forbidden) when the principal has the role.
    NotRole(String),

    /// Passes when the principal has the named permission.
    ///
    /// Returns [`Unauthorized`](LayeCheckResult::Unauthorized) when no principal is present,
    /// [`Forbidden`](LayeCheckResult::Forbidden) when the principal lacks the permission.
    Permission(String),

    /// Passes when the principal does *not* have the named permission.
    ///
    /// Returns [`Unauthorized`](LayeCheckResult::Unauthorized) when no principal is present,
    /// [`Forbidden`](LayeCheckResult::Forbidden) when the principal has the permission.
    NotPermission(String),

    /// Passes when a principal is present and [`Principal::is_authenticated`] returns `true`.
    ///
    /// Returns [`Unauthorized`](LayeCheckResult::Unauthorized) when no principal is present
    /// or when `is_authenticated()` returns `false`.
    Authenticated,

    /// Passes when no principal is present, or when [`Principal::is_authenticated`] returns `false`.
    ///
    /// Returns [`Forbidden`](LayeCheckResult::Forbidden) for authenticated principals.
    /// Use this on login / register routes to redirect users who are already signed in.
    Guest,

    /// Delegates the check to an arbitrary closure.
    ///
    /// The closure receives `Option<&dyn Principal>` and returns `true` to allow, `false` to
    /// deny. When it returns `false`, the result is [`Unauthorized`](LayeCheckResult::Unauthorized)
    /// if no principal was present, or [`Forbidden`](LayeCheckResult::Forbidden) if one was.
    Custom(CustomFn),
}

#[derive(Clone)]
enum PolicyMode {
    All,
    Any,
}

#[derive(Clone)]
enum PolicyCheck {
    Rule(AccessRule),
    Nested(AccessPolicy),
}

/// A composable access policy built from [`AccessRule`]s and nested sub-policies.
///
/// Create a policy with [`require_all`](Self::require_all) (AND semantics) or
/// [`require_any`](Self::require_any) (OR semantics), then chain conditions with
/// [`add_rule`](Self::add_rule) and [`add_policy`](Self::add_policy).
///
/// # Examples
///
/// ```
/// # use laye::{AccessPolicy, AccessRule, LayeCheckResult, Principal};
/// # #[derive(Clone)]
/// # struct MyUser { roles: Vec<String>, permissions: Vec<String>, authenticated: bool }
/// # impl Principal for MyUser {
/// #     fn roles(&self) -> &[String] { &self.roles }
/// #     fn permissions(&self) -> &[String] { &self.permissions }
/// #     fn is_authenticated(&self) -> bool { self.authenticated }
/// # }
/// let user = MyUser { roles: vec!["editor".into()], permissions: vec![], authenticated: true };
///
/// let policy = AccessPolicy::require_all()
///     .add_rule(AccessRule::Authenticated)
///     .add_rule(AccessRule::Role("editor".into()));
///
/// assert_eq!(policy.check(Some(&user)), LayeCheckResult::Authorized);
/// assert_eq!(policy.check(None), LayeCheckResult::Unauthorized);
/// ```
#[derive(Clone)]
pub struct AccessPolicy {
    checks: Vec<PolicyCheck>,
    mode: PolicyMode,
}

impl AccessPolicy {
    /// Creates an AND policy: every rule and sub-policy must pass.
    ///
    /// An empty `require_all` policy always returns [`Authorized`](LayeCheckResult::Authorized).
    ///
    /// # Examples
    ///
    /// ```
    /// # use laye::{AccessPolicy, AccessRule, LayeCheckResult, Principal};
    /// # #[derive(Clone)]
    /// # struct MyUser { roles: Vec<String>, permissions: Vec<String>, authenticated: bool }
    /// # impl Principal for MyUser {
    /// #     fn roles(&self) -> &[String] { &self.roles }
    /// #     fn permissions(&self) -> &[String] { &self.permissions }
    /// #     fn is_authenticated(&self) -> bool { self.authenticated }
    /// # }
    /// let admin = MyUser { roles: vec!["admin".into()], permissions: vec![], authenticated: true };
    ///
    /// let policy = AccessPolicy::require_all()
    ///     .add_rule(AccessRule::Authenticated)
    ///     .add_rule(AccessRule::Role("admin".into()));
    ///
    /// assert_eq!(policy.check(Some(&admin)), LayeCheckResult::Authorized);
    /// ```
    pub fn require_all() -> Self {
        Self {
            checks: vec![],
            mode: PolicyMode::All,
        }
    }

    /// Creates an OR policy: at least one rule or sub-policy must pass.
    ///
    /// An empty `require_any` policy returns [`Unauthorized`](LayeCheckResult::Unauthorized)
    /// for anonymous requests and [`Forbidden`](LayeCheckResult::Forbidden) for authenticated
    /// ones (no condition can ever pass).
    ///
    /// # Examples
    ///
    /// ```
    /// # use laye::{AccessPolicy, AccessRule, LayeCheckResult, Principal};
    /// # #[derive(Clone)]
    /// # struct MyUser { roles: Vec<String>, permissions: Vec<String>, authenticated: bool }
    /// # impl Principal for MyUser {
    /// #     fn roles(&self) -> &[String] { &self.roles }
    /// #     fn permissions(&self) -> &[String] { &self.permissions }
    /// #     fn is_authenticated(&self) -> bool { self.authenticated }
    /// # }
    /// let editor = MyUser { roles: vec!["editor".into()], permissions: vec![], authenticated: true };
    ///
    /// let policy = AccessPolicy::require_any()
    ///     .add_rule(AccessRule::Role("admin".into()))
    ///     .add_rule(AccessRule::Role("editor".into()));
    ///
    /// assert_eq!(policy.check(Some(&editor)), LayeCheckResult::Authorized);
    /// assert_eq!(policy.check(None),          LayeCheckResult::Unauthorized);
    /// ```
    pub fn require_any() -> Self {
        Self {
            checks: vec![],
            mode: PolicyMode::Any,
        }
    }

    /// Appends an [`AccessRule`] to this policy.
    pub fn add_rule(mut self, rule: AccessRule) -> Self {
        self.checks.push(PolicyCheck::Rule(rule));
        self
    }

    /// Appends a nested [`AccessPolicy`] as a single condition in this policy.
    ///
    /// Use this to compose AND-of-OR or OR-of-AND logic:
    ///
    /// ```
    /// # use laye::{AccessPolicy, AccessRule, LayeCheckResult, Principal};
    /// # #[derive(Clone)]
    /// # struct MyUser { roles: Vec<String>, permissions: Vec<String>, authenticated: bool }
    /// # impl Principal for MyUser {
    /// #     fn roles(&self) -> &[String] { &self.roles }
    /// #     fn permissions(&self) -> &[String] { &self.permissions }
    /// #     fn is_authenticated(&self) -> bool { self.authenticated }
    /// # }
    /// let editor = MyUser { roles: vec!["editor".into()], permissions: vec![], authenticated: true };
    ///
    /// // Authenticated AND (admin OR editor)
    /// let policy = AccessPolicy::require_all()
    ///     .add_rule(AccessRule::Authenticated)
    ///     .add_policy(
    ///         AccessPolicy::require_any()
    ///             .add_rule(AccessRule::Role("admin".into()))
    ///             .add_rule(AccessRule::Role("editor".into())),
    ///     );
    ///
    /// assert_eq!(policy.check(Some(&editor)), LayeCheckResult::Authorized);
    /// ```
    pub fn add_policy(mut self, policy: AccessPolicy) -> Self {
        self.checks.push(PolicyCheck::Nested(policy));
        self
    }

    /// Evaluates this policy against an optional principal.
    ///
    /// Pass `Some(&principal)` for authenticated requests and `None` for anonymous requests.
    ///
    /// | Outcome | Returned value |
    /// |---------|---------------|
    /// | All conditions pass | [`Authorized`](LayeCheckResult::Authorized) |
    /// | No principal, auth required | [`Unauthorized`](LayeCheckResult::Unauthorized) |
    /// | Principal present, condition fails | [`Forbidden`](LayeCheckResult::Forbidden) |
    ///
    /// # Examples
    ///
    /// ```
    /// # use laye::{AccessPolicy, AccessRule, LayeCheckResult, Principal};
    /// # #[derive(Clone)]
    /// # struct MyUser { roles: Vec<String>, permissions: Vec<String>, authenticated: bool }
    /// # impl Principal for MyUser {
    /// #     fn roles(&self) -> &[String] { &self.roles }
    /// #     fn permissions(&self) -> &[String] { &self.permissions }
    /// #     fn is_authenticated(&self) -> bool { self.authenticated }
    /// # }
    /// let policy = AccessPolicy::require_all()
    ///     .add_rule(AccessRule::Role("admin".into()));
    ///
    /// let admin  = MyUser { roles: vec!["admin".into()], permissions: vec![], authenticated: true };
    /// let viewer = MyUser { roles: vec![],               permissions: vec![], authenticated: true };
    ///
    /// assert_eq!(policy.check(Some(&admin)),  LayeCheckResult::Authorized);
    /// assert_eq!(policy.check(Some(&viewer)), LayeCheckResult::Forbidden);
    /// assert_eq!(policy.check(None),          LayeCheckResult::Unauthorized);
    /// ```
    pub fn check(&self, principal: Option<&dyn Principal>) -> LayeCheckResult {
        match self.mode {
            PolicyMode::All => {
                for check in &self.checks {
                    let result = eval_check(check, principal);

                    if result != LayeCheckResult::Authorized {
                        return result;
                    }
                }

                LayeCheckResult::Authorized
            }
            PolicyMode::Any => {
                for check in &self.checks {
                    if eval_check(check, principal) == LayeCheckResult::Authorized {
                        return LayeCheckResult::Authorized;
                    }
                }

                if principal.is_none() {
                    LayeCheckResult::Unauthorized
                } else {
                    LayeCheckResult::Forbidden
                }
            }
        }
    }

    /// Converts this policy into a tower [`AccessControlLayer`](crate::tower_middleware::AccessControlLayer).
    ///
    /// Apply the returned layer to an axum `Router` or any tower service. Requests are rejected
    /// with **401** or **403** when the policy fails.
    ///
    /// **Prerequisite:** insert `P` into request extensions from an upstream middleware before
    /// this layer runs. See [`crate::tower_middleware`] for a complete integration guide.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use laye::{AccessPolicy, AccessRule, Principal};
    ///
    /// #[derive(Clone)]
    /// struct MyUser { roles: Vec<String>, permissions: Vec<String>, authenticated: bool }
    /// impl Principal for MyUser {
    ///     fn roles(&self) -> &[String] { &self.roles }
    ///     fn permissions(&self) -> &[String] { &self.permissions }
    ///     fn is_authenticated(&self) -> bool { self.authenticated }
    /// }
    ///
    /// let layer = AccessPolicy::require_all()
    ///     .add_rule(AccessRule::Role("admin".into()))
    ///     .into_tower_layer::<MyUser>();
    /// // router.route("/admin", get(handler).layer(layer))
    /// ```
    #[cfg(feature = "tower")]
    #[cfg_attr(docsrs, doc(cfg(feature = "tower")))]
    pub fn into_tower_layer<P>(self) -> crate::tower_middleware::AccessControlLayer<P>
    where
        P: crate::principal::Principal + Clone + 'static,
    {
        crate::tower_middleware::AccessControlLayer::new(self)
    }

    /// Converts this policy into an actix-web [`PolicyMiddlewareFactory`](crate::actix::PolicyMiddlewareFactory).
    ///
    /// Apply the returned factory via `.wrap()` on an `App` or `Scope`. Requests are rejected
    /// with **401** or **403** when the policy fails.
    ///
    /// **Prerequisite:** insert `P` into request extensions from an upstream middleware before
    /// this middleware runs. See [`crate::actix`] for a complete integration guide.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use laye::{AccessPolicy, AccessRule, Principal};
    ///
    /// #[derive(Clone)]
    /// struct MyUser { roles: Vec<String>, permissions: Vec<String>, authenticated: bool }
    /// impl Principal for MyUser {
    ///     fn roles(&self) -> &[String] { &self.roles }
    ///     fn permissions(&self) -> &[String] { &self.permissions }
    ///     fn is_authenticated(&self) -> bool { self.authenticated }
    /// }
    ///
    /// let factory = AccessPolicy::require_all()
    ///     .add_rule(AccessRule::Role("admin".into()))
    ///     .into_actix_middleware::<MyUser>();
    /// // scope.wrap(factory)
    /// ```
    #[cfg(feature = "actix-web")]
    #[cfg_attr(docsrs, doc(cfg(feature = "actix-web")))]
    pub fn into_actix_middleware<P>(self) -> crate::actix::PolicyMiddlewareFactory<P>
    where
        P: crate::principal::Principal + Clone + 'static,
    {
        crate::actix::PolicyMiddlewareFactory::new(self)
    }
}

fn eval_check(check: &PolicyCheck, principal: Option<&dyn Principal>) -> LayeCheckResult {
    match check {
        PolicyCheck::Rule(rule) => eval_rule(rule, principal),
        PolicyCheck::Nested(policy) => policy.check(principal),
    }
}

fn eval_rule(rule: &AccessRule, principal: Option<&dyn Principal>) -> LayeCheckResult {
    match rule {
        AccessRule::Role(r) => match principal {
            None => LayeCheckResult::Unauthorized,
            Some(p) => {
                if p.has_role(r) {
                    LayeCheckResult::Authorized
                } else {
                    LayeCheckResult::Forbidden
                }
            }
        },
        AccessRule::NotRole(r) => match principal {
            None => LayeCheckResult::Unauthorized,
            Some(p) => {
                if !p.has_role(r) {
                    LayeCheckResult::Authorized
                } else {
                    LayeCheckResult::Forbidden
                }
            }
        },
        AccessRule::Permission(p_str) => match principal {
            None => LayeCheckResult::Unauthorized,
            Some(p) => {
                if p.has_permission(p_str) {
                    LayeCheckResult::Authorized
                } else {
                    LayeCheckResult::Forbidden
                }
            }
        },
        AccessRule::NotPermission(p_str) => match principal {
            None => LayeCheckResult::Unauthorized,
            Some(p) => {
                if !p.has_permission(p_str) {
                    LayeCheckResult::Authorized
                } else {
                    LayeCheckResult::Forbidden
                }
            }
        },
        AccessRule::Authenticated => match principal {
            None => LayeCheckResult::Unauthorized,
            Some(p) => {
                if p.is_authenticated() {
                    LayeCheckResult::Authorized
                } else {
                    LayeCheckResult::Unauthorized
                }
            }
        },
        AccessRule::Guest => match principal {
            None => LayeCheckResult::Authorized,
            Some(p) => {
                if !p.is_authenticated() {
                    LayeCheckResult::Authorized
                } else {
                    LayeCheckResult::Forbidden
                }
            }
        },
        AccessRule::Custom(f) => {
            if f(principal) {
                LayeCheckResult::Authorized
            } else if principal.is_none() {
                LayeCheckResult::Unauthorized
            } else {
                LayeCheckResult::Forbidden
            }
        }
    }
}