diff --git a/packages/shared/src/oidc.test.ts b/packages/shared/src/oidc.test.ts index 58e1bd99b5b81..e115a42cb1a63 100644 --- a/packages/shared/src/oidc.test.ts +++ b/packages/shared/src/oidc.test.ts @@ -185,7 +185,11 @@ describe("token requests", () => { metadata, clientId: "t3-code", refreshToken: "refresh-1", - fetch: async () => jsonResponse({ error: "invalid_grant" }, 400), + fetch: async () => + jsonResponse( + { error: "invalid_grant", error_description: "AADSTS70000: The grant has expired." }, + 400, + ), }).catch((error: unknown) => error); const unavailable = await refreshOidcTokens({ metadata, @@ -194,6 +198,10 @@ describe("token requests", () => { fetch: async () => jsonResponse({}, 503), }).catch((error: unknown) => error); + expect(rejected).toMatchObject({ + oauthError: "invalid_grant", + message: "The token request failed with 400: AADSTS70000: The grant has expired.", + }); expect(isPermanentRefreshFailure(rejected)).toBe(true); expect(isPermanentRefreshFailure(unavailable)).toBe(false); }); diff --git a/packages/shared/src/oidc.ts b/packages/shared/src/oidc.ts index 2f289fcae1dc5..5c2d666735259 100644 --- a/packages/shared/src/oidc.ts +++ b/packages/shared/src/oidc.ts @@ -253,11 +253,17 @@ async function requestTokens( }); const body = (await response.json().catch(() => ({}))) as Record; if (!response.ok) { + const oauthError = typeof body.error === "string" ? body.error : undefined; + const reason = + (typeof body.error_description === "string" ? body.error_description.trim() : "") || + oauthError; throw new OidcError({ code: "token_request_failed", - message: `The token request failed with ${response.status}.`, + message: reason + ? `The token request failed with ${response.status}: ${reason}` + : `The token request failed with ${response.status}.`, status: response.status, - ...(typeof body.error === "string" ? { oauthError: body.error } : {}), + ...(oauthError === undefined ? {} : { oauthError }), }); } if (typeof body.id_token !== "string") {