2021-02-05 14:47:54 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace RetailCrm\ServiceBundle\Security;
|
|
|
|
|
|
|
|
use RetailCrm\ServiceBundle\Response\ErrorJsonResponseFactory;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2022-07-20 13:58:16 +03:00
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException;
|
2021-02-05 14:47:54 +03:00
|
|
|
use Symfony\Component\Security\Core\Security;
|
2022-07-19 16:27:00 +03:00
|
|
|
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
|
|
|
|
2022-07-21 17:39:34 +03:00
|
|
|
class ApiClientAuthenticator extends AbstractClientAuthenticator
|
2021-02-05 14:47:54 +03:00
|
|
|
{
|
|
|
|
public function __construct(
|
|
|
|
ErrorJsonResponseFactory $errorResponseFactory,
|
2022-07-20 13:58:16 +03:00
|
|
|
private Security $security,
|
2021-02-05 14:47:54 +03:00
|
|
|
) {
|
|
|
|
parent::__construct($errorResponseFactory);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function supports(Request $request): bool
|
|
|
|
{
|
|
|
|
if ($this->security->getUser()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $request->request->has(static::AUTH_FIELD);
|
|
|
|
}
|
|
|
|
|
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);
|
2022-07-20 13:58:16 +03:00
|
|
|
if (null === $identifier) {
|
|
|
|
throw new AuthenticationException('Request does not contain authentication data');
|
|
|
|
}
|
2022-07-19 16:27:00 +03:00
|
|
|
|
|
|
|
return new SelfValidatingPassport(
|
2022-07-20 16:25:53 +03:00
|
|
|
new UserBadge(
|
2022-07-21 13:02:32 +03:00
|
|
|
$identifier
|
2022-07-20 16:25:53 +03:00
|
|
|
),
|
2022-07-21 17:39:34 +03:00
|
|
|
[]
|
2022-07-19 16:27:00 +03:00
|
|
|
);
|
2021-02-05 14:47:54 +03:00
|
|
|
}
|
|
|
|
}
|