1- # This is my package laravel-cloudfront-cookies
1+ # Laravel CloudFront Cookies
22
33[ ![ Latest Version on Packagist] ( https://img.shields.io/packagist/v/maize-tech/laravel-cloudfront-cookies.svg?style=flat-square )] ( https://packagist.org/packages/maize-tech/laravel-cloudfront-cookies )
44[ ![ GitHub Tests Action Status] ( https://img.shields.io/github/actions/workflow/status/maize-tech/laravel-cloudfront-cookies/run-tests.yml?branch=main&label=tests&style=flat-square )] ( https://github.com/maize-tech/laravel-cloudfront-cookies/actions?query=workflow%3Arun-tests+branch%3Amain )
55[ ![ GitHub Code Style Action Status] ( https://img.shields.io/github/actions/workflow/status/maize-tech/laravel-cloudfront-cookies/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square )] ( https://github.com/maize-tech/laravel-cloudfront-cookies/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain )
66[ ![ Total Downloads] ( https://img.shields.io/packagist/dt/maize-tech/laravel-cloudfront-cookies.svg?style=flat-square )] ( https://packagist.org/packages/maize-tech/laravel-cloudfront-cookies )
77
8- This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
9-
10- ## Support us
11-
12- [ <img src =" https://github-ads.s3.eu-central-1.amazonaws.com/laravel-cloudfront-cookies.jpg?t=1 " width =" 419px " />] ( https://spatie.be/github-ad-click/laravel-cloudfront-cookies )
13-
14- We invest a lot of resources into creating [ best in class open source packages] ( https://spatie.be/open-source ) . You can support us by [ buying one of our paid products] ( https://spatie.be/open-source/support-us ) .
15-
16- We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [ our contact page] ( https://spatie.be/about-us ) . We publish all received postcards on [ our virtual postcard wall] ( https://spatie.be/open-source/postcards ) .
8+ 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.
179
1810## Installation
1911
@@ -23,37 +15,169 @@ You can install the package via composer:
2315composer require maize-tech/laravel-cloudfront-cookies
2416```
2517
26- You can publish and run the migrations with:
18+ You can publish the config file with:
2719
2820``` bash
29- php artisan vendor:publish --tag=" laravel-cloudfront-cookies-migrations"
30- php artisan migrate
21+ php artisan vendor:publish --tag=" laravel-cloudfront-cookies-config"
3122```
3223
33- You can publish the config file with :
24+ Or use the install command :
3425
3526``` bash
36- php artisan vendor:publish --tag= " laravel- cloudfront-cookies-config "
27+ php artisan cloudfront-cookies:install
3728```
3829
3930This is the contents of the published config file:
4031
4132``` php
4233return [
34+ /*
35+ |--------------------------------------------------------------------------
36+ | CloudFront API Version
37+ |--------------------------------------------------------------------------
38+ |
39+ | The version of the CloudFront API to use. Use 'latest' for the most
40+ | recent version or specify a specific version like '2020-05-31'.
41+ | Default: 'latest'
42+ |
43+ */
44+ 'version' => env('CLOUDFRONT_VERSION'),
45+
46+ /*
47+ |--------------------------------------------------------------------------
48+ | AWS Region
49+ |--------------------------------------------------------------------------
50+ |
51+ | The AWS region where your CloudFront distribution is configured.
52+ | CloudFront is a global service but requires a region for API calls.
53+ | Default: 'us-east-1'
54+ |
55+ */
56+ 'region' => env('CLOUDFRONT_REGION'),
57+
58+ /*
59+ |--------------------------------------------------------------------------
60+ | Resource Key
61+ |--------------------------------------------------------------------------
62+ |
63+ | The CloudFront resource URL pattern that the signed cookies will grant
64+ | access to. This should match your CloudFront distribution URL pattern.
65+ | Example: 'https://d111111abcdef8.cloudfront.net/*'
66+ |
67+ */
68+ 'resource_key' => env('CLOUDFRONT_RESOURCE_KEY'),
69+
70+ /*
71+ |--------------------------------------------------------------------------
72+ | Cookie Domain
73+ |--------------------------------------------------------------------------
74+ |
75+ | The domain for which the signed cookies will be valid. This should
76+ | start with a dot (.) to include all subdomains.
77+ | Example: '.example.com' or '.cloudfront.net'
78+ |
79+ */
80+ 'cookie_domain' => env('CLOUDFRONT_COOKIE_DOMAIN'),
81+
82+ /*
83+ |--------------------------------------------------------------------------
84+ | Private Key
85+ |--------------------------------------------------------------------------
86+ |
87+ | The CloudFront private key used to sign the cookies. This should be
88+ | the full PEM-encoded private key content.
89+ |
90+ */
91+ 'private_key' => env('CLOUDFRONT_PRIVATE_KEY'),
92+
93+ /*
94+ |--------------------------------------------------------------------------
95+ | Key Pair ID
96+ |--------------------------------------------------------------------------
97+ |
98+ | The ID of the CloudFront key pair associated with your private key.
99+ | You can find this in the AWS CloudFront console.
100+ |
101+ */
102+ 'key_pair_id' => env('CLOUDFRONT_KEY_PAIR_ID'),
103+
104+ /*
105+ |--------------------------------------------------------------------------
106+ | Expiration Interval
107+ |--------------------------------------------------------------------------
108+ |
109+ | The duration for which both the signed cookie policy and browser cookies
110+ | will be valid. This value is used for both CloudFront policy expiration
111+ | and browser cookie duration.
112+ |
113+ | Accepts:
114+ | - String: human-readable format like '1 hour', '30 minutes', '1 day'
115+ | - DateInterval: PHP DateInterval instance
116+ | - CarbonInterval: Carbon interval instance
117+ |
118+ | Default: '1 minutes'
119+ |
120+ | Examples:
121+ | - '30 days'
122+ | - '1 week'
123+ | - '2 hours'
124+ | - '45 minutes'
125+ | - CarbonInterval::make(30, Unit::Day)
126+ | - CarbonInterval::days(30)
127+ | - new DateInterval('P30D')
128+ |
129+ */
130+ 'expiration_interval' => env('CLOUDFRONT_EXPIRATION_INTERVAL'),
131+
132+ /*
133+ |--------------------------------------------------------------------------
134+ | Enabled
135+ |--------------------------------------------------------------------------
136+ |
137+ | Enable or disable CloudFront signed cookies. When disabled, cookies will
138+ | not be set even if the middleware is active.
139+ |
140+ | Default: true
141+ |
142+ */
143+ 'enabled' => env('CLOUDFRONT_ENABLED', true),
144+
145+ /*
146+ |--------------------------------------------------------------------------
147+ | Authentication Guard
148+ |--------------------------------------------------------------------------
149+ |
150+ | The authentication guard to use when checking if a user is authenticated
151+ | before setting CloudFront cookies. Set to null to use the default guard.
152+ |
153+ | Default: null (uses default guard)
154+ |
155+ */
156+ 'guard' => env('CLOUDFRONT_GUARD'),
43157];
44158```
45159
46- Optionally, you can publish the views using
160+ ## Usage
47161
48- ``` bash
49- php artisan vendor:publish --tag=" laravel-cloudfront-cookies-views"
162+ Add the ` SignCloudfrontCookies ` middleware to your routes or route groups:
163+
164+ ``` php
165+ use Maize\CloudfrontCookies\Http\Middleware\SignCloudfrontCookies;
166+
167+ Route::middleware(['auth', SignCloudfrontCookies::class])->group(function () {
168+ Route::get('/dashboard', [DashboardController::class, 'index']);
169+ Route::get('/profile', [ProfileController::class, 'show']);
170+ });
50171```
51172
52- ## Usage
173+ ** 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 ` :
53174
54175``` php
55- $cloudfrontCookies = new Maize\CloudfrontCookies();
56- echo $cloudfrontCookies->echoPhrase('Hello, Maize!');
176+ protected $except = [
177+ 'CloudFront-Policy',
178+ 'CloudFront-Signature',
179+ 'CloudFront-Key-Pair-Id',
180+ ];
57181```
58182
59183## Testing
0 commit comments