-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from fyrts/feature/signed-routes
Add URL signing for localized routes
- Loading branch information
Showing
4 changed files
with
100 additions
and
0 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
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,44 @@ | ||
<?php | ||
|
||
namespace ChinLeung\MultilingualRoutes\Macros; | ||
|
||
use Closure; | ||
|
||
class UrlGeneratorMacros | ||
{ | ||
/** | ||
* Create a temporary signed route URL for a named route in the current | ||
* locale. | ||
* | ||
* @param string $name | ||
* @param \DateTimeInterface|\DateInterval|int $expiration | ||
* @param array $parameters | ||
* @param bool $absolute | ||
* @return string | ||
*/ | ||
public function temporarySignedLocalizedRoute(): Closure | ||
{ | ||
return function ($name, $expiration, $parameters = [], $absolute = true) { | ||
return $this->signedLocalizedRoute($name, $parameters, $expiration, $absolute); | ||
}; | ||
} | ||
|
||
/** | ||
* Create a signed route URL for a named route in the current | ||
* locale. | ||
* | ||
* @param string $name | ||
* @param mixed $parameters | ||
* @param \DateTimeInterface|\DateInterval|int|null $expiration | ||
* @param bool $absolute | ||
* @return string | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function signedLocalizedRoute() | ||
{ | ||
return function ($name, $parameters = [], $expiration = null, $absolute = true) { | ||
return $this->signedRoute(locale().".{$name}", $parameters, $expiration, $absolute); | ||
}; | ||
} | ||
} |
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,46 @@ | ||
<?php | ||
|
||
namespace ChinLeung\MultilingualRoutes\Tests; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Route; | ||
use Illuminate\Support\Facades\URL; | ||
use Orchestra\Testbench\TestCase; | ||
|
||
class UrlTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
config(['locales.supported' => [ | ||
'en', 'fr', | ||
]]); | ||
} | ||
|
||
/** @test **/ | ||
public function a_multilingual_route_can_be_signed(): void | ||
{ | ||
Route::multilingual('test'); | ||
|
||
$request = Request::create(URL::signedLocalizedRoute('test')); | ||
|
||
$this->assertTrue(URL::hasValidSignature($request)); | ||
$this->assertTrue(str_starts_with($request->url(), localized_route('test'))); | ||
} | ||
|
||
/** @test **/ | ||
public function a_multilingual_route_can_be_signed_with_temporary_signature(): void | ||
{ | ||
Route::multilingual('test'); | ||
|
||
$request = Request::create(URL::temporarySignedLocalizedRoute('test', now()->addMinutes(30))); | ||
|
||
$this->assertTrue(URL::hasValidSignature($request)); | ||
$this->assertTrue(str_starts_with($request->url(), localized_route('test'))); | ||
|
||
$this->travel(5)->hours(); | ||
|
||
$this->assertFalse(URL::hasValidSignature($request)); | ||
} | ||
} |