Skip to content

Commit 3bdf240

Browse files
wip
1 parent 864dd19 commit 3bdf240

File tree

5 files changed

+92
-15
lines changed

5 files changed

+92
-15
lines changed

composer.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
}
1717
],
1818
"require": {
19-
"php": "^8.4",
20-
"spatie/laravel-package-tools": "^1.16",
21-
"illuminate/contracts": "^10.0||^11.0||^12.0"
19+
"php": "^8.2",
20+
"aws/aws-sdk-php": "^3.343",
21+
"illuminate/contracts": "^10.0||^11.0||^12.0",
22+
"spatie/laravel-package-tools": "^1.16"
2223
},
2324
"require-dev": {
2425
"laravel/pint": "^1.14",
@@ -71,4 +72,4 @@
7172
},
7273
"minimum-stability": "dev",
7374
"prefer-stable": true
74-
}
75+
}

config/cloudfront-cookies.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
<?php
22

3-
// config for Maize/CloudfrontCookies
43
return [
4+
'version' => null,
55

6+
'region' => null,
7+
8+
'public_key_path' => null,
9+
10+
'private_key_path' => null,
11+
12+
// 'domain' => null,
13+
14+
// 'resource' => null,
15+
16+
// 'cookies_expiration' => [
17+
// 'unit' => \Carbon\CarbonInterval::PERIOD_DAYS,
18+
// 'value' => 2,
19+
// ],
20+
21+
// 'cookies_domain' => null,
622
];

src/CloudfrontCookiesServiceProvider.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,26 @@
22

33
namespace Maize\CloudfrontCookies;
44

5-
use Maize\CloudfrontCookies\Commands\CloudfrontCookiesCommand;
5+
use Aws\CloudFront\CloudFrontClient;
66
use Spatie\LaravelPackageTools\Package;
77
use Spatie\LaravelPackageTools\PackageServiceProvider;
88

99
class CloudfrontCookiesServiceProvider extends PackageServiceProvider
1010
{
1111
public function configurePackage(Package $package): void
1212
{
13-
/*
14-
* This class is a Package Service Provider
15-
*
16-
* More info: https://github.com/spatie/laravel-package-tools
17-
*/
1813
$package
1914
->name('laravel-cloudfront-cookies')
20-
->hasConfigFile()
21-
->hasViews()
22-
->hasMigration('create_laravel_cloudfront_cookies_table')
23-
->hasCommand(CloudfrontCookiesCommand::class);
15+
->hasConfigFile();
16+
}
17+
18+
public function packageBooted(): void
19+
{
20+
$this->app->singleton(CloudFrontClient::class, function () {
21+
return new CloudFrontClient([
22+
'version' => config('cloudfront.version'),
23+
'region' => config('cloudfront.region'),
24+
]);
25+
});
2426
}
2527
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace App\Http\Middleware;
4+
5+
use Aws\CloudFront\CloudFrontClient;
6+
use Closure;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Support\Facades\Cookie;
9+
10+
class CloudFrontCookie
11+
{
12+
/**
13+
* Handle an incoming request.
14+
*
15+
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
16+
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
17+
*/
18+
public function handle(Request $request, Closure $next)
19+
{
20+
if (\App::environment('production')) {
21+
22+
$cookies = $this->signACookiePolicy();
23+
24+
$host = parse_url(config('app.url'))['host'];
25+
26+
foreach ($cookies as $key => $value) {
27+
Cookie::queue($key, $value, 60 * 24 * 30, '/', ".{$host}", true);
28+
}
29+
}
30+
31+
return $next($request);
32+
}
33+
34+
public function signACookiePolicy()
35+
{
36+
$resourceKey = config('services.cloudfront.resource_key');
37+
38+
$expires = time() + 3000;
39+
40+
$privateKey = storage_path('private_key.pem');
41+
42+
$keyPairId = config('services.cloudfront.key_id');
43+
44+
$cloudFrontClient = new CloudFrontClient([
45+
'version' => '2014-11-06',
46+
'region' => 'us-east-1',
47+
]);
48+
49+
$policy = '{"Statement":[{"Resource":"'.$resourceKey.'","Condition":{"DateLessThan":{"AWS:EpochTime":'.$expires.'}}}]}';
50+
51+
return $cloudFrontClient->getSignedCookie([
52+
'private_key' => $privateKey,
53+
'expires' => $expires,
54+
'key_pair_id' => $keyPairId,
55+
'policy' => $policy,
56+
]);
57+
}
58+
}

src/Support/Config.php

Whitespace-only changes.

0 commit comments

Comments
 (0)