apiKeyService = $apiKeyService; $this->jwtService = $jwtService; } /** * @param Request $request * @return Response * @throws \InvalidArgumentException */ public function handle(Request $request): Response { $authData = $request->getParsedBody(); if (! isset($authData['apiKey'])) { return new JsonResponse([ 'error' => 'INVALID_ARGUMENT', 'message' => 'You have to provide a valid API key under the "apiKey" param name.', ], self::STATUS_BAD_REQUEST); } // Authenticate using provided API key $apiKey = $this->apiKeyService->getByKey($authData['apiKey']); if ($apiKey === null || ! $apiKey->isValid()) { return new JsonResponse([ 'error' => 'INVALID_API_KEY', 'message' => 'Provided API key does not exist or is invalid.', ], self::STATUS_UNAUTHORIZED); } // Generate a JSON Web Token that will be used for authorization in next requests $token = $this->jwtService->create($apiKey); return new JsonResponse(['token' => $token]); } }