mirror of
https://github.com/shlinkio/shlink.git
synced 2024-12-23 07:33:58 -06:00
Deleted rest token related classes
This commit is contained in:
parent
7b0beb3b8c
commit
258f954a38
@ -1,103 +0,0 @@
|
||||
<?php
|
||||
namespace Shlinkio\Shlink\Core\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Shlinkio\Shlink\Common\Entity\AbstractEntity;
|
||||
use Shlinkio\Shlink\Common\Util\StringUtilsTrait;
|
||||
|
||||
/**
|
||||
* Class RestToken
|
||||
* @author
|
||||
* @link
|
||||
*
|
||||
* @ORM\Entity()
|
||||
* @ORM\Table(name="rest_tokens")
|
||||
*/
|
||||
class RestToken extends AbstractEntity
|
||||
{
|
||||
use StringUtilsTrait;
|
||||
|
||||
/**
|
||||
* The default interval is 20 minutes
|
||||
*/
|
||||
const DEFAULT_INTERVAL = 'PT20M';
|
||||
|
||||
/**
|
||||
* @var \DateTime
|
||||
* @ORM\Column(type="datetime", name="expiration_date", nullable=false)
|
||||
*/
|
||||
protected $expirationDate;
|
||||
/**
|
||||
* @var string
|
||||
* @ORM\Column(nullable=false)
|
||||
*/
|
||||
protected $token;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->updateExpiration();
|
||||
$this->setRandomTokenKey();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \DateTime
|
||||
*/
|
||||
public function getExpirationDate()
|
||||
{
|
||||
return $this->expirationDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \DateTime $expirationDate
|
||||
* @return $this
|
||||
*/
|
||||
public function setExpirationDate($expirationDate)
|
||||
{
|
||||
$this->expirationDate = $expirationDate;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getToken()
|
||||
{
|
||||
return $this->token;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $token
|
||||
* @return $this
|
||||
*/
|
||||
public function setToken($token)
|
||||
{
|
||||
$this->token = $token;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isExpired()
|
||||
{
|
||||
return new \DateTime() > $this->expirationDate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the expiration of the token, setting it to the default interval in the future
|
||||
* @return $this
|
||||
*/
|
||||
public function updateExpiration()
|
||||
{
|
||||
return $this->setExpirationDate((new \DateTime())->add(new \DateInterval(self::DEFAULT_INTERVAL)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a random unique token key for this RestToken
|
||||
* @return RestToken
|
||||
*/
|
||||
public function setRandomTokenKey()
|
||||
{
|
||||
return $this->setToken($this->generateV4Uuid());
|
||||
}
|
||||
}
|
@ -11,7 +11,6 @@ return [
|
||||
'dependencies' => [
|
||||
'factories' => [
|
||||
JWTService::class => AnnotatedFactory::class,
|
||||
Service\RestTokenService::class => AnnotatedFactory::class,
|
||||
Service\ApiKeyService::class => AnnotatedFactory::class,
|
||||
|
||||
Action\AuthenticateAction::class => AnnotatedFactory::class,
|
||||
|
@ -1,98 +0,0 @@
|
||||
<?php
|
||||
namespace Shlinkio\Shlink\Rest\Service;
|
||||
|
||||
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
|
||||
use Shlinkio\Shlink\Core\Entity\RestToken;
|
||||
use Shlinkio\Shlink\Rest\Exception\AuthenticationException;
|
||||
|
||||
class RestTokenService implements RestTokenServiceInterface
|
||||
{
|
||||
/**
|
||||
* @var EntityManagerInterface
|
||||
*/
|
||||
private $em;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $restConfig;
|
||||
|
||||
/**
|
||||
* ShortUrlService constructor.
|
||||
* @param EntityManagerInterface $em
|
||||
* @param array $restConfig
|
||||
*
|
||||
* @Inject({"em", "config.rest"})
|
||||
*/
|
||||
public function __construct(EntityManagerInterface $em, array $restConfig)
|
||||
{
|
||||
$this->em = $em;
|
||||
$this->restConfig = $restConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $token
|
||||
* @return RestToken
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function getByToken($token)
|
||||
{
|
||||
$restToken = $this->em->getRepository(RestToken::class)->findOneBy([
|
||||
'token' => $token,
|
||||
]);
|
||||
if (! isset($restToken)) {
|
||||
throw new InvalidArgumentException(sprintf('RestToken not found for token "%s"', $token));
|
||||
}
|
||||
|
||||
return $restToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and returns a new RestToken if username and password are correct
|
||||
* @param $username
|
||||
* @param $password
|
||||
* @return RestToken
|
||||
* @throws AuthenticationException
|
||||
*/
|
||||
public function createToken($username, $password)
|
||||
{
|
||||
$this->processCredentials($username, $password);
|
||||
|
||||
$restToken = new RestToken();
|
||||
$this->em->persist($restToken);
|
||||
$this->em->flush();
|
||||
|
||||
return $restToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
*/
|
||||
protected function processCredentials($username, $password)
|
||||
{
|
||||
$configUsername = strtolower(trim($this->restConfig['username']));
|
||||
$providedUsername = strtolower(trim($username));
|
||||
$configPassword = trim($this->restConfig['password']);
|
||||
$providedPassword = trim($password);
|
||||
|
||||
if ($configUsername === $providedUsername && $configPassword === $providedPassword) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If credentials are not correct, throw exception
|
||||
throw AuthenticationException::fromCredentials($providedUsername, $providedPassword);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the expiration of provided token, extending its life
|
||||
*
|
||||
* @param RestToken $token
|
||||
*/
|
||||
public function updateExpiration(RestToken $token)
|
||||
{
|
||||
$token->updateExpiration();
|
||||
$this->em->flush();
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
<?php
|
||||
namespace Shlinkio\Shlink\Rest\Service;
|
||||
|
||||
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
|
||||
use Shlinkio\Shlink\Core\Entity\RestToken;
|
||||
use Shlinkio\Shlink\Rest\Exception\AuthenticationException;
|
||||
|
||||
interface RestTokenServiceInterface
|
||||
{
|
||||
/**
|
||||
* @param string $token
|
||||
* @return RestToken
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function getByToken($token);
|
||||
|
||||
/**
|
||||
* Creates and returns a new RestToken if username and password are correct
|
||||
* @param $username
|
||||
* @param $password
|
||||
* @return RestToken
|
||||
* @throws AuthenticationException
|
||||
*/
|
||||
public function createToken($username, $password);
|
||||
|
||||
/**
|
||||
* Updates the expiration of provided token, extending its life
|
||||
*
|
||||
* @param RestToken $token
|
||||
*/
|
||||
public function updateExpiration(RestToken $token);
|
||||
}
|
@ -3,7 +3,6 @@ namespace ShlinkioTest\Shlink\Rest\Middleware;
|
||||
|
||||
use PHPUnit_Framework_TestCase as TestCase;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Shlinkio\Shlink\Core\Entity\RestToken;
|
||||
use Shlinkio\Shlink\Rest\Authentication\JWTService;
|
||||
use Shlinkio\Shlink\Rest\Middleware\CheckAuthenticationMiddleware;
|
||||
use Zend\Diactoros\Response;
|
||||
@ -148,7 +147,6 @@ class CheckAuthenticationMiddlewareTest extends TestCase
|
||||
public function provideCorrectTokenUpdatesExpirationAndFallbacksToNextMiddleware()
|
||||
{
|
||||
$authToken = 'ABC-abc';
|
||||
$restToken = (new RestToken())->setExpirationDate((new \DateTime())->add(new \DateInterval('P1D')));
|
||||
$request = ServerRequestFactory::fromGlobals()->withAttribute(
|
||||
RouteResult::class,
|
||||
RouteResult::fromRouteMatch('bar', 'foo', [])
|
||||
|
@ -1,93 +0,0 @@
|
||||
<?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\Core\Entity\RestToken;
|
||||
use Shlinkio\Shlink\Rest\Service\RestTokenService;
|
||||
|
||||
class RestTokenServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var RestTokenService
|
||||
*/
|
||||
protected $service;
|
||||
/**
|
||||
* @var ObjectProphecy
|
||||
*/
|
||||
protected $em;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->em = $this->prophesize(EntityManager::class);
|
||||
$this->service = new RestTokenService($this->em->reveal(), [
|
||||
'username' => 'foo',
|
||||
'password' => 'bar',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function tokenIsCreatedIfCredentialsAreCorrect()
|
||||
{
|
||||
$this->em->persist(Argument::type(RestToken::class))->shouldBeCalledTimes(1);
|
||||
$this->em->flush()->shouldBeCalledTimes(1);
|
||||
|
||||
$token = $this->service->createToken('foo', 'bar');
|
||||
$this->assertInstanceOf(RestToken::class, $token);
|
||||
$this->assertFalse($token->isExpired());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @expectedException \Shlinkio\Shlink\Rest\Exception\AuthenticationException
|
||||
*/
|
||||
public function exceptionIsThrownWhileCreatingTokenWithWrongCredentials()
|
||||
{
|
||||
$this->service->createToken('foo', 'wrong');
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function restTokenIsReturnedFromTokenString()
|
||||
{
|
||||
$authToken = 'ABC-abc';
|
||||
$theToken = new RestToken();
|
||||
$repo = $this->prophesize(EntityRepository::class);
|
||||
$repo->findOneBy(['token' => $authToken])->willReturn($theToken)->shouldBeCalledTimes(1);
|
||||
$this->em->getRepository(RestToken::class)->willReturn($repo->reveal())->shouldBeCalledTimes(1);
|
||||
|
||||
$this->assertSame($theToken, $this->service->getByToken($authToken));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
* @expectedException \Shlinkio\Shlink\Common\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function exceptionIsThrownWhenRequestingWrongToken()
|
||||
{
|
||||
$authToken = 'ABC-abc';
|
||||
$repo = $this->prophesize(EntityRepository::class);
|
||||
$repo->findOneBy(['token' => $authToken])->willReturn(null)->shouldBeCalledTimes(1);
|
||||
$this->em->getRepository(RestToken::class)->willReturn($repo->reveal())->shouldBeCalledTimes(1);
|
||||
|
||||
$this->service->getByToken($authToken);
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function updateExpirationFlushesEntityManager()
|
||||
{
|
||||
$token = $this->prophesize(RestToken::class);
|
||||
$token->updateExpiration()->shouldBeCalledTimes(1);
|
||||
$this->em->flush()->shouldBeCalledTimes(1);
|
||||
|
||||
$this->service->updateExpiration($token->reveal());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user