Download the PHP package maize-tech/laravel-cloudfront-cookies without Composer
On this page you can find all versions of the php package maize-tech/laravel-cloudfront-cookies. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download maize-tech/laravel-cloudfront-cookies
More information about maize-tech/laravel-cloudfront-cookies
Files in maize-tech/laravel-cloudfront-cookies
Package laravel-cloudfront-cookies
Short Description This is my package laravel-cloudfront-cookies
License MIT
Homepage https://github.com/maize-tech/laravel-cloudfront-cookies
Informations about the package laravel-cloudfront-cookies
Laravel CloudFront Cookies
A Laravel package to easily manage AWS CloudFront signed cookies for authenticated users. This package automatically generates and manages CloudFront signed cookies, allowing you to restrict access to your CloudFront distributions based on user authentication status. Cookies are automatically set for authenticated users and cleared on logout.
Installation
You can install the package via composer:
You can install and configure the package with:
This is the contents of the published config file:
AWS CloudFront Setup
Before using this package, you need to configure AWS CloudFront with signed cookies. This guide assumes you already have an S3 bucket with your assets and a domain name hosted on the internet.
1. Generate Public and Private Keys
Create a new RSA private key:
Extract the public key:
Important: Store the cloudfront-private.key file in your Laravel application's storage directory. This is the default location the package looks for the private key.
2. Create a CloudFront Key Group
- Go to the AWS CloudFront console
- Navigate to Key management > Public keys
- Click Create public key
- Give it a name (e.g., "My App Public Key")
- Paste the content of
cloudfront-public.key - Save and note down the Key ID (you'll need this for
CLOUDFRONT_KEY_PAIR_ID) - Navigate to Key groups and create a new key group
- Add the public key you just created to this key group
3. Create a CloudFront Distribution
Important: Your CloudFront distribution must use the same root domain as your application. For example, if your application is at example.com, your CloudFront domain should be something like assets.example.com or cdn.example.com. This is necessary because cookies can only be set for domains you own.
- Go to the CloudFront console and click Create distribution
- Under Origin domain, select your S3 bucket with the assets
- Under Default cache behavior:
- Set Restrict viewer access to Yes
- Select the key group you created earlier
- Under Settings:
- Add your custom SSL certificate (e.g.,
*.example.com) - Under Alternate domain name (CNAME), add your CloudFront domain (e.g.,
cdn.example.com)
- Add your custom SSL certificate (e.g.,
- Create the distribution (this may take 10-15 minutes to deploy)
- Note down the Distribution domain name (e.g.,
d1234abcd.cloudfront.net) and the full CloudFront URL (e.g.,https://d1234abcd.cloudfront.net/*)
4. Configure DNS
Access your domain name's provider DNS panel and create a CNAME record pointing to your ClouFront distribution, i.e.:
If you are using AWS Route 53, follow these steps:
- Go to Route 53 and select your hosted zone
- Click Create record
- Set the record name to match your CloudFront CNAME (e.g.,
cdn) - Enable the Alias toggle
- Select CloudFront distribution as the alias target
- Select your distribution from the dropdown
- Create the record
If you don't see your distribution in the dropdown, you can use a CNAME record type instead and use the CloudFront domain name as the value.
5. Update S3 Bucket CORS Policy
- Go to your S3 bucket
- Navigate to the Permissions tab
- Scroll to the Cross-origin resource sharing (CORS) section
- Update the policy (replace
example.comandcdn.example.comwith your domains):
Make sure to protect your bucket by seleting "Block all public access" under "Permissions".
Usage
Add the SignCloudfrontCookies middleware to your routes or route groups:
Note for Laravel versions prior to 11: You need to manually exclude CloudFront cookies from encryption. Add the following to your app/Http/Middleware/EncryptCookies.php:
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Enrico De Lazzari
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-cloudfront-cookies with dependencies
aws/aws-sdk-php Version ^3.337
illuminate/contracts Version ^10.0||^11.0||^12.0
spatie/laravel-package-tools Version ^1.16