【问题标题】:AWS Cognito InitiateAuth through Lambda function results into errorAWS Cognito InitiateAuth 通过 Lambda 函数导致错误
【发布时间】:2019-07-24 17:11:09
【问题描述】:

我正在用 Go 编写一个 Lambda 函数来对用户进行身份验证,即我想用于后续 API 调用的 AccessToken/IdToken。

当我从独立程序执行 Go 代码时,它可以工作,InitiateAuth 成功。

当我尝试使用 lambda 函数中的相同代码时,我收到错误 NotAuthorizedException: Unable to verify secret hash for client ....

这是我正在使用的代码 sn-p

func AuthenticateUser(userName string, passWord string) (*cognitoidentityprovider.InitiateAuthOutput, error) {

    username := aws.String(userName)
    password := aws.String(passWord)
    clientID := aws.String(constants.COGNITO_APP_CLIENT_ID)

    params := &cognitoidentityprovider.InitiateAuthInput{
        AuthFlow: aws.String("USER_PASSWORD_AUTH"),
        AuthParameters: map[string]*string{
            "USERNAME": username,
            "PASSWORD": password,
        },
        ClientId: clientID,
    }

    authResponse, authError := cognitoClient.InitiateAuth(params)
    if authError != nil {

        fmt.Println("Error = ", authError)
        return nil, authError
    }

    fmt.Println(authResponse)
    fmt.Println(*authResponse.Session)

    return authResponse, nil
}

我已经给 lambda 用户足够的权限 - 认知 idp:AdminCreateUser - cognito-idp:AdminDeleteUser - 认知 idp:InitiateAuth - cognito-idp:ChangePassword - cognito-idp:AdminRespondToAuthChallenge - 认知 idp:AdminInitiateAuth - cognito-idp:ConfirmForgotPassword

我错过了什么吗?

【问题讨论】:

  • 要验证这确实是 IAM 权限问题,请暂时授予您的 Lambda 函数的完全管理员访问权限,然后重试。如果有效,这确实是缺少 IAM 策略。阅读错误消息主要会为您提供权限中缺少的 API 调用的名称。

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


【解决方案1】:

当我们创建一个新的 App 客户端时,默认情况下它有一个关联的 App 客户端密钥。

我又创建了一个应用程序客户端,没有“客户端密码”。我使用了这个新的 App 客户端。

我修改了代码以使用 API AdminInitiateAuth,而不是 InitiateAuth

我能够成功登录。

这是参考链接,很有用 - Amplify "Unable to verify secret hash for client"

【讨论】:

    猜你喜欢
    • 2020-12-12
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-21
    • 2020-09-27
    相关资源
    最近更新 更多