Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public function __construct(FirewallMap $firewallMap, ContainerInterface $loginL
$this->requestStack = $requestStack;
}

public function createLoginLink(UserInterface $user): LoginLinkDetails
public function createLoginLink(UserInterface $user, Request $request = null): LoginLinkDetails
{
return $this->getLoginLinkHandler()->createLoginLink($user);
return $this->getLoginLinkHandler()->createLoginLink($user, $request);
}

public function consumeLoginLink(Request $request): UserInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function testSuccessfulDecoration()
$loginLinkHandler = $this->createMock(LoginLinkHandlerInterface::class);
$loginLinkHandler->expects($this->once())
->method('createLoginLink')
->with($user)
->with($user, $request)
->willReturn($linkDetails);
$loginLinkHandler->expects($this->once())
->method('consumeLoginLink')
Expand All @@ -47,7 +47,7 @@ public function testSuccessfulDecoration()
$requestStack->push($request);

$linker = new FirewallAwareLoginLinkHandler($firewallMap, $locator, $requestStack);
$actualLinkDetails = $linker->createLoginLink($user);
$actualLinkDetails = $linker->createLoginLink($user, $request);
$this->assertSame($linkDetails, $actualLinkDetails);

$actualUser = $linker->consumeLoginLink($request);
Expand Down
24 changes: 18 additions & 6 deletions src/Symfony/Component/Security/Http/LoginLink/LoginLinkHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
Expand Down Expand Up @@ -49,7 +50,7 @@ public function __construct(UrlGeneratorInterface $urlGenerator, UserProviderInt
$this->expiredStorage = $expiredStorage;
}

public function createLoginLink(UserInterface $user): LoginLinkDetails
public function createLoginLink(UserInterface $user, Request $request = null): LoginLinkDetails
{
$expiresAt = new \DateTimeImmutable(sprintf('+%d seconds', $this->options['lifetime']));

Expand All @@ -60,11 +61,22 @@ public function createLoginLink(UserInterface $user): LoginLinkDetails
'hash' => $this->computeSignatureHash($user, $expires),
];

$url = $this->urlGenerator->generate(
$this->options['route_name'],
$parameters,
UrlGeneratorInterface::ABSOLUTE_URL
);
if ($request) {
$currentRequestContext = $this->urlGenerator->getContext();
$this->urlGenerator->setContext((new RequestContext())->fromRequest($request));
}

try {
$url = $this->urlGenerator->generate(
$this->options['route_name'],
$parameters,
UrlGeneratorInterface::ABSOLUTE_URL
);
} finally {
if ($request) {
$this->urlGenerator->setContext($currentRequestContext);
}
}

return new LoginLinkDetails($url, $expiresAt);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface LoginLinkHandlerInterface
/**
* Generate a link that can be used to authenticate as the given user.
*/
public function createLoginLink(UserInterface $user): LoginLinkDetails;
public function createLoginLink(UserInterface $user, Request $request = null): LoginLinkDetails;

/**
* Validates if this request contains a login link and returns the associated User.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function setUp(): void
* @dataProvider provideCreateLoginLinkData
* @group time-sensitive
*/
public function testCreateLoginLink($user, array $extraProperties)
public function testCreateLoginLink($user, array $extraProperties, Request $request = null)
{
$this->router->expects($this->once())
->method('generate')
Expand All @@ -65,12 +65,23 @@ public function testCreateLoginLink($user, array $extraProperties)
)
->willReturn('https://example.com/login/verify?user=weaverryan&hash=abchash&expires=1601235000');

$loginLink = $this->createLinker([], array_keys($extraProperties))->createLoginLink($user);
$loginLink = $this->createLinker([], array_keys($extraProperties))->createLoginLink($user, $request);
$this->assertSame('https://example.com/login/verify?user=weaverryan&hash=abchash&expires=1601235000', $loginLink->getUrl());
}

public function provideCreateLoginLinkData()
{
yield [
new TestLoginLinkHandlerUser('weaverryan', 'ryan@symfonycasts.com', 'pwhash'),
['emailProperty' => 'ryan@symfonycasts.com', 'passwordProperty' => 'pwhash'],
Request::create('https://example.com'),
];

yield [
new TestLoginLinkHandlerUser('weaverryan', 'ryan@symfonycasts.com', 'pwhash'),
['emailProperty' => 'ryan@symfonycasts.com', 'passwordProperty' => 'pwhash'],
];

yield [
new TestLoginLinkHandlerUser('weaverryan', 'ryan@symfonycasts.com', 'pwhash'),
['emailProperty' => 'ryan@symfonycasts.com', 'passwordProperty' => 'pwhash'],
Expand Down