【问题标题】:authenticated AWS API Gateway via javascript AND credentials provider通过 javascript 和凭证提供程序认证的 AWS API Gateway
【发布时间】:2017-01-29 14:03:12
【问题描述】:

我的设置包括一个带有 IAM 访问控制的 AWS API 网关和用于登录的 AWS cognito。 我已经从一个 Android 应用程序访问了 API,现在想构建一个网络应用程序 (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 中的相关代码 sn-p:

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?我找不到那个so far...的任何API

【问题讨论】:

    标签: javascript amazon-web-services aws-sdk aws-api-gateway


    【解决方案1】:

    感谢 Bob 为我指明了正确的方向!我现在已经弄清楚了,因此为了完整起见,这是我的问题的完整解决方案:

    来自创建 apigClient 的服务:

        return CognitoUtil.getCredentials()
        .then(() => 
              this.apigClient = apigClientFactory.newClient({
              accessKey: AWS.config.credentials.accessKeyId,
              secretKey: AWS.config.credentials.secretAccessKey,
              sessionToken: AWS.config.credentials.sessionToken,
              region: 'eu-central-1'}));
    

    getCredentials() 方法,这是获取所需临时凭证的关键:

    public static getCredentials():Promise{
    return new Promise((resolve, reject) => {
        CognitoUtil.getIdToken({
        callback() {
        },
        callbackWithParam(idTokenJwt:any) {
            let url = 'cognito-idp.' + CognitoUtil._REGION.toLowerCase() + '.amazonaws.com/' + CognitoUtil._USER_POOL_ID;
            let logins = {};
            logins[url] = idTokenJwt;
            let params = {
            IdentityPoolId: CognitoUtil._IDENTITY_POOL_ID, /* required */
            Logins: logins
            };
            AWS.config.region = CognitoUtil._REGION;
            AWS.config.credentials = new AWS.CognitoIdentityCredentials(params);
    
            AWS.config.credentials.refresh(result => {
            console.log(AWS.config.credentials);
            resolve();
            });
    
        }
        });
    });
    }
    

    所以这里的关键见解是,

    1. 我向用户池进行身份验证(显示在我的问题中)
    2. 我将它与身份提供者一起使用来检索临时凭据 (getCredentials)
    3. 我使用 AWS.config.credentials 中的临时凭证来设置 apigClient

    我希望这对其他人也有帮助。当然,我刚刚发布的代码可能会使用一些重构,因此非常欢迎任何 cmet!

    【讨论】:

      【解决方案2】:

      Cognito 实际上由 3 种不同的服务组成:

      • Cognito 您的用户池 - 您在此处集成的内容
      • Cognito Sync - 用于同步用户的用户偏好数据
      • Cognito 联合身份 - 用于将身份(FB、Google 或用户池)联合到您的帐户中并生成凭据。

      API 网关客户端期望的是来自 Cognito 联合身份的凭据。

      请参阅 Cognito documentation 以将您的用户池与 Cognito 联合身份集成。

      【讨论】:

        猜你喜欢
        • 2021-05-25
        • 2015-11-21
        • 2018-05-07
        • 2020-12-11
        • 2016-11-19
        • 2015-02-25
        • 1970-01-01
        • 2016-09-25
        • 2021-01-23
        相关资源
        最近更新 更多