• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemsdk / docker-symfony-api / #74

pending completion
#74

push

DKravtsov
Php 8.2, symfony 6.2, updated RabbitMQ, updated composer dependencies, refactoring.

51 of 51 new or added lines in 44 files covered. (100.0%)

1479 of 2668 relevant lines covered (55.43%)

23.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

26.67
/src/ApiKey/Application/Security/Authenticator/ApiKeyAuthenticator.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace App\ApiKey\Application\Security\Authenticator;
6

7
use App\ApiKey\Application\Security\Provider\ApiKeyUserProvider;
8
use Symfony\Component\HttpFoundation\JsonResponse;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
12
use Symfony\Component\Security\Core\Exception\AuthenticationException;
13
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
14
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
15
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
16
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
17
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
18

19
use function preg_match;
20

21
/**
22
 * Class ApiKeyAuthenticator
23
 *
24
 * @package App\ApiKey
25
 */
26
class ApiKeyAuthenticator extends AbstractAuthenticator
27
{
28
    public function __construct(
29
        private readonly ApiKeyUserProvider $apiKeyUserProvider,
30
    ) {
31
    }
137✔
32

33
    public function supports(Request $request): ?bool
34
    {
35
        return $this->getToken($request) !== '';
137✔
36
    }
37

38
    public function authenticate(Request $request): Passport
39
    {
40
        $token = $this->getToken($request);
×
41
        $apiKey = $this->apiKeyUserProvider->getApiKeyForToken($token);
×
42

43
        if ($apiKey === null) {
×
44
            throw new UserNotFoundException('API key not found');
×
45
        }
46

47
        return new SelfValidatingPassport(new UserBadge($token));
×
48
    }
49

50
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
51
    {
52
        return null;
×
53
    }
54

55
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception): Response
56
    {
57
        $data = [
×
58
            'code' => Response::HTTP_UNAUTHORIZED,
×
59
            'message' => 'Invalid ApiKey',
×
60
        ];
×
61

62
        return new JsonResponse($data, Response::HTTP_UNAUTHORIZED);
×
63
    }
64

65
    private function getToken(Request $request): string
66
    {
67
        preg_match('#^ApiKey (\w+)$#', $request->headers->get('Authorization', ''), $matches);
137✔
68

69
        return $matches[1] ?? '';
137✔
70
    }
71
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc