【问题标题】:API Gateway authentication with Cognito Federated Identities使用 Cognito 联合身份进行 API 网关身份验证
【发布时间】:2016-12-25 10:42:32
【问题描述】:

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

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

谢谢!

【问题讨论】:

    标签: javascript amazon-web-services amazon-cognito aws-api-gateway federated-identity


    【解决方案1】:

    由于您想通过经过身份验证的 Cognito 身份调用 API,所以首先

    1. 修改身份池的身份验证角色以让 api 执行策略,您可以将托管策略“AmazonAPIGatewayInvokeFullAccess”附加到相应的角色
    2. 在相应方法请求下的API网关中,添加授权为 “AWS_IAM”
    3. 您需要在使用“IAM”身份验证时签署请求,此处解释为https://docs.aws.amazon.com/general/latest/gr/signing_aws_api_requests.html

    4. 您可以从 API 网关的阶段面板生成和下载 SDK,然后通过 sdk 调用 api,而不是 #3。

    获得 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) {
        //
    });
    

    【讨论】:

    • 非常感谢帕莎!我会试试这个。
    • 有时 newClient 函数也需要 "region:" 属性
    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    • 2018-04-05
    • 2017-03-25
    • 1970-01-01
    • 2017-05-19
    相关资源
    最近更新 更多