登录
首页 >  Golang >  Go问答

在具有 Cognito 身份验证和 API 网关的 Go lambda 函数中,CognitoIdentityID 为空

来源:stackoverflow

时间:2024-03-17 09:36:24 127浏览 收藏

在使用 Cognito 身份验证和 API 网关的 Go lambda 函数中,获取 CognitoIdentityID 失败,导致无法识别发送请求的应用程序客户端。通过检查请求的 Authorizer 字段,可以获取客户端 ID,从而确定请求的来源。

问题内容

我在 api 网关后面有一个用 go 编写的 aws lambda 函数。我使用 cognito 与应用程序集成和 oauth 客户端凭据授予进行身份验证。我需要知道哪个应用程序客户端在 lambda 函数内发送了请求,但与 cognito 相关的所有字段(cognitoidentityidcognitoindetitypoolidaccountid 等)在上下文和请求中均为空。我只能在请求的标头中看到承载授权。我打印的值如下:

var forwardRequest = func(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
    println(fmt.Sprintf("Request arrived: Stage: %s, Method: %s, Path: %s",
        request.RequestContext.Stage, request.HTTPMethod, request.Path))
    lc, _ := lambdacontext.FromContext(ctx)
    println(fmt.Sprintf("lc: %+v", lc))
    println(fmt.Sprintf("request: %+v", request))
    ...
}

func main() {
    // Make the handler available for Remote Procedure Call by AWS Lambda
    lambda.Start(forwardRequest)
}

我已经在使用 lambda 代理集成,如类似问题中的回答。我附上api网关方法配置的屏幕截图。我无法启用“使用调用者凭据调用”。

我如何知道哪个客户端发送了请求?


正确答案


我找到了答案:cognito 的客户端 id 不在 cognito* 字段中,而是在请求的 authorizer 中:

func(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
    var clientID string
    if claimsMap, ok := request.RequestContext.Authorizer["claims"].(map[string]interface{}); ok {
        clientID, ok = claimsMap["client_id"].(string)
        ...
    }
    ...

好了,本文到此结束,带大家了解了《在具有 Cognito 身份验证和 API 网关的 Go lambda 函数中,CognitoIdentityID 为空》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>