traductions, sentry, form, mariadb, assets
Some checks failed
Cadoles/hydra-sql/pipeline/pr-develop There was a failure building this commit

This commit is contained in:
2022-12-13 15:46:24 +01:00
parent 6fc004a549
commit 9c746638a3
235 changed files with 22613 additions and 179 deletions

View File

@ -3,16 +3,16 @@
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\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Security;
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;
@ -22,18 +22,19 @@ use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPasspor
class PdoUserAuthenticator extends AbstractAuthenticator
{
public const LOGIN_ROUTE = 'app_login';
public const ERROR_LOGIN = 'error_login';
public const ERROR_PASSWORD = 'error_password';
public const ERROR_PDO = 'error_pdo';
private PdoService $pdoService;
protected string $baseUrl;
private Client $client;
private SessionInterface $session;
private PdoService $pdoService;
private UrlGeneratorInterface $router;
public function __construct(string $baseUrl, PdoService $pdoService, Client $client, SessionInterface $session)
public function __construct(string $baseUrl, PdoService $pdoService, UrlGeneratorInterface $router)
{
$this->baseUrl = $baseUrl;
$this->pdoService = $pdoService;
$this->client = $client;
$this->session = $session;
$this->router = $router;
}
/**
@ -53,38 +54,50 @@ class PdoUserAuthenticator extends AbstractAuthenticator
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
{
$message = strtr($exception->getMessageKey(), $exception->getMessageData());
$request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);
return new Response($message, Response::HTTP_FORBIDDEN);
return new RedirectResponse(
$this->router->generate('app_login')
);
}
public function authenticate(Request $request): Passport
{
$login = $request->request->get('login');
$password = $request->request->get('password');
$rememberMe = $request->request->get('_remember_me') ? true : false;
$form = $request->request->get('login');
$login = $form['login'];
$password = $form['password'];
$rememberMe = isset($form['_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) {
$request->getSession()->set(self::ERROR_PDO, true);
throw new AuthenticationException();
}
if ($remoteHashedPassword) {
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) {
$request->getSession()->set(self::ERROR_PASSWORD, true);
throw new AuthenticationException();
} catch (PDOException $e) {
$request->getSession()->set(self::ERROR_PDO, true);
throw new AuthenticationException();
}
}
$request->getSession()->set(self::ERROR_LOGIN, true);
throw new AuthenticationException();
}
}