Plenty of pages out there describing custom login providers via Cognito Identity Pools but seemingly none that use User Pools!? So without further ado, here's how to create a basic grails page that authenticates a user from a Cognito User Pool.
Note: This post assume you have grails, gradle, etc.. installed on your PC, and that you have already created an AWS Account.
This is the pool of users who have access to your application. Head to Your Cognito Page and perform the following steps:
7. Click the Create app client button.
8. Click the Return to pool details link.
9. Click the Create pool button.
10. Take note of the Pool Id and Pool ARN strings at the top of the page (you will need these later).
11. Click the pen next to the App client you created in 1.7 then click the Show Details button.
12. Take note of the App client id and App client secret (you will need these later).
This user is internal and used to call the various authentication API's. This is known to you only (ie it's not the end user's login) and we'll attach policies to it to allow it to interact with Cognito. Head to Your IAM Policies Page and perform the following steps:
This user is internal and used to call the various authentication API's. This is known to you only (ie it's not the end user's login) and we'll attach policies to it to allow it to interact with Cognito. Head to Your IAM Users Page and perform the following steps:
3. Click the Next: Permissions button.
4. Click the Create group button.
5. Provide a Group Name.
6. Use the search field to find the policy created in step 3 and tick the checkbox next to it.
7. Click the Create group button.
8. Ensure the checkbox next to the new Group is selected and click Next: Tags.
9. Click the Next: Review button.
10. Review the user information entered and click Create user button.
11. IMPORTANT On the following screen, click the Show link and make note of the Access key ID and Secret access key strings (you will need these later).
12. Click close
$ grails create-app authTest| Application created at /tmp/authTestUse your favourite text editor to add the following to grails-app/conf/application.yml.
aws: userPool: poolId: <poolId from 1.10> authTest: clientId: <App client id from 1.7> clientSecret: <App client secret from 1.7>Add the following dependencies to the dependencies{} section:
compile "com.amazonaws:aws-java-sdk-cognitoidp:1.11.490" compile "software.amazon.awssdk:cognitoidentity:2.3.9"In the bootRun{} section, replace the default jvmArgs:
jvmArgs('-Dspring.output.ansi.enabled=always')with the following
jvmArgs = ["-Dspring.output.ansi.enabled=always","-Daws.accessKeyId=<access key id from 4.11>","-Daws.secretKey=<secret access key from 4.11> "]Modify grails-app/controllers/authtest/UrlMappings.groovy, replacing:
"/"(view:"/index")with
"/"(controller:"aws")Download and install the following controller grails-app/controllers/authtest/AwsController.groovy
Change the following lines:
Download and install the following view grails-app/views/aws/finish.gsp
Download and install the following view grails-app/views/aws/welcome.gsp
Execute gradle bootRun to startup the server. Navigate to http://localhost:8080/ and click the Login Test button.