简体   繁体   English

AWS api网关和cognito集成

[英]AWS api gateway and cognito integration

I am creating an api and I only want it accessible to authenticated users in my identity pool. 我正在创建一个api,我只希望它可以访问我的身份池中经过身份验证的用户。 The api calls a lambda function that returns "hello world". api调用lambda函数返回“hello world”。 When I set no authentication in my api it works fine, but when I create a custom authorizer and set that as my authentication method for my api it returns null. 当我在我的api中没有设置身份验证时,它工作正常,但是当我创建自定义授权程序并将其设置为我的api的身份验证方法时,它返回null。

This is the link I used to help create my custom authenticator 这是我用来帮助创建自定义身份验证器的链接

Here is a list of stuff I did: 这是我做的事情清单:

  1. I am using a Federate identity and made sure I copied the identity pool ID and region properly in the authorizer.js. 我使用的是Federate标识,并确保我在authorizer.js中正确复制了标识池ID和区域。

  2. I added the Authenticated role, set up in my federate identity, ARN's into the execution role when creating my custom authorizer (not sure if I was suppose to do this). 我在创建自定义授权程序时添加了Authenticated角色,在我的联邦身份中设置,ARN成为执行角色(不确定我是否假设这样做)。

  3. In my Identity Access Management I attached the AmazonAPIGatewayInvokeFullAccess policy to the Cognito Authorization role. 在我的身份访问管理中,我将AmazonAPIGatewayInvokeFullAccess策略附加到Cognito授权角色。

  4. When I passed no headers and made the api call I get an unauthorized message. 当我没有传递任何标题并进行api调用时,我收到了未经授权的消息。

  5. When I passed a fake token in the header and made the api call I get a null message. 当我在标题中传递假令牌并进行api调用时,我收到一条空消息。

  6. When I passed the token provided by AWS in the header and made the api call I get a null message. 当我在标头中传递AWS提供的令牌并进行api调用时,我收到一条空消息。

  7. When I tried testing the api call in AWS I get a return status of 200 and the "Hello World" message 当我尝试在AWS中测试api调用时,返回状态为200,并显示“Hello World”消息

Anyone know what the problem is and how to fix it? 任何人都知道问题是什么以及如何解决它?

-Update- -Update-
Here are the areas I modified from the authorizer.js file from the link. 以下是我从链接中的authorizer.js文件修改的区域。

console.log('Loading function');

var jwt = require('jsonwebtoken'); 
var request = require('request'); 
var jwkToPem = require('jwk-to-pem');

var userPoolId = '{REPLACE_WITH_YOUR_POOL_ID}';
var region = '{REPLACE_WITH_YOUR_REGION}'; //e.g. us-east-1
var iss = 'https://cognito-idp.' + region + '.amazonaws.com/' + userPoolId;
var pems;

The only thing I did was added my cognate userPoolId and region. 我唯一做的就是添加了我的同源userPoolId和region。

I'm just starting out with this so I may be wrong. 我刚刚开始这样做,所以我可能错了。

I think it is important in this scenario to recognize the fact that there are two separate services provided by AWS: 我认为在这种情况下,重要的是要认识到AWS提供了两个独立的服务:

  1. User Pools--An Identity Provider 用户池 - 身份提供商
  2. Identity Pools--Federated Identity Management Service 身份池 - 联合身份管理服务

Importantly User Pools can be used as an Identity Provider for Identity Pools . 重要的是, 用户池可以用作身份池的身份提供者。

Now regarding your problem, you'll notice the link you referenced is for setting up a custom authorizer for User Pools . 现在关于您的问题,您会注意到您引用的链接是为用户池设置自定义授权程序。 Passing the token you receive from Facebook won't work with the code you have. 传递从Facebook收到的令牌将无法使用您拥有的代码。 It would need to be code for validating tokens from Facebook. 它需要是用于验证来自Facebook的令牌的代码。

If you are interested in using API Gateway with Identity Pools, then you would need to use the Identity Pool sdk to generate a temporary access token which can then be used for interacting with your endpoints. 如果您对使用带有标识池的API网关感兴趣,则需要使用标识池sdk生成临时访问令牌,然后可以使用该令牌与端点进行交互。

Instead of using a custom authorizer you would set the authorization settings for your endpoints to use AWS_IAM . 您可以设置端点的授权设置以使用AWS_IAM而不是使用自定义授权AWS_IAM

I think the issues you are running into are in large part a result of poor marketing and documentation on Amazon's part. 我认为您遇到的问题在很大程度上是由于亚马逊的营销和文档不佳造成的。 Categorizing the two services (User Pools and Identity Pools) under "Cognito" makes things way more confusing than if they had treated them as the two separate services they really are. 对“Cognito”下的两个服务(用户池和标识池)进行分类会比将它们视为真正的两个独立服务更加混乱。 Often times documentation or marketing will say Cognito can do this or that, making it unclear which one is providing which functionality. 通常情况下,文档或市场营销会说Cognito可以做到这一点或那样,使得不清楚哪一个提供哪种功能。 Calling them both pools just makes things even worse. 将它们称为两个池只会让事情变得更糟。

UPDATE: Some resources that go into detail about how to work with API Gateway and Cognito: 更新:一些资源详细介绍了如何使用API​​ Gateway和Cognito:

API Gateway recently launched first-party support for Cognito User Pools. API Gateway最近推出了对Cognito用户池的第一方支持。 You shouldn't have to setup a custom authorizer using Lambda anymore. 您不必再使用Lambda设置自定义授权程序。 Just setup a User Pool Authorizer in API Gateway and use it to authenticate users from your pool. 只需在API网关中设置用户池授权程序,并使用它来验证池中的用户。 Details on setting that up can be found here . 有关设置的详细信息,请访问此处

Hope this helps, Ritisha. 希望这有帮助,Ritisha。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM