【问题标题】:Calling AWS API Gateway using AWS Cognito使用 AWS Cognito 调用 AWS API Gateway
【发布时间】:2017-05-29 04:10:34
【问题描述】:

我们使用 AWS V4 签名机制从我们的 iOS 应用程序调用我们的 API Gateway 端点。我们已经在代码中嵌入了访问密钥 ID 和密钥,运行良好。显然这不是一种安全的方式,推荐的方式是使用 AWS Cognito。 我想知道我们如何使用从我的 Objective-C iOS 代码中的 AWSCredentials 对象获得的临时访问密钥和秘密(可能还有会话密钥)向我们的 API Gateway 端点发出安全请求。

我们尝试使用从 Cognito 检索到的临时访问密钥和秘密来生成 V4 签名来代替帐户访问密钥和秘密,但这似乎不是正确的方法。 API Gateway 方法使用 AWS_IAM 作为授权设置启用。

这是我们得到的错误:

{ status code: 403, headers {
    Connection = "keep-alive";
    "Content-Length" = 69;
    "Content-Type" = "application/json";
    Date = "Fri, 13 Jan 2017 10:26:38 GMT";
    Via = "1.1 .....cloudfront.net (CloudFront)";
    "X-Amz-Cf-Id" = "...";
    "X-Cache" = "Error from cloudfront";
    "x-amzn-ErrorType" = UnrecognizedClientException;
    "x-amzn-RequestId" = "...";
} }

使用的 IdentityPoolId 来自在 AWS Cognito 中的联合身份下创建的身份池

AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionUSEast1 identityPoolId:@"us-east-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"];

我们使用未经身份验证的角色,因为我们不需要任何形式的用户特定身份验证。这个角色有以下政策: AmazonAPIGatewayInvokeFullAccess AmazonAPIGatewayPushToCloudWatchLogs CloudFront 完全访问 AmazonCognitoDeveloperAuthenticatedIdentities AmazonAPIGateway 管理员 CloudFront 只读访问 IAM只读访问 AmazonCognitoPowerUser

您能否在此处帮助我如何使用 Cognito 生成 V4 签名或完全绕过该过程。

【问题讨论】:

  • 您是否注册了正在使用您的凭据提供程序的配置?

标签: ios amazon-web-services aws-api-gateway aws-cognito


【解决方案1】:

您似乎收到 UnrecognizedClientException 作为响应,但 API Gateway 不返回 UnrecognizedClientException。您是否有收到错误的请求 ID?

万一您可能忘记注册配置,您需要将配置注册到服务管理器。

AWSCognitoCredentialsProvider *creds = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionUSEast1 identityPoolId:your_cognito_pool_id];

AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:creds];

AWSServiceManager.defaultServiceManager.defaultServiceConfiguration = configuration;

对于您的未经身份验证的角色策略,我认为您向未经身份验证的用户授予了过于强大的权限。如果您希望他们能够调用您的 API,您可以只给他们 AmazonAPIGatewayInvokeFullAccess 甚至将范围缩小到方法级别。

【讨论】:

    【解决方案2】:

    使用从 AWSCredentials 对象获取的变量 sessionKey 的值发送 HTTP 标头“x-amz-security-token”解决了问题:

    [request setValue:sessionToken forHTTPHeaderField:@"X-Amz-Security-Token"];
    

    使用以下方法检索 AWSCredentials 对象:

    [[credentialsProvider credentials] continueWithBlock:^id(AWSTask<AWSCredentials *> *task) {
            if (task.error) {
                DDLogCError(@"failed getting credentials: %@", task.error);
            }
            else {
                AWSCredentials *credentials = task.result;
            }
            return nil;
        }]
    

    是的,已将策略精简为一项 - AmazonAPIGatewayInvokeFullAccess。 感谢您的反馈。

    【讨论】:

      猜你喜欢
      • 2022-11-15
      • 2017-12-30
      • 2017-12-07
      • 2018-12-16
      • 2016-08-24
      • 2023-03-31
      • 2016-09-06
      • 2017-12-11
      • 1970-01-01
      相关资源
      最近更新 更多