refactor(jwt): route JWK algorithm lookup through one helper

Add `windmill_common::jwt::jwk_algorithm` as the single place that reads a
JWK's `alg`, and make `guest_jwt::jwk_algorithms` use it. EE code will call
the same helper, so the upcoming jsonwebtoken bump (which renames the field
to `key_algorithm: Option<KeyAlgorithm>`) only has to touch the helper's body.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Alexander Petric
2026-09-18 09:33:46 -04:00
co-authored by Claude Fable 5.1
parent 01b38320ea
commit 8b3f35f7f1
2 changed files with 25 additions and 1 deletions
+1 -1
View File
@@ -167,7 +167,7 @@ pub fn jwk_algorithms(jwk: &Jwk) -> Option<Vec<Algorithm>> {
{
return None;
}
match (&jwk.algorithm, jwk.common.algorithm) {
match (&jwk.algorithm, crate::jwt::jwk_algorithm(jwk)) {
(AlgorithmParameters::RSA(_), Some(alg)) if RSA_ALGORITHMS.contains(&alg) => {
Some(vec![alg])
}
+24
View File
@@ -74,3 +74,27 @@ pub async fn generate_signature(header_and_payload: &str) -> anyhow::Result<Stri
let result = mac.finalize().into_bytes();
Ok(URL_SAFE_NO_PAD.encode(result))
}
/// The signing algorithm a JWK names in its `alg`, or `None` when it names none (or one
/// that is not a signing algorithm). The only place that reads the field, so callers,
/// including EE ones, stay agnostic of how `jsonwebtoken` models it.
pub fn jwk_algorithm(jwk: &jsonwebtoken::jwk::Jwk) -> Option<jsonwebtoken::Algorithm> {
jwk.common.algorithm
}
#[cfg(test)]
mod tests {
use super::jwk_algorithm;
use jsonwebtoken::{jwk::Jwk, Algorithm};
#[test]
fn jwk_algorithm_reads_alg_when_present() {
let with: Jwk =
serde_json::from_value(serde_json::json!({"kty":"RSA","alg":"RS256","n":"aa","e":"AQAB"}))
.unwrap();
assert_eq!(jwk_algorithm(&with), Some(Algorithm::RS256));
let without: Jwk =
serde_json::from_value(serde_json::json!({"kty":"RSA","n":"aa","e":"AQAB"})).unwrap();
assert_eq!(jwk_algorithm(&without), None);
}
}