javascript - AWS Lambda - Cognito Identity ID 使用相同的登录名更改

标签 javascript amazon-web-services aws-lambda amazon-cognito serverless-framework

我正在使用 AWS Lambda、Cognito 和 API 网关(与 Serverless 协调)为我的网络应用构建 API。

用户使用 Cognito 进行身份验证,然后向 API 发出经过身份验证的请求(从 Serverless Stack 教程复制的模式,我在其中获取他们的 Cognito ID:

event.requestContext.identity.cognitoIdentityId

然后我获取与该 cognitoIdentityId 关联的用户记录以执行基于 Angular 色/权限的逻辑并返回相关数据。

我遇到的问题是,当不同的人(目前与我一起工作的其他开发人员)使用相同的凭据登录,但来自不同的计算机(在某些情况下,国家/地区)时,cognitoIdentityId与他们的请求一起发送的完全不同——对于同一用户 userPool 用户记录!

Note: I am not integrating with any "Federated Identities" (ie, Facebook, etc). This is plain old email sign-in. And everyone is using the same creds, but some people's requests come from different Cognito IDs.

这是非常有问题的,因为我没有看到另一种方法来唯一标识我的数据库中与 Cognito 记录关联的用户记录。

QUESTIONS: Am I missing something? Is there a better way to do this? Is this the expected behavior?


API 当前没有实际插入到数据库中。因为我们的数据结构仍在不断变化,而且应用程序还远未上线,所以我构建了一个 API,它表现就像它与数据库集成一样,并返回数据,但数据只是存储在一个 JSON 文件中。如果相关,我将在下面复制一些相关代码。

一个示例 lambda,用于获取当前用户:

export function getSelf(event, context, callback) {
  const { cognitoID } = parser(event);

  const requester = cognitoID && users.find(u => u.cognitoID === cognitoID);

  try {
    if (requester) {
      return callback(null, success(prep(requester, 0)));
    } else {
      return authError(callback, `No user found with ID: ${cognitoID}`);
    }
  } catch (error) {
    return uncaughtError(callback, error);
  }
}

That parser stuff up top is just a util to get the ID I want.

关联的用户记录可能如下所示:

  {
    cognitoID: 'us-west-2:605249a8-8fc1-40ed-bf89-23bc74ecc232',
    id: 'some-slug',
    email: 'email@whatever.com',
    firstName: 'John',
    lastName: 'Jacob Jingleheimer Schmidt',
    headshot: 'http://fillmurray.com/g/300/300',
    role: 'admin'
  },

最佳答案

Cognito 用户池用于对用户进行身份验证并为您提供 JWT token 。当您想要访问任何 AWS 服务时,您需要 AWS 凭证(访问 key 和 secret key )。这是您应该使用联合身份的地方。您从 Cognito 用户池获得的 token 应与联合身份交换以获得 AWS 凭证以访问其他 AWS 服务。无服务器堆栈也在 detail 中介绍了这一点.

现在,由于您尚未将用户池添加为身份池中的身份验证提供程序,我观察到您正在从 Federated Identities 获取未经身份验证的身份(您可以从 Amazon Cognito 控制台确认这一点),这就是为什么每个团队成员都不同。您应该将用户池添加为身份池中的身份验证提供程序并遵循 documentation提供登录映射中所需的信息。

关于javascript - AWS Lambda - Cognito Identity ID 使用相同的登录名更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46833984/

相关文章:

Javascript: if (previous page == certainurl) { function }

javascript - 简单的 jQuery 自动完成示例在 ASP.NET 应用程序中不起作用

javascript - 如何将react-native-gifted-chat与cloud firestore一起使用?

javascript - 为什么我的 angular.js 应用程序不能在 ie7 上运行?

amazon-ec2 - 从私有(private) EBS AMI 启动实例后的 "Server Refused our key"

amazon-web-services - AWS Lambda > 达到并发限制时会发生什么

amazon-web-services - 云阵 : api-gateway with nested resources - 500 error during testing workflow

symfony - Amazon ElasticBeanstalk 中的单实例 cronjob

json - 删除 Lambda 支持的自定义资源的支持 Lambda 函数

amazon-web-services - 通过云形成创建并执行 aws lambda 函数