使用Cognito联邦身份的API网关身份验证



我想使用Cognito联邦实体(允许通过谷歌等登录),以允许访问API网关的web javascript应用程序。我设法通过与谷歌登录获得Cognito的sessionToken,但我被困在API网关配置上以启用会话令牌。

对于整个联邦实体身份验证工作流程是否有很好的教程?

谢谢!

由于您希望通过经过身份验证的Cognito身份调用api,因此首先

  1. 修改identitypool的授权角色,使其具有api执行策略,您可以将托管策略"AmazonAPIGatewayInvokeFullAccess"附加到相应的角色
  2. 在API网关对应的方法请求下,添加Authorization as"AWS_IAM"
  3. 您需要在使用"IAM"认证时签名请求,这里解释https://docs.aws.amazon.com/general/latest/gr/signing_aws_api_requests.html

  4. 代替#3,您可以从API网关的舞台面板生成和下载SDK,并通过SDK调用API。

一旦获得了cognito会话,就可以使用下面的sdk进行调用

var apigClient = apigClientFactory.newClient({
    accessKey: AWSCognito.config.credentials.accessKeyId,
    secretKey: AWSCognito.config.credentials.secretAccessKey,
    sessionToken: AWSCognito.config.credentials.sessionToken
});
var params = {
    // This is where any modeled request parameters should be added.
    // The key is the parameter name, as it is defined in the API in API Gateway.
};
var body = {};
var additionalParams = {
    // If there are any unmodeled query parameters or headers that must be
    //   sent with the request, add them here.
    headers: {
        'Content-Type': 'application/json'
    },
    queryParams: {}
};
apigClient.<resource><Method>(params, body, additionalParams)
.then(function(result) {
    // 
}).catch(function(err) {
    //
});

最新更新