使用Cognito Federated Identities进行API网关身份验证

Maz*_*oth 8 javascript federated-identity amazon-web-services amazon-cognito aws-api-gateway

我想使用Cognito Federated Entity(允许通过Google登录等),允许访问用于web javascript应用程序的API Gateway.我设法通过与Google签约获得了Cognito的sessionToken,但我坚持使用API​​网关配置来启用会话令牌.

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

谢谢!

小智 16

由于您希望首先通过经过身份验证的Cognito身份调用API

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

  4. 您可以从API网关的舞台面板生成并下载SDK,而不是#3,并通过sdk调用api.

获得认知会话后,您可以使用下面的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) {
    //
});
Run Code Online (Sandbox Code Playgroud)