【问题标题】:How can I access the Cognito username of the caller in a Lamda function?如何在 Lambda 函数中访问调用者的 Cognito 用户名?
【发布时间】: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']"
}

【问题讨论】:

标签: amazon-web-services aws-lambda amazon-cognito


【解决方案1】:

您是在调用 API 网关时发送用户的 idToken,对吧?

我假设您在此解决方案中成功获取了用户的 Cognito 信息。

在您调用 API Gateway 时,您会在令牌中包含一个 Authorization 标头,如下所示:

var url = YOURURL;
var options = {
    method: "GET",
    headers: {
        'Authorization': COGNITOUSER_IDTOKEN
    }
}

fetch(url, options);

获取用户ID Token的方式有很多种,这里就不一一赘述了。

只要您这样做,您就可以通过 Integration Request(在 API Gateway 中的 API 阶段)映射模板访问令牌中的信息。

在你的映射模板中,你可以使用这个:

{
  "username" : "$context.authorizer.claims['cognito:username']"
}

然后,在您的 Lambda 函数中,您可以通过引用“event.username”来访问它。

【讨论】:

  • 我不这样做。我会用谷歌搜索它。我只了解 AWS 文档的一半,所以我很难找到我要找的东西。如果您可以准确指示如何附加 ID 令牌,请更新答案。如果我得到这个工作,我会评论一个资源的链接和说明。
  • 我添加了显示如何发送 ID 令牌的更新代码。
  • 如果你想对你的映射模板进行故障排除,你可以这样做:在你的 lambda 函数中,console.log('Received event: ', event),你将能够看到哪些参数正在传递给您的 lambda。您的映射模板正在传递所有可能的参数,我想这很好,但绝对没有必要。
猜你喜欢
  • 2020-09-12
  • 2021-05-05
  • 2021-10-22
  • 2021-03-02
  • 2020-06-17
  • 1970-01-01
  • 2018-07-17
  • 2018-11-11
  • 1970-01-01
相关资源
最近更新 更多