现在我的头脑正在从有关AWS的所有信息中爆炸。 :)所以我需要你的帮助。
我在AWS上有以下架构:API Gateway,Lambda,Cognito。我正在使用无服务器框架来配置所有内容。 在Lambda上部署了几个功能。假设API和函数类似于:
GET /user
GET /vehicle
POST /vehicle
我有两种类型的用户A和B.A是管理员,应该访问所有内容,但B仅限于某些路径,例如
GET /user >>> only A
GET /vehicle >>> B
POST /vehicle >>> only A
如何使用Cognito,API Gateway和Lambda实现此目的。
到目前为止我发现的信息/想法是:
哪种方式最好?或者有更好的解决方案吗? 不过,一个很好的例子会很好。到目前为止,我发现只有50行代码的示例,这些示例不适合我的情况。所以实际上我的问题可能是我不知道如何以及从哪里开始......
答案 0 :(得分:1)
这可能有点偏见,因为我过去曾使用自定义授权程序。 但它们可能是你提到的最灵活的解决方案。
在有认知授权人之前,我实际上已经将自定义授权程序与认知用户池一起使用了。
当https://aws.amazon.com/blogs/mobile/integrating-amazon-cognito-user-pools-with-api-gateway/帮助我开始做很多事情时。
后来我发现,在我进行大量验证后,我应该利用授权程序发送的策略的缓存,因为用户权限并没有经常改变。为此,我必须作为响应发送一个策略,该策略将涵盖经过身份验证的用户允许调用的所有端点。
编辑: 在自定义授权程序上,您可以check the user information by calling cognito。之后,如果用户是默认用户,您可以为其提供策略
{ "principalId": "yyyyyyyy", // The principal user identification associated with the token sent by the client. "policyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": "execute-api:Invoke",
"Effect": "Allow|Deny",
"Resource": "arn:aws:execute-api:{regionId}:{accountId}:{appId}/{stage}/GET/user"
}
] } }
对于管理员,您只需发送另一个政策
{
"principalId": "yyyyyyyy", // The principal user identification associated with the token sent by the client.
"policyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": "execute-api:Invoke",
"Effect": "Allow|Deny",
"Resource": "arn:aws:execute-api:{regionId}:{accountId}:{appId}/{stage}/GET/user"
},
{
"Action": "execute-api:Invoke",
"Effect": "Allow|Deny",
"Resource": "arn:aws:execute-api:{regionId}:{accountId}:{appId}/{stage}/*/vehicle"
}
]
}
}