use std::fmt;
pub trait JwtVerifier: Send + Sync + Clone + 'static {
type Claims: Send + Sync + Clone + 'static;
type Error: fmt::Display;
fn verify(&self, token: &str) -> Result<Self::Claims, Self::Error>;
fn validate_constraints(
&self,
_claims: &Self::Claims,
constraints: &VerifyConstraints,
) -> Result<(), ConstraintsNotSupported> {
if constraints.issuer.is_some()
|| constraints.audience.is_some()
|| constraints.leeway_secs != 0
{
Err(ConstraintsNotSupported {
reason: "this JwtVerifier does not override `validate_constraints`; \
configure constraints on the verifier itself or implement \
`validate_constraints` on your custom verifier",
})
} else {
Ok(())
}
}
}
#[derive(Debug, Clone)]
pub struct ConstraintsNotSupported {
pub reason: &'static str,
}
impl fmt::Display for ConstraintsNotSupported {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "constraints not enforceable: {}", self.reason)
}
}
#[derive(Default, Clone)]
pub struct VerifyConstraints {
pub issuer: Option<String>,
pub audience: Option<String>,
pub leeway_secs: u64,
}