AWS学习测试环境
AWS Cognito Test Environment
我目前正在使用 AWS Cognito 来管理我们应用程序的用户身份验证。我 运行 遇到的一个障碍是想出一种实施 "test" 或 "qa" 环境的好方法。
我对我的 API 进行了很多自动化测试,这些测试将使用随机数据创建用户。显然,我不希望 Cognito 在此环境中发送实际的 SMS 或电子邮件消息。此外,在进行手动测试时,我们将使用假 phone 号码和电子邮件创建大量用户。有什么方法可以将用户池设置为 "development" 模式,所有消息都以某种方式简单地记录下来吗?
您可以编写预注册 lambda 函数并通过设置 autoConfirmUser 标志在 lambda 函数中自动确认用户。在这种情况下,Cognito 不会发送任何带有确认码的短信或电子邮件。文档中的以下 lambda 示例 (http://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools-working-with-aws-lambda-triggers.html#aws-lambda-triggers-pre-registration-example).
exports.handler = function(event, context) {
// This Lambda function returns a flag to indicate if a user should be auto-confirmed.
// Perform any necessary validations.
// Impose a condition that the minimum length of the username of 5 is imposed on all user pools.
if (event.userName.length < 5) {
var error = new Error('failed!');
context.done(error, event);
}
// Access your resource which contains the list of emails of users who were invited to sign up
// Compare the list of email IDs from the request to the approved list
if(event.userPoolId === "yourSpecialUserPool") {
if (event.request.userAttributes.email in listOfEmailsInvited) {
event.response.autoConfirmUser = true;
}
}
// Return result to Cognito
context.done(null, event);
};
这是我在 AWS Cognito 中创建一个 "staging" 环境用户池的方法,它不会向用户发送真正的通知。实际上涉及几个不同的部分,但我认为我能够涵盖所有内容。话虽如此,如果 Cognito 只是提供一个用户池设置来关闭所有通知,那肯定会很好,这样我就不必在我的代码中编写特定于环境的逻辑。
阻止用户邀请
在我们的应用程序中,我们使用 AdminCreateUser 函数来创建受其他用户邀请的用户。此功能通常会向新用户的 phone 号码或电子邮件发送邀请消息。为了防止这些邀请,您可以提供 MessageAction: 'SUPPRESS'
作为函数参数的参数。像这样:
let params = {
UserPoolId: config.cognitoUserPoolId,
Username: uuid.v4(),
MessageAction: 'SUPPRESS', /* IMPORTANT! */
TemporaryPassword: user.phone_number.slice(-6),
UserAttributes: [
{ Name: 'given_name', Value: user.first_name },
{ Name: 'family_name', Value: user.last_name },
{ Name: 'phone_number', Value: user.phone_number }
]
};
cognito.adminCreateUser(params).promise().then(data => {
console.log(data);
});
此处的官方文档:http://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminCreateUser.html
防止用户属性更新验证
在我们的生产应用中,我们希望用户必须重新验证他们的 phone 号码或电子邮件,如果它发生变化。但在我们的暂存环境中,我们实际上并不需要这个。因此,取消选中 用户池设置中 "Do you want to require verification of emails or phone numbers?" 部分下的电子邮件框和 phone。
我目前正在使用 AWS Cognito 来管理我们应用程序的用户身份验证。我 运行 遇到的一个障碍是想出一种实施 "test" 或 "qa" 环境的好方法。
我对我的 API 进行了很多自动化测试,这些测试将使用随机数据创建用户。显然,我不希望 Cognito 在此环境中发送实际的 SMS 或电子邮件消息。此外,在进行手动测试时,我们将使用假 phone 号码和电子邮件创建大量用户。有什么方法可以将用户池设置为 "development" 模式,所有消息都以某种方式简单地记录下来吗?
您可以编写预注册 lambda 函数并通过设置 autoConfirmUser 标志在 lambda 函数中自动确认用户。在这种情况下,Cognito 不会发送任何带有确认码的短信或电子邮件。文档中的以下 lambda 示例 (http://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools-working-with-aws-lambda-triggers.html#aws-lambda-triggers-pre-registration-example).
exports.handler = function(event, context) {
// This Lambda function returns a flag to indicate if a user should be auto-confirmed.
// Perform any necessary validations.
// Impose a condition that the minimum length of the username of 5 is imposed on all user pools.
if (event.userName.length < 5) {
var error = new Error('failed!');
context.done(error, event);
}
// Access your resource which contains the list of emails of users who were invited to sign up
// Compare the list of email IDs from the request to the approved list
if(event.userPoolId === "yourSpecialUserPool") {
if (event.request.userAttributes.email in listOfEmailsInvited) {
event.response.autoConfirmUser = true;
}
}
// Return result to Cognito
context.done(null, event);
};
这是我在 AWS Cognito 中创建一个 "staging" 环境用户池的方法,它不会向用户发送真正的通知。实际上涉及几个不同的部分,但我认为我能够涵盖所有内容。话虽如此,如果 Cognito 只是提供一个用户池设置来关闭所有通知,那肯定会很好,这样我就不必在我的代码中编写特定于环境的逻辑。
阻止用户邀请
在我们的应用程序中,我们使用 AdminCreateUser 函数来创建受其他用户邀请的用户。此功能通常会向新用户的 phone 号码或电子邮件发送邀请消息。为了防止这些邀请,您可以提供 MessageAction: 'SUPPRESS'
作为函数参数的参数。像这样:
let params = {
UserPoolId: config.cognitoUserPoolId,
Username: uuid.v4(),
MessageAction: 'SUPPRESS', /* IMPORTANT! */
TemporaryPassword: user.phone_number.slice(-6),
UserAttributes: [
{ Name: 'given_name', Value: user.first_name },
{ Name: 'family_name', Value: user.last_name },
{ Name: 'phone_number', Value: user.phone_number }
]
};
cognito.adminCreateUser(params).promise().then(data => {
console.log(data);
});
此处的官方文档:http://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminCreateUser.html
防止用户属性更新验证
在我们的生产应用中,我们希望用户必须重新验证他们的 phone 号码或电子邮件,如果它发生变化。但在我们的暂存环境中,我们实际上并不需要这个。因此,取消选中 用户池设置中 "Do you want to require verification of emails or phone numbers?" 部分下的电子邮件框和 phone。