-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This implements the ability to generate signed (and tempoorary) URLs. These URLs may be easily verified as having been generated by your application and not modified by the end-user.
- Loading branch information
1 parent
5bc990e
commit 3ed733f
Showing
6 changed files
with
211 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/Illuminate/Routing/Exceptions/InvalidSignatureException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Illuminate\Routing\Exceptions; | ||
|
||
use Exception; | ||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||
|
||
class InvalidSignatureException extends HttpException | ||
{ | ||
/** | ||
* Create a new exception instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
parent::__construct(401, 'Invalid signature.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Illuminate\Routing\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Routing\Exceptions\InvalidSignatureException; | ||
|
||
class ValidateSignature | ||
{ | ||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* @return \Illuminate\Http\Response | ||
* | ||
* @throws \Illuminate\Routing\Exceptions\InvalidSignatureException | ||
*/ | ||
public function handle($request, Closure $next) | ||
{ | ||
if ($request->hasValidSignature($request)) { | ||
return $next($request); | ||
} | ||
|
||
throw new InvalidSignatureException; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Routing; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Carbon; | ||
use Orchestra\Testbench\TestCase; | ||
use Illuminate\Support\Facades\URL; | ||
use Illuminate\Support\Facades\Route; | ||
use Illuminate\Routing\Middleware\ValidateSignature; | ||
|
||
/** | ||
* @group integration | ||
*/ | ||
class UrlSigningTest extends TestCase | ||
{ | ||
public function test_signing_url() | ||
{ | ||
Route::get('/foo/{id}', function (Request $request, $id) { | ||
return $request->hasValidSignature() ? 'valid' : 'invalid'; | ||
})->name('foo'); | ||
|
||
$this->assertTrue(is_string($url = URL::signedRoute('foo', ['id' => 1]))); | ||
$this->assertEquals('valid', $this->get($url)->original); | ||
} | ||
|
||
public function test_temporary_signed_urls() | ||
{ | ||
Route::get('/foo/{id}', function (Request $request, $id) { | ||
return $request->hasValidSignature() ? 'valid' : 'invalid'; | ||
})->name('foo'); | ||
|
||
Carbon::setTestNow(Carbon::create(2018, 1, 1)); | ||
$this->assertTrue(is_string($url = URL::temporarySignedRoute('foo', now()->addMinutes(5), ['id' => 1]))); | ||
$this->assertEquals('valid', $this->get($url)->original); | ||
|
||
Carbon::setTestNow(Carbon::create(2018, 1, 1)->addMinutes(10)); | ||
$this->assertEquals('invalid', $this->get($url)->original); | ||
} | ||
|
||
public function test_signed_middleware() | ||
{ | ||
Route::get('/foo/{id}', function (Request $request, $id) { | ||
return $request->hasValidSignature() ? 'valid' : 'invalid'; | ||
})->name('foo')->middleware(ValidateSignature::class); | ||
|
||
Carbon::setTestNow(Carbon::create(2018, 1, 1)); | ||
$this->assertTrue(is_string($url = URL::temporarySignedRoute('foo', now()->addMinutes(5), ['id' => 1]))); | ||
$this->assertEquals('valid', $this->get($url)->original); | ||
} | ||
|
||
public function test_signed_middleware_with_invalid_url() | ||
{ | ||
Route::get('/foo/{id}', function (Request $request, $id) { | ||
return $request->hasValidSignature() ? 'valid' : 'invalid'; | ||
})->name('foo')->middleware(ValidateSignature::class); | ||
|
||
Carbon::setTestNow(Carbon::create(2018, 1, 1)); | ||
$this->assertTrue(is_string($url = URL::temporarySignedRoute('foo', now()->addMinutes(5), ['id' => 1]))); | ||
Carbon::setTestNow(Carbon::create(2018, 1, 1)->addMinutes(10)); | ||
|
||
$response = $this->get($url); | ||
$response->assertStatus(401); | ||
} | ||
} |
3ed733f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suprised this made it in the core, seems like something better suited as a package.
So would a good use case for this be the password-resetting feature in core? It will generate a signed url to reset the password so won't any longer need the password_resets table? Related: #17499