Skip to content

Commit 4e4bd50

Browse files
wip
1 parent 1289a25 commit 4e4bd50

File tree

12 files changed

+732
-49
lines changed

12 files changed

+732
-49
lines changed

README.md

Lines changed: 145 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
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:
2315
composer 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

3930
This is the contents of the published config file:
4031

4132
```php
4233
return [
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

config/cloudfront-cookies.php

Lines changed: 118 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,127 @@
11
<?php
22

33
return [
4-
'version' => null,
4+
/*
5+
|--------------------------------------------------------------------------
6+
| CloudFront API Version
7+
|--------------------------------------------------------------------------
8+
|
9+
| The version of the CloudFront API to use. Use 'latest' for the most
10+
| recent version or specify a specific version like '2020-05-31'.
11+
| Default: 'latest'
12+
|
13+
*/
14+
'version' => env('CLOUDFRONT_VERSION'),
515

6-
'region' => null,
16+
/*
17+
|--------------------------------------------------------------------------
18+
| AWS Region
19+
|--------------------------------------------------------------------------
20+
|
21+
| The AWS region where your CloudFront distribution is configured.
22+
| CloudFront is a global service but requires a region for API calls.
23+
| Default: 'us-east-1'
24+
|
25+
*/
26+
'region' => env('CLOUDFRONT_REGION'),
727

8-
'resource_key' => null,
28+
/*
29+
|--------------------------------------------------------------------------
30+
| Resource Key
31+
|--------------------------------------------------------------------------
32+
|
33+
| The CloudFront resource URL pattern that the signed cookies will grant
34+
| access to. This should match your CloudFront distribution URL pattern.
35+
| Example: 'https://d111111abcdef8.cloudfront.net/*'
36+
|
37+
*/
38+
'resource_key' => env('CLOUDFRONT_RESOURCE_KEY'),
939

10-
'cookie_domain' => null,
40+
/*
41+
|--------------------------------------------------------------------------
42+
| Cookie Domain
43+
|--------------------------------------------------------------------------
44+
|
45+
| The domain for which the signed cookies will be valid. This should
46+
| start with a dot (.) to include all subdomains.
47+
| Example: '.example.com' or '.cloudfront.net'
48+
|
49+
*/
50+
'cookie_domain' => env('CLOUDFRONT_COOKIE_DOMAIN'),
1151

12-
'private_key' => null,
52+
/*
53+
|--------------------------------------------------------------------------
54+
| Private Key
55+
|--------------------------------------------------------------------------
56+
|
57+
| The CloudFront private key used to sign the cookies. This should be
58+
| the full PEM-encoded private key content.
59+
|
60+
*/
61+
'private_key' => env('CLOUDFRONT_PRIVATE_KEY'),
1362

14-
'key_pair_id' => null,
63+
/*
64+
|--------------------------------------------------------------------------
65+
| Key Pair ID
66+
|--------------------------------------------------------------------------
67+
|
68+
| The ID of the CloudFront key pair associated with your private key.
69+
| You can find this in the AWS CloudFront console.
70+
|
71+
*/
72+
'key_pair_id' => env('CLOUDFRONT_KEY_PAIR_ID'),
73+
74+
/*
75+
|--------------------------------------------------------------------------
76+
| Expiration Interval
77+
|--------------------------------------------------------------------------
78+
|
79+
| The duration for which both the signed cookie policy and browser cookies
80+
| will be valid. This value is used for both CloudFront policy expiration
81+
| and browser cookie duration.
82+
|
83+
| Accepts:
84+
| - String: human-readable format like '1 hour', '30 minutes', '1 day'
85+
| - DateInterval: PHP DateInterval instance
86+
| - CarbonInterval: Carbon interval instance
87+
|
88+
| Default: '1 minutes'
89+
|
90+
| Examples:
91+
| - '30 days'
92+
| - '1 week'
93+
| - '2 hours'
94+
| - '45 minutes'
95+
| - CarbonInterval::make(30, Unit::Day)
96+
| - CarbonInterval::days(30)
97+
| - new DateInterval('P30D')
98+
|
99+
*/
100+
'expiration_interval' => env('CLOUDFRONT_EXPIRATION_INTERVAL'),
101+
102+
/*
103+
|--------------------------------------------------------------------------
104+
| Enabled
105+
|--------------------------------------------------------------------------
106+
|
107+
| Enable or disable CloudFront signed cookies. When disabled, cookies will
108+
| not be set even if the middleware is active.
109+
|
110+
| Default: true
111+
|
112+
*/
113+
'enabled' => env('CLOUDFRONT_ENABLED', true),
114+
115+
/*
116+
|--------------------------------------------------------------------------
117+
| Authentication Guard
118+
|--------------------------------------------------------------------------
119+
|
120+
| The authentication guard to use when checking if a user is authenticated
121+
| before setting CloudFront cookies. Set to null to use the default guard.
122+
|
123+
| Default: null (uses default guard)
124+
|
125+
*/
126+
'guard' => env('CLOUDFRONT_GUARD'),
15127
];

phpstan.neon.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ parameters:
55
level: 5
66
paths:
77
- src
8-
- config
98
tmpDir: build/phpstan
109
checkOctaneCompatibility: true
1110
checkModelProperties: true

0 commit comments

Comments
 (0)