use chrono::{DateTime, Utc};
use tracing::debug;
use typesec_core::policy::RequestContext;
use crate::model::{ConstraintOperand, ConstraintOperator, OdrlConstraint};
#[derive(Debug, Clone, Default)]
pub struct ConstraintContext {
pub purpose: Option<String>,
pub now: Option<DateTime<Utc>>,
pub custom: std::collections::HashMap<String, String>,
}
impl ConstraintContext {
pub fn with_purpose(mut self, purpose: impl Into<String>) -> Self {
self.purpose = Some(purpose.into());
self
}
pub fn with_time(mut self, t: DateTime<Utc>) -> Self {
self.now = Some(t);
self
}
pub fn with(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.custom.insert(key.into(), value.into());
self
}
fn effective_now(&self) -> DateTime<Utc> {
self.now.unwrap_or_else(Utc::now)
}
}
impl From<&RequestContext> for ConstraintContext {
fn from(ctx: &RequestContext) -> Self {
Self {
purpose: ctx.purpose.clone(),
now: None,
custom: ctx.custom.clone(),
}
}
}
pub fn evaluate(constraint: &OdrlConstraint, ctx: &ConstraintContext) -> bool {
debug!(
left = %constraint.left_operand,
op = ?constraint.operator,
right = %constraint.right_operand,
"evaluating constraint"
);
match &constraint.left_operand {
ConstraintOperand::Purpose => evaluate_purpose(constraint, ctx),
ConstraintOperand::DateTime => evaluate_datetime(constraint, ctx),
ConstraintOperand::Count => evaluate_custom_operand("count", constraint, ctx),
ConstraintOperand::Custom(name) => evaluate_custom_operand(name, constraint, ctx),
}
}
fn evaluate_custom_operand(
operand: &str,
constraint: &OdrlConstraint,
ctx: &ConstraintContext,
) -> bool {
if let Some(val) = ctx.custom.get(operand) {
evaluate_string_op(&constraint.operator, val, &constraint.right_operand)
} else {
debug!("unknown constraint left operand '{operand}' — failing closed");
false
}
}
fn evaluate_purpose(constraint: &OdrlConstraint, ctx: &ConstraintContext) -> bool {
let actual = match ctx.purpose.as_deref() {
Some(p) => p,
None => return false,
};
evaluate_string_op(&constraint.operator, actual, &constraint.right_operand)
}
fn evaluate_datetime(constraint: &OdrlConstraint, ctx: &ConstraintContext) -> bool {
let now = ctx.effective_now();
let rhs = match constraint.right_operand.parse::<DateTime<Utc>>() {
Ok(dt) => dt,
Err(e) => {
debug!(
"could not parse dateTime '{}': {e}",
constraint.right_operand
);
return false;
}
};
match constraint.operator {
ConstraintOperator::Lt => now < rhs,
ConstraintOperator::Lteq => now <= rhs,
ConstraintOperator::Gt => now > rhs,
ConstraintOperator::Gteq => now >= rhs,
ConstraintOperator::Eq => now == rhs,
ConstraintOperator::Neq => now != rhs,
ConstraintOperator::IsPartOf => false, }
}
fn evaluate_string_op(op: &ConstraintOperator, actual: &str, expected: &str) -> bool {
match op {
ConstraintOperator::Eq => match (actual.parse::<f64>(), expected.parse::<f64>()) {
(Ok(a), Ok(b)) => a == b,
_ => actual == expected,
},
ConstraintOperator::Neq => match (actual.parse::<f64>(), expected.parse::<f64>()) {
(Ok(a), Ok(b)) => a != b,
_ => actual != expected,
},
ConstraintOperator::IsPartOf => expected.split(',').any(|v| v.trim() == actual),
ConstraintOperator::Lt
| ConstraintOperator::Lteq
| ConstraintOperator::Gt
| ConstraintOperator::Gteq => match (actual.parse::<f64>(), expected.parse::<f64>()) {
(Ok(a), Ok(b)) => apply_ordering(op, &a, &b),
_ => apply_ordering(op, &actual, &expected),
},
}
}
fn apply_ordering<T: PartialOrd>(op: &ConstraintOperator, a: &T, b: &T) -> bool {
match op {
ConstraintOperator::Lt => a < b,
ConstraintOperator::Lteq => a <= b,
ConstraintOperator::Gt => a > b,
ConstraintOperator::Gteq => a >= b,
_ => false,
}
}
#[cfg(test)]
mod tests;