Skip to content

Commit 693bb22

Browse files
liamdiprosedbkr
andauthored
Handle when aud OIDC claim is an Array (#4584)
* Handle when `aud` OIDC claim is an Array 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. * Clarify `aud` OIDC claim check * Fix for prettier --------- Co-authored-by: David Baker <[email protected]>
1 parent 315e81b commit 693bb22

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

spec/unit/oidc/validate.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,23 @@ describe("validateIdToken()", () => {
170170
expect(logger.error).toHaveBeenCalledWith("Invalid ID token", new Error("Invalid audience"));
171171
});
172172

173+
it("should not throw when audience is an array that includes clientId", () => {
174+
mocked(jwtDecode).mockReturnValue({
175+
...validDecodedIdToken,
176+
aud: [clientId],
177+
});
178+
expect(() => validateIdToken(idToken, issuer, clientId, nonce)).not.toThrow();
179+
});
180+
181+
it("should throw when audience is an array that does not include clientId", () => {
182+
mocked(jwtDecode).mockReturnValue({
183+
...validDecodedIdToken,
184+
aud: [`${clientId},uiop`, "asdf"],
185+
});
186+
expect(() => validateIdToken(idToken, issuer, clientId, nonce)).toThrow(new Error(OidcError.InvalidIdToken));
187+
expect(logger.error).toHaveBeenCalledWith("Invalid ID token", new Error("Invalid audience"));
188+
});
189+
173190
it("should throw when nonce does not match", () => {
174191
mocked(jwtDecode).mockReturnValue({
175192
...validDecodedIdToken,

src/oidc/validate.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ export const validateIdToken = (
179179
* 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.
180180
* EW: Don't accept tokens with other untrusted audiences
181181
* */
182-
if (claims.aud !== clientId) {
182+
const sanitisedAuds = typeof claims.aud === "string" ? [claims.aud] : claims.aud;
183+
if (!sanitisedAuds.includes(clientId)) {
183184
throw new Error("Invalid audience");
184185
}
185186

0 commit comments

Comments
 (0)