-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[Security] Magic login link authentication #38177
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
159 changes: 159 additions & 0 deletions
159
src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/LoginLinkFactory.php
This file contains hidden or 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,159 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <fabien@symfony.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory; | ||
|
|
||
| use Symfony\Component\Config\Definition\Builder\NodeBuilder; | ||
| use Symfony\Component\Config\Definition\Builder\NodeDefinition; | ||
| use Symfony\Component\Config\FileLocator; | ||
| use Symfony\Component\DependencyInjection\ChildDefinition; | ||
| use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
| use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; | ||
| use Symfony\Component\DependencyInjection\Reference; | ||
| use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface; | ||
| use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; | ||
| use Symfony\Component\Security\Http\LoginLink\LoginLinkHandler; | ||
|
|
||
| /** | ||
| * @internal | ||
| * @experimental in 5.2 | ||
| */ | ||
| class LoginLinkFactory extends AbstractFactory implements AuthenticatorFactoryInterface | ||
| { | ||
| public function addConfiguration(NodeDefinition $node) | ||
| { | ||
| /** @var NodeBuilder $builder */ | ||
| $builder = $node->children(); | ||
|
|
||
| $builder | ||
| ->scalarNode('check_route') | ||
| ->isRequired() | ||
| ->info('Route that will validate the login link - e.g. app_login_link_verify') | ||
fabpot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ->end() | ||
| ->arrayNode('signature_properties') | ||
| ->prototype('scalar')->end() | ||
| ->requiresAtLeastOneElement() | ||
| ->info('An array of properties on your User that are used to sign the link. If any of these change, all existing links will become invalid') | ||
| ->example(['email', 'password']) | ||
| ->end() | ||
| ->integerNode('lifetime') | ||
| ->defaultValue(600) | ||
| ->info('The lifetime of the login link in seconds') | ||
| ->end() | ||
| ->integerNode('max_uses') | ||
| ->defaultNull() | ||
| ->info('Max number of times a login link can be used - null means unlimited within lifetime.') | ||
| ->end() | ||
| ->scalarNode('used_link_cache') | ||
| ->info('Cache service id used to expired links of max_uses is set') | ||
| ->end() | ||
| ->scalarNode('success_handler') | ||
| ->info(sprintf('A service id that implements %s', AuthenticationSuccessHandlerInterface::class)) | ||
| ->end() | ||
| ->scalarNode('failure_handler') | ||
| ->info(sprintf('A service id that implements %s', AuthenticationFailureHandlerInterface::class)) | ||
| ->end() | ||
| ->scalarNode('provider') | ||
| ->info('the user provider to load users from.') | ||
| ->end() | ||
| ; | ||
|
|
||
| foreach (array_merge($this->defaultSuccessHandlerOptions, $this->defaultFailureHandlerOptions) as $name => $default) { | ||
| if (\is_bool($default)) { | ||
| $builder->booleanNode($name)->defaultValue($default); | ||
| } else { | ||
| $builder->scalarNode($name)->defaultValue($default); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public function getKey() | ||
| { | ||
| return 'login-link'; | ||
| } | ||
|
|
||
| public function createAuthenticator(ContainerBuilder $container, string $firewallName, array $config, string $userProviderId): string | ||
| { | ||
| if (!class_exists(LoginLinkHandler::class)) { | ||
| throw new \LogicException('Login login link requires symfony/security-http:^5.2.'); | ||
| } | ||
|
|
||
| if (!$container->hasDefinition('security.authenticator.login_link')) { | ||
| $loader = new PhpFileLoader($container, new FileLocator(\dirname(__DIR__).'/../../Resources/config')); | ||
| $loader->load('security_authenticator_login_link.php'); | ||
| } | ||
|
|
||
| if (null !== $config['max_uses'] && !isset($config['used_link_cache'])) { | ||
| $config['used_link_cache'] = 'security.authenticator.cache.expired_links'; | ||
| } | ||
|
|
||
| $expiredStorageId = null; | ||
| if (isset($config['used_link_cache'])) { | ||
| $expiredStorageId = 'security.authenticator.expired_login_link_storage.'.$firewallName; | ||
| $container | ||
| ->setDefinition($expiredStorageId, new ChildDefinition('security.authenticator.expired_login_link_storage')) | ||
| ->replaceArgument(0, new Reference($config['used_link_cache'])) | ||
| ->replaceArgument(1, $config['lifetime']); | ||
| } | ||
|
|
||
| $linkerId = 'security.authenticator.login_link_handler.'.$firewallName; | ||
| $linkerOptions = [ | ||
| 'route_name' => $config['check_route'], | ||
| 'lifetime' => $config['lifetime'], | ||
| 'max_uses' => $config['max_uses'] ?? null, | ||
| ]; | ||
| $container | ||
| ->setDefinition($linkerId, new ChildDefinition('security.authenticator.abstract_login_link_handler')) | ||
| ->replaceArgument(1, new Reference($userProviderId)) | ||
| ->replaceArgument(3, $config['signature_properties']) | ||
| ->replaceArgument(5, $linkerOptions) | ||
| ->replaceArgument(6, $expiredStorageId ? new Reference($expiredStorageId) : null) | ||
| ->addTag('security.authenticator.login_linker', ['firewall' => $firewallName]) | ||
| ; | ||
|
|
||
| $authenticatorId = 'security.authenticator.login_link.'.$firewallName; | ||
| $container | ||
| ->setDefinition($authenticatorId, new ChildDefinition('security.authenticator.login_link')) | ||
| ->replaceArgument(0, new Reference($linkerId)) | ||
| ->replaceArgument(2, new Reference($this->createAuthenticationSuccessHandler($container, $firewallName, $config))) | ||
| ->replaceArgument(3, new Reference($this->createAuthenticationFailureHandler($container, $firewallName, $config))) | ||
| ->replaceArgument(4, [ | ||
| 'check_route' => $config['check_route'], | ||
| ]); | ||
|
|
||
| return $authenticatorId; | ||
| } | ||
|
|
||
| public function getPosition() | ||
| { | ||
| return 'form'; | ||
| } | ||
|
|
||
| protected function createAuthProvider(ContainerBuilder $container, string $id, array $config, string $userProviderId) | ||
| { | ||
| throw new \Exception('The old authentication system is not supported with login_link.'); | ||
| } | ||
fabpot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| protected function getListenerId() | ||
| { | ||
| throw new \Exception('The old authentication system is not supported with login_link.'); | ||
| } | ||
|
|
||
| protected function createListener(ContainerBuilder $container, string $id, array $config, string $userProvider) | ||
| { | ||
| throw new \Exception('The old authentication system is not supported with login_link.'); | ||
| } | ||
|
|
||
| protected function createEntryPoint(ContainerBuilder $container, string $id, array $config, ?string $defaultEntryPointId) | ||
| { | ||
| throw new \Exception('The old authentication system is not supported with login_link.'); | ||
| } | ||
| } | ||
63 changes: 63 additions & 0 deletions
63
src/Symfony/Bundle/SecurityBundle/LoginLink/FirewallAwareLoginLinkHandler.php
This file contains hidden or 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,63 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <fabien@symfony.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\Bundle\SecurityBundle\LoginLink; | ||
|
|
||
| use Psr\Container\ContainerInterface; | ||
| use Symfony\Bundle\SecurityBundle\Security\FirewallMap; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
| use Symfony\Component\HttpFoundation\RequestStack; | ||
| use Symfony\Component\Security\Core\User\UserInterface; | ||
| use Symfony\Component\Security\Http\LoginLink\LoginLinkDetails; | ||
| use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface; | ||
|
|
||
| /** | ||
| * Decorates the login link handler for the current firewall. | ||
| * | ||
| * @author Ryan Weaver <ryan@symfonycasts.com> | ||
| */ | ||
| class FirewallAwareLoginLinkHandler implements LoginLinkHandlerInterface | ||
| { | ||
| private $firewallMap; | ||
| private $loginLinkHandlerLocator; | ||
| private $requestStack; | ||
|
|
||
| public function __construct(FirewallMap $firewallMap, ContainerInterface $loginLinkHandlerLocator, RequestStack $requestStack) | ||
| { | ||
| $this->firewallMap = $firewallMap; | ||
| $this->loginLinkHandlerLocator = $loginLinkHandlerLocator; | ||
| $this->requestStack = $requestStack; | ||
| } | ||
|
|
||
| public function createLoginLink(UserInterface $user): LoginLinkDetails | ||
| { | ||
| return $this->getLoginLinkHandler()->createLoginLink($user); | ||
| } | ||
|
|
||
| public function consumeLoginLink(Request $request): UserInterface | ||
| { | ||
| return $this->getLoginLinkHandler()->consumeLoginLink($request); | ||
| } | ||
|
|
||
| private function getLoginLinkHandler(): LoginLinkHandlerInterface | ||
| { | ||
| if (null === $request = $this->requestStack->getCurrentRequest()) { | ||
| throw new \LogicException('Cannot determine the correct LoginLinkHandler to use: there is no active Request and so, the firewall cannot be determined. Try using the specific login link handler service.'); | ||
| } | ||
|
|
||
| $firewallName = $this->firewallMap->getFirewallConfig($request)->getName(); | ||
| if (!$this->loginLinkHandlerLocator->has($firewallName)) { | ||
| throw new \InvalidArgumentException(sprintf('No login link handler found. Did you add a login_link key under your "%s" firewall?', $firewallName)); | ||
| } | ||
|
|
||
| return $this->loginLinkHandlerLocator->get($firewallName); | ||
| } | ||
| } |
72 changes: 72 additions & 0 deletions
72
src/Symfony/Bundle/SecurityBundle/Resources/config/security_authenticator_login_link.php
This file contains hidden or 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,72 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Symfony package. | ||
| * | ||
| * (c) Fabien Potencier <fabien@symfony.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
|
||
| use Symfony\Bundle\SecurityBundle\LoginLink\FirewallAwareLoginLinkHandler; | ||
| use Symfony\Component\Security\Http\Authenticator\LoginLinkAuthenticator; | ||
| use Symfony\Component\Security\Http\LoginLink\ExpiredLoginLinkStorage; | ||
| use Symfony\Component\Security\Http\LoginLink\LoginLinkHandler; | ||
| use Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface; | ||
|
|
||
| return static function (ContainerConfigurator $container) { | ||
| $container->services() | ||
| ->set('security.authenticator.login_link', LoginLinkAuthenticator::class) | ||
| ->abstract() | ||
| ->args([ | ||
| abstract_arg('the login link handler instance'), | ||
| service('security.http_utils'), | ||
| abstract_arg('authentication success handler'), | ||
| abstract_arg('authentication failure handler'), | ||
| abstract_arg('options'), | ||
| ]) | ||
|
|
||
| ->set('security.authenticator.abstract_login_link_handler', LoginLinkHandler::class) | ||
| ->abstract() | ||
| ->args([ | ||
| service('router'), | ||
| abstract_arg('user provider'), | ||
| service('property_accessor'), | ||
| abstract_arg('signature properties'), | ||
| '%kernel.secret%', | ||
| abstract_arg('options'), | ||
| abstract_arg('expired login link storage'), | ||
| ]) | ||
|
|
||
| ->set('security.authenticator.expired_login_link_storage', ExpiredLoginLinkStorage::class) | ||
| ->abstract() | ||
| ->args([ | ||
| abstract_arg('cache pool service'), | ||
| abstract_arg('expired login link storage'), | ||
| ]) | ||
|
|
||
| ->set('security.authenticator.cache.expired_links') | ||
| ->parent('cache.app') | ||
| ->private() | ||
| ->tag('cache.pool') | ||
|
|
||
| ->set('security.authenticator.firewall_aware_login_link_handler', FirewallAwareLoginLinkHandler::class) | ||
| ->args([ | ||
| service('security.firewall.map'), | ||
| tagged_locator('security.authenticator.login_linker', 'firewall'), | ||
| service('request_stack'), | ||
| ]) | ||
| ->alias(LoginLinkHandlerInterface::class, 'security.authenticator.firewall_aware_login_link_handler') | ||
|
|
||
| ->set('security.authenticator.entity_login_link_user_handler', EntityLoginLinkUserHandler::class) | ||
| ->abstract() | ||
| ->args([ | ||
| service('doctrine'), | ||
| abstract_arg('user entity class name'), | ||
| ]) | ||
|
|
||
| ; | ||
| }; |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.