2016-08-06 14:18:27 +03:00
|
|
|
<?php
|
2017-10-12 11:13:20 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-08-06 14:18:27 +03:00
|
|
|
namespace Shlinkio\Shlink\Rest\Service;
|
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
|
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
2018-09-28 23:08:01 +03:00
|
|
|
use function sprintf;
|
2016-08-06 14:18:27 +03:00
|
|
|
|
|
|
|
class ApiKeyService implements ApiKeyServiceInterface
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var EntityManagerInterface
|
|
|
|
*/
|
|
|
|
private $em;
|
|
|
|
|
|
|
|
public function __construct(EntityManagerInterface $em)
|
|
|
|
{
|
|
|
|
$this->em = $em;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new ApiKey with provided expiration date
|
|
|
|
*
|
|
|
|
* @param \DateTime $expirationDate
|
|
|
|
* @return ApiKey
|
|
|
|
*/
|
2018-07-31 20:53:59 +03:00
|
|
|
public function create(\DateTime $expirationDate = null): ApiKey
|
2016-08-06 14:18:27 +03:00
|
|
|
{
|
|
|
|
$key = new ApiKey();
|
2018-05-03 14:21:43 +03:00
|
|
|
if ($expirationDate !== null) {
|
2016-08-06 14:18:27 +03:00
|
|
|
$key->setExpirationDate($expirationDate);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->em->persist($key);
|
|
|
|
$this->em->flush();
|
|
|
|
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if provided key is a valid api key
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-07-31 20:53:59 +03:00
|
|
|
public function check(string $key): bool
|
2016-08-06 14:18:27 +03:00
|
|
|
{
|
2017-12-27 18:23:54 +03:00
|
|
|
/** @var ApiKey|null $apiKey */
|
2016-08-07 20:13:40 +03:00
|
|
|
$apiKey = $this->getByKey($key);
|
2017-12-27 18:23:54 +03:00
|
|
|
return $apiKey !== null && $apiKey->isValid();
|
2016-08-06 14:18:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disables provided api key
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return ApiKey
|
2017-12-27 18:23:54 +03:00
|
|
|
* @throws InvalidArgumentException
|
2016-08-06 14:18:27 +03:00
|
|
|
*/
|
2018-07-31 20:53:59 +03:00
|
|
|
public function disable(string $key): ApiKey
|
2016-08-06 14:18:27 +03:00
|
|
|
{
|
2017-12-27 18:23:54 +03:00
|
|
|
/** @var ApiKey|null $apiKey */
|
2016-08-07 20:13:40 +03:00
|
|
|
$apiKey = $this->getByKey($key);
|
2017-12-27 18:23:54 +03:00
|
|
|
if ($apiKey === null) {
|
2016-08-06 14:18:27 +03:00
|
|
|
throw new InvalidArgumentException(sprintf('API key "%s" does not exist and can\'t be disabled', $key));
|
|
|
|
}
|
|
|
|
|
|
|
|
$apiKey->disable();
|
|
|
|
$this->em->flush();
|
|
|
|
return $apiKey;
|
|
|
|
}
|
2016-08-06 19:08:09 +03:00
|
|
|
|
|
|
|
/**
|
2017-12-27 18:23:54 +03:00
|
|
|
* Lists all existing api keys
|
2016-08-06 19:08:09 +03:00
|
|
|
*
|
|
|
|
* @param bool $enabledOnly Tells if only enabled keys should be returned
|
|
|
|
* @return ApiKey[]
|
|
|
|
*/
|
2018-07-31 20:53:59 +03:00
|
|
|
public function listKeys(bool $enabledOnly = false): array
|
2016-08-06 19:08:09 +03:00
|
|
|
{
|
|
|
|
$conditions = $enabledOnly ? ['enabled' => true] : [];
|
2018-07-31 20:53:59 +03:00
|
|
|
/** @var ApiKey[] $apiKeys */
|
|
|
|
$apiKeys = $this->em->getRepository(ApiKey::class)->findBy($conditions);
|
|
|
|
return $apiKeys;
|
2016-08-06 19:08:09 +03:00
|
|
|
}
|
2016-08-07 20:13:40 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tries to find one API key by its key string
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return ApiKey|null
|
|
|
|
*/
|
2018-07-31 20:53:59 +03:00
|
|
|
public function getByKey(string $key): ?ApiKey
|
2016-08-07 20:13:40 +03:00
|
|
|
{
|
2017-12-27 18:23:54 +03:00
|
|
|
/** @var ApiKey|null $apiKey */
|
|
|
|
$apiKey = $this->em->getRepository(ApiKey::class)->findOneBy([
|
2016-08-07 20:13:40 +03:00
|
|
|
'key' => $key,
|
|
|
|
]);
|
2017-12-27 18:23:54 +03:00
|
|
|
return $apiKey;
|
2016-08-07 20:13:40 +03:00
|
|
|
}
|
2016-08-06 14:18:27 +03:00
|
|
|
}
|