Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
9 / 9
75.00% covered (warning)
75.00%
6 / 8
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
InternalAppAuthenticator
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
9 / 9
75.00% covered (warning)
75.00%
6 / 8
100.00% covered (success)
100.00%
5 / 5
7.77
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 authenticate
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
5 / 5
50.00% covered (danger)
50.00%
2 / 4
100.00% covered (success)
100.00%
1 / 1
4.12
 supports
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 onAuthenticationSuccess
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 onAuthenticationFailure
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Security\Authenticator;
4
5use App\Security\ApiUser;
6use Symfony\Component\DependencyInjection\Attribute\Autowire;
7use Symfony\Component\HttpFoundation\JsonResponse;
8use Symfony\Component\HttpFoundation\Request;
9use Symfony\Component\HttpFoundation\Response;
10use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
11use Symfony\Component\Security\Core\Exception\AuthenticationException;
12use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
13use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
14use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
15use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
16
17/**
18 * @author Wilhelm Zwertvaegher
19 */
20class InternalAppAuthenticator extends AbstractAuthenticator
21{
22    public function __construct(
23        #[Autowire('%internal_app.key_header%')]
24        private readonly string $internalAppKeyHeader,
25        #[Autowire('%internal_app.key%')]
26        private readonly string $internalAppKey,
27    ) {
28    }
29
30    public function authenticate(Request $request): Passport
31    {
32        $appKey = $request->headers->get($this->internalAppKeyHeader);
33
34        if (!$appKey || $appKey !== $this->internalAppKey) {
35            throw new AuthenticationException('Invalid app key');
36        }
37
38        return new SelfValidatingPassport(
39            new UserBadge('internal', fn (string $userIdentifier) => new ApiUser($userIdentifier, ['ROLE_INTERNAL'])),
40        );
41    }
42
43    public function supports(Request $request): ?bool
44    {
45        return $request->headers->has($this->internalAppKeyHeader);
46    }
47
48    public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
49    {
50        return null;
51    }
52
53    public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
54    {
55        $data = [
56            'message' => $exception->getMessage(),
57        ];
58
59        return new JsonResponse($data, Response::HTTP_UNAUTHORIZED);
60    }
61}