use std::time::Duration;
pub const DEFAULT_ALLOWED_ALGORITHMS: &[&str] = &[
"RS256", "RS384", "RS512", "PS256", "PS384", "PS512", "ES256", "ES384",
];
#[derive(Clone, Debug)]
pub struct ValidationOptions {
pub(crate) expected_issuer: Option<String>,
pub(crate) expected_audience: Option<String>,
pub(crate) expected_nonce: Option<String>,
pub(crate) clock_skew: Duration,
pub(crate) required_claims: Vec<String>,
pub(crate) allowed_algorithms: Vec<String>,
pub(crate) require_exp: bool,
pub(crate) require_nbf: bool,
}
impl ValidationOptions {
pub fn new() -> Self {
Self::builder().build()
}
pub fn builder() -> ValidationOptionsBuilder {
ValidationOptionsBuilder::new()
}
}
impl Default for ValidationOptions {
fn default() -> Self {
Self::new()
}
}
#[derive(Clone, Debug)]
pub struct ValidationOptionsBuilder {
expected_issuer: Option<String>,
expected_audience: Option<String>,
expected_nonce: Option<String>,
clock_skew: Duration,
required_claims: Vec<String>,
allowed_algorithms: Vec<String>,
require_exp: bool,
require_nbf: bool,
}
impl ValidationOptionsBuilder {
fn new() -> Self {
Self {
expected_issuer: None,
expected_audience: None,
expected_nonce: None,
clock_skew: Duration::ZERO,
required_claims: Vec::new(),
allowed_algorithms: DEFAULT_ALLOWED_ALGORITHMS
.iter()
.map(|a| (*a).to_string())
.collect(),
require_exp: true,
require_nbf: false,
}
}
pub fn issuer(mut self, issuer: impl Into<String>) -> Self {
self.expected_issuer = Some(issuer.into());
self
}
pub fn audience(mut self, audience: impl Into<String>) -> Self {
self.expected_audience = Some(audience.into());
self
}
pub fn expected_nonce(mut self, nonce: impl Into<String>) -> Self {
self.expected_nonce = Some(nonce.into());
self
}
pub fn clock_skew(mut self, skew: Duration) -> Self {
self.clock_skew = skew;
self
}
pub fn required_claims<I, S>(mut self, claims: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.required_claims = claims.into_iter().map(Into::into).collect();
self
}
pub fn allowed_algorithms<I, S>(mut self, algs: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
let algs: Vec<String> = algs
.into_iter()
.map(Into::into)
.filter(|a| !a.is_empty() && !a.eq_ignore_ascii_case("none"))
.collect();
if !algs.is_empty() {
self.allowed_algorithms = algs;
}
self
}
pub fn require_exp(mut self, require: bool) -> Self {
self.require_exp = require;
self
}
pub fn require_nbf(mut self, require: bool) -> Self {
self.require_nbf = require;
self
}
pub fn build(self) -> ValidationOptions {
ValidationOptions {
expected_issuer: self.expected_issuer,
expected_audience: self.expected_audience,
expected_nonce: self.expected_nonce,
clock_skew: self.clock_skew,
required_claims: self.required_claims,
allowed_algorithms: self.allowed_algorithms,
require_exp: self.require_exp,
require_nbf: self.require_nbf,
}
}
}
impl Default for ValidationOptionsBuilder {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn defaults_are_empty_with_asymmetric_allowlist() {
let opts = ValidationOptions::new();
assert!(opts.expected_issuer.is_none());
assert!(opts.expected_audience.is_none());
assert!(opts.expected_nonce.is_none());
assert_eq!(opts.clock_skew, Duration::ZERO);
assert!(opts.required_claims.is_empty());
assert!(opts.require_exp);
assert!(!opts.require_nbf);
assert_eq!(opts.allowed_algorithms, DEFAULT_ALLOWED_ALGORITHMS);
assert!(!opts.allowed_algorithms.iter().any(|a| a.starts_with("HS")));
assert!(!opts.allowed_algorithms.iter().any(|a| a == "none"));
}
#[test]
fn builder_records_expectations() {
let opts = ValidationOptions::builder()
.issuer("https://issuer.example.com")
.audience("test-client")
.expected_nonce("n-123")
.clock_skew(Duration::from_secs(60))
.required_claims(["scope", "email"])
.build();
assert_eq!(
opts.expected_issuer.as_deref(),
Some("https://issuer.example.com")
);
assert_eq!(opts.expected_audience.as_deref(), Some("test-client"));
assert_eq!(opts.expected_nonce.as_deref(), Some("n-123"));
assert_eq!(opts.clock_skew, Duration::from_secs(60));
assert_eq!(opts.required_claims, vec!["scope", "email"]);
}
#[test]
fn builder_toggles_exp_and_nbf_requirements() {
let opts = ValidationOptions::builder()
.require_exp(false)
.require_nbf(true)
.build();
assert!(!opts.require_exp);
assert!(opts.require_nbf);
}
#[test]
fn empty_nonce_is_recorded_as_set() {
let opts = ValidationOptions::builder().expected_nonce("").build();
assert_eq!(opts.expected_nonce.as_deref(), Some(""));
}
#[test]
fn custom_allowlist_filters_none_and_empty() {
let opts = ValidationOptions::builder()
.allowed_algorithms(["RS256", "none", "", "ES256"])
.build();
assert_eq!(opts.allowed_algorithms, vec!["RS256", "ES256"]);
}
#[test]
fn empty_allowlist_override_keeps_default() {
let opts = ValidationOptions::builder()
.allowed_algorithms(["none", ""])
.build();
assert_eq!(opts.allowed_algorithms, DEFAULT_ALLOWED_ALGORITHMS);
}
}