Example #1
0
 /**
  * @param Request $request
  * @param Response $response
  * @param callable|null $out
  * @return null|Response
  */
 public function dispatch(Request $request, Response $response, callable $out = null)
 {
     $authData = $request->getParsedBody();
     if (!isset($authData['apiKey'])) {
         return new JsonResponse(['error' => RestUtils::INVALID_ARGUMENT_ERROR, 'message' => $this->translator->translate('You have to provide a valid API key under the "apiKey" param name.')], 400);
     }
     // Authenticate using provided API key
     $apiKey = $this->apiKeyService->getByKey($authData['apiKey']);
     if (!isset($apiKey) || !$apiKey->isValid()) {
         return new JsonResponse(['error' => RestUtils::INVALID_API_KEY_ERROR, 'message' => $this->translator->translate('Provided API key does not exist or is invalid.')], 401);
     }
     // Generate a JSON Web Token that will be used for authorization in next requests
     $token = $this->jwtService->create($apiKey);
     return new JsonResponse(['token' => $token]);
 }
Example #2
0
 /**
  * @test
  */
 public function listEnabledFindsOnlyEnabledApiKeys()
 {
     $repo = $this->prophesize(EntityRepository::class);
     $repo->findBy(['enabled' => true])->willReturn([])->shouldBeCalledTimes(1);
     $this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
     $this->service->listKeys(true);
 }