Skip to content

Commit a05451d

Browse files
kwilczThangHuuVu
andauthored
feat(providers): Improve error validation for Microsoft EntraID provider errors before decoding token (#12876)
* Check for EntraID error before decoding token * Improve error message for better logs * Apply suggestions from code review --------- Co-authored-by: Thang Vu <[email protected]>
1 parent d935665 commit a05451d

File tree

1 file changed

+16
-6
lines changed
  • packages/core/src/lib/actions/callback/oauth

1 file changed

+16
-6
lines changed

packages/core/src/lib/actions/callback/oauth/callback.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,23 @@ export async function handleOAuth(
190190
case "microsoft-entra-id":
191191
case "azure-ad": {
192192
/**
193-
* These providers need the authorization server metadata to be re-processed
194-
* based on the `id_token`'s `tid` claim
195-
* @see https://github.com/MicrosoftDocs/azure-docs/issues/113944
193+
* These providers return errors in the response body and
194+
* need the authorization server metadata to be re-processed
195+
* based on the `id_token`'s `tid` claim.
196+
* @see: https://learn.microsoft.com/en-us/entra/identity-platform/v2-oauth2-auth-code-flow#error-response-1
196197
*/
197-
const { tid } = decodeJwt(
198-
(await codeGrantResponse.clone().json()).id_token
199-
)
198+
const responseJson = await codeGrantResponse.clone().json()
199+
if (responseJson.error) {
200+
const cause = {
201+
providerId: provider.id,
202+
...responseJson,
203+
}
204+
throw new OAuthCallbackError(
205+
`OAuth Provider returned an error: ${responseJson.error}`,
206+
cause
207+
)
208+
}
209+
const { tid } = decodeJwt(responseJson.id_token)
200210
if (typeof tid === "string") {
201211
const tenantRe = /microsoftonline\.com\/(\w+)\/v2\.0/
202212
const tenantId = as.issuer?.match(tenantRe)?.[1] ?? "common"

0 commit comments

Comments
 (0)