![](/img/trans.png)
[英]Why boto3 dose not recognize my AWS_ACCESS_KEY_ID in Django on EC2?
[英]AWS Cognito Boto3 Error on Confirm Device: Invalid device key given
我一直在使用啟用 MFA 的 Python、Django 和 Boto3 創建 AWS Cognito 流。
我的身份驗證流程如下:
initial_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"]
response_to_auth_challenge : 在單獨的 django rest 端點上調用
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),
}
)
基於這篇文章,我想實現確認設備,以便在下次登錄時跳過 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):給定的設備密鑰無效。
任何人都可以幫助解釋為什么會發生這種情況嗎?
所以我找到了一個對我有用的東西。
在您的挑戰響應中,您需要為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 上有更好的記錄,否則他們至少會有更好的錯誤消息。
那為我解決了問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.