我创建了一个Migrate User Lambda函数,并将其连接到AWS Cognito Hosted UI。调用了Lambda,我在CloudWatch日志中看到了日志语句。除了托管UI的行为似乎与预期不符之外,一切似乎都正常。这几乎就像Lambda没有被调用一样。但我知道是因为我看到了日志语句,托管的UI总是显示“您输入的用户名或密码无效”,而我希望它将用户带到重置密码工作流。
我的节点功能为:
const https = require('https')
exports.handler = (event, context, callback) => {
if ( event.triggerSource == "UserMigration_Authentication" ) {
LoginUser(event.userName, event.request.password, function(user, message){
console.log("Finished LoginUser call.");
if ( user ) {
console.log("Got a valid user with userId=" + user.userId);
event.response.userAttributes = {
"userName" : user.userName,
"email": user.emailAddress,
"email_verified": "true",
"picture" : user.imageUrl,
"family_name" : user.lastName,
"given_name" : user.firstName,
"custom:userId" : user.userId
};
event.response.finalUserStatus = "RESET_REQUIRED";
event.response.messageAction = "SUPPRESS";
event.response.desiredDeliveryMediums = "EMAIL";
console.log("Done: " + JSON.stringify(event));
//callback(null,event);
context.succeed(event);
}
else {
console.log("User was not found. " + message );
callback(message, event);
}
});
}
else if ( event.triggerSource == "UserMigration_ForgotPassword" ) {
/*
* Check to see if the user exists. If so, then tell cognito
* to proceed, given the email address we looked up.
*/
LookupUser(event.userName, function(user, message){
if ( user ) {
event.response.userAttributes = {
"email": user.emailAddress,
"email_verified": "true"
};
event.response.messageAction = "SUPPRESS";
console.log("Done: " + JSON.stringify(event));
//callback(null,event);
context.succeed(event);
}
else {
callback(message, event);
}
});
}
else {
callback("Bad triggerSource " + event.triggerSource);
}
};
/*
* Lookup a user
*/
function LookupUser(userName, UserCallback) {
var pathUri = "/api/user?op=or&userName=" + encodeURIComponent(userName);
console.log("pathUri=" + pathUri);
var headers = {
"x-k-Id": process.env.kmpzPublicKey,
"x-k-Sig": process.env.kmpzSecretKey
};
var options = {
host: "www.mydomainhere.com",
port: 443,
path: pathUri,
method: "GET",
headers: headers
};
var req = https.request(options, function(res) {
res.on('data', function(data) {
var user = JSON.parse(data);
console.log(user);
if ( user.error ) {
UserCallback(null, user.error.message);
}
else if ( user.userId ) {
UserCallback(user, "OK");
}
else {
UserCallback(null, "Unexpected response from userService. Please contact Kompoz Customer Support.");
}
});
});
req.on('error', function(e) {
UserCallback(null, e.message);
});
req.end();
};
/*
* Login a user
*/
function LoginUser(userName, userPassword, UserCallback) {
var pathUri = "/api/auth/migrate?userName=" + encodeURIComponent(userName) + "&password=" + encodeURIComponent(userPassword);
console.log("pathUri=" + pathUri);
var headers = {
"x-k-Id": process.env.kmpzPublicKey,
"x-k-Sig": process.env.kmpzSecretKey
};
var options = {
host: "www.mydomainhere.com",
port: 443,
path: pathUri,
method: "POST",
headers: headers
};
var req = https.request(options, function(res) {
res.on('data', function(data) {
var user = JSON.parse(data);
console.log(user);
if ( user.error ) {
UserCallback(null, user.error.message);
}
else if ( user.userId ) {
UserCallback(user, "OK");
}
else {
UserCallback(null, "Unexpected response from userService. Please contact Kompoz Customer Support.");
}
});
});
req.on('error', function(e) {
UserCallback(null, e.message);
});
req.end();
};
在CloudWatch日志中,我看到了响应:
{
"version": "1",
"triggerSource": "UserMigration_Authentication",
"region": "us-east-1",
"userPoolId": "us-east-1_******",
"userName": "exampleUser",
"callerContext": {
"awsSdkVersion": "aws-sdk-unknown-unknown",
"clientId": "2s12aui53t9a4n85orc3d3ul0u"
},
"request": {
"password": "*******",
"validationData": null,
"userAttributes": null
},
"response": {
"userAttributes": {
"userName": "exampleUser",
"email": "me@example.com",
"email_verified": "true",
"picture": "https://s3.amazonaws.com/example-s3/images/m1006-20140119-033844-p2.jpg",
"family_name": "Smith",
"given_name": "John",
"custom:userId": 1006
},
"forceAliasCreation": null,
"finalUserStatus": "RESET_REQUIRED",
"messageAction": "SUPPRESS",
"desiredDeliveryMediums": "EMAIL"
}
}
我得到的是:
2条答案
按热度按时间rekjcdws1#
希望我能帮上忙。只是来这里说“我也是”。你收到HTTP/401错误了吗?我的lambda也在运行,但我无法通过错误消息。在cloudwatch中没有日志,在cloudtrail中没有提示。
HTML/401
λ响应
ao218c7q2#
1.关于用户名/密码不正确的问题,一个可能的问题是,在设置用户池登录选项时,您同时选择了用户名和电子邮件。在这种情况下,用户池不允许为用户名设置电子邮件。对于此配置,仅当用户名为非电子邮件字符串时才允许迁移。