|
1 |
| -# laravel-cloudfront-url-signer |
| 1 | +# Create CloudFront signed URLs in Laravel 5.5+ |
2 | 2 | Easy to use Laravel 5.5+ wrapper around the official AWS PHP SDK which allows to sign URLs to access Private Content through CloudFront CDN
|
| 3 | + |
| 4 | +Inspired by [laravel-url-signer](https://github.com/spatie/laravel-url-signer) |
| 5 | + |
| 6 | +[](https://packagist.org/packages/dreamonkey/laravel-cloudfront-url-signer) |
| 7 | +[](https://packagist.org/packages/dreamonkey/laravel-cloudfront-url-signer) |
| 8 | + |
| 9 | +This package can create canned policies signed URLs for CloudFront which expires after a given time. This is done by wrapping the AWS SDK method adding a Laravel-style configuration. |
| 10 | + |
| 11 | +This is how you can create signed URL that's valid for 30 days: |
| 12 | + |
| 13 | +```php |
| 14 | +CloudFrontUrlSigner::sign('https://myapp.com/resource', 30); |
| 15 | +``` |
| 16 | + |
| 17 | +The output will be compliant with [CloudFront specifications](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-creating-signed-url-canned-policy.html) |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +The package can be installed via Composer: |
| 22 | + |
| 23 | +``` |
| 24 | +composer require dreamonkey/laravel-cloudfront-url-signer |
| 25 | +``` |
| 26 | + |
| 27 | +## Configuration |
| 28 | + |
| 29 | +The configuration file can optionally be published via: |
| 30 | + |
| 31 | +``` |
| 32 | +php artisan vendor:publish --provider="Dreamonkey\CloudFrontUrlSigner\UrlSignerServiceProvider" |
| 33 | +``` |
| 34 | + |
| 35 | +This is the content of the file: |
| 36 | + |
| 37 | +```php |
| 38 | +return [ |
| 39 | + /* |
| 40 | + * The default expiration time of a URL in days. |
| 41 | + */ |
| 42 | + 'default_expiration_time_in_days' => 1, |
| 43 | + |
| 44 | + /* |
| 45 | + * The private key used to sign all URLs. |
| 46 | + */ |
| 47 | + 'private_key_path' => storage_path(env('CLOUDFRONT_PRIVATE_KEY_PATH', 'trusted-signer.pem')), |
| 48 | + |
| 49 | + /* |
| 50 | + * Identifies the CloudFront key pair associated |
| 51 | + * to the trusted signer which validates signed URLs. |
| 52 | + */ |
| 53 | + 'key_pair_id' => env('CLOUDFRONT_KEY_PAIR_ID', ''), |
| 54 | + |
| 55 | + /* |
| 56 | + * AWS region to connect to. |
| 57 | + */ |
| 58 | + 'region' => env('AWS_DEFAULT_REGION', 'us-west-2'), |
| 59 | + |
| 60 | + /* |
| 61 | + * CloudFront API version, by default it uses the latest available. |
| 62 | + */ |
| 63 | + 'version' => env('CLOUDFRONT_API_VERSION', 'latest'), |
| 64 | + |
| 65 | +]; |
| 66 | +``` |
| 67 | +## Usage |
| 68 | + |
| 69 | +### Signing URLs |
| 70 | +URL's can be signed with the `sign` method: |
| 71 | +```php |
| 72 | +CloudFrontUrlSigner::sign('https://myapp.com/resource'); |
| 73 | +``` |
| 74 | +By default the lifetime of an URL is one day. This value can be change in the config-file. |
| 75 | +If you want a custom life time, you can specify the number of days the URL should be valid: |
| 76 | + |
| 77 | +```php |
| 78 | +// The generated URL will be valid for 5 days. |
| 79 | +CloudFrontUrlSigner::sign('https://myapp.com/resource', 5); |
| 80 | +``` |
| 81 | + |
| 82 | +For fine grained control, you may also pass a `DateTime` instance as the second parameter. The url |
| 83 | +will be valid up to that moment. This example uses Carbon for convenience: |
| 84 | +```php |
| 85 | +// This URL will be valid up until 2 hours from the moment it was generated. |
| 86 | +CloudFrontUrlSigner::sign('https://myapp.com/resource', Carbon\Carbon::now()->addHours(2) ); |
| 87 | +``` |
| 88 | + |
| 89 | +## Changelog |
| 90 | + |
| 91 | +Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. |
| 92 | + |
| 93 | +## Testing |
| 94 | + |
| 95 | +``` bash |
| 96 | +$ vendor/bin/phpunit |
| 97 | +``` |
| 98 | + |
| 99 | +## Contributing |
| 100 | + |
| 101 | +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. |
| 102 | + |
| 103 | +## Security |
| 104 | + |
| 105 | +If you discover any security related issues, please email [email protected] instead of using the issue tracker. |
| 106 | + |
| 107 | +## Credits |
| 108 | + |
| 109 | +- [Paolo Caleffi](https://github.com/IlCallo) |
| 110 | + |
| 111 | +## License |
| 112 | + |
| 113 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments