-
-
Notifications
You must be signed in to change notification settings - Fork 801
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace custom authenticator passport with custom badge usage (#1978)
- Loading branch information
Showing
13 changed files
with
267 additions
and
106 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
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
81 changes: 81 additions & 0 deletions
81
src/Security/Http/Authentication/AuthenticationFailureHandler.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,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the HWIOAuthBundle package. | ||
* | ||
* (c) Hardware Info <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace HWI\Bundle\OAuthBundle\Security\Http\Authentication; | ||
|
||
use HWI\Bundle\OAuthBundle\Security\Core\Exception\AccountNotLinkedException; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\Session\SessionInterface; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Symfony\Component\Routing\RouterInterface; | ||
use Symfony\Component\Security\Core\Exception\AuthenticationException; | ||
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface; | ||
use Twig\Environment; | ||
|
||
final class AuthenticationFailureHandler implements AuthenticationFailureHandlerInterface | ||
{ | ||
public function __construct( | ||
private readonly RequestStack $requestStack, | ||
private readonly RouterInterface $router, | ||
private readonly Environment $twig, | ||
private readonly bool $connect | ||
) { | ||
} | ||
|
||
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): Response | ||
{ | ||
$error = $exception->getPrevious(); | ||
|
||
if ($this->connect && $error instanceof AccountNotLinkedException) { | ||
$key = time(); | ||
$session = $request->hasSession() ? $request->getSession() : $this->getSession(); | ||
if ($session) { | ||
if (!$session->isStarted()) { | ||
$session->start(); | ||
} | ||
|
||
$session->set('_hwi_oauth.registration_error.'.$key, $error); | ||
} | ||
|
||
return new RedirectResponse( | ||
$this->router->generate('hwi_oauth_connect_registration', ['key' => $key], UrlGeneratorInterface::ABSOLUTE_PATH) | ||
); | ||
} | ||
|
||
if ($error instanceof AuthenticationException) { | ||
$error = $error->getMessageKey(); | ||
} else { | ||
$error = $exception->getMessageKey(); | ||
} | ||
|
||
return new Response( | ||
$this->twig->render('@HWIOAuth/Connect/login.html.twig', ['error' => $error]) | ||
); | ||
} | ||
|
||
private function getSession(): ?SessionInterface | ||
{ | ||
if (method_exists($this->requestStack, 'getSession')) { | ||
return $this->requestStack->getSession(); | ||
} | ||
|
||
if ((null !== $request = $this->requestStack->getCurrentRequest()) && $request->hasSession()) { | ||
return $request->getSession(); | ||
} | ||
|
||
return null; | ||
} | ||
} |
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
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
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
Oops, something went wrong.