Use Cognito as the Authorizer to add user authentication to the previous one. The finished product looks like this. The User will be authenticated by her Cognito and will be able to call the API or sign up herself.
"Try new user registration & sign-in with Cognito without programming" There is an easy-to-understand article, so it is good to execute it as it is. Many Cognito samples start by writing a sample client, but this article should use his AWS CLI to understand the basic setup and behavior without any app code.
I will try it
--In Cognito user pool
> create user pool
> check default
――There are various setting items, but most of them can be left as default. Only the following two points should be able to be done properly
--Uncheck Create Client Secret
under App Client
. Only here, after creating the user pool, it will not appear in the UI and can not be changed [^ 2]
--Cognito
> User pool
> App client
> Authentication flow settings`` Enable user name password authentication of management API for authentication (check ALLOW_ADMIN_USER_PASSWORD_AUTH)
) [^ 1]
--If you create a pool, these two will be assigned, so make a note of it. -** Pool ID ** us-east-2_BLQx42sOb -** App client ID ** 6m2vpnxplvjpgm7sasgj1n1qe2
[^ 1]: If you don't do this, when you try to sign in with the CLI An error occurred (InvalidParameterException) when calling the AdminInitiateAuth operation: Auth flow not enabled for this client
[^ 2]: If you don't do this, you will get angry when you sign up the user with CLI An error occurred (NotAuthorizedException) when calling the SignUp operation: Unable to verify secret hash for client <CLIENT_ID>
After creating the pool, create a user with General Settings
> Users and Groups
and set the Account Status
to CONFIRMED
.
Put the necessary parameters in the environment variables
export AWS_USER_POOL_ID=us-east-2_BLQx42sOb
export AWS_CLIENT_ID=6m2vpnxplvjpgm7sasgj1n1qe2
export [email protected]
export AWS_PASSWORD=aLoo4@ni8
log in
cognito-signin.sh
aws cognito-idp admin-initiate-auth \
--user-pool-id ${AWS_USER_POOL_ID} \
--client-id ${AWS_CLIENT_ID} \
--auth-flow ADMIN_NO_SRP_AUTH \
--auth-parameters "USERNAME=${AWS_USER_EMAIL},PASSWORD=${AWS_PASSWORD}"
Tokens etc. will come back
{
"ChallengeParameters": {},
"AuthenticationResult": {
"AccessToken": "eyJraWQiOiJ2XC82ZVI2UEYraFwvSHR5cGFjYWV2a20ydHhHZWx0QUFObzBSUlVMZnpscDg9IiwiYWxnIjoiUlMyNTYifQ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ydXNhbkBtYWMuY29tIn0.s3Dfkiinhfxog0LheQXfX7jVEHUJGcC7b-8XwAegqnGcYTbt9EsJW9HJtW3q1owjBiMkPXKwJ1_hEjg2iTwYnqu1EYXEzWtgOj8n4X1iM2TYO-9ra1zmfvbJ7X3uTC8F4msPfLklqGDq34UXAY_vf-E0VbPyxBXllTzi2FY-3oFiAyxkGA1n8KqJ7AhobtxMnSOSjHoni78q_uxExZJ2PxZ0ruSg-hsKwfKlN6Pf593WHpeSC2RK_gnCr3qhzC1lRbv9_hUWbALXqMl3vowMLz9k4IV_oC9BU07LLIExR7wmI2_ZmTa6ts8TDATolcIDXNK-cBtsHn97aBul747XgQ"
}
}
All you need is the token, so in the query
TOKEN=$(aws cognito-idp admin-initiate-auth \
--user-pool-id ${AWS_USER_POOL_ID} \
--client-id ${AWS_CLIENT_ID} \
--auth-flow ADMIN_NO_SRP_AUTH \
--auth-parameters "USERNAME=${AWS_USER_EMAIL},PASSWORD=${AWS_PASSWORD}" \
--query "AuthenticationResult.IdToken" | sed "s/\"//g") && echo ${ID_TOKEN}
Now you can get tokens with $ TOKEN
Specify the user pool created above with the authorizer
of API Gateway
Don't forget to deploy the API
As before, put the token in $ TOKEN
and hit the API endpoint
curl -H "Authorization: $TOKEN" https://<api-id>.execute-api.us-east-2.amazonaws.com/dev_1/projects | jq
curl -X POST -d '{"project-id": 200, "name": "Typing", "is-default": false}' -H "Authorization: $TOKEN" https://<api-id>.execute-api.us-east-2.amazonaws.com/dev_1/projects | jq
curl -X DELETE -d '{"project-id": 200}' -H "Authorization: $TOKEN" https://<api-id>.execute-api.us-east-2.amazonaws.com/dev_1/projects | jq
curl -X PUT -d '{"project-id": 200, "name": "Yoga"}' -H "Authorization: $TOKEN" https://<api-id>.execute-api.us-east-2.amazonaws.com/dev_1/projects | jq
It worked!
It worked with the CLI, so let's finally write the front-end client next time.