From 3c3edacd7cabff50cde220311bb6e309b7558d4e Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Fri, 10 Jan 2025 21:01:19 -0600 Subject: [PATCH] Support cases where the aud token is a list of valid audiences. --- buffoonery.nimble | 2 +- src/buffoonery/auth.nim | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/buffoonery.nimble b/buffoonery.nimble index 5be52b6..6b075b8 100644 --- a/buffoonery.nimble +++ b/buffoonery.nimble @@ -1,6 +1,6 @@ # Package -version = "0.4.2" +version = "0.4.3" author = "Jonathan Bernard" description = "Jonathan's opinionated extensions and auth layer for Jester." license = "MIT" diff --git a/src/buffoonery/auth.nim b/src/buffoonery/auth.nim index fd6f769..ab6eb3d 100644 --- a/src/buffoonery/auth.nim +++ b/src/buffoonery/auth.nim @@ -143,8 +143,15 @@ proc validateJWT*(ctx: ApiAuthContext, jwt: JWT) = if jwt.claims.sub.isNone: failAuth "Missing 'sub' claim." if jwt.claims.exp.isNone: failAuth "Missing or invalid 'exp' claim." - if not ctx.validAudiences.contains(jwt.claims.aud.get): - failAuth "JWT is not for us (invalid audience)." + if jwt.claims["aud"].get.kind == JString: + # If the token is for a single audience, check that it is for us. + if not ctx.validAudiences.contains(jwt.claims.aud.get): + failAuth "JWT is not for us (invalid audience)." + elif jwt.claims["aud"].get.kind == JArray: + # If the token is for multiple audiences, check that at least one is for us. + let auds = jwt.claims["aud"].get.getElems + if not auds.anyIt(ctx.validAudiences.contains(it.getStr)): + failAuth "JWT is not for us (invalid audience)." let signingAlgorithm = jwt.header.alg.get