【问题标题】:AWS Cognito Boto3 Error on Confirm Device: Invalid device key given确认设备上的 AWS Cognito Boto3 错误:给出的设备密钥无效
【发布时间】:2020-03-22 12:42:14
【问题描述】:

我一直在使用启用 MFA 的 Python、Django 和 Boto3 创建 AWS Cognito 流。

我的身份验证流程如下:

initiate_auth:在 django rest 端点上调用

response = client.initiate_auth(
            ClientId=settings.AWS_COGNITO_CLIENT_ID,
            AuthFlow='USER_PASSWORD_AUTH',
            AuthParameters={
                'USERNAME': email,
                'SECRET_HASH': get_secret_hash(email),
                'PASSWORD': password,
            }
        )

if "ChallengeName" in response:
            data["mfa"] = True
            data["session"] = response["Session"]

respond_to_auth_challenge:在单独的 django 休息端点上调用

response = client.respond_to_auth_challenge(
            ClientId=settings.AWS_COGNITO_CLIENT_ID,
            ChallengeName='SMS_MFA',
            Session=session,
            ChallengeResponses={
                'USERNAME': email,
                'SMS_MFA_CODE': code,
                'SECRET_HASH': get_secret_hash(email),
            }
        )

基于此post,我想实现确认设备,以便在下次登录时跳过 MFA。因此,在响应身份验证挑战后,我有以下代码:

device_key = response['AuthenticationResult']['NewDeviceMetadata']['DeviceKey']
        device_group_key = response['AuthenticationResult']['NewDeviceMetadata']['DeviceGroupKey']

        device_password, device_secret_verifier_config = generate_hash_device(device_group_key, device_key)

        device = client.confirm_device(
            AccessToken=response["AuthenticationResult"]["AccessToken"],
            DeviceKey=device_key,
            DeviceSecretVerifierConfig=device_secret_verifier_config,
            DeviceName=email
        )

但我总是得到

未知错误调用 ConfirmDevice 操作时发生错误 (InvalidParameterException):给定的设备密钥无效。

谁能帮助解释为什么会发生这种情况?

【问题讨论】:

  • 你解决过这个问题吗?

标签: django python-3.x boto3 amazon-cognito


【解决方案1】:

所以我找到了适合我的东西。

在您的挑战响应中,您需要从initial_auth 的响应中传递用户名

在您的代码中应该存储在response["Username"]

那么在调用respond_to_auth_challenge时会用到这个参数


response = client.respond_to_auth_challenge(
            ClientId=settings.AWS_COGNITO_CLIENT_ID,
            ChallengeName='SMS_MFA',
            Session=session,
            ChallengeResponses={
                'USERNAME': username, // response["Username"] <--------
                'SMS_MFA_CODE': code,
                'SECRET_HASH': get_secret_hash(email),
            }
        )

我希望它在 AWS 上得到更好的记录,或者他们至少会有更好的错误消息。

这解决了我的问题。

【讨论】:

    猜你喜欢
    • 2019-11-14
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    • 2016-10-09
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多