use crate::{Error, Result};
use serde_json::Value;
#[derive(Debug, Clone, Copy)]
pub struct Leeway { pub seconds: i64 }
impl Default for Leeway { fn default() -> Self { Self { seconds: 0 } } }
pub fn validate_claim_times(
payload: &serde_json::Map<String, Value>,
validate_times: bool,
leeway: Leeway,
now_ts: i64,
) -> Result<()> {
if !validate_times { return Ok(()); }
if let Some(exp) = payload.get("exp").and_then(|v| v.as_i64()) {
if now_ts > exp + leeway.seconds { return Err(Error::Claims("expired".into())); }
}
if let Some(nbf) = payload.get("nbf").and_then(|v| v.as_i64()) {
if now_ts + leeway.seconds < nbf { return Err(Error::Claims("not yet valid".into())); }
}
Ok(())
}