无法通过 AWS Cognito API 重新发送验证码

And*_*708 4 amazon-web-services amazon-cognito

我有一个 AWS Cognito 用户池,其中用户是使用AdminCreateUser操作通过 Cognito 的 API 创建的,它工作正常。这会向用户发送一封验证电子邮件,其中包含一个临时密码。到现在为止还挺好。

现在用户没有收到此验证电子邮件,因此我需要使用ResendConfirmationCode操作再次发送它。我试图用下面的 PHP 代码来做到这一点。

$userPoolId = '[POOL_ID_HERE]';
$backendAppId = '[APP_ID_HERE]';
$clientSecret = '[SECRET_HERE]';
$username = '[UUID_HERE]';

$secretHash = base64_encode(hash_hmac('sha256', $username . $backendAppId, $clientSecret, true));

$cognitoIdp->resendConfirmationCode([
    'ClientId' => $backendAppId,
    'SecretHash' => $secretHash,
    'Username' => $username,
]);
Run Code Online (Sandbox Code Playgroud)

这给了我以下错误:

Aws/CognitoIdentityProvider/Exception/CognitoIdentityProviderException 带有消息“在“ https://cognito-idp.eu-central-1.amazonaws.com ”上执行“ResendConfirmationCode”时出错;AWS HTTP 错误:客户端错误:POST https://cognito-idp.eu-central-1.amazonaws.com 导致400 Bad Request响应:{"__type":"NotAuthorizedException","message":"无法为此用户重新发送确认代码"} NotAuthorizedException(客户端):无法为此重新发送确认代码用户 - {"__type":"NotAuthorizedException","message":"无法为该用户重新发送确认码"}'

我正在使用对用户池具有以下 IAM 权限的用户的凭据:

  • cognito-idp:AdminDeleteUser
  • cognito-idp:AdminCreateUser
  • cognito-idp:AdminAddUserToGroup
  • cognito-idp:ResendConfirmationCode

如果我使用IAM Policy Simulator测试权限,它会给我绿灯,说一切正常。据我所知,cognito-idp:ResendConfirmationCode操作应该足够了,因为在创建用户时发送验证电子邮件工作正常。

我在这里做错了什么?另一种方法是再次调用 AdminCreateUser 操作,将MessageAction参数设置为RESEND。这将强制为现有用户重新发送验证电子邮件,但如果我可以让它工作,我更喜欢使用 ResendConfirmationCode 操作。

有任何想法吗?谢谢!

Ark*_*jee 7

我了解您希望您的 Web 应用程序最终用户在注册后由于某种原因没有收到确认码时再次收到确认码,我也了解您在注册时收到“NotAuthorizedException”尝试从使用 PHP SDK 的代码运行 ResendConfirmationCode API 调用。

ResendConfirmationCode API 调用[1] 可以在注册API 调用[2] 之后使用,它不是AdminCreateUser Authentication 流程的一部分,这就是抛出错误的原因。AdminCreateUser API 调用将新用户的状态更改为“强制更改密码状态”,在使用 AdminCreateUser 创建新用户后,ResendConfirmationCode 调用或 ForgotPassword 调用均无法工作。

如果您希望您的最终用户再次获得确认代码,您可以使用 AdminCreateUser API 调用本身并在 PHP 代码的 MessageAction 中设置“RESEND”标志。根据我对 Amazon Cognito 的理解,在此特定用例中没有其他方法可以再次发送验证消息。PHP中API调用根据官方文档举例如下[3]:

$result = $client->adminCreateUser([
    'DesiredDeliveryMediums' => ['<string>', ...],
    'ForceAliasCreation' => true || false,
    'MessageAction' => 'RESEND|SUPPRESS',
    'TemporaryPassword' => '<string>',
    'UserAttributes' => [
        [
            'Name' => '<string>', // REQUIRED
            'Value' => '<string>',
        ],
        // ...
    ],
    'UserPoolId' => '<string>', // REQUIRED
    'Username' => '<string>', // REQUIRED
    'ValidationData' => [
        [
            'Name' => '<string>', // REQUIRED
            'Value' => '<string>',
        ],
        // ...
    ],
]);
Run Code Online (Sandbox Code Playgroud)

使用设置 'MessageAction' 为 'RESEND' 后,最终用户应该能够再次收到验证消息。

参考

[1]。https://docs.aws.amazon.com/cli/latest/reference/cognito-idp/resend-confirmation-code.html

[2]。https://docs.aws.amazon.com/cli/latest/reference/cognito-idp/sign-up.html

[3]。https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-cognito-idp-2016-04-18.html#admincreateuser