javascript – 如何在AWS上获取凭据?我必须使用AWS Cognito SDK或AWS SDK吗?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 如何在AWS上获取凭据?我必须使用AWS Cognito SDK或AWS SDK吗?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3332字,纯文字阅读大概需要5分钟。
内容图文
我正在使用AWS SDK为AWS S3构建一个丰富的前端客户端.它可以很好地将IAM凭证硬编码到AWS SDK配置中,但我想通过AWS Cognito配置对最终用户的访问权限.
如何获取我在Cognito用户池中创建的用户的凭据?
AWS文档非常不清楚.我见过使用AWS SDK的示例和我见过的示例,您需要拥有不同的AWS Cognito SDK.哪个文档文章是正确的?
对于这个特定项目,我根本不想使用第三方联合身份.
更新2:
AWS Cognito SDK已合并到名为AWS Amplify的新库中.
https://github.com/aws/aws-amplify
更新1:
问题是双重的.理解术语和正确设置Cognito是第一部分.感谢Bruce0让我在那里跑步.第二部分是javascript部分.事实证明,您需要两个单独的库来完全从javascript验证Cognito用户.
这是我提出的解决方案.如果它看起来很陌生,请确保您已经使用promises / async / await研究了es6.要在节点中运行它,您只需要使用babel,预设es2015,预设stage-2和babel-transform-runtime插件对其进行预处理.如果你想在浏览器中运行它,你可能需要使用带有webpack的babel-loader(设置非常复杂,只是预警).
import AWS from 'aws-sdk/global';
import S3 from 'aws-sdk/clients/s3';
import {
AuthenticationDetails,
CognitoUser,
CognitoUserPool,
} from 'amazon-cognito-identity-js';
const REGION = 'some-string-value';
const USER_POOL_ID = 'some-string-value';
const IDENTITY_POOL_ID = 'some-string-value';
const APP_CLIENT_ID = 'some-string-value';
const POOL_KEY = `cognito-idp.${REGION}.amazonaws.com/${USER_POOL_ID}`;
let Username = 'some-string-value';
let Password = 'some-string-value';
let authenticationDetails = new AuthenticationDetails({
Username,
Password
});
let userPool = new CognitoUserPool({
UserPoolId: USER_POOL_ID,
ClientId: APP_CLIENT_ID
});
let cognitoUser = new CognitoUser({
Username,
Pool: userPool
});
let skateboards = {
mfaRequired(codeDeliveryDetails) {
let mfaCode = prompt('MFA code is required!');
cognitoUser.sendMFACode(mfaCode, mfaRequired);
},
newPasswordRequired(userAttributes, requiredAttributes) {
delete userAttributes.email_verified; // it's returned but not valid to submit
let newPassword = prompt('A new password is required!');
cognitoUser.completeNewPasswordChallenge(newPassword, userAttributes, newPasswordRequired);
}
};
let updateAWSCreds = (jwtToken) => {
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: IDENTITY_POOL_ID,
Logins: {
[POOL_KEY]: jwtToken
}
});
};
let authenticateCognitoUser = async ({mfaRequired, newPasswordRequired} = skateboards) => {
return new Promise((resolve, reject) => {
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess(result) {
let jwtToken = result.getIdToken().getJwtToken();
updateAWSCreds(jwtToken);
resolve();
},
onFailure(err) {
reject(err);
},
mfaRequired,
newPasswordRequired
});
});
};
let doSomethingInS3ForExample = async () => {
await authenticateCognitoUser();
// now do your stuff
};
doSomethingInS3ForExample();
https://gist.github.com/sbussard/7344c6e1f56051da0758d1403a4343b1
解决方法:
在Cognito中获取凭据的唯一方法是创建联合身份池. cognitoIdentity和credentialsProvider方法是您获取凭据的方式.
您不必使用联合身份来使用联合身份池,但您需要拥有identityId(以及那些仅存在于池中)以获取凭据.
因此,您将使用cognitoUserPool作为联合身份池中的唯一身份验证提供程序(不会进行任何联合),并且该身份池会提供IAM角色(通常为Authenticated和Unauthenticated)(但您可以拥有更多角色).
这是一个关于powerpoint的链接,解释了Cognito的一些基本混淆方面,我希望有所帮助. Cognito outline
最后,我建议您使用AWS Mobile Hub(至少作为示例)开始实施,而不是使用IOS SDK.移动中心是一个薄的包装,但做了很多起伏,并且很好地构建.
内容总结
以上是互联网集市为您收集整理的javascript – 如何在AWS上获取凭据?我必须使用AWS Cognito SDK或AWS SDK吗?全部内容,希望文章能够帮你解决javascript – 如何在AWS上获取凭据?我必须使用AWS Cognito SDK或AWS SDK吗?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。