What is the REST (or CLI) API for logging in to Amazon Cognito user pools

Rakib picture Rakib · Jun 21, 2016 · Viewed 14.4k times · Source

How do i make logins happen via Amazon Cognito REST APIs (for user pools) on platforms for which there is no official SDK? - Note that i am asking for user pools - not identity pools.


Synopsis


Amazon cognito provides 3 kinds of logins:

  • federated logins (creates identity pools) - using social connects like FB, Twitter, G+ etc
  • AWS managed logins (creates user pools) - using Amazon's own managed signup, signin, forgot password, reset password services
  • developer provided logins (my custom designed authentication service managed by myself)

I am using the second one (with User Pools)


Amazon cognito has several SDKs for android, iOS, javascript, Xamarin etc. Cognito also provides REST APIs for building on platforms other than those supported by official SDKs. I am building an app for a different platform and, hence, REST API is my only way as there is no official SDK for my platform.

The Cognito REST API provides various endpoints for 'sign up', 'forgot password', 'confirm verification' etc, but surprisingly, the REST API does not have any endpoint for simple signin / login.


From Cognito CLI API docs I have all the OFFICIAL CLI APIs necessary to "signup users", "confirm signups", "change passwords", "verify phone numbers", "forgot passwords" etc. Surprisingly there is no CLI API mentioned for LOGINs. I was hoping there should be some CLI API like "$ aws cognito-idp log-in" just like there is for "$ aws cognito-idp sign-up" or for "$ aws cognito-idp forgot-password" etc.


Also from this getting started tutorial it talks about "*what should be done with tokens received AFTER successful authentication of a user*". However, it doesn't talk about HOW TO make the successful authentication happen on the first place with Cognito User Pool APIs. Examples are available only for Android, iOS, javascript SDKs. There are no authentication examples available for platforms which do not have SDKs.


Hence, How do i make logins happen via Amazon Cognito REST APIs (for user pools) on platforms for which there is no official SDK?

Answer

andrewjj picture andrewjj · Nov 16, 2018

This curl command works for me:

curl -X POST --data @aws-auth-data.json \
-H 'X-Amz-Target: AWSCognitoIdentityProviderService.InitiateAuth' \
-H 'Content-Type: application/x-amz-json-1.1' \
https://cognito-idp.us-east-1.amazonaws.com/

Where aws-auth-data.json is:

{
   "AuthParameters" : {
      "USERNAME" : "[email protected]",
      "PASSWORD" : "yourpassword"
   },
   "AuthFlow" : "USER_PASSWORD_AUTH",
   "ClientId" : "75........................"
}

The user pool client must allow USER_PASSWORD_AUTH for this to work - that's an AWS-side setting.