问题描述
我有一个Cognito用户池和身份池.我已经在用户池中创建了一个用户.我使用了lambda为该用户获取了令牌,即访问,刷新,ID令牌.现在,我想要生成临时凭据,即该用户访问AWS服务的访问密钥和秘密访问密钥.我该怎么办?这是我用来生成令牌的代码.
I have a cognito user pool and identity pool. I have created an user in user pool. I got the tokens i.e. access, refresh, id tokens using lambda for that user. Now I want to generate the temporary credentials i.e. access key and secrete access key for that user to access the aws services. How could I do this?This is piece of code i used to generate tokens.
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);
我应该对此进行哪些更改以获取凭据?谢谢....
what changes should i do in this to get credentials? Thank you....
推荐答案
在下面的代码中,我正在基于联合身份检索令牌以及临时凭据.
IN the below code i am retrieving tokens along with temporary cred's based on federated identities.
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);
}
});
});
}
根据您的需要更改必要的参数.
Change the necessary parameters according to yours.
希望对您有帮助
这篇关于如何使用Cognito身份池生成用于访问AWS服务的临时凭证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!