use std::fmt;
use std::sync::Arc;
use id_effect::{Effect, HList, Selector, need, succeed};
use crate::principal::CurrentPrincipal;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Denial {
reason: String,
policy: Option<String>,
}
impl Denial {
pub fn new(reason: impl Into<String>) -> Self {
Self {
reason: reason.into(),
policy: None,
}
}
pub fn reason(&self) -> &str {
&self.reason
}
pub fn policy(&self) -> Option<&str> {
self.policy.as_deref()
}
#[must_use]
pub fn attributed_to(mut self, name: &str) -> Self {
if self.policy.is_none() {
self.policy = Some(name.to_owned());
}
self
}
}
impl fmt::Display for Denial {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.policy {
Some(name) => write!(f, "{name}: {}", self.reason),
None => f.write_str(&self.reason),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Decision {
Allow,
Deny(Denial),
}
impl Decision {
pub fn deny(reason: impl Into<String>) -> Self {
Self::Deny(Denial::new(reason))
}
pub fn allow_if(allowed: bool, reason: impl Into<String>) -> Self {
if allowed {
Self::Allow
} else {
Self::deny(reason)
}
}
pub fn is_allowed(&self) -> bool {
matches!(self, Self::Allow)
}
pub fn is_denied(&self) -> bool {
!self.is_allowed()
}
pub fn into_result(self, principal: &CurrentPrincipal) -> Result<(), AuthzError> {
match self {
Self::Allow => Ok(()),
Self::Deny(_) if !principal.is_authenticated() => Err(AuthzError::Unauthenticated),
Self::Deny(denial) => Err(AuthzError::Denied(denial)),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum AuthzError {
Unauthenticated,
Denied(Denial),
UnknownPolicy(String),
ResourceMismatch {
policy: String,
expected: &'static str,
found: &'static str,
},
Evaluation(String),
}
impl AuthzError {
pub fn http_status(&self) -> u16 {
match self {
Self::Unauthenticated => 401,
Self::Denied(_) => 403,
Self::UnknownPolicy(_) | Self::ResourceMismatch { .. } | Self::Evaluation(_) => 500,
}
}
}
impl fmt::Display for AuthzError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Unauthenticated => f.write_str("authentication required"),
Self::Denied(denial) => write!(f, "forbidden: {denial}"),
Self::UnknownPolicy(name) => write!(f, "unknown policy `{name}`"),
Self::ResourceMismatch {
policy,
expected,
found,
} => write!(
f,
"policy `{policy}` is defined for `{expected}`, evaluated with `{found}`"
),
Self::Evaluation(msg) => write!(f, "policy evaluation failed: {msg}"),
}
}
}
impl std::error::Error for AuthzError {}
pub type PolicyEffect = Effect<Decision, AuthzError>;
pub trait Policy<Res: ?Sized>: Send + Sync + 'static {
fn evaluate(&self, principal: &CurrentPrincipal, resource: &Res) -> PolicyEffect;
}
pub type BoxPolicy<Res> = Arc<dyn Policy<Res>>;
impl<Res: ?Sized, P: Policy<Res> + ?Sized> Policy<Res> for Arc<P> {
fn evaluate(&self, principal: &CurrentPrincipal, resource: &Res) -> PolicyEffect {
(**self).evaluate(principal, resource)
}
}
impl<Res: ?Sized, P: Policy<Res> + ?Sized> Policy<Res> for Box<P> {
fn evaluate(&self, principal: &CurrentPrincipal, resource: &Res) -> PolicyEffect {
(**self).evaluate(principal, resource)
}
}
pub trait PolicyExt: Sized + Send + Sync + 'static {
fn and<P>(self, other: P) -> And<Self, P> {
And(self, other)
}
fn or<P>(self, other: P) -> Or<Self, P> {
Or(self, other)
}
fn negate(self, reason: impl Into<String>) -> Not<Self> {
Not(self, reason.into())
}
fn boxed<Res: ?Sized>(self) -> BoxPolicy<Res>
where
Self: Policy<Res>,
{
Arc::new(self)
}
}
impl<P: Sized + Send + Sync + 'static> PolicyExt for P {}
#[derive(Clone, Debug)]
pub struct And<A, B>(A, B);
impl<Res: ?Sized, A: Policy<Res>, B: Policy<Res>> Policy<Res> for And<A, B> {
fn evaluate(&self, principal: &CurrentPrincipal, resource: &Res) -> PolicyEffect {
let second = self.1.evaluate(principal, resource);
self
.0
.evaluate(principal, resource)
.flat_map(move |first| match first {
Decision::Allow => second,
denied => succeed(denied),
})
}
}
#[derive(Clone, Debug)]
pub struct Or<A, B>(A, B);
impl<Res: ?Sized, A: Policy<Res>, B: Policy<Res>> Policy<Res> for Or<A, B> {
fn evaluate(&self, principal: &CurrentPrincipal, resource: &Res) -> PolicyEffect {
let second = self.1.evaluate(principal, resource);
self
.0
.evaluate(principal, resource)
.flat_map(move |first| match first {
Decision::Allow => succeed(Decision::Allow),
Decision::Deny(_) => second,
})
}
}
#[derive(Clone, Debug)]
pub struct Not<A>(A, String);
impl<Res: ?Sized, A: Policy<Res>> Policy<Res> for Not<A> {
fn evaluate(&self, principal: &CurrentPrincipal, resource: &Res) -> PolicyEffect {
let reason = self.1.clone();
self
.0
.evaluate(principal, resource)
.map(move |inner| match inner {
Decision::Allow => Decision::deny(reason),
Decision::Deny(_) => Decision::Allow,
})
}
}
pub fn all_of<Res: ?Sized + 'static>(policies: Vec<BoxPolicy<Res>>) -> AllOf<Res> {
AllOf(policies)
}
pub struct AllOf<Res: ?Sized>(Vec<BoxPolicy<Res>>);
impl<Res: ?Sized + 'static> Policy<Res> for AllOf<Res> {
fn evaluate(&self, principal: &CurrentPrincipal, resource: &Res) -> PolicyEffect {
let steps: Vec<PolicyEffect> = self
.0
.iter()
.map(|p| p.evaluate(principal, resource))
.collect();
steps
.into_iter()
.rev()
.fold(succeed(Decision::Allow), |rest, step| {
step.flat_map(move |d| match d {
Decision::Allow => rest,
denied => succeed(denied),
})
})
}
}
pub fn any_of<Res: ?Sized + 'static>(policies: Vec<BoxPolicy<Res>>) -> AnyOf<Res> {
AnyOf(policies)
}
pub struct AnyOf<Res: ?Sized>(Vec<BoxPolicy<Res>>);
impl<Res: ?Sized + 'static> Policy<Res> for AnyOf<Res> {
fn evaluate(&self, principal: &CurrentPrincipal, resource: &Res) -> PolicyEffect {
let mut steps: Vec<PolicyEffect> = self
.0
.iter()
.map(|p| p.evaluate(principal, resource))
.collect();
let Some(last) = steps.pop() else {
return succeed(Decision::deny("no policy allowed"));
};
steps.into_iter().rev().fold(last, |rest, step| {
step.flat_map(move |d| match d {
Decision::Allow => succeed(Decision::Allow),
Decision::Deny(_) => rest,
})
})
}
}
impl<Res: ?Sized> fmt::Debug for AllOf<Res> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("AllOf").field(&self.0.len()).finish()
}
}
impl<Res: ?Sized> fmt::Debug for AnyOf<Res> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("AnyOf").field(&self.0.len()).finish()
}
}
pub fn allow_all() -> Constant {
Constant(Decision::Allow)
}
pub fn deny_all(reason: impl Into<String>) -> Constant {
Constant(Decision::deny(reason))
}
#[derive(Clone, Debug)]
pub struct Constant(Decision);
impl<Res: ?Sized> Policy<Res> for Constant {
fn evaluate(&self, _: &CurrentPrincipal, _: &Res) -> PolicyEffect {
succeed(self.0.clone())
}
}
pub fn authenticated() -> Authenticated {
Authenticated
}
#[derive(Clone, Copy, Debug)]
pub struct Authenticated;
impl<Res: ?Sized> Policy<Res> for Authenticated {
fn evaluate(&self, principal: &CurrentPrincipal, _: &Res) -> PolicyEffect {
succeed(Decision::allow_if(
principal.is_authenticated(),
"authentication required",
))
}
}
pub fn require_role(role: impl Into<String>) -> RequireRole {
RequireRole(vec![role.into()])
}
pub fn require_any_role<S: Into<String>>(roles: impl IntoIterator<Item = S>) -> RequireRole {
RequireRole(roles.into_iter().map(Into::into).collect())
}
#[derive(Clone, Debug)]
pub struct RequireRole(Vec<String>);
impl<Res: ?Sized> Policy<Res> for RequireRole {
fn evaluate(&self, principal: &CurrentPrincipal, _: &Res) -> PolicyEffect {
if self.0.iter().any(|r| principal.has_role(r)) {
succeed(Decision::Allow)
} else {
succeed(Decision::deny(format!(
"requires role {}",
self.0.join(" or ")
)))
}
}
}
pub fn predicate<Res, F>(reason: impl Into<String>, check: F) -> PredicatePolicy<F>
where
Res: ?Sized,
F: Fn(&CurrentPrincipal, &Res) -> bool + Send + Sync + 'static,
{
PredicatePolicy {
reason: reason.into(),
check,
}
}
#[derive(Clone)]
pub struct PredicatePolicy<F> {
reason: String,
check: F,
}
impl<F> fmt::Debug for PredicatePolicy<F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PredicatePolicy")
.field("reason", &self.reason)
.finish()
}
}
impl<Res, F> Policy<Res> for PredicatePolicy<F>
where
Res: ?Sized,
F: Fn(&CurrentPrincipal, &Res) -> bool + Send + Sync + 'static,
{
fn evaluate(&self, principal: &CurrentPrincipal, resource: &Res) -> PolicyEffect {
succeed(Decision::allow_if(
(self.check)(principal, resource),
self.reason.as_str(),
))
}
}
pub fn from_effect<Res, F>(evaluate: F) -> EffectPolicy<F>
where
Res: ?Sized,
F: Fn(&CurrentPrincipal, &Res) -> PolicyEffect + Send + Sync + 'static,
{
EffectPolicy(evaluate)
}
#[derive(Clone)]
pub struct EffectPolicy<F>(F);
impl<F> fmt::Debug for EffectPolicy<F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("EffectPolicy")
}
}
impl<Res, F> Policy<Res> for EffectPolicy<F>
where
Res: ?Sized,
F: Fn(&CurrentPrincipal, &Res) -> PolicyEffect + Send + Sync + 'static,
{
fn evaluate(&self, principal: &CurrentPrincipal, resource: &Res) -> PolicyEffect {
(self.0)(principal, resource)
}
}
pub fn check_with<P, Res, R, I>(policy: P, resource: Res) -> Effect<Decision, AuthzError, R>
where
P: Policy<Res>,
Res: Send + Sync + 'static,
R: Selector<CurrentPrincipal, I> + HList,
{
need::<CurrentPrincipal>().flat_map(move |p| policy.evaluate(&p, &resource).narrow())
}
pub fn authorize_with<P, Res, R, I>(policy: P, resource: Res) -> Effect<(), AuthzError, R>
where
P: Policy<Res>,
Res: Send + Sync + 'static,
R: Selector<CurrentPrincipal, I> + HList,
{
need::<CurrentPrincipal>().flat_map(move |p| {
policy
.evaluate(&p, &resource)
.flat_map(move |d| into_effect(d, &p))
.narrow()
})
}
pub(crate) fn into_effect(
decision: Decision,
principal: &CurrentPrincipal,
) -> Effect<(), AuthzError> {
match decision.into_result(principal) {
Ok(()) => succeed(()),
Err(e) => id_effect::fail(e),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::principal::{BasicPrincipal, principal_env};
use id_effect::{Cause, Exit, fail};
fn alice() -> CurrentPrincipal {
CurrentPrincipal::new(BasicPrincipal::new("alice").with_role("accountant"))
}
fn decide<P: Policy<()>>(policy: &P, who: &CurrentPrincipal) -> Decision {
match policy.evaluate(who, &()).block_on() {
Exit::Success(d) => d,
other => panic!("unexpected exit: {other:?}"),
}
}
#[test]
fn constants_and_authenticated() {
assert_eq!(decide(&allow_all(), &alice()), Decision::Allow);
assert_eq!(
decide(&deny_all("closed"), &alice()),
Decision::deny("closed")
);
assert!(decide(&authenticated(), &alice()).is_allowed());
assert!(decide(&authenticated(), &CurrentPrincipal::anonymous()).is_denied());
}
#[test]
fn roles_must_be_held() {
assert!(decide(&require_role("accountant"), &alice()).is_allowed());
let Decision::Deny(denial) = decide(&require_any_role(["owner", "admin"]), &alice()) else {
panic!("expected a denial")
};
assert_eq!(denial.reason(), "requires role owner or admin");
assert_eq!(denial.policy(), None);
}
#[test]
fn and_or_not_short_circuit() {
let yes = allow_all();
let no = deny_all("no");
assert!(decide(&yes.clone().and(yes.clone()), &alice()).is_allowed());
assert_eq!(
decide(&no.clone().and(yes.clone()), &alice()),
Decision::deny("no")
);
assert_eq!(
decide(&yes.clone().and(deny_all("second")), &alice()),
Decision::deny("second")
);
assert!(decide(&no.clone().or(yes.clone()), &alice()).is_allowed());
assert!(decide(&yes.clone().or(no.clone()), &alice()).is_allowed());
assert_eq!(
decide(&no.clone().or(deny_all("last")), &alice()),
Decision::deny("last")
);
assert_eq!(
decide(&yes.negate("inverted"), &alice()),
Decision::deny("inverted")
);
assert!(decide(&no.negate("unused"), &alice()).is_allowed());
}
#[test]
fn all_of_and_any_of() {
let both = all_of(vec![
allow_all().boxed(),
require_role("accountant").boxed(),
]);
assert!(decide(&both, &alice()).is_allowed());
let one_denies = all_of(vec![
allow_all().boxed(),
deny_all("x").boxed(),
deny_all("y").boxed(),
]);
assert_eq!(decide(&one_denies, &alice()), Decision::deny("x"));
assert!(decide(&all_of::<()>(vec![]), &alice()).is_allowed());
let either = any_of(vec![deny_all("a").boxed(), allow_all().boxed()]);
assert!(decide(&either, &alice()).is_allowed());
let none = any_of(vec![deny_all("a").boxed(), deny_all("b").boxed()]);
assert_eq!(decide(&none, &alice()), Decision::deny("b"));
assert_eq!(
decide(&any_of::<()>(vec![]), &alice()),
Decision::deny("no policy allowed")
);
assert_eq!(format!("{both:?} {none:?}"), "AllOf(2) AnyOf(2)");
}
#[test]
fn predicate_and_effect_policies() {
let even = predicate("odd", |_: &CurrentPrincipal, n: &u32| n.is_multiple_of(2));
assert!(even.evaluate(&alice(), &4).block_on() == Exit::Success(Decision::Allow));
assert_eq!(
even.evaluate(&alice(), &3).block_on(),
Exit::Success(Decision::deny("odd"))
);
assert!(format!("{even:?}").contains("odd"));
let lookup = from_effect(|p: &CurrentPrincipal, _: &()| {
let id = p.id().to_owned();
id_effect::sync(move || Decision::allow_if(id == "alice", "not alice"))
});
assert!(decide(&lookup, &alice()).is_allowed());
assert_eq!(format!("{lookup:?}"), "EffectPolicy");
let broken =
from_effect(|_: &CurrentPrincipal, _: &()| fail(AuthzError::Evaluation("db down".into())));
assert_eq!(
broken.evaluate(&alice(), &()).block_on(),
Exit::Failure(Cause::Fail(AuthzError::Evaluation("db down".into())))
);
}
#[test]
fn shared_policies_delegate() {
let shared: Arc<RequireRole> = Arc::new(require_role("accountant"));
assert!(decide(&shared, &alice()).is_allowed());
let boxed: Box<dyn Policy<()>> = Box::new(deny_all("boxed"));
assert_eq!(decide(&boxed, &alice()), Decision::deny("boxed"));
}
#[test]
fn decisions_and_denials() {
assert!(Decision::allow_if(true, "x").is_allowed());
let d = Denial::new("nope")
.attributed_to("p")
.attributed_to("ignored");
assert_eq!(d.policy(), Some("p"));
assert_eq!(d.to_string(), "p: nope");
assert_eq!(Denial::new("bare").to_string(), "bare");
assert_eq!(Decision::Allow.into_result(&alice()), Ok(()));
assert_eq!(
Decision::deny("no").into_result(&alice()),
Err(AuthzError::Denied(Denial::new("no")))
);
assert_eq!(
Decision::deny("no").into_result(&CurrentPrincipal::anonymous()),
Err(AuthzError::Unauthenticated)
);
}
#[test]
fn errors_display_and_map_to_http_status() {
let cases = [
(AuthzError::Unauthenticated, 401, "authentication required"),
(AuthzError::Denied(Denial::new("r")), 403, "forbidden: r"),
(
AuthzError::UnknownPolicy("p".into()),
500,
"unknown policy `p`",
),
(
AuthzError::ResourceMismatch {
policy: "p".into(),
expected: "A",
found: "B",
},
500,
"policy `p` is defined for `A`, evaluated with `B`",
),
(
AuthzError::Evaluation("io".into()),
500,
"policy evaluation failed: io",
),
];
for (err, status, text) in cases {
assert_eq!(err.http_status(), status);
assert_eq!(err.to_string(), text);
}
}
#[test]
fn check_and_authorize_read_the_principal_capability() {
let env = || principal_env(BasicPrincipal::new("alice").with_role("accountant"));
let checked = check_with(require_role("owner"), ()).provide_all(env());
assert_eq!(
checked.block_on(),
Exit::Success(Decision::deny("requires role owner"))
);
let ok = authorize_with(require_role("accountant"), ()).provide_all(env());
assert_eq!(ok.block_on(), Exit::Success(()));
let denied = authorize_with(require_role("owner"), ()).provide_all(env());
assert_eq!(
denied.block_on(),
Exit::Failure(Cause::Fail(AuthzError::Denied(Denial::new(
"requires role owner"
))))
);
let anon = authorize_with(authenticated(), ()).provide_all(principal_env(crate::Anonymous));
assert_eq!(
anon.block_on(),
Exit::Failure(Cause::Fail(AuthzError::Unauthenticated))
);
}
}