我有一个cognito用户池和身份池。我在用户池中创建了一个用户。我为该用户获取了令牌,即使用lambda 访问,刷新,id令牌。现在我想要生成临时凭证,即访问密钥并为该用户分配访问密钥以访问aws服务。我怎么能这样做? 这是我用来生成令牌的一段代码。
var authenticationDetails = new cognito.AuthenticationDetails(authenticationData);
var userData = {
Username : '*****',
Pool : userPool
};
var cognitoUser = new cognito.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function (result) {
console.log(result);
我应该做些什么改变才能获得凭据?谢谢....
答案 0 :(得分:1)
在下面的代码中,我正在检索基于联邦身份的令牌以及临时凭证。
var data = {
UserPoolId: YOURUSER_POOL_ID,
ClientId: YOURAPP_CLIENT_ID,
};
var userPool = new cognito.CognitoUserPool(data);
var cognitoUser = userPool.getCurrentUser();
if (cognitoUser != null) {
cognitoUser.getSession(function(err, session) {
if (err) {
console.log(err);
return;
}
console.log('session validity: ' + session.isValid());
console.log('session Identity token: ' + session.getIdToken().getJwtToken());
AWS.config.region = YOURREGION;
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId : YOURIDENTITY_POOL_ID,
Logins : {
// Change the key below according to the specific region your user pool is in.
'cognito-idp.YOURREGIONNAME.amazonaws.com/YOURUSERPOOLID': session.getIdToken().getJwtToken()
}
});
AWS.config.credentials.get(function(err,data) {
if (!err) {
var id = AWS.config.credentials.identityId;
var key = AWS.config.credentials.accessKeyId;
var secretkey = AWS.config.credentials.secretAccessKey;
var sessionToken = AWS.config.credentials.sessionToken;
console.log('Cognito Identity ID '+ id);
console.log('Cognito Key '+ key);
console.log('Cognito Secret Key '+ secretkey);
console.log('Cognito SessionToken '+ sessionToken);
}
});
});
}
根据您的需要更改必要的参数。
希望对您有帮助