【发布时间】:2017-08-31 08:16:21
【问题描述】:
我正在尝试使用带有用户池的 AWS Cognito 通过我的 API 进行身份验证。我有一个通过 AWS Cognito 用户池创建的用户,我正在尝试使用该用户登录。
我得到的错误是CredentialsError: Missing credentials in config。具体来说,它告诉我我需要一个IdentityPoolId。但我似乎在我的 AWS 控制台中找不到这个 IdentityPoolId。我的用户池从哪里得到这个?我看到的只是一个 Pool id 和一个 Pool ARN。
相关源码:
var aws = require('aws-sdk');
aws.config.update({
region: 'us-east-1',
credentials: new aws.CognitoIdentityCredentials({
IdentityPoolId: '???'
})
});
var authUser = function(params, callback)
{
if (!params || !params.Email || !params._password)
{
callback(new Error('Invalid parameters.'));
return false;
}
var cognito = new aws.CognitoIdentityServiceProvider();
var authParams = {
AuthFlow: 'USER_SRP_AUTH', // not sure what this means...
ClientId: conf.AWSConfig.ClientId,
AuthParameters: {
Username: params.Email,
Password: params._password
}
};
cognito.initiateAuth(authParams, function(err, data)
{
if (err)
{
console.log('Error details: ' + util.inspect(err));
callback(err);
return false;
}
callback(null, {success: true, data: data});
});
}
对于authParams 对象,我不确定AuthFlow 应该是什么。看着http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html#initiateAuth-property,我似乎应该使用USER_SRP_AUTH。
编辑:
我相信我可能已经找到了我的IdentityPoolId 的地址。我查看了我的Federated Identities 部分,并在编辑身份池时在Authentication Providers 部分下添加了相应的User Pool。
我将Cognito 的Authentication Provider 与我通过输入该用户池的User Pool ID 和App Client ID 创建的用户池相关联。现在使用相同的代码使用Identity pool ID,我得到了错误CredentialsError: Missing credentials in config。它说Unauthorized access is not supported for this identity pool。好的...我正在尝试授权用户...我是否需要创建一个未经身份验证的角色,以便用户在未经身份验证时可以进行身份验证?如果这是我需要做的,那似乎很愚蠢。
编辑 2:
我还应该注意,我能够使用 Javascript SDK(不是 nodejs)登录并获得 AccessToken 以及 IdToken 和 RefreshToken。我这样做不需要IdentityPoolId。我唯一需要的是UserPoolId 和ClientId。
var authenticateUser = function(onSuccessCallback)
{
var authData = {
Username: getUserName(), // gets username from an html text field
Password: getPassword() // gets password from an html password field
};
var authDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authData);
var cognitoUser = getCognitoUser();
cognitoUser.authenticateUser(authDetails,
{
onSuccess: function(result)
{
console.log('access token: ' + result.getAccessToken().getJwtToken());
console.log('idToken: ' + result.idToken.jwtToken);
console.log(result);
if (onSuccessCallback && typeof(onSuccessCallback) == 'function')
{
onSuccessCallback(cognitoUser);
}
},
onFailure: function(err)
{
// UserNotConfirmedException: User is not confirmed.
console.log('authError');
alert(err);
}
});
}
【问题讨论】:
标签: node.js amazon-web-services aws-cognito