Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Allow signed URLs with custom key resolver #44254

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Illuminate/Routing/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,17 @@ public function setKeyResolver(callable $keyResolver)
return $this;
}

/**
* Clone a new instance of the URL generator 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.
*
Expand Down
35 changes: 35 additions & 0 deletions tests/Routing/RoutingUrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a typo, it should probably say tampered

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you send in a PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR for fixing the typo: #51238


$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
Expand Down