AWS Cognito + API 网关,基于用户名授权
AWS Cognito + API Gateway, Authorize based on username
我有一个 API 网关 HTTP API。对于某些路由,我附加了一个 Cognito 用户池授权方,它运行良好。但是现在我想对授权添加一个限制,以便只有具有正确用户名的用户才能访问某个 API.
比如路由是PUT /users/{username}
,我只希望对应用户名的用户能够编辑his/her个人资料信息。对于其他用户,应该是未授权的。
这似乎是一个常见的用例,我该怎么做?我研究了示波器,但这似乎不是解决方案。我是否必须为此编写自定义 Lambda 授权方,而不是使用 Cognito 授权方?如果可以,您能否提供一个示例,因为我没有编写 Lambda 授权程序的经验。
您可以使用映射模板在 API 网关中构建请求,并从那里使用存储在 Cognito 令牌中的信息 https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#context-variable-reference。
对于您对 /users/{username}
的特定请求,您可以使用 if 仅在用户匹配 How to use IF condition in AWS Api gateway mapping templates. can i use if condition without using foreach? 时提供所需信息。
或者,您可以简化 PUT 到 /users 的路径,然后从 cognito 令牌中提取用户名并在映射模板中使用它 How can I access the Cognito username of the caller in a Lamda function?。
我有一个 API 网关 HTTP API。对于某些路由,我附加了一个 Cognito 用户池授权方,它运行良好。但是现在我想对授权添加一个限制,以便只有具有正确用户名的用户才能访问某个 API.
比如路由是PUT /users/{username}
,我只希望对应用户名的用户能够编辑his/her个人资料信息。对于其他用户,应该是未授权的。
这似乎是一个常见的用例,我该怎么做?我研究了示波器,但这似乎不是解决方案。我是否必须为此编写自定义 Lambda 授权方,而不是使用 Cognito 授权方?如果可以,您能否提供一个示例,因为我没有编写 Lambda 授权程序的经验。
您可以使用映射模板在 API 网关中构建请求,并从那里使用存储在 Cognito 令牌中的信息 https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#context-variable-reference。
对于您对 /users/{username}
的特定请求,您可以使用 if 仅在用户匹配 How to use IF condition in AWS Api gateway mapping templates. can i use if condition without using foreach? 时提供所需信息。
或者,您可以简化 PUT 到 /users 的路径,然后从 cognito 令牌中提取用户名并在映射模板中使用它 How can I access the Cognito username of the caller in a Lamda function?。