【问题标题】:Amazon Cognito "RespondToAuthChallenge" - returns a NotAuthorizedException, Incorrect username or passwordAmazon Cognito "RespondToAuthChallenge" - 返回 NotAuthorizedException、不正确的用户名或密码
【发布时间】:2021-03-27 09:27:39
【问题描述】:

在 cognito 用户池中,我有两个不同的组:管理员和用户。我想允许管理员用户创建 cognito 用户,所以我使用 CognitoIdentityServiceProvider 的 AdminCreateUser 方法创建了新用户。 参考链接 - https://docs.aws.amazon.com/cognito/latest/developerguide/how-to-create-user-accounts.html

现在我想授权用户池中的用户。我正在关注此链接 - https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-authentication-flow.html - 客户端身份验证流程。 但是“RespondToAuthChallenge” - 返回 NotAuthorizedException,用户名或密码不正确。

import * as AWS from 'aws-sdk'
import { SRPClient, calculateSignature, getNowString } from 'amazon-user-pool-srp-client'


const userPoolId = 'XXX'
const ClientId = 'XXX'

const verifyUser = () => {
    AWS.config.region = 'us-west-2'
    const srp = new SRPClient(userPoolId)
    const SRP_A = srp.calculateA()
    var params = {
      AuthFlow: 'USER_SRP_AUTH',
      ClientId,
      AuthParameters: {
        USERNAME: formState.email,
        SRP_A,
      },
    }
    let cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider()
    cognitoidentityserviceprovider.initiateAuth(params, function (err, data) {
      if (err) {
        const error = err.message ? err.message : err
        console.log(error)
      }
      else {
        console.log(data) // --> data.session is undefined
        if (data.ChallengeParameters && data.ChallengeName) {
          const passwordAuthenticationKey =
            srp.getPasswordAuthenticationKey(
              data.ChallengeParameters.USER_ID_FOR_SRP,
              formState.password,
              data.ChallengeParameters.SRP_B,
              data.ChallengeParameters.SALT
            )
          const dateNow = getNowString()
          const signatureString = calculateSignature(
            passwordAuthenticationKey,
            userPoolId,
            data.ChallengeParameters.USER_ID_FOR_SRP,
            data.ChallengeParameters.SECRET_BLOCK, dateNow
          )

          var params = {
            ChallengeName: data.ChallengeName,
            ClientId,
            ChallengeResponses: {
              // PASSWORD_VERIFIER
              PASSWORD_CLAIM_SIGNATURE: signatureString,
              PASSWORD_CLAIM_SECRET_BLOCK: data.ChallengeParameters.SECRET_BLOCK,
              TIMESTAMP: dateNow,
              USERNAME: data.ChallengeParameters.USER_ID_FOR_SRP,
            },
            Session: data.Session, // undefined
          };
          cognitoidentityserviceprovider.respondToAuthChallenge(params, function (err, data) {
            if (err) {
              const error = err.message ? err.message : err
              console.log(error) // Incorrect username or password
            }
            else {
              console.log(data)
            }
          })
        }
      }
    })
  }

我创建了差异用户,但没有运气。用户注册但 respondToAuthChallenge 抛出错误。 InitiateAuth 响应返回challengeName 和challengeParameters 但未提供会话。

我发现了这个我无法理解的陈述 - “当 RespondToAuthChallenge 密码证明的下一个操作运行时,Amazon Cognito 返回一个通用的 NotAuthorizedException 错误,指示用户名或密码不正确。”在https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pool-managing-errors.html

如果您有任何解决方案/想法,请告诉我,谢谢!!

【问题讨论】:

    标签: amazon-cognito aws-sdk-js aws-userpools


    【解决方案1】:

    首先,根据RespondToAuthChallenge 文档,Session 字段不是必填字段,因此如果未提供,您甚至可能不需要将其传递给 PASSWORD_VERIFIER 质询。

    其次,您能否包含完整的错误消息以及您从initialAuth 和respondToAuthChallenge 操作中获得的响应,以便我看一下?

    还请记住,initialAuth 操作响应中的会话字段是带有大写“S”的“会话”(这里是 initiateAuth operation 的文档),因此您必须使用获取会话值而不是 data.session data.Session 代替。

    【讨论】:

    猜你喜欢
    • 2022-01-24
    • 2022-07-05
    • 1970-01-01
    • 2015-02-11
    • 2016-12-02
    • 2021-03-14
    • 2021-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多