Skip to content

Commit 10b58d0

Browse files
wip
1 parent 1808075 commit 10b58d0

File tree

1 file changed

+80
-7
lines changed

1 file changed

+80
-7
lines changed

README.md

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,7 @@ You can install the package via composer:
1515
composer require maize-tech/laravel-cloudfront-cookies
1616
```
1717

18-
You can publish the config file with:
19-
20-
```bash
21-
php artisan vendor:publish --tag="laravel-cloudfront-cookies-config"
22-
```
23-
24-
Or use the install command:
18+
You can install and configure the package with:
2519

2620
```bash
2721
php artisan cloudfront-cookies:install
@@ -157,6 +151,85 @@ return [
157151
];
158152
```
159153

154+
## AWS CloudFront Setup
155+
156+
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.
157+
158+
### 1. Generate Public and Private Keys
159+
160+
Create a new RSA private key:
161+
162+
```bash
163+
openssl genrsa -out cloudfront-private.key 2048
164+
```
165+
166+
Extract the public key:
167+
168+
```bash
169+
openssl rsa -pubout -in cloudfront-private.key -out cloudfront-public.key
170+
```
171+
172+
**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.
173+
174+
### 2. Create a CloudFront Key Group
175+
176+
1. Go to the AWS CloudFront console
177+
2. Navigate to **Key management** > **Public keys**
178+
3. Click **Create public key**
179+
4. Give it a name (e.g., "My App Public Key")
180+
5. Paste the content of `cloudfront-public.key`
181+
6. Save and note down the **Key ID** (you'll need this for `CLOUDFRONT_KEY_PAIR_ID`)
182+
7. Navigate to **Key groups** and create a new key group
183+
8. Add the public key you just created to this key group
184+
185+
### 3. Create a CloudFront Distribution
186+
187+
**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.
188+
189+
1. Go to the CloudFront console and click **Create distribution**
190+
2. Under **Origin domain**, select your S3 bucket with the assets
191+
3. Under **Default cache behavior**:
192+
- Set **Restrict viewer access** to **Yes**
193+
- Select the key group you created earlier
194+
4. Under **Settings**:
195+
- Add your custom SSL certificate (e.g., `*.example.com`)
196+
- Under **Alternate domain name (CNAME)**, add your CloudFront domain (e.g., `cdn.example.com`)
197+
5. Create the distribution (this may take 10-15 minutes to deploy)
198+
6. Note down the **Distribution domain name** (e.g., `d1234abcd.cloudfront.net`) and the full CloudFront URL (e.g., `https://d1234abcd.cloudfront.net/*`)
199+
200+
### 4. Configure Route 53 DNS
201+
202+
1. Go to Route 53 and select your hosted zone
203+
2. Click **Create record**
204+
3. Set the record name to match your CloudFront CNAME (e.g., `cdn`)
205+
4. Enable the **Alias** toggle
206+
5. Select **CloudFront distribution** as the alias target
207+
6. Select your distribution from the dropdown
208+
7. Create the record
209+
210+
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.
211+
212+
### 5. Update S3 Bucket CORS Policy
213+
214+
1. Go to your S3 bucket
215+
2. Navigate to the **Permissions** tab
216+
3. Scroll to the **Cross-origin resource sharing (CORS)** section
217+
4. Update the policy (replace `example.com` and `cdn.example.com` with your domains):
218+
219+
```json
220+
[
221+
{
222+
"AllowedHeaders": ["*"],
223+
"AllowedMethods": ["GET", "HEAD"],
224+
"AllowedOrigins": [
225+
"https://example.com",
226+
"https://cdn.example.com"
227+
],
228+
"ExposeHeaders": ["ETag"]
229+
}
230+
]
231+
```
232+
160233
## Usage
161234

162235
Add the `SignCloudfrontCookies` middleware to your routes or route groups:

0 commit comments

Comments
 (0)