use std::collections::HashSet;
use std::fmt;
use std::sync::Arc;
use std::time::Duration;
use id_effect::{Effect, UtcDateTime, WallClock, fail, succeed};
use jsonwebtoken::errors::{Error as JwtLibError, ErrorKind};
use jsonwebtoken::jwk::{CommonParameters, KeyAlgorithm, PublicKeyUse};
pub use jsonwebtoken::jwk::{Jwk, JwkSet};
pub use jsonwebtoken::{Algorithm, DecodingKey, EncodingKey};
use jsonwebtoken::{AlgorithmFamily, Header, Validation, decode, decode_header, encode};
use serde_json::{Map, Value};
use crate::error::AuthError;
use crate::principal::{AuthPrincipal, AuthScheme, split_list};
pub const DEFAULT_LEEWAY: Duration = Duration::from_secs(60);
pub const DEFAULT_TTL: Duration = Duration::from_secs(15 * 60);
pub const MIN_HMAC_KEY_LEN: usize = 32;
pub const DANGEROUS_ANY_ISSUER: &str = "id_effect_auth::jwt::JwtVerifier::dangerous_any_issuer";
pub const DANGEROUS_ANY_AUDIENCE: &str = "id_effect_auth::jwt::JwtVerifier::dangerous_any_audience";
fn check_hmac_len(len: usize) -> Result<(), AuthError> {
if len < MIN_HMAC_KEY_LEN {
return Err(AuthError::Key(format!(
"an HMAC secret needs at least {MIN_HMAC_KEY_LEN} bytes, this one has {len}"
)));
}
Ok(())
}
impl From<JwtLibError> for AuthError {
fn from(e: JwtLibError) -> Self {
match e.kind() {
ErrorKind::InvalidSignature | ErrorKind::InvalidAlgorithm => Self::InvalidCredentials,
ErrorKind::ExpiredSignature => Self::Expired,
ErrorKind::ImmatureSignature => Self::NotYetValid,
ErrorKind::InvalidIssuer => Self::InvalidClaims("iss is not accepted".into()),
ErrorKind::InvalidAudience => Self::InvalidClaims("aud is not accepted".into()),
ErrorKind::InvalidSubject => Self::InvalidClaims("sub is not accepted".into()),
ErrorKind::MissingRequiredClaim(c) => Self::InvalidClaims(format!("missing claim {c}")),
ErrorKind::InvalidClaimFormat(c) => Self::InvalidClaims(format!("claim {c} is malformed")),
ErrorKind::InvalidToken | ErrorKind::Base64(_) | ErrorKind::Json(_) | ErrorKind::Utf8(_) => {
Self::Malformed("not a well-formed JWT".into())
}
_ => Self::Key(e.to_string()),
}
}
}
#[derive(Clone)]
pub struct JwtKey {
kid: Option<String>,
algorithms: Vec<Algorithm>,
key: DecodingKey,
}
impl fmt::Debug for JwtKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("JwtKey")
.field("kid", &self.kid)
.field("algorithms", &self.algorithms)
.finish_non_exhaustive()
}
}
impl JwtKey {
pub fn new(key: DecodingKey, algorithms: impl IntoIterator<Item = Algorithm>) -> Self {
let family = key.family();
Self {
kid: None,
algorithms: algorithms
.into_iter()
.filter(|a| family.algorithms().contains(a))
.collect(),
key,
}
}
fn family(key: DecodingKey) -> Self {
let algorithms = key.family().algorithms().to_vec();
Self::new(key, algorithms)
}
pub fn hmac(secret: &[u8]) -> Self {
Self::family(DecodingKey::from_secret(secret))
}
pub fn rsa_pem(pem: &[u8]) -> Result<Self, AuthError> {
DecodingKey::from_rsa_pem(pem)
.map(Self::family)
.map_err(|e| AuthError::Key(e.to_string()))
}
pub fn ec_pem(pem: &[u8]) -> Result<Self, AuthError> {
DecodingKey::from_ec_pem(pem)
.map(Self::family)
.map_err(|e| AuthError::Key(e.to_string()))
}
pub fn ed_pem(pem: &[u8]) -> Result<Self, AuthError> {
DecodingKey::from_ed_pem(pem)
.map(Self::family)
.map_err(|e| AuthError::Key(e.to_string()))
}
pub fn from_jwk(jwk: &Jwk) -> Result<Self, AuthError> {
let key = DecodingKey::from_jwk(jwk).map_err(|e| AuthError::Key(e.to_string()))?;
let mut out = match jwk.common.key_algorithm {
Some(alg) => Self::new(key, [signing_algorithm(alg)?]),
None => Self::family(key),
};
out.kid = jwk.common.key_id.clone();
Ok(out)
}
#[must_use]
pub fn with_kid(mut self, kid: impl Into<String>) -> Self {
self.kid = Some(kid.into());
self
}
#[inline]
pub fn kid(&self) -> Option<&str> {
self.kid.as_deref()
}
#[inline]
pub fn algorithms(&self) -> &[Algorithm] {
&self.algorithms
}
fn allows(&self, alg: Algorithm) -> bool {
self.algorithms.contains(&alg)
}
fn check_strength(&self) -> Result<(), AuthError> {
if self.key.family() != AlgorithmFamily::Hmac {
return Ok(());
}
check_hmac_len(self.key.try_get_as_bytes().map_or(0, <[u8]>::len))
}
}
fn signing_algorithm(alg: KeyAlgorithm) -> Result<Algorithm, AuthError> {
let name = serde_json::to_value(alg)
.ok()
.and_then(|v| v.as_str().map(str::to_owned))
.unwrap_or_default();
name
.parse()
.map_err(|_| AuthError::Key(format!("{name} is not a JWS signing algorithm")))
}
pub(crate) fn select_key(
keys: &[JwtKey],
kid: Option<&str>,
alg: Algorithm,
) -> Result<JwtKey, AuthError> {
let candidates: Vec<&JwtKey> = keys.iter().filter(|k| k.allows(alg)).collect();
let by_kid = kid.and_then(|kid| candidates.iter().find(|k| k.kid() == Some(kid)));
let unnamed = || candidates.iter().find(|k| k.kid.is_none());
let only = || (candidates.len() == 1 && kid.is_none()).then(|| &candidates[0]);
by_kid
.or_else(unnamed)
.or_else(only)
.map(|k| (*k).clone())
.ok_or_else(|| {
AuthError::UnknownKey(match kid {
Some(kid) => format!("kid {kid:?} ({alg:?})"),
None => format!("no key for {alg:?}"),
})
})
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ClaimMapping {
pub subject: String,
pub roles: Vec<String>,
pub permissions: Vec<String>,
pub tenant: Option<String>,
}
impl Default for ClaimMapping {
fn default() -> Self {
Self {
subject: "sub".into(),
roles: vec!["roles".into()],
permissions: vec!["permissions".into(), "scope".into(), "scp".into()],
tenant: Some("tenant".into()),
}
}
}
impl ClaimMapping {
#[must_use]
pub fn roles<S: Into<String>>(mut self, claims: impl IntoIterator<Item = S>) -> Self {
self.roles = claims.into_iter().map(Into::into).collect();
self
}
#[must_use]
pub fn permissions<S: Into<String>>(mut self, claims: impl IntoIterator<Item = S>) -> Self {
self.permissions = claims.into_iter().map(Into::into).collect();
self
}
#[must_use]
pub fn subject(mut self, claim: impl Into<String>) -> Self {
self.subject = claim.into();
self
}
#[must_use]
pub fn tenant(mut self, claim: Option<&str>) -> Self {
self.tenant = claim.map(str::to_owned);
self
}
pub fn principal(&self, claims: Map<String, Value>) -> Result<AuthPrincipal, AuthError> {
let subject = match lookup(&claims, &self.subject) {
Some(Value::String(s)) if !s.is_empty() => s.clone(),
_ => {
return Err(AuthError::InvalidClaims(format!(
"claim {} must be a non-empty string",
self.subject
)));
}
};
let list = |names: &[String]| {
let mut out: Vec<String> = Vec::new();
for value in names.iter().filter_map(|n| lookup(&claims, n)) {
for item in string_items(value) {
if !out.contains(&item) {
out.push(item);
}
}
}
out
};
let mut principal = AuthPrincipal::new(subject)
.with_roles(list(&self.roles))
.with_permissions(list(&self.permissions))
.with_scheme(AuthScheme::Bearer);
if let Some(tenant) = self
.tenant
.as_deref()
.and_then(|t| lookup(&claims, t))
.and_then(Value::as_str)
{
principal = principal.with_tenant(tenant);
}
Ok(
claims
.into_iter()
.fold(principal, |p, (name, value)| p.with_claim(name, value)),
)
}
}
fn lookup<'c>(claims: &'c Map<String, Value>, name: &str) -> Option<&'c Value> {
if let Some(v) = claims.get(name) {
return Some(v);
}
let mut parts = name.split('.');
let first = claims.get(parts.next()?)?;
parts.try_fold(first, |v, part| v.get(part))
}
fn string_items(value: &Value) -> Vec<String> {
match value {
Value::String(s) => split_list(s),
Value::Array(items) => items
.iter()
.filter_map(Value::as_str)
.map(str::to_owned)
.collect(),
_ => Vec::new(),
}
}
#[derive(Clone)]
enum Keys {
Static(Arc<[JwtKey]>),
#[cfg(feature = "jwks")]
Jwks(crate::jwks::JwksClient),
}
#[derive(Clone)]
pub struct JwtVerifier {
keys: Keys,
algorithms: Option<Vec<Algorithm>>,
issuers: Vec<String>,
audiences: Vec<String>,
required: Vec<String>,
require_exp: bool,
leeway: Duration,
mapping: ClaimMapping,
clock: WallClock,
any_issuer: bool,
any_audience: bool,
}
impl fmt::Debug for JwtVerifier {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let keys = match &self.keys {
Keys::Static(keys) => format!("{} static", keys.len()),
#[cfg(feature = "jwks")]
Keys::Jwks(client) => format!("JWKS {}", client.url()),
};
f.debug_struct("JwtVerifier")
.field("keys", &keys)
.field("issuers", &self.issuers)
.field("audiences", &self.audiences)
.field("leeway", &self.leeway)
.finish_non_exhaustive()
}
}
impl JwtVerifier {
fn with_keys(keys: Keys) -> Self {
Self {
keys,
algorithms: None,
issuers: Vec::new(),
audiences: Vec::new(),
required: Vec::new(),
require_exp: true,
leeway: DEFAULT_LEEWAY,
mapping: ClaimMapping::default(),
clock: WallClock::system(),
any_issuer: false,
any_audience: false,
}
}
pub fn new(keys: impl IntoIterator<Item = JwtKey>) -> Self {
Self::with_keys(Keys::Static(keys.into_iter().collect()))
}
#[cfg(feature = "jwks")]
pub fn jwks(jwks: crate::jwks::JwksClient) -> Self {
Self::with_keys(Keys::Jwks(jwks))
}
#[must_use]
pub fn issuer(mut self, issuer: impl Into<String>) -> Self {
self.issuers.push(issuer.into());
self
}
#[must_use]
pub fn audience(mut self, audience: impl Into<String>) -> Self {
self.audiences.push(audience.into());
self
}
#[must_use]
pub fn dangerous_any_issuer(mut self) -> Self {
id_effect::observability::posture::record_dangerous(
DANGEROUS_ANY_ISSUER,
"H7: a JwtVerifier may accept tokens from any issuer",
);
self.any_issuer = true;
self
}
#[deprecated(
since = "0.11.0",
note = "renamed to dangerous_any_issuer (ADR 0013 naming)"
)]
#[must_use]
pub fn any_issuer_dangerous(self) -> Self {
self.dangerous_any_issuer()
}
#[must_use]
pub fn dangerous_any_audience(mut self) -> Self {
id_effect::observability::posture::record_dangerous(
DANGEROUS_ANY_AUDIENCE,
"H7: a JwtVerifier may accept tokens minted for any audience",
);
self.any_audience = true;
self
}
#[deprecated(
since = "0.11.0",
note = "renamed to dangerous_any_audience (ADR 0013 naming)"
)]
#[must_use]
pub fn any_audience_dangerous(self) -> Self {
self.dangerous_any_audience()
}
fn check_expectations(&self) -> Result<(), AuthError> {
let pinned = !self.issuers.is_empty() || !self.audiences.is_empty();
if pinned || (self.any_issuer && self.any_audience) {
return Ok(());
}
Err(AuthError::Config(
"a JwtVerifier needs an issuer or an audience (or both dangerous_any_issuer() and \
dangerous_any_audience())"
.into(),
))
}
#[must_use]
pub fn algorithms(mut self, algorithms: impl IntoIterator<Item = Algorithm>) -> Self {
self.algorithms = Some(algorithms.into_iter().collect());
self
}
#[must_use]
pub fn require_claim(mut self, claim: impl Into<String>) -> Self {
self.required.push(claim.into());
self
}
#[must_use]
pub fn require_exp(mut self, require: bool) -> Self {
self.require_exp = require;
self
}
#[must_use]
pub fn leeway(mut self, leeway: Duration) -> Self {
self.leeway = leeway;
self
}
#[must_use]
pub fn claims(mut self, mapping: ClaimMapping) -> Self {
self.mapping = mapping;
self
}
#[must_use]
pub fn with_clock(mut self, clock: WallClock) -> Self {
self.clock = clock;
self
}
pub fn verify(&self, token: &str) -> Effect<AuthPrincipal, AuthError> {
let mapping = self.mapping.clone();
self
.verify_claims(token)
.flat_map(move |claims| match mapping.principal(claims) {
Ok(p) => succeed(p),
Err(e) => fail(e),
})
}
pub fn verify_claims(&self, token: &str) -> Effect<Map<String, Value>, AuthError> {
if let Err(e) = self.check_expectations() {
return fail(e);
}
let header = match decode_header(token) {
Ok(h) => h,
Err(_) => return fail(AuthError::Malformed("not a well-formed JWT".into())),
};
if let Some(allowed) = &self.algorithms
&& !allowed.contains(&header.alg)
{
return fail(AuthError::InvalidCredentials);
}
let (this, token) = (self.clone(), token.to_owned());
let alg = header.alg;
self.key(header.kid, alg).flat_map(move |key| {
if let Err(e) = key.check_strength() {
return fail(e);
}
let claims = match decode::<Map<String, Value>>(&token, &key.key, &this.validation(alg)) {
Ok(data) => data.claims,
Err(e) => return fail(AuthError::from(e)),
};
this
.clock
.now()
.flat_map(move |now| match this.check(&claims, now) {
Ok(()) => succeed(claims),
Err(e) => fail(e),
})
})
}
fn key(&self, kid: Option<String>, alg: Algorithm) -> Effect<JwtKey, AuthError> {
match &self.keys {
Keys::Static(keys) => match select_key(keys, kid.as_deref(), alg) {
Ok(k) => succeed(k),
Err(e) => fail(e),
},
#[cfg(feature = "jwks")]
Keys::Jwks(client) => client.key(kid, alg),
}
}
fn validation(&self, alg: Algorithm) -> Validation {
let mut v = Validation::new(alg);
v.validate_exp = false;
v.validate_nbf = false;
v.leeway = 0;
v.required_spec_claims = HashSet::new();
if !self.issuers.is_empty() {
v.set_issuer(&self.issuers);
v.required_spec_claims.insert("iss".into());
}
v.validate_aud = !self.audiences.is_empty();
if v.validate_aud {
v.set_audience(&self.audiences);
v.required_spec_claims.insert("aud".into());
}
v
}
fn check(&self, claims: &Map<String, Value>, now: UtcDateTime) -> Result<(), AuthError> {
let now = now.0.as_second();
let leeway = i64::try_from(self.leeway.as_secs()).unwrap_or(i64::MAX);
let time = |name: &str| -> Result<Option<i64>, AuthError> {
match claims.get(name) {
None => Ok(None),
Some(v) => v
.as_i64()
.or_else(|| v.as_f64().map(|f| f as i64))
.map(Some)
.ok_or_else(|| AuthError::InvalidClaims(format!("claim {name} must be a number"))),
}
};
match time("exp")? {
Some(exp) if now >= exp.saturating_add(leeway) => return Err(AuthError::Expired),
None if self.require_exp => return Err(AuthError::InvalidClaims("missing claim exp".into())),
_ => {}
}
if let Some(nbf) = time("nbf")?
&& now.saturating_add(leeway) < nbf
{
return Err(AuthError::NotYetValid);
}
match self.required.iter().find(|c| lookup(claims, c).is_none()) {
Some(missing) => Err(AuthError::InvalidClaims(format!("missing claim {missing}"))),
None => Ok(()),
}
}
}
#[derive(Clone)]
pub struct JwtIssuer {
key: EncodingKey,
alg: Algorithm,
kid: Option<String>,
issuer: Option<String>,
audience: Option<String>,
ttl: Duration,
clock: WallClock,
}
impl fmt::Debug for JwtIssuer {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("JwtIssuer")
.field("alg", &self.alg)
.field("kid", &self.kid)
.field("issuer", &self.issuer)
.field("audience", &self.audience)
.field("ttl", &self.ttl)
.finish_non_exhaustive()
}
}
impl JwtIssuer {
pub fn new(alg: Algorithm, key: EncodingKey) -> Result<Self, AuthError> {
if !key.family().algorithms().contains(&alg) {
return Err(AuthError::Key(format!(
"{alg:?} cannot sign with a {:?} key",
key.family()
)));
}
Ok(Self {
key,
alg,
kid: None,
issuer: None,
audience: None,
ttl: DEFAULT_TTL,
clock: WallClock::system(),
})
}
pub fn hs256(secret: &[u8]) -> Self {
Self {
key: EncodingKey::from_secret(secret),
alg: Algorithm::HS256,
kid: None,
issuer: None,
audience: None,
ttl: DEFAULT_TTL,
clock: WallClock::system(),
}
}
pub fn rsa_pem(alg: Algorithm, pem: &[u8]) -> Result<Self, AuthError> {
let key = EncodingKey::from_rsa_pem(pem).map_err(|e| AuthError::Key(e.to_string()))?;
Self::new(alg, key)
}
pub fn ec_pem(alg: Algorithm, pem: &[u8]) -> Result<Self, AuthError> {
let key = EncodingKey::from_ec_pem(pem).map_err(|e| AuthError::Key(e.to_string()))?;
Self::new(alg, key)
}
#[must_use]
pub fn kid(mut self, kid: impl Into<String>) -> Self {
self.kid = Some(kid.into());
self
}
#[must_use]
pub fn issuer(mut self, issuer: impl Into<String>) -> Self {
self.issuer = Some(issuer.into());
self
}
#[must_use]
pub fn audience(mut self, audience: impl Into<String>) -> Self {
self.audience = Some(audience.into());
self
}
#[must_use]
pub fn ttl(mut self, ttl: Duration) -> Self {
self.ttl = ttl;
self
}
#[must_use]
pub fn with_clock(mut self, clock: WallClock) -> Self {
self.clock = clock;
self
}
#[inline]
pub fn algorithm(&self) -> Algorithm {
self.alg
}
pub fn issue(&self, principal: &AuthPrincipal) -> Effect<String, AuthError> {
use id_effect_authz::Principal as _;
let mut claims: Map<String, Value> = principal
.claims()
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
claims.insert("sub".into(), principal.id().into());
if !principal.roles().is_empty() {
claims.insert("roles".into(), principal.roles().into());
}
if !principal.permissions().is_empty() {
claims.insert("permissions".into(), principal.permissions().into());
}
if let Some(tenant) = principal.tenant() {
claims.insert("tenant".into(), tenant.into());
}
self.issue_claims(claims)
}
pub fn issue_claims(&self, claims: Map<String, Value>) -> Effect<String, AuthError> {
if self.key.family() == AlgorithmFamily::Hmac
&& let Err(e) = check_hmac_len(self.key.as_bytes().len())
{
return fail(e);
}
let this = self.clone();
self.clock.now().flat_map(move |now| {
let now = now.0.as_second();
let ttl = i64::try_from(this.ttl.as_secs()).unwrap_or(i64::MAX);
let mut claims = claims;
let mut default = |name: &str, value: Value| {
claims.entry(name.to_owned()).or_insert(value);
};
if let Some(iss) = &this.issuer {
default("iss", iss.as_str().into());
}
if let Some(aud) = &this.audience {
default("aud", aud.as_str().into());
}
default("iat", now.into());
default("exp", now.saturating_add(ttl).into());
match crate::apikey::random_hex(16) {
Ok(jti) => default("jti", jti.into()),
Err(e) => return fail(e),
}
let mut header = Header::new(this.alg);
header.kid.clone_from(&this.kid);
match encode(&header, &claims, &this.key) {
Ok(token) => succeed(token),
Err(e) => fail(AuthError::Key(e.to_string())),
}
})
}
pub fn jwk(&self) -> Result<Jwk, AuthError> {
if self.key.family() == AlgorithmFamily::Hmac {
return Err(AuthError::Key("an HMAC secret has no public key".into()));
}
let mut jwk =
Jwk::from_encoding_key(&self.key, self.alg).map_err(|e| AuthError::Key(e.to_string()))?;
jwk.common = CommonParameters {
public_key_use: Some(PublicKeyUse::Signature),
key_algorithm: Some(KeyAlgorithm::from(self.alg)),
key_id: self.kid.clone(),
..CommonParameters::default()
};
Ok(jwk)
}
pub fn verification_key(&self) -> Result<JwtKey, AuthError> {
let key = if self.key.family() == AlgorithmFamily::Hmac {
JwtKey::new(DecodingKey::from_secret(self.key.as_bytes()), [self.alg])
} else {
JwtKey::from_jwk(&self.jwk()?)?
};
Ok(match &self.kid {
Some(kid) => key.with_kid(kid.clone()),
None => key,
})
}
}
#[cfg(test)]
mod tests {
use std::time::Instant;
use id_effect::{Cause, Exit};
use id_effect_authz::Principal;
use serde_json::json;
use super::*;
const SECRET: &[u8] = b"0123456789abcdef0123456789abcdef";
fn run<A: Send + 'static>(eff: Effect<A, AuthError>) -> Result<A, AuthError> {
match eff.block_on() {
Exit::Success(a) => Ok(a),
Exit::Failure(Cause::Fail(e)) => Err(e),
Exit::Failure(c) => panic!("unexpected cause: {c:?}"),
}
}
fn clock_at(secs: i64) -> WallClock {
WallClock::anchored(Instant::now(), UtcDateTime::unsafe_make(secs * 1000))
}
fn claims(v: Value) -> Map<String, Value> {
match v {
Value::Object(m) => m,
_ => unreachable!(),
}
}
fn hs(claims_v: Value) -> String {
let issuer = JwtIssuer::hs256(SECRET).with_clock(clock_at(1_000));
run(issuer.issue_claims(claims(claims_v))).unwrap()
}
fn verifier() -> JwtVerifier {
JwtVerifier::new([JwtKey::hmac(SECRET)])
.with_clock(clock_at(1_000))
.dangerous_any_issuer()
.dangerous_any_audience()
}
#[test]
fn issued_tokens_round_trip_with_mapped_claims() {
let issuer = JwtIssuer::hs256(SECRET)
.issuer("iss-a")
.audience("aud-a")
.kid("k1")
.ttl(Duration::from_secs(60))
.with_clock(clock_at(1_000));
assert_eq!(issuer.algorithm(), Algorithm::HS256);
let alice = AuthPrincipal::new("alice")
.with_role("viewer")
.with_permission("ledger:read")
.with_tenant("acme")
.with_claim("email", "a@example.com");
let token = run(issuer.issue(&alice)).unwrap();
let v = verifier().issuer("iss-a").audience("aud-a");
let p = run(v.verify(&token)).unwrap();
assert_eq!(p.id(), "alice");
assert_eq!(p.roles(), ["viewer"]);
assert_eq!(p.permissions(), ["ledger:read"]);
assert_eq!(Principal::tenant(&p), Some("acme"));
assert_eq!(p.claim("email"), Some(&json!("a@example.com")));
assert_eq!(p.claim("exp"), Some(&json!(1_060)));
assert_eq!(p.claim("iat"), Some(&json!(1_000)));
assert!(p.claim("jti").is_some());
assert_eq!(p.scheme(), Some(AuthScheme::Bearer));
let header = decode_header(&token).unwrap();
assert_eq!(header.kid.as_deref(), Some("k1"));
let vk = issuer.verification_key().unwrap();
assert_eq!(vk.kid(), Some("k1"));
assert!(
run(
JwtVerifier::new([vk])
.issuer("iss-a")
.with_clock(clock_at(1_000))
.verify(&token)
)
.is_ok()
);
assert!(issuer.jwk().is_err(), "HMAC secrets are never published");
}
#[test]
fn time_checks_use_the_clock_and_leeway() {
let token = hs(json!({"sub": "a", "exp": 1_100, "nbf": 900}));
let at = |secs| verifier().with_clock(clock_at(secs));
assert!(run(at(1_000).verify(&token)).is_ok());
assert!(run(at(1_159).verify(&token)).is_ok(), "within leeway");
assert_eq!(run(at(1_160).verify(&token)), Err(AuthError::Expired));
assert_eq!(
run(at(1_100).leeway(Duration::ZERO).verify(&token)),
Err(AuthError::Expired)
);
assert!(run(at(840).verify(&token)).is_ok(), "nbf within leeway");
assert_eq!(run(at(839).verify(&token)), Err(AuthError::NotYetValid));
let float_exp = hs(json!({"sub": "a", "exp": 1_100.5}));
assert!(run(verifier().verify(&float_exp)).is_ok());
let bad_exp = hs(json!({"sub": "a", "exp": "soon"}));
assert!(matches!(
run(verifier().verify(&bad_exp)),
Err(AuthError::InvalidClaims(_))
));
}
#[test]
fn exp_and_other_claims_can_be_required() {
let no_exp = {
let mut m = claims(json!({"sub": "a"}));
m.insert("exp".into(), Value::Null);
let mut header = Header::new(Algorithm::HS256);
header.kid = None;
m.remove("exp");
encode(&header, &m, &EncodingKey::from_secret(SECRET)).unwrap()
};
assert_eq!(
run(verifier().verify(&no_exp)),
Err(AuthError::InvalidClaims("missing claim exp".into()))
);
assert!(run(verifier().require_exp(false).verify(&no_exp)).is_ok());
let token = hs(json!({"sub": "a", "org": {"id": 7}}));
assert!(run(verifier().require_claim("org.id").verify(&token)).is_ok());
assert_eq!(
run(verifier().require_claim("email").verify(&token)),
Err(AuthError::InvalidClaims("missing claim email".into()))
);
}
#[test]
fn issuer_audience_and_signature_are_enforced() {
let token = hs(json!({"sub": "a", "iss": "iss-a", "aud": ["aud-a", "aud-b"]}));
assert!(run(verifier().issuer("iss-a").audience("aud-b").verify(&token)).is_ok());
assert!(matches!(
run(verifier().issuer("iss-x").verify(&token)),
Err(AuthError::InvalidClaims(m)) if m.contains("iss")
));
assert!(matches!(
run(verifier().audience("aud-x").verify(&token)),
Err(AuthError::InvalidClaims(m)) if m.contains("aud")
));
let bare = hs(json!({"sub": "a"}));
assert!(matches!(
run(verifier().audience("aud-a").verify(&bare)),
Err(AuthError::InvalidClaims(m)) if m.contains("aud")
));
let wrong = JwtVerifier::new([JwtKey::hmac(b"another secret of at least 32 bytes")])
.with_clock(clock_at(1_000))
.dangerous_any_issuer()
.dangerous_any_audience();
assert_eq!(run(wrong.verify(&bare)), Err(AuthError::InvalidCredentials));
let at = bare.rfind('.').unwrap() + 1;
let flipped = if bare[at..].starts_with('A') {
"B"
} else {
"A"
};
let tampered = format!("{}{flipped}{}", &bare[..at], &bare[at + 1..]);
assert_eq!(
run(verifier().verify(&tampered)),
Err(AuthError::InvalidCredentials)
);
assert!(matches!(
run(verifier().verify("not.a.jwt")),
Err(AuthError::Malformed(_))
));
assert_eq!(
run(verifier().algorithms([Algorithm::HS512]).verify(&bare)),
Err(AuthError::InvalidCredentials)
);
assert!(run(verifier().algorithms([Algorithm::HS256]).verify(&bare)).is_ok());
}
#[test]
fn claim_mapping_reads_nested_namespaced_and_scope_claims() {
let token = hs(json!({
"sub": "a",
"uid": "u-9",
"realm_access": {"roles": ["admin", "viewer"]},
"https://example.com/roles": ["x"],
"scope": "read write",
"scp": ["read", "audit"],
"org": "acme"
}));
let mapping = ClaimMapping::default()
.roles(["realm_access.roles", "https://example.com/roles"])
.subject("uid")
.tenant(Some("org"));
let p = run(verifier().claims(mapping).verify(&token)).unwrap();
assert_eq!(p.id(), "u-9");
assert_eq!(p.roles(), ["admin", "viewer", "x"]);
assert_eq!(p.permissions(), ["read", "write", "audit"]);
assert_eq!(Principal::tenant(&p), Some("acme"));
let none = ClaimMapping::default().permissions(["nope"]).tenant(None);
let p = run(verifier().claims(none).verify(&token)).unwrap();
assert!(p.permissions().is_empty());
assert_eq!(Principal::tenant(&p), None);
let no_sub = hs(json!({"name": "a"}));
assert!(matches!(
run(verifier().verify(&no_sub)),
Err(AuthError::InvalidClaims(m)) if m.contains("sub")
));
assert!(string_items(&json!(7)).is_empty());
}
#[test]
fn key_selection_by_kid_and_family() {
let a = JwtKey::hmac(b"a").with_kid("a");
let b = JwtKey::hmac(b"b").with_kid("b");
let unnamed = JwtKey::hmac(b"u");
let keys = [a.clone(), b.clone()];
assert_eq!(
select_key(&keys, Some("b"), Algorithm::HS256)
.unwrap()
.kid(),
Some("b")
);
assert!(matches!(
select_key(&keys, Some("c"), Algorithm::HS256),
Err(AuthError::UnknownKey(m)) if m.contains("\"c\"")
));
assert!(matches!(
select_key(&keys, None, Algorithm::HS256),
Err(AuthError::UnknownKey(m)) if m.contains("HS256")
));
assert_eq!(
select_key(std::slice::from_ref(&a), None, Algorithm::HS256)
.unwrap()
.kid(),
Some("a")
);
let with_unnamed = [a, unnamed];
assert_eq!(
select_key(&with_unnamed, Some("zz"), Algorithm::HS256)
.unwrap()
.kid(),
None
);
assert!(select_key(&keys, Some("a"), Algorithm::RS256).is_err());
let narrowed = JwtKey::new(
DecodingKey::from_secret(b"s"),
[Algorithm::HS512, Algorithm::RS256],
);
assert_eq!(narrowed.algorithms(), [Algorithm::HS512]);
assert!(format!("{narrowed:?}").contains("HS512"));
}
#[test]
fn key_material_errors() {
assert!(matches!(JwtKey::rsa_pem(b"nope"), Err(AuthError::Key(_))));
assert!(matches!(JwtKey::ec_pem(b"nope"), Err(AuthError::Key(_))));
assert!(matches!(JwtKey::ed_pem(b"nope"), Err(AuthError::Key(_))));
assert!(matches!(
JwtIssuer::rsa_pem(Algorithm::RS256, b"nope"),
Err(AuthError::Key(_))
));
assert!(matches!(
JwtIssuer::ec_pem(Algorithm::ES256, b"nope"),
Err(AuthError::Key(_))
));
assert!(matches!(
JwtIssuer::new(Algorithm::RS256, EncodingKey::from_secret(b"s")),
Err(AuthError::Key(_))
));
let issuer = JwtIssuer::new(Algorithm::HS384, EncodingKey::from_secret(b"s")).unwrap();
assert!(format!("{issuer:?}").contains("HS384"));
let verifier = verifier().issuer("i").audience("a");
let dbg = format!("{verifier:?}");
assert!(dbg.contains("1 static") && dbg.contains("\"i\""), "{dbg}");
}
#[test]
fn h7_jwt_without_issuer_or_audience_is_rejected() {
let token = hs(json!({"sub": "a", "iss": "iss-a", "aud": "aud-a"}));
let lax = JwtVerifier::new([JwtKey::hmac(SECRET)]).with_clock(clock_at(1_000));
let Err(AuthError::Config(m)) = run(lax.verify(&token)) else {
panic!("a verifier pinning neither iss nor aud must not accept tokens")
};
assert!(m.contains("issuer or an audience"), "{m}");
assert_eq!(AuthError::Config(m).http_status(), 500);
assert!(run(lax.clone().issuer("iss-a").verify(&token)).is_ok());
assert!(run(lax.clone().audience("aud-a").verify(&token)).is_ok());
assert!(matches!(
run(lax.audience("aud-b").verify(&token)),
Err(AuthError::InvalidClaims(m)) if m.contains("aud")
));
}
#[test]
fn h7_dangerous_opt_out_is_explicit() {
let token = hs(json!({"sub": "a"}));
let lax = JwtVerifier::new([JwtKey::hmac(SECRET)]).with_clock(clock_at(1_000));
for half in [
lax.clone().dangerous_any_issuer(),
lax.clone().dangerous_any_audience(),
] {
assert!(matches!(
run(half.verify(&token)),
Err(AuthError::Config(_))
));
}
let both = lax.dangerous_any_issuer().dangerous_any_audience();
assert_eq!(run(both.verify(&token)).unwrap().id(), "a");
}
#[test]
fn h7_dangerous_any_issuer_and_audience_are_recorded() {
let token = hs(json!({"sub": "a"}));
let lax = JwtVerifier::new([JwtKey::hmac(SECRET)]).with_clock(clock_at(1_000));
#[allow(deprecated)]
let shim = lax.clone().any_issuer_dangerous().any_audience_dangerous();
assert_eq!(run(shim.verify(&token)).unwrap().id(), "a");
let _ = lax.dangerous_any_issuer().dangerous_any_audience();
let recorded = id_effect::observability::posture::dangerous_opt_outs();
for id in [DANGEROUS_ANY_ISSUER, DANGEROUS_ANY_AUDIENCE] {
let (_, reopens) = recorded
.iter()
.find(|(found, _)| *found == id)
.expect("recorded");
assert!(reopens.starts_with("H7:"), "{id}: {reopens}");
}
}
#[test]
fn m15_short_hmac_key_rejected() {
let short = b"31 bytes, one short of HS256 ok";
assert_eq!(short.len(), MIN_HMAC_KEY_LEN - 1);
let issued = run(JwtIssuer::hs256(short).issue(&AuthPrincipal::new("a")));
assert!(
matches!(&issued, Err(AuthError::Key(m)) if m.contains("at least 32 bytes")),
"{issued:?}"
);
let other_route = JwtIssuer::new(Algorithm::HS384, EncodingKey::from_secret(short)).unwrap();
assert!(matches!(
run(other_route.issue(&AuthPrincipal::new("a"))),
Err(AuthError::Key(_))
));
let mut header = Header::new(Algorithm::HS256);
header.kid = None;
let forged = encode(
&header,
&claims(json!({"sub": "a", "exp": 2_000})),
&EncodingKey::from_secret(short),
)
.unwrap();
let weak = JwtVerifier::new([JwtKey::hmac(short)])
.with_clock(clock_at(1_000))
.dangerous_any_issuer()
.dangerous_any_audience();
assert!(matches!(
run(weak.verify(&forged)),
Err(AuthError::Key(m)) if m.contains("has 31")
));
assert!(run(JwtIssuer::hs256(SECRET).issue(&AuthPrincipal::new("a"))).is_ok());
}
#[test]
fn library_errors_map_to_auth_errors() {
let cases = [
(ErrorKind::InvalidSignature, AuthError::InvalidCredentials),
(ErrorKind::ExpiredSignature, AuthError::Expired),
(ErrorKind::ImmatureSignature, AuthError::NotYetValid),
(
ErrorKind::InvalidSubject,
AuthError::InvalidClaims("sub is not accepted".into()),
),
(
ErrorKind::MissingRequiredClaim("x".into()),
AuthError::InvalidClaims("missing claim x".into()),
),
(
ErrorKind::InvalidClaimFormat("x".into()),
AuthError::InvalidClaims("claim x is malformed".into()),
),
];
for (kind, expected) in cases {
assert_eq!(AuthError::from(JwtLibError::from(kind)), expected);
}
assert!(matches!(
AuthError::from(JwtLibError::from(ErrorKind::InvalidKeyFormat)),
AuthError::Key(_)
));
}
}