91 lines
3.4 KiB
PHP
91 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Security;
|
|
|
|
use App\Entity\User;
|
|
use App\Hydra\Client;
|
|
use App\Pdo\Exception\InvalidPasswordException;
|
|
use App\Services\PdoService;
|
|
use PDOException;
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException;
|
|
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
|
|
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge;
|
|
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
|
|
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
|
|
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
|
|
|
|
class PdoUserAuthenticator extends AbstractAuthenticator
|
|
{
|
|
public const LOGIN_ROUTE = 'app_login';
|
|
|
|
private PdoService $pdoService;
|
|
protected string $baseUrl;
|
|
private Client $client;
|
|
private SessionInterface $session;
|
|
|
|
public function __construct(string $baseUrl, PdoService $pdoService, Client $client, SessionInterface $session)
|
|
{
|
|
$this->baseUrl = $baseUrl;
|
|
$this->pdoService = $pdoService;
|
|
$this->client = $client;
|
|
$this->session = $session;
|
|
}
|
|
|
|
/**
|
|
* Called on every request to decide if this authenticator should be
|
|
* used for the request. Returning `false` will cause this authenticator
|
|
* to be skipped.
|
|
*/
|
|
public function supports(Request $request): bool
|
|
{
|
|
return self::LOGIN_ROUTE === $request->attributes->get('_route') && $request->isMethod('POST');
|
|
}
|
|
|
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey): ?Response
|
|
{
|
|
return new RedirectResponse($this->baseUrl.'/connect/login-accept');
|
|
}
|
|
|
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
|
|
{
|
|
$message = strtr($exception->getMessageKey(), $exception->getMessageData());
|
|
|
|
return new Response($message, Response::HTTP_FORBIDDEN);
|
|
}
|
|
|
|
public function authenticate(Request $request): Passport
|
|
{
|
|
$login = $request->request->get('login');
|
|
$password = $request->request->get('password');
|
|
$rememberMe = $request->request->get('_remember_me') ? true : false;
|
|
try {
|
|
// requête préparée
|
|
$remoteHashedPassword = $this->pdoService->fetchPassword($login);
|
|
} catch (PDOException $e) {
|
|
dd($e);
|
|
}
|
|
try {
|
|
$this->pdoService->verifyPassword($password, $remoteHashedPassword);
|
|
$attributes = $this->pdoService->fetchDatas($login);
|
|
$user = new User($login, $password, $attributes, $rememberMe);
|
|
$loader = function (string $userIdentifier) use ($user) {
|
|
return $user->getLogin() == $userIdentifier ? $user : null;
|
|
};
|
|
$passport = new SelfValidatingPassport(new UserBadge($login, $loader));
|
|
// if ($rememberMe) {
|
|
// $passport->addBadge(new RememberMeBadge());
|
|
// }
|
|
$passport->setAttribute('attributes', $user->getAttributes());
|
|
|
|
return $passport;
|
|
} catch (InvalidPasswordException $e) {
|
|
throw new AuthenticationException();
|
|
}
|
|
}
|
|
}
|