【发布时间】:2019-03-09 22:55:25
【问题描述】:
我已在 API Gateway 中为 API 端点添加了授权。
这将似乎相关的identity 属性添加到事件中。
identity:
{
cognitoIdentityPoolId: null,
accountId: null,
cognitoIdentityId: null,
caller: null,
sourceIp: 'detracted',
accessKey: null,
cognitoAuthenticationType: null,
cognitoAuthenticationProvider: null,
userArn: null,
userAgent: 'Amazon CloudFront',
user: null },
apiId: 'detracted' },
body: null,
isBase64Encoded: false
}
但那里大多是null。那么如何访问调用者的 Cognito 用户名呢?
编辑:
我添加了一个映射模板。但是这是一个 GET 请求,所以我不知道它是否有任何效果,因为工具提示说明数据已附加到正文。
## See http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
## This template will pass through all parameters including path, querystring, header, stage variables, and context through to the integration endpoint via the body/payload
#set($allParams = $input.params())
{
"body-json" : $input.json('$'),
"params" : {
#foreach($type in $allParams.keySet())
#set($params = $allParams.get($type))
"$type" : {
#foreach($paramName in $params.keySet())
"$paramName" : "$util.escapeJavaScript($params.get($paramName))"
#if($foreach.hasNext),#end
#end
}
#if($foreach.hasNext),#end
#end
},
"stage-variables" : {
#foreach($key in $stageVariables.keySet())
"$key" : "$util.escapeJavaScript($stageVariables.get($key))"
#if($foreach.hasNext),#end
#end
},
"context" : {
"account-id" : "$context.identity.accountId",
"api-id" : "$context.apiId",
"api-key" : "$context.identity.apiKey",
"authorizer-principal-id" : "$context.authorizer.principalId",
"caller" : "$context.identity.caller",
"cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider",
"cognito-authentication-type" : "$context.identity.cognitoAuthenticationType",
"cognito-identity-id" : "$context.identity.cognitoIdentityId",
"cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId",
"http-method" : "$context.httpMethod",
"stage" : "$context.stage",
"source-ip" : "$context.identity.sourceIp",
"user" : "$context.identity.user",
"user-agent" : "$context.identity.userAgent",
"user-arn" : "$context.identity.userArn",
"request-id" : "$context.requestId",
"resource-id" : "$context.resourceId",
"resource-path" : "$context.resourcePath",
"username" : "$context.authorizer.claims['cognito:username']"
}
}
"event" : {
"username" : "$context.authorizer.claims['cognito:username']"
}
【问题讨论】:
-
您是否已将 API Gteway 与文档中提到的 cognito 集成? docs.aws.amazon.com/apigateway/latest/developerguide/…
标签: amazon-web-services aws-lambda amazon-cognito