javascript - 通过 javascript 和凭证提供商进行身份验证的 AWS API 网关

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

我的设置包括具有 IAM 访问控制功能的 AWS API 网关和用于登录的 AWS cognito。 我已经从 Android 应用程序访问了 API,现在想构建一个 Web 应用程序(Angular2)来执行相同的操作。 在 Android 上,我使用 AWSCognitoCredentialsProvider 为 API SDK 提供所需的凭证。 (http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-generate-sdk.html)

不幸的是,我不知道如何/是否可以使用 javascript SDK 做到这一点?

我使用cognito登录并获取 session ID、访问 token 等没有遇到任何问题。但是,API SDK要求我提供accessKey和secretKey。

以下是生成的 API SDK 中的相关代码片段:

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?我无法找到任何 API so far...

最佳答案

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

来自创建 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

我希望这对其他人也有帮助。当然,我刚刚发布的代码可能需要进行一些重构,因此非常欢迎对此提出任何评论!

关于javascript - 通过 javascript 和凭证提供商进行身份验证的 AWS API 网关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39614546/

相关文章:

javascript - 克隆后选择元素不起作用

javascript - firebase result.value 为 null 并等待

amazon-web-services - 防止在AWS API Gateway中缓存非200个响应

node.js - 有什么方法可以为用户生成认知身份验证 token 而无需在 aws-sdk 或 aws-amplify 中提供密码?

javascript - 如何根据前一个下拉值的选择启用/禁用第二个下拉列表值

amazon-web-services - 请求超时(HTTP 408): loadbalancer backed by ecs-fargate with nginx image

python - Boto3 - 在不使用访问 key 的情况下使用 Amazon Cognito 进行身份验证

javascript - Dynamodb 中的项目的值为 undefined

node.js - S3 API 的 getObject 回调内部没有任何内容在 Lambda 函数中运行

javascript - 无效值错误: setPosition: not a LatLng or LatLngLiteral: not an Object