From f654f6ffec83ff23bd1710e99bbedcfff6cfa585 Mon Sep 17 00:00:00 2001 From: konradoboza Date: Wed, 19 Jun 2024 12:17:33 +0200 Subject: [PATCH] improved code according to PHPStan reports --- phpstan-baseline.neon | 25 ------------------- src/lib/Security/JWTAuthenticator.php | 12 +++++++-- .../JWTTokenMutationFormatEventSubscriber.php | 8 +++++- 3 files changed, 17 insertions(+), 28 deletions(-) diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index d540be5..ed36eed 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -2305,31 +2305,6 @@ parameters: count: 1 path: src/lib/Schema/Worker.php - - - message: "#^Access to an undefined property GraphQL\\\\Language\\\\AST\\\\BooleanValueNode\\|GraphQL\\\\Language\\\\AST\\\\EnumValueNode\\|GraphQL\\\\Language\\\\AST\\\\FloatValueNode\\|GraphQL\\\\Language\\\\AST\\\\IntValueNode\\|GraphQL\\\\Language\\\\AST\\\\ListValueNode\\|GraphQL\\\\Language\\\\AST\\\\NullValueNode\\|GraphQL\\\\Language\\\\AST\\\\ObjectValueNode\\|GraphQL\\\\Language\\\\AST\\\\StringValueNode\\|GraphQL\\\\Language\\\\AST\\\\VariableNode\\:\\:\\$value\\.$#" - count: 1 - path: src/lib/Security/JWTAuthenticator.php - - - - message: "#^Parameter \\#1 \\$user of method Lexik\\\\Bundle\\\\JWTAuthenticationBundle\\\\Services\\\\JWTTokenManagerInterface\\:\\:create\\(\\) expects Symfony\\\\Component\\\\Security\\\\Core\\\\User\\\\UserInterface, Symfony\\\\Component\\\\Security\\\\Core\\\\User\\\\UserInterface\\|null given\\.$#" - count: 1 - path: src/lib/Security/JWTAuthenticator.php - - - - message: "#^Cannot call method getContent\\(\\) on Symfony\\\\Component\\\\HttpFoundation\\\\Response\\|null\\.$#" - count: 1 - path: src/lib/Security/JWTTokenMutationFormatEventSubscriber.php - - - - message: "#^Cannot call method setContent\\(\\) on Symfony\\\\Component\\\\HttpFoundation\\\\Response\\|null\\.$#" - count: 1 - path: src/lib/Security/JWTTokenMutationFormatEventSubscriber.php - - - - message: "#^Method Ibexa\\\\GraphQL\\\\Security\\\\JWTTokenMutationFormatEventSubscriber\\:\\:formatMutationResponseData\\(\\) should return string but returns string\\|false\\.$#" - count: 1 - path: src/lib/Security/JWTTokenMutationFormatEventSubscriber.php - - message: "#^Cannot cast object to string\\.$#" count: 1 diff --git a/src/lib/Security/JWTAuthenticator.php b/src/lib/Security/JWTAuthenticator.php index 44fe9f3..6714986 100644 --- a/src/lib/Security/JWTAuthenticator.php +++ b/src/lib/Security/JWTAuthenticator.php @@ -85,10 +85,15 @@ public function authenticate(Request $request): Passport */ public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response { + $user = $token->getUser(); + if ($user === null) { + throw new AuthenticationException('No authenticated user found.', 401); + } + return new Response( json_encode( [ - 'token' => $this->tokenManager->create($token->getUser()), + 'token' => $this->tokenManager->create($user), 'message' => null, ], JSON_THROW_ON_ERROR @@ -131,7 +136,10 @@ private function extractCredentials(string $graphqlQuery): array $parsed, [ NodeKind::ARGUMENT => static function (ArgumentNode $node) use (&$credentials): void { - $credentials[$node->name->value] = (string)$node->value->value; + /** @var \GraphQL\Language\AST\StringValueNode $nodeValue */ + $nodeValue = $node->value; + + $credentials[$node->name->value] = $nodeValue->value; }, ] ); diff --git a/src/lib/Security/JWTTokenMutationFormatEventSubscriber.php b/src/lib/Security/JWTTokenMutationFormatEventSubscriber.php index e71153f..e55d4c4 100644 --- a/src/lib/Security/JWTTokenMutationFormatEventSubscriber.php +++ b/src/lib/Security/JWTTokenMutationFormatEventSubscriber.php @@ -31,6 +31,10 @@ public static function getSubscribedEvents(): array public function onAuthorizationFinishes(LoginSuccessEvent|LoginFailureEvent $event): void { $response = $event->getResponse(); + if ($response === null) { + return; + } + $response->setContent( $this->formatMutationResponseData($response->getContent()) ); @@ -41,10 +45,12 @@ public function onAuthorizationFinishes(LoginSuccessEvent|LoginFailureEvent $eve */ private function formatMutationResponseData(mixed $data): string { - return json_encode([ + $formatted = json_encode([ 'data' => [ 'CreateToken' => json_decode($data, true, 512, JSON_THROW_ON_ERROR), ], ]); + + return $formatted === false ? '' : $formatted; } }