capy-auth 0.10.0

Optional database-backed browser sessions and explicit authorization for Capy.
Documentation
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.
///
/// ```
/// use capy_auth::Policy;
/// struct DeleteComment;
/// impl Policy<i64, i64> for DeleteComment {
///     type Action = ();
///     fn allows(user_id: &i64, _: (), author_id: &i64) -> bool { user_id == author_id }
/// }
/// assert!(DeleteComment::authorize(&7, (), &7).is_ok());
/// assert!(DeleteComment::authorize(&8, (), &7).is_err());
/// ```
/// Load application roles/ownership from trusted database state before evaluating.
/// Policies are explicit synchronous decisions, not automatic database filters.
/// Always enforce them on the server; separate public visibility queries can return
/// 404 for drafts without leaking their existence.
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)
        }
    }
}