1
0
mirror of synced 2024-11-25 06:16:06 +03:00
service-bundle/Security/FrontApiClientAuthenticator.php

57 lines
1.6 KiB
PHP
Raw Normal View History

2021-02-05 14:47:54 +03:00
<?php
namespace RetailCrm\ServiceBundle\Security;
2022-07-19 16:27:00 +03:00
use App\Repository\ConnectionRepository;
2021-02-05 14:47:54 +03:00
use RetailCrm\ServiceBundle\Response\ErrorJsonResponseFactory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;
2022-07-19 16:27:00 +03:00
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;
2021-02-05 14:47:54 +03:00
class FrontApiClientAuthenticator extends AbstractClientAuthenticator
{
private $security;
2022-07-19 16:27:00 +03:00
private $repository;
2021-02-05 14:47:54 +03:00
public function __construct(
ErrorJsonResponseFactory $errorResponseFactory,
2022-07-19 16:27:00 +03:00
Security $security,
ConnectionRepository $repository
2021-02-05 14:47:54 +03:00
) {
parent::__construct($errorResponseFactory);
$this->security = $security;
2022-07-19 16:27:00 +03:00
$this->repository = $repository;
2021-02-05 14:47:54 +03:00
}
/**
* {@inheritdoc }
*/
public function supports(Request $request): bool
{
if ($this->security->getUser()) {
return false;
}
return $request->request->has(static::AUTH_FIELD);
}
/**
* {@inheritdoc }
*/
2022-07-19 16:27:00 +03:00
public function authenticate(Request $request): Passport
2021-02-05 14:47:54 +03:00
{
2022-07-19 16:27:00 +03:00
$identifier = $request->request->get(static::AUTH_FIELD);
return new SelfValidatingPassport(
new UserBadge($identifier, function ($userIdentifier) {
return $this->repository->findByIdentifier($userIdentifier);
}),
[new RememberMeBadge()]
);
2021-02-05 14:47:54 +03:00
}
}