Skip to content

Commit c6700b9

Browse files
committedMar 4, 2025
Validate the aud claim of JwtAccessToken
1 parent a9e32ff commit c6700b9

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed
 

‎src/simple_openid_connect/data.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,13 @@ class JwtAccessToken(OpenidBaseModel):
342342
scope: Optional[str] = None
343343
"OPTIONAL. Scopes to which the token grants access. Multiple scopes are encoded space separated. If the openid scope value is not present, the behavior is entirely unspecified. Other scope values MAY be present."
344344

345-
def validate_extern(self, issuer: str) -> None:
345+
def validate_extern(self, issuer: str, client_id: Union[str, None] = None) -> None:
346346
"""
347347
Validate this access token with external data for consistency.
348348
349349
:param issuer: The issuer that this token is supposed to originate from.
350350
Should usually be :data:`ProviderMetadata.issuer`.
351+
:param client_id: The client id of this client
351352
"""
352353
# validate issuer
353354
validate_that(
@@ -358,6 +359,23 @@ def validate_extern(self, issuer: str) -> None:
358359
# validate expiry
359360
validate_that(self.exp > time.time(), "The access token is expired")
360361

362+
# validate audience
363+
if client_id:
364+
validate_that(
365+
self.aud is not None,
366+
"The access token does not contain the required audience value",
367+
)
368+
if isinstance(self.aud, str):
369+
validate_that(
370+
self.aud == client_id,
371+
"The access tokens audience does not contain own client_id",
372+
)
373+
elif isinstance(self.aud, list):
374+
validate_that(
375+
client_id in self.aud,
376+
"The access tokens audience does not contain own client_id",
377+
)
378+
361379

362380
class UserinfoRequest(OpenidBaseModel):
363381
"""

0 commit comments

Comments
 (0)