-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature Request] REST API with Cognito User Pool authorization #345
Comments
@lawmicha thanks for opening the issue, I see the customer need here and this is something the CLI could support out of the box. I take this to the team for discussion and get back to you. |
I agree this is really important. API+Lambda+Cognito did not work out of the box for me! |
I need this feature. Cognito User Pool Authorization provides us to powerful and useful permission management. |
I spent hours banging my head on this and went through multiple tutorials and digging through code. Thank you for such a great walk-thru and this needs to be added! |
Great walk through. Thanks a lot. Hope this automation is added soon. for such an important feature. It is essentially the same as AdminQueries API. It should be easy for the Amplify team to copy what has done over. |
@lawmicha I wondered if something may have changed or if perhaps a step is missing. I have tried a couple times, at first with the auth I already had set up and the removed all the api, functions & auth and tried again and it doesn't seem to generate I had previously tried manually adding the Cognito Authorizer in another project and realized when I ran push again for some changes the Authorizer got trashed, so I'm hoping this will solve that. Thanks for any info you can provide. |
Hi @smakinson,
This sounds like expected behavior but also a problem if |
@lawmicha Thanks for your response, you are correct, it is a javascript project. Hoping to find a way around it losing the Authorizer within Amplify.
so it seems that is not the file I want. I'll keep digging. |
@smakinson I just want to note that this is a feature request, and not necessarily steps for developers to follow and get working, they are steps provided to assist in turning this into an Amplify CLI feature. Following these steps are not supported by Amplify CLI. As we've seen the issue with the configuration file being overwritten. |
@lawmicha Incase this is helpful for others I thought I'd post up here what I am looking at doing with the cloudformation template, and if you have a moment I have 2 questions, one general and the other (without taking too much of your time) if you might know the fix for an error:
in the
the methods have the authorizer attached by adding the following under
and further down in the file I'm trying to reference it like:
Next I ran The error in question is the one I see when I run
Maybe I'm just mistyping something, hopefully this can be helpful for anyone in a javascript amplify project until the cli supports it. Thanks for any insight. |
Thanks @smakinson for posting your details. I'm not very familar with those files you are asking about in your questions. + @SwaySway et al to help answer |
Hello @smakinson,
The docs here specify a way to get the user pool id into another stack. What you are looking for is the UserPoolArn. For that you'll need to import will be different, I've detailed this below. You'll need to use a different ref for the ARN such as "authMyAuthResourceNameUserPoolArn": {
"Type": "String"
} In "authMyAuthResourceNameUserPoolArn": {
"Fn::GetAtt": [
"MyAuthResourceName",
"Outputs.UserPoolArn",
"Arn"
]
} Currently arn is not included in the outputs of the auth stack, this is something we can consider as a part of this feature request. For this you'll want to export the Arn as well from the auth stack. UserPoolArn:
Value: !GetAtt UserPool.Arn
Description: Arn for cognito userpool The last thing here to edit would be to change the "api": {
"demorest": {
"service": "API Gateway",
"providerPlugin": "awscloudformation",
"dependsOn": [
{
"category": "function",
"resourceName": "demoRestFN",
"attributes": [
"Name",
"Arn"
]
},
{
"category": "auth",
"resourceName": "MyAuthResourceName",
"attributes": [
"UserPoolArn"
]
}
]
}
} All that's left here is to run |
@SwaySway Thank you for your help. I currently have manually setup an api for this, do you think its worth (risky?) going this route or would it be better to wait on the feature request and change it then? |
This is a great workaround for now for creating Cognito Authorizers. Perhaps in a similiar vein, Is it safe to edit lambdaFunction-cloudformation-template.json? When does it get overwritten ? If I needed to add custom resources (ie a lambda policy), is the recommended approach to modify this cfn template ? Im looking for an analogy to the CustomResources.json template in the api/stacks folder. |
I'm trying to accomplish the same thing (calling API GW with Cognito Auth) and made it work with above method. However: my call only succeeds if I use an ID Token to authenticate:
It's not working with an access token:
Fails with 401 and body I'd expect an access token to work instead of an id token. Is there anything I'm doing wrong? |
What's the latest status on this? Been scratching my head for hours trying to figure out why this was not working out of the box. The latest CLI makes you go through steps to set it all up, but does not build a CloudFormation template that actually works. Again, it is blowing my mind how often I have to fix this stuff myself in vs having the Amplify CLI do what it is supposed to do. Does the Amplify product team not have any QA engineers? |
I got this working using @lawmicha 's steps and @jwoehrle 's code. To my understanding, I'm going to have to follow these steps every time we deploy (though I haven't tested this part yet). Is there any other workaround that will work automatically? Edit: It is working well, but does require this configuration to be set every time the API is updated from the amplify CLI. |
Any news on getting this implemented? Overriding the amplify config manually is a time-consuming process for a feature that should already be there. |
Here's from the documentation. With the COGNITO_USER_POOLS authorizer, if the OAuth Scopes option isn't specified, API Gateway treats the supplied token as an identity token and verifies the claimed identity against the one from the user pool. Otherwise, API Gateway treats the supplied token as an access token and verifies the access scopes that are claimed in the token against the authorization scopes declared on the method. My last comment got deleted, pretty good Team. |
Is this being worked on? This seems like a very important feature and i was surprised when it did not work out of the box. Have been trying to solve an issue for some hours now and stumbled upon this now |
This would be a very useful feature! For now, we have to set the authorizer for all resources after every "amplify update api", because amplify overwrites the template file : ( |
@johnEthicalTechnology Thank you so much for this. Could you also explain how to restrict the api access? |
The above solution no longer works due to some changes in how cloud formation templates are generated / overridden in AWS Amplify (which is now done with CDK instead of directly modifying the cloud formation template). I threw together a gist for an https://gist.github.com/evankirkiles/9de81f026a2e2c961a2b6a3d80d35519 Feel free to recommend changes! |
@evankirkiles Thanks for sharing! Your gist worked great for me. |
Hello, I've used this manual to configure Cognito User Pool authorizer, but during the deployment, I'm getting this error:
I did not forget to replace your auth name here :) Any thoughts? And one more question, we can make the function private from CLI. Then, there is information about the user in event.requestContext.identity. Why not use it? |
shit, every time I add new API path I need to recreate the Authorizer & redeploy the api kinda a pain when quickly prototyping project :( |
Hi @shkomg I'm experiencing exactly the same problem. If override I get the same errors @sshvaiko had. UPDATE: It effectively works using Amplify-CLI 9.1.0 but in my opinion could lead to security issues. Workarounds could be to modify the script or to add a env. var. for example. |
Hi @rondondaniel I definitely expect it working out of the box with default CLI prompts and waiting for Amplify team to resolve this. Meanwhile for me updating over the API Gateway once in while isn't perfect but Ok. It just simpler to remember VS changes in code, at least for me :) |
@sshvaiko @rondondaniel no need to manually update the values. That's the whole idea of the script, right :| |
I had the same issue as @shkomg and @rondondaniel. |
It is pretty bad that Amplify does not support this out of box In a word, there are 3 steps to achieve this. Step1: Step2:
Step3: Update config applying the above CloudFormation
|
I have the same experience and the same frustrations. I think it has less to do with the product team than what I expect is a disconnect between how executives have positioned Amplify, and what the team is actually resourced and directed to do. Mismanagement in other words. |
It has been 3 years since this feature was requested and no updates yet. It is very disappointing to see the Amplify team not taking any action on this. |
As near as I can tell Amplify is dead. The whole thing is riddled with fatal bugs that eventually crop up and wreck you project. I regret ever using it. |
Still not fixed. |
OMG just came to add a simplay lambda with Cognito auth and see the hoops you've got to jump through. What a pile of junk |
Nothing in my software experience has wasted more of my time than Amplify. Use Heroku. |
I can suggest an easier solution for NodeJS, you can use this library https://github.com/awslabs/aws-jwt-verify in the lambda function itself. |
Hi guys, for who are still looking for solutions of adding authorizers, here's the link I found in the amplify doc, which shows how to add cognito user pool authorizer or custom lambda authorizer : https://docs.amplify.aws/javascript/build-a-backend/restapi/override-api-gateway/ |
Is your feature request related to a problem? Please describe.
Amplify CLI provides a way to create a REST api (API Gateway + lambda) with IAM authorization. in the JS docs, there are manual steps to add cognito user pool as the authorizator for the requests to API gateway: https://aws-amplify.github.io/docs/js/api#cognito-user-pools-authorization (however, this could be improved, see step 11 below). Amplify iOS (aws-amplify/amplify-swift#312) and Android also supports this API with user pools as the auth mechanism.
The pain point is here is that Amplify CLI doesn't support creating API Gateway + Cognito User Pool authorizator. I'm opening this issue here to provide more details around what needs to done to enable API Gateway with Cognito.
The following steps show how to set up an API endpoint with APIGateway and Lambda source. The auth configured will be Cognito User Pool.
Initialize an amplify project.
amplify init
Create an API Gateway which proxies requests to an AWS Lambda with no authorization needed.
amplify add api
.amplify add auth
Provision the resources. Run
amplify push
to provision the API Gateway, Lambda, and the Cognito User Pool.In
amplifyconfiguration.json
. updateauthorizationType
toAMAZON_COGNITO_USER_POOLS
like soFind your API name. Run
amplify console
to open the AWS Console. The latest deployment activty logs will indicate the API Gateway that is provisioned. There will be a Resource ID that looks like<api name> (api)
. Navigate to API Gateway console, select your API.Find your Cognito User Pool name by click on the Authentication tab in the AWS Console.
Add Cognito User Pool as an authorization mechanism. Select Authorizers, click on "+ Create New Authorizer",
Cognito
as the typeAuthorization
Any
. You will see a Test section, Method Request, Method Response, Integration Request, etcThere are some additional gotcha's that need to be noted here:
amplify push
, so developer will need to programmatically instantiate Amplify.Describe the solution you'd like
A clear and concise description of what you want to happen.
amplify add api
Alternatives
JS docs could use some improvements with step number 11, for example it doesn't guide the develop to re-deploy the API. iOS and Android Amplify docs will be written simiarly.
Dedupped to
aws-amplify/amplify-cli#3058
#438 (Just realized this one is for API + API Key, not user pool)
The text was updated successfully, but these errors were encountered: