-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security] AuthenticatorManager to make "authenticators" first-class security #33558
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
fabpot
merged 30 commits into
symfony:master
from
wouterj:security/deprecate-providers-listeners
Apr 21, 2020
Merged
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
c321f4d
Created GuardAuthenticationManager to make Guard first-class Security
wouterj a6890db
Created HttpBasicAuthenticator and some Guard traits
wouterj 9b7fddd
Integrated GuardAuthenticationManager in the SecurityBundle
wouterj a172bac
Added FormLogin and Anonymous authenticators
wouterj 526f756
Added GuardManagerListener
wouterj 5013258
Add provider key in PreAuthenticationGuardToken
wouterj 5efa892
Create a new core AuthenticatorInterface
wouterj fa4b3ec
Implemented password migration for the new authenticators
wouterj 4c06236
Fixes after testing in Demo application
wouterj 873b949
Mark new core authenticators as experimental
wouterj b923e4c
Enabled remember me for the GuardManagerListener
wouterj b14a5e8
Moved new authenticator to the HTTP namespace
wouterj 999ec27
Refactor to an event based authentication approach
wouterj 7859977
Removed all mentions of 'guard' in the new system
wouterj 1c810d5
Added support for lazy firewalls
wouterj ddf430f
Added remember me functionality
wouterj 09bed16
Only load old manager if new system is disabled
wouterj 44cc76f
Use one AuthenticatorManager per firewall
wouterj bf1a452
Merge AuthenticatorManager and AuthenticatorHandler
wouterj 60d396f
Added automatically CSRF protected authenticators
wouterj 59f49b2
Rename AuthenticatingListener
wouterj 6b9d78d
Added tests
wouterj ba3754a
Differentiate between interactive and non-interactive authenticators
wouterj f5e11e5
Reverted changes to the Guard component
wouterj 95edc80
Added pre-authenticated authenticators (X.509 & REMOTE_USER)
wouterj 7ef6a7a
Use the firewall event dispatcher
wouterj 0fe5083
Added JSON login authenticator
wouterj 9ea32c4
Also use authentication failure/success handlers in FormLoginAuthenti…
wouterj 50224aa
Introduce Passport & Badges to extend authenticators
wouterj b1e040f
Rename providerKey to firewallName for more consistent naming
wouterj 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
Create a new core AuthenticatorInterface
This is an iteration on the AuthenticatorInterface of the Guard, to allow more flexibility so it can be used as a real replaced of the authentication providers and listeners.
- Loading branch information
commit 5efa89239550057ff87edd3926562869f179626d
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
...Bundle/SecurityBundle/DependencyInjection/Security/Factory/EntryPointFactoryInterface.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,25 @@ | ||
<?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\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* @author Wouter de Jong <wouter@wouterj.nl> | ||
*/ | ||
interface EntryPointFactoryInterface | ||
{ | ||
/** | ||
* Creates the entry point and returns the service ID. | ||
*/ | ||
public function createEntryPoint(ContainerBuilder $container, string $id, array $config, ?string $defaultEntryPointId): string; | ||
} |
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
35 changes: 35 additions & 0 deletions
35
src/Symfony/Component/Security/Core/Authentication/Authenticator/AbstractAuthenticator.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,35 @@ | ||
<?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\Security\Core\Authentication\Authenticator; | ||
|
||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken; | ||
|
||
/** | ||
* An optional base class that creates the necessary tokens for you. | ||
* | ||
* @author Ryan Weaver <ryan@knpuniversity.com> | ||
*/ | ||
abstract class AbstractAuthenticator implements AuthenticatorInterface | ||
{ | ||
/** | ||
* Shortcut to create a PostAuthenticationGuardToken for you, if you don't really | ||
* care about which authenticated token you're using. | ||
* | ||
* @return PostAuthenticationGuardToken | ||
*/ | ||
public function createAuthenticatedToken(UserInterface $user, string $providerKey): TokenInterface | ||
{ | ||
return new PostAuthenticationGuardToken($user, $providerKey, $user->getRoles()); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...y/Component/Security/Core/Authentication/Authenticator/AbstractFormLoginAuthenticator.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,62 @@ | ||
<?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\Security\Core\Authentication\Authenticator; | ||
|
||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Security\Core\Exception\AuthenticationException; | ||
use Symfony\Component\Security\Core\Security; | ||
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; | ||
|
||
/** | ||
* A base class to make form login authentication easier! | ||
* | ||
* @author Ryan Weaver <ryan@knpuniversity.com> | ||
*/ | ||
4949 | abstract class AbstractFormLoginAuthenticator extends AbstractAuthenticator implements AuthenticationEntryPointInterface | |
{ | ||
/** | ||
* Return the URL to the login page. | ||
*/ | ||
abstract protected function getLoginUrl(): string; | ||
|
||
/** | ||
* Override to change what happens after a bad username/password is submitted. | ||
*/ | ||
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): Response | ||
{ | ||
if ($request->hasSession()) { | ||
$request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception); | ||
} | ||
|
||
$url = $this->getLoginUrl(); | ||
|
||
return new RedirectResponse($url); | ||
} | ||
|
||
public function supportsRememberMe(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Override to control what happens when the user hits a secure page | ||
* but isn't logged in yet. | ||
*/ | ||
public function start(Request $request, AuthenticationException $authException = null): Response | ||
{ | ||
$url = $this->getLoginUrl(); | ||
|
||
return new RedirectResponse($url); | ||
} | ||
} |
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
129 changes: 129 additions & 0 deletions
129
src/Symfony/Component/Security/Core/Authentication/Authenticator/AuthenticatorInterface.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,129 @@ | ||
<?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\Security\Core\Authentication\Authenticator; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Exception\AuthenticationException; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
/** | ||
* The interface for all authenticators. | ||
* | ||
* @author Ryan Weaver <ryan@knpuniversity.com> | ||
* @author Amaury Leroux de Lens <amaury@lerouxdelens.com> | ||
* @author Wouter de Jong <wouter@wouterj.nl> | ||
*/ | ||
interface AuthenticatorInterface | ||
{ | ||
/** | ||
* Does the authenticator support the given Request? | ||
* | ||
* If this returns false, the authenticator will be skipped. | ||
*/ | ||
public function supports(Request $request): ?bool; | ||
|
||
/** | ||
* Get the authentication credentials from the request and return them | ||
* as any type (e.g. an associate array). | ||
* | ||
* Whatever value you return here will be passed to getUser() and checkCredentials() | ||
* | ||
* For example, for a form login, you might: | ||
* | ||
* return [ | ||
* 'username' => $request->request->get('_username'), | ||
* 'password' => $request->request->get('_password'), | ||
* ]; | ||
* | ||
* Or for an API token that's on a header, you might use: | ||
* | ||
* return ['api_key' => $request->headers->get('X-API-TOKEN')]; | ||
* | ||
* @return mixed Any non-null value | ||
* | ||
* @throws \UnexpectedValueException If null is returned | ||
*/ | ||
public function getCredentials(Request $request); | ||
|
||
/** | ||
* Return a UserInterface object based on the credentials. | ||
* | ||
* You may throw an AuthenticationException if you wish. If you return | ||
* null, then a UsernameNotFoundException is thrown for you. | ||
* | ||
* @param mixed $credentials the value returned from getCredentials() | ||
* | ||
* @throws AuthenticationException | ||
*/ | ||
public function getUser($credentials): ?UserInterface; | ||
|
||
/** | ||
* Returns true if the credentials are valid. | ||
* | ||
* If false is returned, authentication will fail. You may also throw | ||
* an AuthenticationException if you wish to cause authentication to fail. | ||
* | ||
* @param mixed $credentials the value returned from getCredentials() | ||
* | ||
* @throws AuthenticationException | ||
*/ | ||
public function checkCredentials($credentials, UserInterface $user): bool; | ||
|
||
/** | ||
* Create an authenticated token for the given user. | ||
* | ||
* If you don't care about which token class is used or don't really | ||
* understand what a "token" is, you can skip this method by extending | ||
* the AbstractAuthenticator class from your authenticator. | ||
* | ||
* @see AbstractAuthenticator | ||
*/ | ||
public function createAuthenticatedToken(UserInterface $user, string $providerKey): TokenInterface; | ||
|
||
/** | ||
* Called when authentication executed, but failed (e.g. wrong username password). | ||
* | ||
* This should return the Response sent back to the user, like a | ||
* RedirectResponse to the login page or a 403 response. | ||
* | ||
* If you return null, the request will continue, but the user will | ||
* not be authenticated. This is probably not what you want to do. | ||
*/ | ||
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response; | ||
|
||
/** | ||
* Called when authentication executed and was successful! | ||
* | ||
* This should return the Response sent back to the user, like a | ||
* RedirectResponse to the last page they visited. | ||
* | ||
* If you return null, the current request will continue, and the user | ||
* will be authenticated. This makes sense, for example, with an API. | ||
*/ | ||
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $providerKey): ?Response; | ||
|
||
/** | ||
* Does this method support remember me cookies? | ||
* | ||
* Remember me cookie will be set if *all* of the following are met: | ||
* A) This method returns true | ||
* B) The remember_me key under your firewall is configured | ||
* C) The "remember me" functionality is activated. This is usually | ||
* done by having a _remember_me checkbox in your form, but | ||
* can be configured by the "always_remember_me" and "remember_me_parameter" | ||
* parameters under the "remember_me" firewall key | ||
* D) The onAuthenticationSuccess method returns a Response object | ||
*/ | ||
public function supportsRememberMe(): bool; | ||
} |
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.