Skip to content

Managed Login with Cognito and PKCE flow

0

I am working with a new Cognito User pool and app and implementing the login flow using PKCE flow so I do not have to expose the client secret.

i am using the new Managed Login in my application. I verified the auth code and the codeVerifier before sending request to retrieve tokens. However, I get a 400 saying the request in not formed correctly. Even thought I have verified header and body. When i add the client_secret to the request it works. I have enabled Authorization code grant udner the Managed Login Config for my client. What is wrong, does Managed Login Support PKCE flow ? Is there a configuration i am missing?

2 Answers
0

Yes, Amazon Cognito's Managed Login does support the PKCE (Proof Key for Code Exchange) flow, which is recommended for single-page applications and mobile applications where you don't want to expose a client secret.

For your issue, there are a few things to check:

  1. Make sure you've properly configured your app client to allow the authorization code grant with PKCE. This should be enabled in your app client settings.

  2. When making the initial authorization request to the /oauth2/authorize endpoint, ensure you're including:

  • code_challenge parameter (the base64 URL-encoded SHA-256 hash of your code verifier)
  • code_challenge_method=S256 parameter
  1. When exchanging the authorization code for tokens at the /oauth2/token endpoint, make sure you're including:
  • The code_verifier parameter (the original random string you generated)
  • The code parameter (the authorization code you received)
  • The grant_type=authorization_code parameter
  • The client_id parameter
  • The redirect_uri parameter (must match the one used in the authorization request)

The fact that adding the client secret makes it work suggests that your app client might be configured as a confidential client rather than a public client. For PKCE flow, you should configure your app client as a public client that doesn't require a client secret. Check your app client configuration to ensure it's set up correctly for PKCE.

If you've verified all these settings and are still experiencing issues, double-check the format of your token endpoint request, ensuring all parameters are correctly formatted and encoded.
Sources
Using PKCE in authorization code grants - Amazon Cognito
The redirect and authorization endpoint - Amazon Cognito
How authentication works with Amazon Cognito - Amazon Cognito
Choose an Amazon Cognito authentication flow for enterprise applications - AWS Prescriptive Guidance

answered a year ago

0

Cognito's Managed Login does support PKCE flow, but there are specific configuration requirements that might be causing your 400 error. Make sure you have setup the follow config properly:

  1. Client Configuration: • Enable "Authorization code grant" • Set the client to "Public client" (not Confidential client which maybe the issue in your case)

  2. Cross check if your token exchange request includes:

=================================

POST /oauth2/token Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code &client_id=<your_client_id> &code=<authorization_code> &redirect_uri=<same_redirect_uri_used_in_auth_request> &code_verifier=<your_code_verifier>

=================================

Notes:

  1. If your client is configured as "Confidential," it will always require client_secret, even with PKCE
  2. Ensure your initial authorization request included code_challenge and code_challenge_method=S256
  3. The redirect_uri in token request must exactly match the one used in authorization request
AWS

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.