-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security] Add FirewallUserAuthenticator
- authenticate users in any firewall
#39346
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
77 changes: 77 additions & 0 deletions
77
src/Symfony/Bundle/SecurityBundle/Security/FirewallUserAuthenticator.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,77 @@ | ||
<?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\Security; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\BadgeInterface; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport; | ||
use Symfony\Component\Security\Http\Event\AuthenticationTokenCreatedEvent; | ||
use Symfony\Component\Security\Http\Event\LoginSuccessEvent; | ||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; | ||
|
||
/** | ||
* @author Martin Kirilov <wucdbm@gmail.com> | ||
* | ||
* @final | ||
* @experimental in 5.2 | ||
*/ | ||
class FirewallUserAuthenticator | ||
{ | ||
private $firewallLocator; | ||
private $eventDispatcher; | ||
|
||
public function __construct(ContainerInterface $firewallLocator, EventDispatcherInterface $eventDispatcher) | ||
{ | ||
$this->firewallLocator = $firewallLocator; | ||
$this->eventDispatcher = $eventDispatcher; | ||
} | ||
|
||
/** | ||
* @param BadgeInterface[] $badges Optionally, pass some Passport badges to use for the manual login | ||
*/ | ||
public function authenticateUser(UserInterface $user, Request $request, string $firewallName, array $badges = []): void | ||
{ | ||
// TODO Tests | ||
// TODO Throw if not master request? | ||
if (!$request->hasSession()) { | ||
return; | ||
} | ||
|
||
if (!$this->firewallLocator->has('security.firewall.map.context.'.$firewallName)) { | ||
throw new \LogicException(sprintf('Firewall "%s" not found. Did you register your firewall?', $firewallName)); | ||
} | ||
|
||
/** @var FirewallContext $firewallContext */ | ||
$firewallContext = $this->firewallLocator->get('security.firewall.map.context.'.$firewallName); | ||
// todo can firewall config ever be null? | ||
$firewallConfig = $firewallContext->getConfig(); | ||
|
||
// Note: We're only using PreAuthenticatedAuthenticator because of Symfony\Component\Security\Http\EventListener\RememberMeListener | ||
$authenticator = new PreAuthenticatedAuthenticator(); | ||
// create PreAuthenticatedToken token for the User | ||
$token = $authenticator->createAuthenticatedToken($passport = new SelfValidatingPassport(new UserBadge($user->getUsername(), function () use ($user) { return $user; }), $badges), $firewallName); | ||
|
||
// announce the authenticated token | ||
$token = $this->eventDispatcher->dispatch(new AuthenticationTokenCreatedEvent($token))->getAuthenticatedToken(); | ||
|
||
$this->eventDispatcher->dispatch($loginSuccessEvent = new LoginSuccessEvent($authenticator, $passport, $token, $request, null, $firewallName)); | ||
|
||
$sessionKey = '_security_'.$firewallConfig->getContext(); | ||
$session = $request->getSession(); | ||
// increments the internal session usage index | ||
$session->getMetadataBag(); | ||
$session->set($sessionKey, serialize($token)); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/Symfony/Bundle/SecurityBundle/Security/PreAuthenticatedAuthenticator.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,59 @@ | ||
<?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\Security; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Exception\AuthenticationException; | ||
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface; | ||
|
||
/** | ||
* This class is only used in FirewallUserAuthenticator. | ||
* Its sole reason if existence is LoginSuccessEvent requiring an authenticator. | ||
* | ||
* @author Martin Kirilov <wucdbm@gmail.com> | ||
* | ||
* @internal | ||
* | ||
* @experimental in 5.2 | ||
*/ | ||
class PreAuthenticatedAuthenticator implements AuthenticatorInterface | ||
{ | ||
public function supports(Request $request): ?bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function authenticate(Request $request): PassportInterface | ||
{ | ||
throw new \LogicException(sprintf('"%s" does not support "%s::authenticate()" calls.', static::class, AuthenticatorInterface::class)); | ||
} | ||
|
||
public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface | ||
{ | ||
// TODO Tests | ||
return new PreAuthenticatedToken($passport->getUser(), null, $firewallName, $passport->getUser()->getRoles()); | ||
} | ||
|
||
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response | ||
{ | ||
return null; | ||
} | ||
|
||
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response | ||
{ | ||
return null; | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this should be introduced as experimental, it needs to target
6.0
please