0

現時点では、ユーザーが適切に認証されているかどうかに基づいて、サーバー HTML 用の AWS ApiGateway + Lambda を使用したアーキテクチャを念頭に置いています。この Cognito とカスタム Lambda オーソライザーを実現しようとしています。Lambda が常に HTML を返し、渡された Cookie に基づいて、ログイン/ログアウト状態の HTML を生成するようにしたいと考えています。私の考えでは、トークンの検証を行い、Lambda を生成する HTML にヘッダーを渡す別のオーソライザーを持つことが理想的です。

どうすればこれを達成できますか?

AWS Sam テンプレートを使用して CF スタックを定義しています。現在のテンプレートを参照してください:

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: A Lambda function for rendering HTML pages with authentication
Resources:
  WebAppGenerator:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: app.handler
      Runtime: nodejs12.x
      CodeUri: .
      Description: A Lambda that generates HTML pages dynamically
      MemorySize: 128
      Timeout: 20
      Events:
        ProxyRoute:
          Type: Api
          Properties:
            RestApiId: !Ref WebAppApi
            Path: /{proxy+}
            Method: GET
  WebAppApi:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      Auth:
        DefaultAuthorizer: WebTokenAuthorizer
        Authorizers:
          WebTokenAuthorizer:
            FunctionArn: !GetAtt WebAppTokenAuthorizer.Arn
  WebAppTokenAuthorizer:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: .
      Handler: authorizer.handler
      Runtime: nodejs12.x

私のオーソライザー (Typescript) では、常に「許可」効果を持つポリシーを生成することを考えていました。ただし、認証トークン (まだ Cookie ベースではない) が見つからない場合は、既に 403 が返されています。以下を参照してください。


function generatePolicy(principalId: string, isAuthorized: boolean, resource): APIGatewayAuthorizerResult {
    const result: APIGatewayAuthorizerResult = {
        principalId,
        policyDocument: {
            Version: '2012-10-17',
            Statement: []
        }
    };

    if (resource) {
        result.policyDocument.Statement[0] = {
            Action: 'execute-api:Invoke',
            Effect: 'Allow',
            Resource: resource
        };
    }

    result.context = {
        isAuthorized
    };

    return result
}
4

1 に答える 1