项目作者: henrist

项目描述 :
CloudFront authorization with Cognito for CDK
高级语言: TypeScript
项目地址: git://github.com/henrist/cdk-cloudfront-auth.git
创建时间: 2020-07-11T01:58:54Z
项目社区:https://github.com/henrist/cdk-cloudfront-auth

开源协议:MIT License

下载


CloudFront authorization with Cognito for CDK

Easily add Cognito-based authorization to your CloudFront distribution,
to place static files behind authorization.

This is based on https://github.com/aws-samples/cloudfront-authorization-at-edge.

Usage

  1. npm install @henrist/cdk-cloudfront-auth

Deploy the Lambda@Edge functions to us-east-1:

  1. // In a stack deployed to us-east-1.
  2. const authLambdas = new AuthLambdas(this, "AuthLambdas", {
  3. regions: ["eu-west-1"], // Regions to make Lambda version params available.
  4. })

Deploy the Cognito and CloudFront setup in whatever region
of your choice:

  1. const auth = new CloudFrontAuth(this, "Auth", {
  2. cognitoAuthDomain: `${domain.domainName}.auth.${region}.amazoncognito.com`,
  3. authLambdas, // AuthLambdas from above
  4. userPool, // Cognito User Pool
  5. })
  6. const distribution = new cloudfront.Distribution(this, "Distribution", {
  7. defaultBehavior: auth.createProtectedBehavior(origin),
  8. additionalBehaviors: auth.createAuthPagesBehaviors(origin),
  9. })
  10. auth.updateClient("ClientUpdate", {
  11. signOutUrl: `https://${distribution.distributionDomainName}${auth.signOutRedirectTo}`,
  12. callbackUrl: `https://${distribution.distributionDomainName}${auth.callbackPath}`,
  13. })

If using CloudFrontWebDistribution instead of Distribution:

  1. const distribution = new cloudfront.CloudFrontWebDistribution(this, "Distribution", {
  2. originConfigs: [
  3. {
  4. behaviors: [
  5. ...auth.authPages,
  6. {
  7. isDefaultBehavior: true,
  8. lambdaFunctionAssociations: auth.authFilters,
  9. },
  10. ],
  11. },
  12. ],
  13. })

Customizing authorization

The CloudFrontAuth construct accepts a requireGroupAnyOf property
that causes access to be restricted to only users in specific groups.