public function execute(InputInterface $input, OutputInterface $output) { $enabledOnly = $input->getOption('enabledOnly'); $list = $this->apiKeyService->listKeys($enabledOnly); $table = new Table($output); if ($enabledOnly) { $table->setHeaders([$this->translator->translate('Key'), $this->translator->translate('Expiration date')]); } else { $table->setHeaders([$this->translator->translate('Key'), $this->translator->translate('Is enabled'), $this->translator->translate('Expiration date')]); } /** @var ApiKey $row */ foreach ($list as $row) { $key = $row->getKey(); $expiration = $row->getExpirationDate(); $rowData = []; $formatMethod = !$row->isEnabled() ? 'getErrorString' : ($row->isExpired() ? 'getWarningString' : 'getSuccessString'); if ($enabledOnly) { $rowData[] = $this->{$formatMethod}($key); } else { $rowData[] = $this->{$formatMethod}($key); $rowData[] = $this->{$formatMethod}($this->getEnabledSymbol($row)); } $rowData[] = isset($expiration) ? $expiration->format(\DateTime::ISO8601) : '-'; $table->addRow($rowData); } $table->render(); }
public function execute(InputInterface $input, OutputInterface $output) { $apiKey = $input->getArgument('apiKey'); try { $this->apiKeyService->disable($apiKey); $output->writeln(sprintf($this->translator->translate('API key %s properly disabled'), '<info>' . $apiKey . '</info>')); } catch (\InvalidArgumentException $e) { $output->writeln(sprintf('<error>' . $this->translator->translate('API key "%s" does not exist.') . '</error>', $apiKey)); } }
/** * @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]); }
public function execute(InputInterface $input, OutputInterface $output) { $expirationDate = $input->getOption('expirationDate'); $apiKey = $this->apiKeyService->create(isset($expirationDate) ? new \DateTime($expirationDate) : null); $output->writeln($this->translator->translate('Generated API key') . sprintf(': <info>%s</info>', $apiKey)); }