use jsonwebtoken::jwk::JwkSet;
use jsonwebtoken::{decode, decode_header, Algorithm, DecodingKey, Validation};
use serde_json::Value;
use crate::{AuthError, Authenticator, Principal};
pub const DEFAULT_TENANT_CLAIM: &str = "tenant";
pub const DEFAULT_PERMISSION_CLAIM: &str = "permissions";
const ALLOWED_ALGORITHMS: [Algorithm; 2] = [Algorithm::RS256, Algorithm::ES256];
pub struct JwtAuthenticator {
jwks: JwkSet,
validation: Validation,
tenant_claim: String,
permission_claim: String,
}
impl JwtAuthenticator {
#[must_use]
pub fn new(jwks: JwkSet, issuer: impl Into<String>, audience: impl Into<String>) -> Self {
let mut validation = Validation::new(Algorithm::RS256);
validation.algorithms = ALLOWED_ALGORITHMS.to_vec();
validation.set_issuer(&[issuer.into()]);
validation.set_audience(&[audience.into()]);
validation.validate_nbf = true;
Self {
jwks,
validation,
tenant_claim: DEFAULT_TENANT_CLAIM.to_string(),
permission_claim: DEFAULT_PERMISSION_CLAIM.to_string(),
}
}
pub fn from_jwks_json(
jwks_json: &str,
issuer: impl Into<String>,
audience: impl Into<String>,
) -> Result<Self, serde_json::Error> {
let jwks: JwkSet = serde_json::from_str(jwks_json)?;
Ok(Self::new(jwks, issuer, audience))
}
#[must_use]
pub fn with_tenant_claim(mut self, claim: impl Into<String>) -> Self {
self.tenant_claim = claim.into();
self
}
#[must_use]
pub fn with_permission_claim(mut self, claim: impl Into<String>) -> Self {
self.permission_claim = claim.into();
self
}
fn permissions_from(&self, claims: &Value) -> std::collections::BTreeSet<String> {
if let Some(array) = claims.get(&self.permission_claim).and_then(Value::as_array) {
return array
.iter()
.filter_map(Value::as_str)
.map(str::to_string)
.collect();
}
if let Some(scope) = claims.get("scope").and_then(Value::as_str) {
return scope.split_whitespace().map(str::to_string).collect();
}
std::collections::BTreeSet::new()
}
}
impl Authenticator for JwtAuthenticator {
fn authenticate(&self, bearer: &str) -> Result<Principal, AuthError> {
if bearer.is_empty() {
return Err(AuthError::MissingCredentials);
}
let header = decode_header(bearer).map_err(|_| AuthError::InvalidToken)?;
if !ALLOWED_ALGORITHMS.contains(&header.alg) {
return Err(AuthError::InvalidToken);
}
let kid = header.kid.ok_or(AuthError::InvalidToken)?;
let jwk = self.jwks.find(&kid).ok_or(AuthError::InvalidToken)?;
let key = DecodingKey::from_jwk(jwk).map_err(|_| AuthError::InvalidToken)?;
let mut validation = self.validation.clone();
validation.algorithms = vec![header.alg];
let data =
decode::<Value>(bearer, &key, &validation).map_err(|_| AuthError::InvalidToken)?;
let claims = data.claims;
let subject = claims
.get("sub")
.and_then(Value::as_str)
.ok_or(AuthError::InvalidToken)?
.to_string();
let tenant = claims
.get(&self.tenant_claim)
.and_then(Value::as_str)
.map(str::to_string);
let permissions = self.permissions_from(&claims);
Ok(Principal {
subject,
tenant,
permissions,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use jsonwebtoken::{encode, get_current_timestamp, EncodingKey, Header};
use serde_json::json;
const PRIVATE_KEY_PEM: &[u8] = include_bytes!("../tests/fixtures/private_key.pem");
const JWKS_JSON: &str = include_str!("../tests/fixtures/jwks.json");
const ISSUER: &str = "https://id.example.com/";
const AUDIENCE: &str = "seiva-api";
fn jwks() -> JwkSet {
serde_json::from_str(JWKS_JSON).expect("the fixture JWKS parses")
}
fn authenticator() -> JwtAuthenticator {
JwtAuthenticator::new(jwks(), ISSUER, AUDIENCE)
}
fn sign(alg: Algorithm, kid: Option<&str>, claims: &Value) -> String {
let mut header = Header::new(alg);
header.kid = kid.map(str::to_string);
let key = EncodingKey::from_rsa_pem(PRIVATE_KEY_PEM).expect("fixture key loads");
encode(&header, claims, &key).expect("signing succeeds")
}
fn valid_claims() -> Value {
json!({
"sub": "alice",
"iss": ISSUER,
"aud": AUDIENCE,
"exp": get_current_timestamp() + 3600,
"tenant": "acme",
"permissions": ["notes.read", "notes.create"],
})
}
#[test]
fn a_valid_token_maps_claims_to_a_principal() {
let token = sign(Algorithm::RS256, Some("test-key-1"), &valid_claims());
let p = authenticator().authenticate(&token).expect("valid token");
assert_eq!(p.subject, "alice");
assert_eq!(p.tenant.as_deref(), Some("acme"));
assert!(p.has("notes.read") && p.has("notes.create"));
}
#[test]
fn a_tampered_signature_is_rejected() {
let token = sign(Algorithm::RS256, Some("test-key-1"), &valid_claims());
let mut bytes = token.into_bytes();
let last = bytes.last_mut().unwrap();
*last = if *last == b'A' { b'B' } else { b'A' };
let tampered = String::from_utf8(bytes).unwrap();
assert_eq!(
authenticator().authenticate(&tampered),
Err(AuthError::InvalidToken)
);
}
#[test]
fn the_wrong_audience_is_rejected() {
let mut claims = valid_claims();
claims["aud"] = json!("someone-else");
let token = sign(Algorithm::RS256, Some("test-key-1"), &claims);
assert_eq!(
authenticator().authenticate(&token),
Err(AuthError::InvalidToken)
);
}
#[test]
fn the_wrong_issuer_is_rejected() {
let mut claims = valid_claims();
claims["iss"] = json!("https://evil.example/");
let token = sign(Algorithm::RS256, Some("test-key-1"), &claims);
assert_eq!(
authenticator().authenticate(&token),
Err(AuthError::InvalidToken)
);
}
#[test]
fn an_expired_token_is_rejected() {
let mut claims = valid_claims();
claims["exp"] = json!(get_current_timestamp() - 3600);
let token = sign(Algorithm::RS256, Some("test-key-1"), &claims);
assert_eq!(
authenticator().authenticate(&token),
Err(AuthError::InvalidToken)
);
}
#[test]
fn an_unknown_key_id_is_rejected() {
let token = sign(Algorithm::RS256, Some("some-other-kid"), &valid_claims());
assert_eq!(
authenticator().authenticate(&token),
Err(AuthError::InvalidToken)
);
}
#[test]
fn a_disallowed_algorithm_is_rejected() {
let mut header = Header::new(Algorithm::HS256);
header.kid = Some("test-key-1".to_string());
let token = encode(
&header,
&valid_claims(),
&EncodingKey::from_secret(b"attacker-chosen-secret"),
)
.unwrap();
assert_eq!(
authenticator().authenticate(&token),
Err(AuthError::InvalidToken)
);
}
#[test]
fn an_empty_bearer_is_missing_credentials() {
assert_eq!(
authenticator().authenticate(""),
Err(AuthError::MissingCredentials)
);
}
#[test]
fn permissions_fall_back_to_the_scope_claim() {
let mut claims = valid_claims();
claims.as_object_mut().unwrap().remove("permissions");
claims["scope"] = json!("notes.read notes.delete");
let token = sign(Algorithm::RS256, Some("test-key-1"), &claims);
let p = authenticator().authenticate(&token).expect("valid");
assert!(p.has("notes.read") && p.has("notes.delete"));
}
}