Skip to main content

cloudcomponents Logo

@cloudcomponents/cdk-cloudfront-authorization

Build Status cdkdx typescript python Mentioned in Awesome CDK

CloudFront with Cognito authentication using Lambda@Edge

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

Install

TypeScript/JavaScript:

npm i @cloudcomponents/cdk-cloudfront-authorization

Python:

pip install cloudcomponents.cdk-cloudfront-authorization

How to use SPA

import { SpaAuthorization, SpaDistribution } from '@cloudcomponents/cdk-cloudfront-authorization';
import { Stack, StackProps, aws_cognito } from 'aws-cdk-lib';
import { Construct } from 'constructs';

export class CloudFrontAuthorizationStack extends Stack {
  constructor(scope: Construct, id: string, props: StackProps) {
    super(scope, id, props);

    const userPool = new aws_cognito.UserPool(this, 'UserPool', {
      selfSignUpEnabled: false,
      userPoolName: 'cloudfront-authorization-userpool',
    });

    // UserPool must have a domain!
    userPool.addDomain('Domain', {
      cognitoDomain: {
        domainPrefix: 'cloudcomponents',
      },
    });

    const authorization = new SpaAuthorization(this, 'Authorization', {
      userPool,
    });

    new SpaDistribution(this, 'Distribution', {
      authorization,
    });
  }
}

How to use StaticSite

import { SpaAuthorization, SpaDistribution } from '@cloudcomponents/cdk-cloudfront-authorization';
import { Stack, StackProps, aws_cognito } from 'aws-cdk-lib';
import { Construct } from 'constructs';

export class CloudFrontAuthorizationStack extends Stack {
  constructor(scope: Construct, id: string, props: StackProps) {
    super(scope, id, props);

    const userPool = new aws_cognito.UserPool(this, 'UserPool', {
      selfSignUpEnabled: false,
      userPoolName: 'cloudfront-authorization-userpool',
    });

    // UserPool must have a domain!
    userPool.addDomain('Domain', {
      cognitoDomain: {
        domainPrefix: 'cloudcomponents',
      },
    });

    const authorization = new StaticSiteAuthorization(this, 'Authorization', {
      userPool,
    });

    new StaticSiteDistribution(this, 'Distribution', {
      authorization,
    });
  }
}

Identity Providers

Identity providers can be specified in the authorization object. To make sure that the user pool client is created after the identity provider, please specify a dependency using "addDependency".

const identityProvider = UserPoolIdentityProviderAmazon(this, "IdentityProvider", {
  // ...
})
const authorization = new SpaAuthorization(this, 'Authorization_SPA', {
  // ...
  identityProviders: [cognito.UserPoolClientIdentityProvider.AMAZON],
};
authorization.userPoolClient.node.addDependency(identityProvider);

SPA mode vs. Static Site mode

SPA

  • User Pool client does not use a client secret
  • The cookies with JWT's are not "http only", so that they can be read and used by the SPA (e.g. to display the user name, or to refresh tokens)
  • 404's (page not found on S3) will return index.html, to enable SPA-routing

Static Site

  • Enforce use of a client secret
  • Set cookies to be http only by default (unless you've provided other cookie settings explicitly)
  • No special error handling

API Reference

See API.md.

Example

See more complete examples.

License

MIT

Metadata

Release files for cloudcomponents.cdk-cloudfront-authorization 2.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cloudcomponents.cdk-cloudfront-authorization 2.4.0
File Size Uploaded
cloudcomponents.cdk-cloudfront-authorization-2.4.0.tar.gz 1.2 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for cloudcomponents.cdk-cloudfront-authorization 2.4.0
File Interpreter ABI Platform
cloudcomponents.cdk_cloudfront_authorization-2.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 2.3 MB

Release files / cloudcomponents.cdk-cloudfront-authorization-2.4.0.tar.gz

Download URL cloudcomponents.cdk-cloudfront-authorization-2.4.0.tar.gz
Size 1.2 MB
Tags Source
SHA-256 checksum
How to use checksums
dcd500e0cc9fb591d2fd8702abd3d2c036dabcc98fec44c6461785f0aa9b570d
BLAKE2b-256 checksum
How to use checksums
8fff7183ff4d1d491db89bcf7bafc6fe151aa771f3831032417268d807318938
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.2

Release files / cloudcomponents.cdk_cloudfront_authorization-2.4.0-py3-none-any.whl

Download URL cloudcomponents.cdk_cloudfront_authorization-2.4.0-py3-none-any.whl
Size 1.2 MB
Tags Python 3
SHA-256 checksum
How to use checksums
0c5757447635018a5f39a3bc15722aa4011e7f524abcb30e13a2f3cabe1c6fec
BLAKE2b-256 checksum
How to use checksums
c38e24b3ace8db8117c26f80b61185c9f7ac0495a40a19b640eeb32661ffe4d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.2

Release history Release notifications | RSS feed

This release

2.4.0 This release

2 release files

2.3.0

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.51.0

2 release files

1.50.0

2 release files

1.49.0

2 release files

1.48.0

2 release files

1.47.0

2 release files

1.46.0

2 release files

1.44.0

2 release files

1.43.0

2 release files

1.41.1

2 release files

1.41.0

2 release files

1.40.0

2 release files

1.39.1

2 release files

1.39.0

2 release files

1.38.1

2 release files

1.38.0

2 release files

1.36.0

2 release files

1.35.0

2 release files

1.32.0

2 release files

1.31.1

2 release files

1.31.0

2 release files

1.30.0

2 release files

1.29.0

2 release files

1.27.0

2 release files

1.26.0

2 release files

1.25.1

2 release files

1.23.0

2 release files

1.22.1

2 release files

1.22.0

2 release files

1.21.0

2 release files

1.20.0

2 release files

1.17.0

2 release files

1.16.0

2 release files

1.15.2

2 release files

1.15.0

2 release files

1.14.0

2 release files

1.11.0

2 release files

1.10.0

2 release files

1.9.0

2 release files

1.8.0

2 release files

1.7.0

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.2

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.7

2 release files

1.1.6

2 release files

1.1.5

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page