mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-23 21:27:44 +03:00
Created APiKeyService and tests
This commit is contained in:
parent
2767a14101
commit
7b746f76b0
5 changed files with 273 additions and 3 deletions
|
@ -44,10 +44,12 @@ class AuthenticateAction extends AbstractRestAction
|
|||
public function dispatch(Request $request, Response $response, callable $out = null)
|
||||
{
|
||||
$authData = $request->getParsedBody();
|
||||
if (! isset($authData['username'], $authData['password'])) {
|
||||
if (! isset($authData['apiKey'], $authData['username'], $authData['password'])) {
|
||||
return new JsonResponse([
|
||||
'error' => RestUtils::INVALID_ARGUMENT_ERROR,
|
||||
'message' => $this->translator->translate('You have to provide both "username" and "password"'),
|
||||
'message' => $this->translator->translate(
|
||||
'You have to provide a valid API key under the "apiKey" param name.'
|
||||
),
|
||||
], 400);
|
||||
}
|
||||
|
||||
|
|
|
@ -84,7 +84,7 @@ class ApiKey extends AbstractEntity
|
|||
return false;
|
||||
}
|
||||
|
||||
return $this->expirationDate >= new \DateTime();
|
||||
return $this->expirationDate < new \DateTime();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -105,6 +105,16 @@ class ApiKey extends AbstractEntity
|
|||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables this API key
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function disable()
|
||||
{
|
||||
return $this->setEnabled(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells if this api key is enabled and not expired
|
||||
*
|
||||
|
|
85
module/Rest/src/Service/ApiKeyService.php
Normal file
85
module/Rest/src/Service/ApiKeyService.php
Normal file
|
@ -0,0 +1,85 @@
|
|||
<?php
|
||||
namespace Shlinkio\Shlink\Rest\Service;
|
||||
|
||||
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
|
||||
class ApiKeyService implements ApiKeyServiceInterface
|
||||
{
|
||||
/**
|
||||
* @var EntityManagerInterface
|
||||
*/
|
||||
private $em;
|
||||
|
||||
/**
|
||||
* ApiKeyService constructor.
|
||||
* @param EntityManagerInterface $em
|
||||
*
|
||||
* @Inject({"em"})
|
||||
*/
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
{
|
||||
$this->em = $em;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new ApiKey with provided expiration date
|
||||
*
|
||||
* @param \DateTime $expirationDate
|
||||
* @return ApiKey
|
||||
*/
|
||||
public function create(\DateTime $expirationDate = null)
|
||||
{
|
||||
$key = new ApiKey();
|
||||
if (isset($expirationDate)) {
|
||||
$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
|
||||
*/
|
||||
public function check($key)
|
||||
{
|
||||
/** @var ApiKey $apiKey */
|
||||
$apiKey = $this->em->getRepository(ApiKey::class)->findOneBy([
|
||||
'key' => $key,
|
||||
]);
|
||||
if (! isset($apiKey)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $apiKey->isValid();
|
||||
}
|
||||
|
||||
/**
|
||||
* Disables provided api key
|
||||
*
|
||||
* @param string $key
|
||||
* @return ApiKey
|
||||
*/
|
||||
public function disable($key)
|
||||
{
|
||||
/** @var ApiKey $apiKey */
|
||||
$apiKey = $this->em->getRepository(ApiKey::class)->findOneBy([
|
||||
'key' => $key,
|
||||
]);
|
||||
if (! isset($apiKey)) {
|
||||
throw new InvalidArgumentException(sprintf('API key "%s" does not exist and can\'t be disabled', $key));
|
||||
}
|
||||
|
||||
$apiKey->disable();
|
||||
$this->em->flush();
|
||||
return $apiKey;
|
||||
}
|
||||
}
|
31
module/Rest/src/Service/ApiKeyServiceInterface.php
Normal file
31
module/Rest/src/Service/ApiKeyServiceInterface.php
Normal file
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
namespace Shlinkio\Shlink\Rest\Service;
|
||||
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
|
||||
interface ApiKeyServiceInterface
|
||||
{
|
||||
/**
|
||||
* Creates a new ApiKey with provided expiration date
|
||||
*
|
||||
* @param \DateTime $expirationDate
|
||||
* @return ApiKey
|
||||
*/
|
||||
public function create(\DateTime $expirationDate = null);
|
||||
|
||||
/**
|
||||
* Checks if provided key is a valid api key
|
||||
*
|
||||
* @param string $key
|
||||
* @return bool
|
||||
*/
|
||||
public function check($key);
|
||||
|
||||
/**
|
||||
* Disables provided api key
|
||||
*
|
||||
* @param string $key
|
||||
* @return ApiKey
|
||||
*/
|
||||
public function disable($key);
|
||||
}
|
142
module/Rest/test/Service/ApiKeyServiceTest.php
Normal file
142
module/Rest/test/Service/ApiKeyServiceTest.php
Normal file
|
@ -0,0 +1,142 @@
|
|||
<?php
|
||||
namespace ShlinkioTest\Shlink\Rest\Service;
|
||||
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use PHPUnit_Framework_TestCase as TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
use Shlinkio\Shlink\Rest\Service\ApiKeyService;
|
||||
|
||||
class ApiKeyServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var ApiKeyService
|
||||
*/
|
||||
protected $service;
|
||||
/**
|
||||
* @var ObjectProphecy
|
||||
*/
|
||||
protected $em;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->em = $this->prophesize(EntityManager::class);
|
||||
$this->service = new ApiKeyService($this->em->reveal());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function keyIsProperlyCreated()
|
||||
{
|
||||
$this->em->flush()->shouldBeCalledTimes(1);
|
||||
$this->em->persist(Argument::type(ApiKey::class))->shouldBeCalledTimes(1);
|
||||
|
||||
$key = $this->service->create();
|
||||
$this->assertNull($key->getExpirationDate());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function keyIsProperlyCreatedWithExpirationDate()
|
||||
{
|
||||
$this->em->flush()->shouldBeCalledTimes(1);
|
||||
$this->em->persist(Argument::type(ApiKey::class))->shouldBeCalledTimes(1);
|
||||
|
||||
$date = new \DateTime('2030-01-01');
|
||||
$key = $this->service->create($date);
|
||||
$this->assertSame($date, $key->getExpirationDate());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function checkReturnsFalseWhenKeyIsInvalid()
|
||||
{
|
||||
$repo = $this->prophesize(EntityRepository::class);
|
||||
$repo->findOneBy(['key' => '12345'])->willReturn(null)
|
||||
->shouldBeCalledTimes(1);
|
||||
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
|
||||
|
||||
$this->assertFalse($this->service->check('12345'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function checkReturnsFalseWhenKeyIsDisabled()
|
||||
{
|
||||
$key = new ApiKey();
|
||||
$key->disable();
|
||||
$repo = $this->prophesize(EntityRepository::class);
|
||||
$repo->findOneBy(['key' => '12345'])->willReturn($key)
|
||||
->shouldBeCalledTimes(1);
|
||||
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
|
||||
|
||||
$this->assertFalse($this->service->check('12345'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function checkReturnsFalseWhenKeyIsExpired()
|
||||
{
|
||||
$key = new ApiKey();
|
||||
$key->setExpirationDate((new \DateTime())->sub(new \DateInterval('P1D')));
|
||||
$repo = $this->prophesize(EntityRepository::class);
|
||||
$repo->findOneBy(['key' => '12345'])->willReturn($key)
|
||||
->shouldBeCalledTimes(1);
|
||||
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
|
||||
|
||||
$this->assertFalse($this->service->check('12345'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function checkReturnsTrueWhenConditionsAreFavorable()
|
||||
{
|
||||
$repo = $this->prophesize(EntityRepository::class);
|
||||
$repo->findOneBy(['key' => '12345'])->willReturn(new ApiKey())
|
||||
->shouldBeCalledTimes(1);
|
||||
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
|
||||
|
||||
$this->assertTrue($this->service->check('12345'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @expectedException \Shlinkio\Shlink\Common\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function disableThrowsExceptionWhenNoTokenIsFound()
|
||||
{
|
||||
$repo = $this->prophesize(EntityRepository::class);
|
||||
$repo->findOneBy(['key' => '12345'])->willReturn(null)
|
||||
->shouldBeCalledTimes(1);
|
||||
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
|
||||
|
||||
$this->service->disable('12345');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function disableReturnsDisabledKeyWhenFOund()
|
||||
{
|
||||
$key = new ApiKey();
|
||||
$repo = $this->prophesize(EntityRepository::class);
|
||||
$repo->findOneBy(['key' => '12345'])->willReturn($key)
|
||||
->shouldBeCalledTimes(1);
|
||||
$this->em->getRepository(ApiKey::class)->willReturn($repo->reveal());
|
||||
|
||||
$this->em->flush()->shouldBeCalledTimes(1);
|
||||
|
||||
$this->assertTrue($key->isEnabled());
|
||||
$returnedKey = $this->service->disable('12345');
|
||||
$this->assertFalse($key->isEnabled());
|
||||
$this->assertSame($key, $returnedKey);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue