-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security] Add a Not_Full_Fledged_handler in ExceptionListener from security login #57661
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
Open
eltharin
wants to merge
8
commits into
symfony:7.4
Choose a base branch
from
eltharin:notfullfletchedHandler
base: 7.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+273
−21
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4b1a99a
add security.firewalls.not_full_fledged_handler option
eltharin 588566a
review changes
eltharin d170f59
change follow review
eltharin 8e80350
changing descritpion
eltharin bcc1a41
Update src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security…
eltharin 38a442b
fabbot comments
eltharin 91e23d0
starTAuthentication
eltharin 0fb0b24
change target version
eltharin 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
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
64 changes: 64 additions & 0 deletions
64
src/Symfony/Component/Security/Http/Authorization/NotFullFledgedEqualNormalLoginHandler.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,64 @@ | ||
<?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\Http\Authorization; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\HttpKernel\Event\ExceptionEvent; | ||
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter; | ||
use Symfony\Component\Security\Core\Exception\AccessDeniedException; | ||
use Symfony\Component\Security\Core\Exception\InsufficientAuthenticationException; | ||
|
||
/** | ||
* NotFullFledgedHandler for considering NotFullFledged Login equal to Normal Login except if IS_AUTHENTICATED_FULLY is asked | ||
* If IS_AUTHENTICATED_FULLY is in access denied Exception Attrribute, user is redirect to | ||
* startAuthentication function in AuthenticationTrustResolver | ||
* Otherwise The original AccessDeniedException is passing to accessDeniedHandler or ErrorPage or Thrown. | ||
* | ||
* @author Roman JOLY <eltharin18@outlook.fr> | ||
*/ | ||
class NotFullFledgedEqualNormalLoginHandler implements NotFullFledgedHandlerInterface | ||
{ | ||
public function handle(ExceptionEvent $event, AccessDeniedException $exception, AuthenticationTrustResolverInterface $trustResolver, ?TokenInterface $token, ?LoggerInterface $logger, callable $startAuthenticationCallback): bool | ||
{ | ||
if (!$trustResolver->isAuthenticated($token)) { | ||
$this->reauthenticate($startAuthenticationCallback, $event, $token, $exception, $logger); | ||
} | ||
|
||
foreach ($exception->getAttributes() as $attribute) { | ||
if (\in_array($attribute, [AuthenticatedVoter::IS_AUTHENTICATED_FULLY])) { | ||
$this->reauthenticate($startAuthenticationCallback, $event, $token, $exception, $logger); | ||
|
||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private function reauthenticate(callable $startAuthenticationCallback, ExceptionEvent $event, ?TokenInterface $token, AccessDeniedException $exception, ?LoggerInterface $logger): void | ||
{ | ||
$logger?->debug('Access denied, the user is not fully authenticated; redirecting to authentication entry point.', ['exception' => $exception]); | ||
|
||
try { | ||
$insufficientAuthenticationException = new InsufficientAuthenticationException('Full authentication is required to access this resource.', 0, $exception); | ||
if (null !== $token) { | ||
$insufficientAuthenticationException->setToken($token); | ||
} | ||
|
||
$event->setResponse($startAuthenticationCallback($event->getRequest(), $insufficientAuthenticationException)); | ||
} catch (\Exception $e) { | ||
$event->setThrowable($e); | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/Symfony/Component/Security/Http/Authorization/NotFullFledgedHandlerInterface.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,38 @@ | ||
<?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\Http\Authorization; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\HttpKernel\Event\ExceptionEvent; | ||
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Exception\AccessDeniedException; | ||
|
||
/** | ||
* This is used by the ExceptionListener to translate an AccessDeniedException | ||
* to a Response object. | ||
* | ||
* @author Roman JOLY <eltharin18@outlook.fr> | ||
*/ | ||
interface NotFullFledgedHandlerInterface | ||
{ | ||
/** | ||
* Allow to manage NotFullFledged cases when ExceptionListener catch AccessDeniedException | ||
* This function can make checks and event / exception changes to change the Response | ||
* It returns a boolean for break or not after that or continue the ExceptionListener process to decorate Exception and their response. | ||
* | ||
* @param $startAuthenticationCallback callable for call start function from | ||
* | ||
* @return bool break handleAccessDeniedException function in ExceptionListener after handle | ||
*/ | ||
public function handle(ExceptionEvent $event, AccessDeniedException $exception, AuthenticationTrustResolverInterface $trustResolver, ?TokenInterface $token, ?LoggerInterface $logger, callable $startAuthenticationCallback): bool; | ||
} |
51 changes: 51 additions & 0 deletions
51
...ponent/Security/Http/Authorization/NotFullFledgedRedirectToStartAuthenticationHandler.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,51 @@ | ||
<?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\Http\Authorization; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\HttpKernel\Event\ExceptionEvent; | ||
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Exception\AccessDeniedException; | ||
use Symfony\Component\Security\Core\Exception\InsufficientAuthenticationException; | ||
|
||
/** | ||
* NotFullFledgedHandler for considering NotFullFledged Login has to be redirect to login page if AccessDeniedException is thrown | ||
* When an AccessDeniedException is thrown and user is not full fledged logged, he is redirected to login page with | ||
* start function from authenticationEntryPoint. | ||
* | ||
* @author Roman JOLY <eltharin18@outlook.fr> | ||
*/ | ||
class NotFullFledgedRedirectToStartAuthenticationHandler implements NotFullFledgedHandlerInterface | ||
{ | ||
public function handle(ExceptionEvent $event, AccessDeniedException $exception, AuthenticationTrustResolverInterface $trustResolver, ?TokenInterface $token, ?LoggerInterface $logger, callable $startAuthenticationCallback): bool | ||
{ | ||
if (!$trustResolver->isFullFledged($token)) { | ||
$logger?->debug('Access denied, the user is not fully authenticated; redirecting to authentication entry point.', ['exception' => $exception]); | ||
|
||
try { | ||
$insufficientAuthenticationException = new InsufficientAuthenticationException('Full authentication is required to access this resource.', 0, $exception); | ||
if (null !== $token) { | ||
$insufficientAuthenticationException->setToken($token); | ||
} | ||
|
||
$event->setResponse($startAuthenticationCallback($event->getRequest(), $insufficientAuthenticationException)); | ||
} catch (\Exception $e) { | ||
$event->setThrowable($e); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
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.
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.