conttas / laravel-signed-url
Signed (unique) URL package for Laravel.
Requires
- php: >=5.6.0
- illuminate/http: >=5.2 <5.6.12
- illuminate/support: >=5.2 <5.6.12
- spatie/url-signer: ^1.0.1
This package is not auto-updated.
Last update: 2024-10-28 00:06:01 UTC
README
This package can create URLs with a limited lifetime. This is done by adding an expiration date and a signature to the URL.
This is how you can create signed URL that's valid for 30 days:
SignedUrl::sign('https://myapp.com/protected-route', 30);
The output will look like this:
https://app.com/protected-route?expires=xxxxxx&signature=xxxxxx
The URL can be validated with the validate
-function.
SignedUrl::validate('https://app.com/protected-route?expires=xxxxxx&signature=xxxxxx');
The package also provides a middleware to protect routes.
Installation
As you would have guessed the package can be installed via Composer:
composer require conttas/laravel-signed-url
This package intends to provide tools for formatting and conversion monetary values in an easy, yet powerful way for Laravel projects. In older versions of the framework, just add the serviceprovider, and optionally register the facade:
// config/app.php 'providers' => [ ... Akaunting\SignedUrl\Provider::class, ]; 'aliases' => [ ... 'SignedUrl' => Akaunting\SignedUrl\Facade::class, ];
Configuration
The configuration file can optionally be published via:
php artisan vendor:publish --provider=signed-url
This is the content of the file:
return [ /* * This string is used the to generate a signature. You should * keep this value secret. */ 'signatureKey' => env('APP_KEY'), /* * The default expiration time of a URL in days. */ 'default_expiration_time_in_days' => 1, /* * These strings are used a parameter names in a signed url. */ 'parameters' => [ 'expires' => 'expires', 'signature' => 'signature', ], /* |-------------------------------------------------------------------------- | Middleware |-------------------------------------------------------------------------- | | This option indicates the middleware to change language. | */ 'middleware' => 'Akaunting\SignedUrl\Middleware\ValidateSignedUrl', ];
Usage
Signing URLs
URL's can be signed with the sign
-method:
SignedUrl::sign('https://myapp.com/protected-route');
By default the lifetime of an URL is one day. This value can be change in the config-file. If you want a custom life time, you can specify the number of days the URL should be valid:
//the generated URL will be valid for 5 days. SignedUrl::sign('https://myapp.com/protected-route', 5);
For fine grained control, you may also pass a DateTime
instance as the second parameter. The url
will be valid up to that moment. This example uses Carbon for convenience:
//This URL will be valid up until 2 hours from the moment it was generated. SignedUrl::sign('https://myapp.com/protected-route', Carbon\Carbon::now()->addHours(2) );
Validating URLs
To validate a signed URL, simply call the validate()
-method. This return a boolean.
SignedUrl::validate('https://app.com/protected-route?expires=xxxxxx&signature=xxxxxx');
Protecting routes with middleware
The package also provides a middleware to protect routes:
Route::get('protected-route', ['middleware' => 'signed', function () { return 'Hello secret world!'; }]);
Your app will abort with a 403 status code if the route is called without a valid signature.
Changelog
Please see Releases for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email dev@codions.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see LICENSE for more information.