1
0
mirror of synced 2024-11-28 23:56:03 +03:00
service-bundle/Security/CallbackClientAuthenticator.php

42 lines
1.4 KiB
PHP
Raw Normal View History

2021-02-05 14:47:54 +03:00
<?php
namespace RetailCrm\ServiceBundle\Security;
2022-07-20 13:58:16 +03:00
use Doctrine\Persistence\ObjectRepository;
use RetailCrm\ServiceBundle\Response\ErrorJsonResponseFactory;
2021-02-05 14:47:54 +03:00
use Symfony\Component\HttpFoundation\Request;
2022-07-20 13:58:16 +03:00
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
2022-07-19 16:27:00 +03:00
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
2022-07-20 13:58:16 +03:00
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
2021-02-05 14:47:54 +03:00
class CallbackClientAuthenticator extends AbstractClientAuthenticator
{
2022-07-20 13:58:16 +03:00
public function __construct(
ErrorJsonResponseFactory $errorResponseFactory,
private ObjectRepository $repository,
) {
parent::__construct($errorResponseFactory);
2021-02-05 14:47:54 +03:00
}
2022-07-20 13:58:16 +03:00
public function supports(Request $request): bool
2021-02-05 14:47:54 +03:00
{
2022-07-20 13:58:16 +03:00
return $request->request->has(static::AUTH_FIELD) || $request->query->has(static::AUTH_FIELD);
2021-02-05 14:47:54 +03:00
}
2022-07-19 16:27:00 +03:00
public function authenticate(Request $request): Passport
{
$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(
new UserBadge($identifier, function ($userIdentifier) {
2022-07-20 13:58:16 +03:00
return $this->repository->findOneBy([static::AUTH_FIELD => $userIdentifier]);
2022-07-19 16:27:00 +03:00
}),
[]
);
}
2021-02-05 14:47:54 +03:00
}