简体   繁体   English

如何通过 Cognito 用户 ID 限制访问 AWS API 网关端点

[英]How to restricted to access AWS API Gateway endpoint by Cognito user id

I am trying to implement an e-commerce application backend by using AWS Cognito, Api-Gateway, and Lambda.我正在尝试使用 AWS Cognito、Api-Gateway 和 Lambda 来实现电子商务应用程序后端。

To create a new Item, users must be logged in. Once created the item, the Logged used will be the creator of the item.要创建新项目,用户必须登录。创建项目后,使用的登录者将成为项目的创建者。 I already created an Item-create endpoint and added the Cognito-JWT authorizer for the Apigateway endpoint.我已经创建了一个 Item-create 端点并为 Apigateway 端点添加了 Cognito-JWT 授权方。

Now I need to implement Item-update endpoint.现在我需要实现 Item-update 端点。 The relevant Item update must be allowed only for the creator of the Item.必须仅允许项目的创建者进行相关的项目更新。 API maybe like this. API 可能是这样的。

/items/{item-id} PUT
body : { title, price... }
header { Authorization: Cognito-JWT-Token }

What is the best way to implement this kind of feature using the AWS ecosystem.使用 AWS 生态系统实现此类功能的最佳方式是什么。 Or any best practices to implement this kind of feature.或实现此类功能的任何最佳实践。

You would create a property owner for each item that is a user ID.您将为作为用户 ID 的每个项目创建一个财产owner Normally you'd use the sub claim from the token which is a unique ID.通常你会使用来自令牌的sub声明,它是一个唯一的 ID。 However, for various reasons, I wouldn't recommend using the subject value with cognito.但是,出于各种原因,我不建议将主题值与 cognito 一起使用。 You can use email just ensure safeguards are in place if someone deletes their account and a new one is created with the same email.您可以使用 email 确保在有人删除他们的帐户并使用相同的 email 创建新帐户时采取保护措施。

Whatever you choose, in your handler compare the owner with the validated token claim.无论您选择什么,在您的处理程序中将所有者与经过验证的令牌声明进行比较。 Allow if they match, disallow if they don't.如果匹配则允许,如果不匹配则禁止。

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

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