我目前正在将 2FA 功能构建到网站中。
身份验证后端是 AWS Cognito IDP。
我了解了如何配对设备,下面是一些伪代码。
# 1. Authenticatie with username and password
client = boto3.client("cognito-idp", <region>)
response = client.admin_initiate_auth(
UserPoolId=user_pool_id,
ClientId=app_client_id,
AuthFlow='ADMIN_NO_SRP_AUTH',
AuthParameters={
"USERNAME": username,
"PASSWORD": password
})
auth_result = response['AuthenticationResult']
access_token = auth_result["AccessToken"]
# 2. associate token (i.e. generate QR code for MFA device paing)
response = client.associate_software_token(AccessToken=access_token)
... (generate QR, let user scan QR, let user enter TOTP user_code)
response = client.verify_software_token(AccessToken=access_token, UserCode=user_code)
# 3. set MFA preference for this user
response = client.admin_set_user_mfa_preference(
UserPoolId=user_pool_id,
Username=username,
SoftwareTokenMfaSettings={'Enabled': True, 'PreferredMfa': True})
我觉得这里面有一个差距。因为二维码/配对工作流程只需完成一次;在后续会话中应该进行(用户名、密码、TOTP)身份验证并且不再进行设备配对。
问题:如何识别 MFA 设备配对是否已到位,以便我们可以跳过该部分?
根据https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cognito-idp/client/get_user.html它在下面说atrribute
UserMFAsettingList(列表)–
为用户激活的 MFA 选项。可能的值 此列表中是 SMS_MFA 和 SOFTWARE_TOKEN_MFA。
(字符串)–
应为您提供与用户关联的 MFA 设备。