-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(UserBundle): Add FrontendLoginLinkHandler and updated README (#15)
- Loading branch information
1 parent
6475423
commit 9045642
Showing
3 changed files
with
97 additions
and
2 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
79 changes: 79 additions & 0 deletions
79
lib/RoadizUserBundle/src/Security/FrontendLoginLinkHandler.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,79 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\UserBundle\Security; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Security\Core\Signature\SignatureHasher; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
use Symfony\Component\Security\Http\LoginLink\LoginLinkDetails; | ||
use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface; | ||
|
||
final readonly class FrontendLoginLinkHandler implements LoginLinkHandlerInterface | ||
{ | ||
/** | ||
* @var array|int[] | ||
*/ | ||
private array $options; | ||
|
||
public function __construct( | ||
private LoginLinkHandlerInterface $decorated, | ||
private string $frontendLoginCheckRoute, | ||
private SignatureHasher $signatureHasher, | ||
private array $frontendLoginLinkRequestRoutes, | ||
array $options = [] | ||
) { | ||
$this->options = array_merge([ | ||
'lifetime' => 600, | ||
], $options); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function createLoginLink( | ||
UserInterface $user, | ||
?Request $request = null, | ||
int $lifetime = null | ||
): LoginLinkDetails { | ||
if (null === $request) { | ||
throw new \InvalidArgumentException('Request cannot be null.'); | ||
} | ||
/* | ||
* If user does not request a login link from `$frontendLoginLinkRequestRoutes`, we fallback to the decorated handler | ||
*/ | ||
if (!\in_array($request->attributes->get('_route'), $this->frontendLoginLinkRequestRoutes, true)) { | ||
return $this->decorated->createLoginLink($user, $request); | ||
} | ||
|
||
$expires = time() + ($lifetime ?: $this->options['lifetime']); | ||
$expiresAt = new \DateTimeImmutable('@' . $expires); | ||
|
||
$parameters = [ | ||
'user' => $user->getUserIdentifier(), | ||
'expires' => $expires, | ||
'hash' => $this->signatureHasher->computeSignatureHash($user, $expires), | ||
]; | ||
$redirect = $request->getPayload()->get('redirect'); | ||
if (\is_string($redirect)) { | ||
$parameters['redirect'] = $redirect; | ||
} | ||
|
||
$url = $this->frontendLoginCheckRoute . '?' . http_build_query($parameters); | ||
|
||
if (filter_var($url, FILTER_VALIDATE_URL) === false) { | ||
throw new \InvalidArgumentException(sprintf('The URL "%s" is not valid.', $url)); | ||
} | ||
|
||
return new LoginLinkDetails($url, $expiresAt); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function consumeLoginLink(Request $request): UserInterface | ||
{ | ||
return $this->decorated->consumeLoginLink($request); | ||
} | ||
} |