以下是使用 AWS Cognito 的正确注册流程,包括电子邮件验证和使用短信或TOTP的MFA的代码示例:
import boto3
client = boto3.client('cognito-idp')
response = client.create_user_pool(
PoolName='my-user-pool',
AutoVerifiedAttributes=['email'],
MfaConfiguration='OPTIONAL',
MfaTypes=['SMS', 'SOFTWARE_TOKEN_MFA'],
EmailVerificationSubject='Your verification code',
EmailVerificationMessage='Your verification code is {####}',
SmsVerificationMessage='Your verification code is {####}'
)
user_pool_id = response['UserPool']['Id']
response = client.create_user_pool_client(
UserPoolId=user_pool_id,
ClientName='my-user-pool-client',
GenerateSecret=False
)
user_pool_client_id = response['UserPoolClient']['ClientId']
response = client.sign_up(
ClientId=user_pool_client_id,
Username='user@example.com',
Password='password',
UserAttributes=[
{
'Name': 'email',
'Value': 'user@example.com'
}
]
)
# 获取 Cognito 的用户 ID
cognito_user_id = response['UserSub']
response = client.admin_confirm_sign_up(
UserPoolId=user_pool_id,
Username=cognito_user_id
)
response = client.admin_set_user_mfa_preference(
UserPoolId=user_pool_id,
Username=cognito_user_id,
SoftwareTokenMfaSettings={
'Enabled': True
},
SMSMfaSettings={
'Enabled': True
}
)
以上代码示例演示了如何使用 AWS SDK for Python (Boto3) 创建用户池、用户池客户端,注册用户并进行电子邮件验证和启用 MFA。请确保在运行代码之前已安装并配置了 AWS CLI,并且具有相应的访问密钥和权限。