From 8f181e69e3bfa4b39d5c361d564c8d504968e912 Mon Sep 17 00:00:00 2001 From: Roy de Vos Burchart Date: Thu, 22 Sep 2022 13:55:07 +0200 Subject: [PATCH 1/2] Support cloning UrlGenerator withKeyResolver --- src/Illuminate/Routing/UrlGenerator.php | 11 +++++++ tests/Routing/RoutingUrlGeneratorTest.php | 35 +++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/Illuminate/Routing/UrlGenerator.php b/src/Illuminate/Routing/UrlGenerator.php index cdfb2c122428..b7ba8e2595f2 100755 --- a/src/Illuminate/Routing/UrlGenerator.php +++ b/src/Illuminate/Routing/UrlGenerator.php @@ -811,6 +811,17 @@ public function setKeyResolver(callable $keyResolver) return $this; } + /** + * Create a new instance of the UrlGenerator with a different encryption key resolver. + * + * @param callable $keyResolver + * @return \Illuminate\Routing\UrlGenerator + */ + public function withKeyResolver(callable $keyResolver) + { + return (clone $this)->setKeyResolver($keyResolver); + } + /** * Get the root controller namespace. * diff --git a/tests/Routing/RoutingUrlGeneratorTest.php b/tests/Routing/RoutingUrlGeneratorTest.php index bb14513b7056..5c29954d5bae 100755 --- a/tests/Routing/RoutingUrlGeneratorTest.php +++ b/tests/Routing/RoutingUrlGeneratorTest.php @@ -867,6 +867,41 @@ public function testRouteGenerationWithBackedEnums() $this->assertSame('http://www.foo.com/foo/fruits', $url->route('foo.bar', CategoryBackedEnum::Fruits)); } + + public function testSignedUrlWithKeyResolver() + { + $url = new UrlGenerator( + $routes = new RouteCollection, + $request = Request::create('http://www.foo.com/') + ); + $url->setKeyResolver(function () { + return 'secret'; + }); + + $route = new Route(['GET'], 'foo', ['as' => 'foo', function () { + // + }]); + $routes->add($route); + + $request = Request::create($url->signedRoute('foo')); + + $this->assertTrue($url->hasValidSignature($request)); + + $request = Request::create($url->signedRoute('foo').'?tempered=true'); + + $this->assertFalse($url->hasValidSignature($request)); + + $url2 = $url->withKeyResolver(function () { + return 'other-secret'; + }); + + $this->assertFalse($url2->hasValidSignature($request)); + + $request = Request::create($url2->signedRoute('foo')); + + $this->assertTrue($url2->hasValidSignature($request)); + $this->assertFalse($url->hasValidSignature($request)); + } } class RoutableInterfaceStub implements UrlRoutable From 9b8419c6328533dd3420a2e0d491bd6a60bd3b7c Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 22 Sep 2022 12:59:58 -0500 Subject: [PATCH 2/2] Update UrlGenerator.php --- src/Illuminate/Routing/UrlGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Routing/UrlGenerator.php b/src/Illuminate/Routing/UrlGenerator.php index b7ba8e2595f2..56a116d405a2 100755 --- a/src/Illuminate/Routing/UrlGenerator.php +++ b/src/Illuminate/Routing/UrlGenerator.php @@ -812,7 +812,7 @@ public function setKeyResolver(callable $keyResolver) } /** - * Create a new instance of the UrlGenerator with a different encryption key resolver. + * Clone a new instance of the URL generator with a different encryption key resolver. * * @param callable $keyResolver * @return \Illuminate\Routing\UrlGenerator