【发布时间】: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