2017 © Pedro Peláez
 

library laravel-url-signer

Laravel implementation of spatie/signed-url

image

spatie/laravel-url-signer

Laravel implementation of spatie/signed-url

  • Wednesday, March 21, 2018
  • by Spatie
  • Repository
  • 16 Watchers
  • 453 Stars
  • 99,587 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 36 Forks
  • 0 Open issues
  • 11 Versions
  • 12 % Grown

The README.md

Create signed URLs with a limited lifetime in Laravel

Latest Version on Packagist Build Status Quality Score Total Downloads, (*1)

This package can create URLs with a limited lifetime. This is done by adding an expiration date and a signature to the URL., (*2)

The difference with Laravel's native route signing is that using this package:, (*3)

  • you can easily use signed URLs between different apps
  • the signing secret used is not tied to the app key
  • you can easily sign any URL (and not only a route belonging to your app)

This is how you can create signed URL that's valid for 30 days:, (*4)

use Spatie\UrlSigner\Laravel\Facades\UrlSigner;

UrlSigner::sign('https://myapp.com/protected-route', now()->addDays(30));

The output will look like this:, (*5)

https://app.com/protected-route?expires=xxxxxx&signature=xxxxxx

The URL can be validated with the validate-function., (*6)

// returns `true` if the signed URL is valid, `false` if not
UrlSigner::validate('https://app.com/protected-route?expires=xxxxxx&signature=xxxxxx');

The package also provides a middleware to protect routes., (*7)

Support us

, (*8)

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products., (*9)

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. We publish all received postcards on our virtual postcard wall., (*10)

Installation

As you would have guessed the package can be installed via composer:, (*11)

composer require spatie/laravel-url-signer

You must set an environment variable called URL_SIGNER_SIGNATURE_KEY and set it to a long secret value. This value will be used to sign and validate signed URLs., (*12)

php artisan generate:url-signer-signature-key
        {--s|show : Display the key instead of modifying files.}
        {--always-no : Skip generating key if it already exists.}
        {--f|force : Skip confirmation when overwriting an existing key.}

The configuration file can optionally be published via:, (*13)

php artisan vendor:publish --tag="url-signer-config"

This is the content of the file:, (*14)

return [
    /*
    * This string is used the to generate a signature. You should
    * keep this value secret.
    */
    'signature_key' => env('URL_SIGNER_SIGNATURE_KEY'),

    /*
     * The default expiration time of a URL in seconds.
     */
    'default_expiration_time_in_seconds' => 60 * 60 * 24,

    /*
     * These strings are used a parameter names in a signed url.
     */
    'parameters' => [
        'expires' => 'expires',
        'signature' => 'signature',
    ],
];

Usage

URL's can be signed with the sign-method:, (*15)

use Spatie\UrlSigner\Laravel\Facades\UrlSigner;

UrlSigner::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 lifetime, you can specify the number of days the URL should be valid:, (*16)

use Spatie\UrlSigner\Laravel\Facades\UrlSigner;

// the generated URL will be valid for 5 minutes.
UrlSigner::sign('https://myapp.com/protected-route', now()->addMinutes(5));

// alternatively you could also pass the amount of seconds
UrlSigner::sign('https://myapp.com/protected-route', 60 * 5);

Validating URLs

To validate a signed URL, simply call the validate()-method. This method returns a boolean., (*17)

use Spatie\UrlSigner\Laravel\Facades\UrlSigner;

UrlSigner::validate('https://app.com/protected-route?expires=xxxxxx&signature=xxxxxx');

Protecting routes with middleware

The package provides a middleware to protect routes., (*18)

To use it you must first register the Spatie\UrlSigner\Laravel\Middleware\ValidateSignature as route middleware in your HTTP kernel., (*19)

// in app/Http/Kernel.php

protected $routeMiddleware = [
    // ...
    'signed-url' => \Spatie\UrlSigner\Laravel\Middleware\ValidateSignature::class,
];

Next, you can apply it on any route you want., (*20)

Route::get('protected-route', fn () => 'Hello secret world!')
    ->middleware('signed-url');

Your app will abort with a 403 status code if the route is called without a valid signature., (*21)

Changelog

Please see CHANGELOG for more information what has changed recently., (*22)

Testing

You can run the test using this command:, (*23)

composer test

Usage outside Laravel

If you're working on a non-Laravel project, you can use the framework agnostic version., (*24)

Contributing

Please see CONTRIBUTING for details., (*25)

Security

If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker., (*26)

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using., (*27)

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium., (*28)

We publish all received postcards on our company website., (*29)

Credits

License

The MIT License (MIT). Please see License File for more information., (*30)

The Versions

08/02 2018
07/11 2017
13/08 2017
09/02 2017

2.0.0

2.0.0.0 https://github.com/spatie/laravel-url-signer

Laravel implementation of spatie/signed-url

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-url-signer

13/01 2016
04/01 2016
22/12 2015

1.1.0

1.1.0.0 https://github.com/spatie/laravel-url-signer

Laravel implementation of spatie/signed-url

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-url-signer

15/08 2015

1.0.0

1.0.0.0 https://github.com/spatie/laravel-url-signer

Laravel implementation of spatie/signed-url

  Sources   Download

MIT

The Requires

 

The Development Requires

spatie laravel-url-signer