我使用AWS Lambda + Cognito (用户池+联合身份)+ API网关。用户在WEB应用程序中使用amazon-cognito-identity-js进行身份验证,并通过aws-api-gateway-client调用应用程序接口。API网关方法具有AWS_IAM授权器。如何在Lambda函数中获取用户名(从用户池中)?
发布于 2017-09-09 11:01:07
使用aws-api-gateway-client修改发送到Lambda函数的请求,以在请求报头中传递JWT ID Token。您可能需要ensure your API gateway is configured to forward headers。
apigClient.invokeApi(
params,
pathTemplate,
method,
{ { headers: { IDToken } } },
body);ID令牌应该在这里使用,因为它的有效负载包含cognito:用户名字段
使用amazon-cognito-identity-js进行身份验证后获得ID Token。
您可以在lambda处理程序函数中从请求的头部解析此字段。
Verify its signature,然后再信任其有效负载的内容。
import { util } from 'aws-sdk/global';
exports.handler = function(event, context) {
// Parse ID Token from request header
const headers = event.headers;
const idToken = headers.IDToken;
...
};发布于 2020-11-05 19:15:20
您可以使用event.identity.username
exports.handler = async (event, _, callback) => {
try {
console.log('event', event);
console.log('event.identity.username', event.identity.username);
const userId = event.identity.username;
console.log('userId', userId);
callback(null, true);
} catch(e) {
console.log(e);
callback(e);
}
};https://stackoverflow.com/questions/46125535
复制相似问题