我的设置包括一个带有IAM访问控制的AWS API网关和用于登录的AWS cognito . 我已经从Android应用程序访问了API,现在想构建一个Web应用程序(angular2)来做同样的事情 . 在Android上,我使用AWSCognitoCredentialsProvider为API SDK提供所需的凭据 . (http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-generate-sdk.html)
不幸的是我无法想象如何/如果我可以用javascript SDK做到这一点?
我可以毫不费力地使用cognito登录并获取会话ID,访问令牌等 . 但是,API SDK要求我提供accessKey和secretKey .
以下是生成的API SDK中的相关代码段:
var authType = 'NONE';
if (sigV4ClientConfig.accessKey !== undefined && sigV4ClientConfig.accessKey !== '' && sigV4ClientConfig.secretKey !== undefined && sigV4ClientConfig.secretKey !== '') {
authType = 'AWS_IAM';
}
换句话说,我有这部分工作(从一些示例代码):
static authenticate(username:string, password:string, callback:CognitoCallback) {
AWSCognito.config.update({accessKeyId: 'anything', secretAccessKey: 'anything'})
let authenticationData = {
Username: username,
Password: password,
};
let authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
let userData = {
Username: username,
Pool: CognitoUtil.getUserPool()
};
console.log("Authenticating the user");
let cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
console.log(AWS.config);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
callback.cognitoCallback(null, result);
},
onFailure: function (err) {
callback.cognitoCallback(err.message, null);
},
});
}
现在我想用这个:
this.apigClient = apigClientFactory.newClient({
accessKey: "anything",
secretAccessKey: "anything",
sessionToken: "nothing",
region: 'eu-central-1'
如何从AWSCognito中获取accessKey,secretAccessKey和sessionToken?我无法找到任何API so far...
2 回答
Cognito实际上由3种不同的服务组成:
Cognito您的用户池 - 您在此处集成了什么
Cognito Sync - 用于同步用户的用户首选项数据
Cognito联合身份 - 用于将身份(FB,Google或用户池)联合到您的帐户并生成凭据 .
API Gateway客户端期望的是来自Cognito Federated Identity的凭据 .
请参阅Cognito documentation以将您的用户池与Cognito Federated Identity集成 .
谢谢鲍勃,指出我正确的方向!我现在想出来了,因此为了完整起见,这里是我问题的完整解决方案:
从创建apigClient的服务:
getCredentials()方法,它是获取所需临时凭证的关键:
因此,这里的关键见解是
我对用户池进行了身份验证(在我的问题中显示)
我使用身份提供程序来检索临时凭证(getCredentials)
我使用AWS.config.credentials中的临时凭证来设置apigClient
我希望这对其他人也有帮助 . 当然我刚发布的代码可能会使用一些重构,因此对此非常欢迎!