chore (login) #43 : remaniement connexion sql, passage à 1 requête au lieu de 2 pour performances

This commit is contained in:
2024-10-08 14:36:12 +02:00
parent fe4d683c20
commit d707a91694
4 changed files with 67 additions and 79 deletions

View File

@ -10,6 +10,7 @@ use App\SQLLogin\Exception\DataToFetchConfigurationException;
use App\SQLLogin\Exception\InvalidSQLPasswordException;
use App\SQLLogin\Exception\LoginElementsConfigurationException;
use App\SQLLogin\Exception\SecurityPatternConfigurationException;
use App\SQLLogin\SQLLoginRequest;
use Exception;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
@ -34,12 +35,18 @@ class SQLLoginUserAuthenticator extends AbstractLoginFormAuthenticator
private string $baseUrl;
private SQLLoginService $sqlLoginService;
private PasswordEncoder $passwordHasher;
private SQLLoginRequest $sqlLoginRequest;
public function __construct(string $baseUrl, SQLLoginService $sqlLoginService, PasswordEncoder $passwordHasher)
{
public function __construct(
string $baseUrl,
SQLLoginService $sqlLoginService,
PasswordEncoder $passwordHasher,
SQLLoginRequest $sqlLoginRequest
) {
$this->baseUrl = $baseUrl;
$this->sqlLoginService = $sqlLoginService;
$this->passwordHasher = $passwordHasher;
$this->sqlLoginRequest = $sqlLoginRequest;
}
/**
@ -72,14 +79,20 @@ class SQLLoginUserAuthenticator extends AbstractLoginFormAuthenticator
$rememberMe = isset($form['_remember_me']) ? true : false;
$session = $request->getSession();
try {
// requête préparée
list($remoteHashedPassword, $remoteSalt) = $this->sqlLoginService->fetchPassword($login);
$datas = $this->sqlLoginService->fetchPasswordAndDatas($login);
$remoteHashedPassword = $datas[$this->sqlLoginRequest->getPasswordColumnName()];
unset($datas[$this->sqlLoginRequest->getPasswordColumnName()]);
$remoteSalt = $datas[$this->sqlLoginRequest->getSaltColumnName()];
unset($datas[$this->sqlLoginRequest->getSaltColumnName()]);
} catch (DatabaseConnectionException $e) {
$session->set(self::ERROR_PDO, true);
throw new AuthenticationException();
} catch (LoginElementsConfigurationException $e) {
$session->set(self::ERROR_CONFIGURATION, true);
throw new AuthenticationException();
} catch (DataToFetchConfigurationException $e) {
$session->set(self::ERROR_DATA_TO_FETCH_CONFIGURATION, true);
throw new AuthenticationException();
} catch (Exception $exception) {
$request->getSession()->set(self::ERROR_LOGIN, true);
throw new AuthenticationException();
@ -92,8 +105,7 @@ class SQLLoginUserAuthenticator extends AbstractLoginFormAuthenticator
try {
// Comparaison remote hash et hash du input password + salt
$this->passwordHasher->verify($remoteHashedPassword, $plaintextPassword, $remoteSalt);
$attributes = $this->sqlLoginService->fetchDatas($login);
$user = new User($login, $remoteHashedPassword, $attributes, $rememberMe);
$user = new User($login, $remoteHashedPassword, $datas, $rememberMe);
$loader = function (string $userIdentifier) use ($user) {
return $user->getLogin() == $userIdentifier ? $user : null;