Evaluate whether all conditions on a grant are satisfied. Returns `Ok(())` if all conditions pass, `Err(reason)` if any fails.
(
conditions: &[GrantCondition],
auth: &AuthContext,
client_ip: &str,
)
| 50 | /// |
| 51 | /// Returns `Ok(())` if all conditions pass, `Err(reason)` if any fails. |
| 52 | pub fn evaluate_conditions( |
| 53 | conditions: &[GrantCondition], |
| 54 | auth: &AuthContext, |
| 55 | client_ip: &str, |
| 56 | ) -> crate::Result<()> { |
| 57 | for cond in conditions { |
| 58 | match cond { |
| 59 | GrantCondition::Temporal { |
| 60 | start_hour, |
| 61 | end_hour, |
| 62 | days, |
| 63 | } => { |
| 64 | let now = current_time_components(); |
| 65 | let hour = now.0; |
| 66 | let weekday = now.1; |
| 67 | |
| 68 | if hour < *start_hour || hour >= *end_hour { |
| 69 | return Err(crate::Error::RejectedAuthz { |
| 70 | tenant_id: auth.tenant_id, |
| 71 | resource: format!( |
| 72 | "temporal condition: current hour {hour} not in {start_hour}..{end_hour}" |
| 73 | ), |
| 74 | }); |
| 75 | } |
| 76 | if !days.is_empty() && !days.contains(&weekday) { |
| 77 | return Err(crate::Error::RejectedAuthz { |
| 78 | tenant_id: auth.tenant_id, |
| 79 | resource: format!("temporal condition: day {weekday} not in allowed days"), |
| 80 | }); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | GrantCondition::RequireMfa => { |
| 85 | // MFA is indicated by $auth.metadata.mfa_verified = "true". |
| 86 | let mfa_ok = auth |
| 87 | .metadata |
| 88 | .get("mfa_verified") |
| 89 | .is_some_and(|v| v == "true"); |
| 90 | if !mfa_ok { |
| 91 | return Err(crate::Error::RejectedAuthz { |
| 92 | tenant_id: auth.tenant_id, |
| 93 | resource: "MFA verification required".to_string(), |
| 94 | }); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | GrantCondition::RequireIp { allowed_cidrs } => { |
| 99 | let ip_ok = super::blacklist::ip::check_ip_against_cidrs(client_ip, allowed_cidrs) |
| 100 | .is_some(); |
| 101 | if !ip_ok { |
| 102 | return Err(crate::Error::RejectedAuthz { |
| 103 | tenant_id: auth.tenant_id, |
| 104 | resource: format!("IP {client_ip} not in allowed ranges"), |
| 105 | }); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | GrantCondition::StepUpAuth { max_age_secs } => { |
nothing calls this directly
no test coverage detected