Amazon web services 使用Cognito与IAM和Amplify进行授权

Amazon web services 使用Cognito与IAM和Amplify进行授权,amazon-web-services,aws-api-gateway,amazon-cognito,amazon-iam,aws-amplify,Amazon Web Services,Aws Api Gateway,Amazon Cognito,Amazon Iam,Aws Amplify,我正在尝试创建一个应用程序,用户可以在其中登录,然后根据其角色访问某些api资源 用户被移动到用户池中的组中,每个组都有一个自定义IAM角色 我曾经使用CLI工具来创建身份验证、api网关和lambda函数。 这是在amplify中设置的API ? Please select from one of the below mentioned services: REST ? Please select the REST API you would want to update standardAp

我正在尝试创建一个应用程序,用户可以在其中登录,然后根据其角色访问某些api资源

用户被移动到用户池中的组中,每个组都有一个自定义IAM角色

我曾经使用CLI工具来创建身份验证、api网关和lambda函数。 这是在amplify中设置的API

? Please select from one of the below mentioned services: REST
? Please select the REST API you would want to update standardApi
? What would you like to do Update path
? Please select the path you would want to edit /std
? Provide a path (e.g., /book/{isbn}): /std
? Choose a Lambda source Use a Lambda function already added in the current Ampl
ify project
? Choose the Lambda function to invoke by this path standardFunction
? Restrict API access Yes
? Restrict access by? Individual Groups
? Select groups: standard
? What kind of access do you want for standard users? create, read, update, dele
te
Successfully updated resource
在控制台中签入时,将按预期创建资源

问题是,在尝试调用API时,我收到了MissingAuthenticationTokenException

这是电话

const apiName = 'standardApi';
const path = '/std';
const myInit = {
    headers: {},
    response: false
};
API.get(apiName, path, myInit)
.then(response => {
    console.log(response);
})
.catch(error => {
    console.log(error.response);
});
我的理解是Amplify sdk应该自动使用正确的身份验证值填充请求头

如果我尝试手动传入访问令牌

headers: {
    Authorization: (await Auth.currentSession()).getIdToken().getJwtToken()
},
我收到不完整的SignatureException消息

Authorization header requires 'Credential' parameter. Authorization header requires 'Signature' parameter. Authorization header requires 'SignedHeaders' parameter. Authorization header requires existence of either a 'X-Amz-Date' or a 'Date' header.
尝试

headers: {
    Authorization: `Bearer ${(await Auth.currentSession()).getIdToken().getJwtToken()}
},
导致不完整的SignatureException,没有消息

其他帖子建议可能没有正确指定端点,如果我从控制台中的api中删除auth并重新部署,端点将按预期命中。此外,如果我使用cognito用户池创建一个自定义授权器,那么在登录时会正确命中端点,并传递一个承载令牌

只是IAM的案例不起作用

检查ID令牌表明传入了正确的用户名、组和角色

"cognito:groups": Array [ "standard" ]
"cognito:preferred_role": "arn:aws:iam::************:role/region-*_*********-standardGroupRole"
"cognito:roles": Array [ "arn:aws:iam::************:role/region-*_*********-standardGroupRole"]
"cognito:username": "0a******-****-****-****-************"

正在使用自动生成的aws导出文件执行放大配置步骤,该文件包含用户池、标识池和客户端应用程序的正确条目。

我知道这可能有点晚,但我只想做与您尝试做的相同的事情

这就是我要做的,让它工作:

  • 在UI中包括身份验证组件,以便使用经过身份验证的用户的凭据完成API调用
  • 确保在对Cognito进行身份验证后(使用属于指定组的用户)执行API调用
  • 不要包含任何“授权”标题(我查看了代码,发现设置授权标题时不会生成AWS签名)
  • 确保您的lambda代码返回CORS头(因此,我在CloudFront中遇到了InternalServerErrorException)
执行此操作时,我可以在浏览器的开发人员控制台中看到请求已签名,并且具有API网关进行IAM授权验证所需的所有标头

我希望这朵云对某些人有用,因为我花了很多时间才弄明白

问候,,
Andres Llanos

您必须按照[docs][1][1]:@OscarNevarez中的说明签署您的请求,谢谢。看起来它能满足我的需要。我最终使用了lambda授权人来完成这项工作。