跳至內容

Amplify Auth not handling authorization code from Entra redirect (manual token exchange required)

0

I’ve integrated Entra with Cognito. When I click on my app for SSO, it redirects to my login page with an authorization code in the URL.

On the login page, I currently have to manually extract the code from the URL and make an Axios call to the Cognito endpoint to exchange it for access, refresh, and ID tokens.

I tried using signInWithRedirect, but it’s not working as expected—it prompts me to log in again instead of handling the existing authorization code.

Is there any built-in method in Amplify Auth or amplify/ui-angular that can automatically handle this code exchange and token retrieval, instead of manually calling the Cognito API every time?

已提問 2 個月前檢視次數 53 次

1 個回答
4
已接受的答案

"The short answer is: Amplify is designed for SP-initiated flows, meaning the process must start within your app via signInWithRedirect(). Amplify won't automatically 'pick up' a code it didn't request itself due to OAuth2 security standards (PKCE)."

When you arrive at your app with a code already in the URL (likely an IdP-initiated flow from Entra), Amplify’s internal OAuth handler ignores it because it lacks the corresponding 'state' and 'PKCE verifier' that it would have created if it had started the request itself.

Is there a built-in method?

Not specifically for a 'pre-existing' code. However, you have two options to avoid manual Axios calls:

  1. The 'Amplify Way' (Recommended): Don't redirect from Entra with a code directly. Instead, redirect the user to your app's login page and call signInWithRedirect({ provider: 'IdentityServer' }) (your Entra provider name). This ensures Amplify manages the PKCE flow and token exchange automatically.

  2. The Custom Hub Listener: If you must handle an external code, you are already doing the 'correct' workaround by exchanging it manually. Amplify's Auth category doesn't have a handleCode(code) method because it needs to validate the session state for security.

see also:

專家

已回答 2 個月前

專家

已審閱 2 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。