如何在 AWS 上获取凭证?我必须使用 AWS Cognito SDK 还是 AWS SDK?

How do I get credentials on AWS? Must I use AWS Cognito SDK or AWS SDK?

我正在使用 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。要在 node 中 运行 它,你只需要使用 babel、preset es2015、preset stage-2 和 babel-transform-运行time 插件对其进行预处理。如果您想 运行 在浏览器中使用它,您可能需要将 babel-loader 与 webpack 一起使用(众所周知,设置起来非常复杂,只是一个预警)。

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 角色(通常是已验证和未验证)(但您可以拥有更多角色) .

这是一个 link 幻灯片,解释了 Cognito 的一些基本的令人困惑的方面,希望对您有所帮助。 Cognito outline

最后,我建议您使用 AWS Mobile Hub(至少作为示例)开始实施,而不是使用 IOS SDK。 Mobile Hub 是一个薄包装,但做了很多繁重的工作,而且架构很好。

下面这对我有用,我设置了 cognito 来创建一个身份池,并设置了 IAM 来将配置文件添加到用户(我完全遵循了这个 link:https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/s3-example-photo-album.html

import S3 from 'aws-sdk/clients/s3'
import AWS from 'aws-sdk/global'

AWS.config.update({
  region: process.env.AZ_REGION,
  credentials: new AWS.CognitoIdentityCredentials({
    IdentityPoolId: 'XXX'
  })
});

const s3 = new AWS.S3({
apiVersion: '2006-03-01',
params: {Bucket: process.env.AZ_BUCKETNAME},
      });

我只希望我们不需要导入 aws-sdk/global 它本身是 206k unminified。对用户来说太多了。