Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
13 / 13 |
|
100.00% |
9 / 9 |
|
75.00% |
6 / 8 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| InternalAppAuthenticator | |
100.00% |
13 / 13 |
|
100.00% |
9 / 9 |
|
75.00% |
6 / 8 |
|
100.00% |
5 / 5 |
7.77 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| authenticate | |
100.00% |
6 / 6 |
|
100.00% |
5 / 5 |
|
50.00% |
2 / 4 |
|
100.00% |
1 / 1 |
4.12 | |||
| supports | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| onAuthenticationSuccess | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| onAuthenticationFailure | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Security\Authenticator; |
| 4 | |
| 5 | use App\Security\ApiUser; |
| 6 | use Symfony\Component\DependencyInjection\Attribute\Autowire; |
| 7 | use Symfony\Component\HttpFoundation\JsonResponse; |
| 8 | use Symfony\Component\HttpFoundation\Request; |
| 9 | use Symfony\Component\HttpFoundation\Response; |
| 10 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 11 | use Symfony\Component\Security\Core\Exception\AuthenticationException; |
| 12 | use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator; |
| 13 | use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; |
| 14 | use Symfony\Component\Security\Http\Authenticator\Passport\Passport; |
| 15 | use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport; |
| 16 | |
| 17 | /** |
| 18 | * @author Wilhelm Zwertvaegher |
| 19 | */ |
| 20 | class 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 | } |
Below are the source code lines that represent each code path as identified by Xdebug. Please note a path is not
necessarily coterminous with a line, a line may contain multiple paths and therefore show up more than once.
Please also be aware that some paths may include implicit rather than explicit branches, e.g. an if statement
always has an else as part of its logical flow even if you didn't write one.
| 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 | } |
| 30 | public function authenticate(Request $request): Passport |
| 31 | { |
| 32 | $appKey = $request->headers->get($this->internalAppKeyHeader); |
| 33 | |
| 34 | if (!$appKey || $appKey !== $this->internalAppKey) { |
| 34 | if (!$appKey || $appKey !== $this->internalAppKey) { |
| 34 | if (!$appKey || $appKey !== $this->internalAppKey) { |
| 35 | throw new AuthenticationException('Invalid app key'); |
| 30 | public function authenticate(Request $request): Passport |
| 31 | { |
| 32 | $appKey = $request->headers->get($this->internalAppKeyHeader); |
| 33 | |
| 34 | if (!$appKey || $appKey !== $this->internalAppKey) { |
| 34 | if (!$appKey || $appKey !== $this->internalAppKey) { |
| 34 | if (!$appKey || $appKey !== $this->internalAppKey) { |
| 38 | return new SelfValidatingPassport( |
| 39 | new UserBadge('internal', fn (string $userIdentifier) => new ApiUser($userIdentifier, ['ROLE_INTERNAL'])), |
| 40 | ); |
| 41 | } |
| 30 | public function authenticate(Request $request): Passport |
| 31 | { |
| 32 | $appKey = $request->headers->get($this->internalAppKeyHeader); |
| 33 | |
| 34 | if (!$appKey || $appKey !== $this->internalAppKey) { |
| 34 | if (!$appKey || $appKey !== $this->internalAppKey) { |
| 35 | throw new AuthenticationException('Invalid app key'); |
| 30 | public function authenticate(Request $request): Passport |
| 31 | { |
| 32 | $appKey = $request->headers->get($this->internalAppKeyHeader); |
| 33 | |
| 34 | if (!$appKey || $appKey !== $this->internalAppKey) { |
| 34 | if (!$appKey || $appKey !== $this->internalAppKey) { |
| 38 | return new SelfValidatingPassport( |
| 39 | new UserBadge('internal', fn (string $userIdentifier) => new ApiUser($userIdentifier, ['ROLE_INTERNAL'])), |
| 40 | ); |
| 41 | } |
| 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 | } |
| 48 | public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response |
| 49 | { |
| 50 | return null; |
| 51 | } |
| 43 | public function supports(Request $request): ?bool |
| 44 | { |
| 45 | return $request->headers->has($this->internalAppKeyHeader); |
| 46 | } |
| 39 | new UserBadge('internal', fn (string $userIdentifier) => new ApiUser($userIdentifier, ['ROLE_INTERNAL'])), |
| 3 | namespace App\Security\Authenticator; |
| 4 | |
| 5 | use App\Security\ApiUser; |
| 6 | use Symfony\Component\DependencyInjection\Attribute\Autowire; |
| 7 | use Symfony\Component\HttpFoundation\JsonResponse; |
| 8 | use Symfony\Component\HttpFoundation\Request; |
| 9 | use Symfony\Component\HttpFoundation\Response; |
| 10 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 11 | use Symfony\Component\Security\Core\Exception\AuthenticationException; |
| 12 | use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator; |
| 13 | use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; |
| 14 | use Symfony\Component\Security\Http\Authenticator\Passport\Passport; |
| 15 | use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport; |
| 16 | |
| 17 | /** |
| 18 | * @author Wilhelm Zwertvaegher |
| 19 | */ |
| 20 | class 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 | } |