Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/shared/src/oidc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
});
Expand Down
10 changes: 8 additions & 2 deletions packages/shared/src/oidc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,17 @@ async function requestTokens(
});
const body = (await response.json().catch(() => ({}))) as Record<string, unknown>;
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") {
Expand Down
Loading