Amazon web services 使用AWS.CognitoIdentityServiceProvider更改密码
我正在试图找出如何使用AWS.CognitoIdentityServiceProvider的 我需要将以下内容作为参数传递:Amazon web services 使用AWS.CognitoIdentityServiceProvider更改密码,amazon-web-services,login,access-token,amazon-cognito,Amazon Web Services,Login,Access Token,Amazon Cognito,我正在试图找出如何使用AWS.CognitoIdentityServiceProvider的 我需要将以下内容作为参数传递: { PreviousPassword: 'STRING_VALUE', /* required */ ProposedPassword: 'STRING_VALUE', /* required */ AccessToken: 'STRING_VALUE' } 我在Lambda函数中使用它,那么如何获得访问令牌呢?我有cognitoIdentityPoolId
{
PreviousPassword: 'STRING_VALUE', /* required */
ProposedPassword: 'STRING_VALUE', /* required */
AccessToken: 'STRING_VALUE'
}
我在Lambda函数中使用它,那么如何获得访问令牌呢?我有
cognitoIdentityPoolId
和cognitoIdentityId
要使用,但我不知道这个访问令牌是哪个。标识池id和标识id是Cognito概念,而ChangePassword API是一个。它们是两种不同的服务——将用户池视为身份池的身份提供者
简短的版本是,您可以通过与用户池中的用户登录来获取访问令牌。这样做将返回访问令牌、id令牌和刷新令牌。也就是说,一个常见的主题是在Lambda端使用各种用户池API的管理员版本,因为您可能没有用户凭据。因为没有管理员版本的changePassword,您必须首先作为您试图影响的用户进行身份验证,然后才能调用changePassword例程。我花了很长时间才弄明白这一点,而且似乎没有其他文章涉及到使用管理员调用和用户池运行NodeJS lambda函数的情况,在这种情况下,您希望支持“admin”更改用户密码。我的(当前工作)代码如下。注:我认为防止管理员更改用户密码是AWS深思熟虑的设计决定,因此我不确定下面的解决方法将持续有效多长时间
const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
// Accept a POST with a JSON structure containing the
// refresh token provided during the original user login,
// and an old and new password.
function changeUserPassword(event, context, callback) {
// Extract relevant JSON into a request dict (This is my own utility code)
let requiredFields = ['old_password','new_password','refresh_token'];
let request = Utils.extractJSON(event['body'], requiredFields);
if (request == false) {
Utils.errorResponse("Invalid JSON or missing required fields", context.awsRequestId, callback);
return; // Abort here
}
// This function can NOT be handled by admin APIs, so we need to
// authenticate the user (not the admin) and use that
// authentication instead.
let refreshToken = request['refresh_token']
// Authenticate as the user first, so we can call user version
// of the ChangePassword API
cognitoidentityserviceprovider.adminInitiateAuth({
AuthFlow: 'REFRESH_TOKEN',
ClientId: Config.ClientId,
UserPoolId: Config.UserPoolId,
AuthParameters: {
'REFRESH_TOKEN': refreshToken
},
ContextData: getContextData(event)
}, function(err, data) {
if(err){
Utils.errorResponse(err['message'], context.awsRequestId, callback);
return // Abort here
} else {
// Now authenticated as user, change the password
let accessToken = data['AuthenticationResult']['AccessToken'] // Returned from auth - diff signature than Authorization header
let oldPass = request['old_password']
let newPass = request['new_password']
let params = {
AccessToken: accessToken, /* required */
PreviousPassword: oldPass, /* required */
ProposedPassword: newPass /* required */
}
// At this point, really just a pass through
cognitoidentityserviceprovider.changePassword(params, function(err2, data2) {
if(err2){
let message = {
err_message: err2['message'],
access_token: accessToken
}
Utils.errorResponse(message, context.awsRequestId, callback);
} else {
let response = {
'success': 'OK',
'response_data': data2 // Always seems to be empty
}
callback(response)
}
});
}
});
}
在使用AWS Lambda时,您无需担心访问令牌,只需将用户名和密码以及poolID传递给Cognoto函数adminSetUserPassword()。此函数将轻松更新密码
const updateCognitoPassword = async(user_name, password) => {
try {
var changePasswordParams = {
Password: password,
Permanent: true,
Username: user_name.trim(),
UserPoolId: constants.user_pool_id
};
let data = await cognitoidentityserviceprovider.adminSetUserPassword(changePasswordParams).promise();
return data;
}
catch (err) {
throw new Error(err);
}
};
如何从API调用执行登录?在我的例子中,我将cognito与AWS ALB一起使用。所以我在标题或JWT中都没有得到刷新令牌。建议您从AWS文档开始。这是服务器端的问题/答案,而不是客户端。请参阅:虽然此代码片段可能会解决此问题,但它没有解释为什么或如何回答此问题。请,因为这确实有助于提高你的文章质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。您可以使用该按钮改进此答案,以获得更多选票和声誉!谢谢,编辑了更多的解释