<?php
namespace App\Security;
use App\Security\User;
use App\Service\Api\ApiServicePortalService;
use App\Service\Interfaces\ApiInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Http\Util\TargetPathTrait;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
class UserAuthenticator extends AbstractLoginFormAuthenticator
{
use TargetPathTrait;
public const LOGIN_ROUTE = 'login';
private UrlGeneratorInterface $urlGenerator;
private ApiServicePortalService $apiServicePortalService;
public function __construct(UrlGeneratorInterface $urlGenerator, ApiServicePortalService $apiServicePortalService)
{
$this->urlGenerator = $urlGenerator;
$this->apiServicePortalService = $apiServicePortalService;
}
public function authenticate(Request $request): Passport
{
$username = $request->request->get('username', '');
$password = $request->request->get('password', '');
$request->getSession()->set(Security::LAST_USERNAME, $username);
$resource = sprintf('user/check-credentials/%s/%s', $username, User::ROLE_PROVIDER);
$options = [
ApiInterface::HEADERS => ['user-password' => $password]
];
$response = $this->apiServicePortalService->apiRequest(
ApiInterface::GET,
$resource,
[],
$options
);
if (ApiInterface::API_SUCCESS_CODE_200 !== $response[ApiInterface::RESPONSE_CODE]) {
$msg = 'Fallo en la conexión';
if (isset($response[ApiInterface::RESPONSE_STATUS][ApiInterface::RESPONSE_STATUS_MESSAGE])) {
$msg = $response[ApiInterface::RESPONSE_STATUS][ApiInterface::RESPONSE_STATUS_MESSAGE];
}
throw new CustomUserMessageAuthenticationException(
$msg
);
}
$userUuid = $response[ApiInterface::RESPONSE_DATA]['user']['uuid'];
/* Using callback function in UserBadge
Not call to src/Security/UserProvider.php::loadUserByIdentifier
*/
return new SelfValidatingPassport(
new UserBadge($userUuid, function ($userUuid) use ($response) {
$userName = $response[ApiInterface::RESPONSE_DATA]['user']['username'];
$userEmail = $response[ApiInterface::RESPONSE_DATA]['user']['email'];
return (new User())
->setUsername($userName)
->setUuid($userUuid)
->setEmail($userEmail)
->setRoles([User::ROLE_PROVIDER]);
})
);
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
{
if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) {
return new RedirectResponse($targetPath);
}
// For example:
return new RedirectResponse($this->urlGenerator->generate('work_order_list'));
}
protected function getLoginUrl(Request $request): string
{
return $this->urlGenerator->generate(self::LOGIN_ROUTE);
}
}