Skip to content

Commit

Permalink
Handle when aud OIDC claim is an Array
Browse files Browse the repository at this point in the history
The `aud` claim of OIDC id_tokens [can be an array](https://github.com/authts/oidc-client-ts/blob/ce6d694639c58e6a1c80904efdac5eda82b82042/src/Claims.ts#L92) but the existing logic
incorrectly assumes `aud` is always a string.

This PR adds the necessary check.
  • Loading branch information
liamdiprose committed Dec 11, 2024
1 parent 8155b0a commit 9c0d8e8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions spec/unit/oidc/validate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ describe("validateIdToken()", () => {
expect(logger.error).toHaveBeenCalledWith("Invalid ID token", new Error("Invalid audience"));
});

it("should not throw for a list of trusted audiences", () => {
mocked(jwtDecode).mockReturnValue({
...validDecodedIdToken,
aud: [clientId],
});
expect(() => validateIdToken(idToken, issuer, clientId, nonce)).not.toThrow();
});

it("should throw when nonce does not match", () => {
mocked(jwtDecode).mockReturnValue({
...validDecodedIdToken,
Expand Down
2 changes: 1 addition & 1 deletion src/oidc/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export const validateIdToken = (
* The ID Token MUST be rejected if the ID Token does not list the Client as a valid audience, or if it contains additional audiences not trusted by the Client.
* EW: Don't accept tokens with other untrusted audiences
* */
if (claims.aud !== clientId) {
if (claims.aud !== clientId && !(Array.isArray(claims.aud) && claims.aud.includes(clientId))) {
throw new Error("Invalid audience");
}

Expand Down

0 comments on commit 9c0d8e8

Please sign in to comment.