capy-auth 0.7.0

Optional database-backed browser sessions and explicit authorization for Capy.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use capy_core::{AppError, Result};

/// A small explicit policy: applications choose their identity, action, and resource types.
/// Use an action enum or distinct action types; no runtime permission registry is required.
pub trait Policy<User, Resource> {
    /// Application-owned action type.
    type Action;
    /// Decide whether this authenticated user may perform the action.
    fn allows(user: &User, action: Self::Action, resource: &Resource) -> bool;
    /// Return Capy's 403 envelope when denied. To hide a resource, map denial to NotFound.
    fn authorize(user: &User, action: Self::Action, resource: &Resource) -> Result<()> {
        if Self::allows(user, action, resource) {
            Ok(())
        } else {
            Err(AppError::Forbidden)
        }
    }
}