diff --git a/module/Core/src/Entity/RestToken.php b/module/Core/src/Entity/RestToken.php deleted file mode 100644 index 865c83b9..00000000 --- a/module/Core/src/Entity/RestToken.php +++ /dev/null @@ -1,103 +0,0 @@ -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()); - } -} diff --git a/module/Rest/config/dependencies.config.php b/module/Rest/config/dependencies.config.php index e0cc13f7..685de2c3 100644 --- a/module/Rest/config/dependencies.config.php +++ b/module/Rest/config/dependencies.config.php @@ -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, diff --git a/module/Rest/src/Service/RestTokenService.php b/module/Rest/src/Service/RestTokenService.php deleted file mode 100644 index b9dd4a9d..00000000 --- a/module/Rest/src/Service/RestTokenService.php +++ /dev/null @@ -1,98 +0,0 @@ -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(); - } -} diff --git a/module/Rest/src/Service/RestTokenServiceInterface.php b/module/Rest/src/Service/RestTokenServiceInterface.php deleted file mode 100644 index 1e03cbaa..00000000 --- a/module/Rest/src/Service/RestTokenServiceInterface.php +++ /dev/null @@ -1,32 +0,0 @@ -setExpirationDate((new \DateTime())->add(new \DateInterval('P1D'))); $request = ServerRequestFactory::fromGlobals()->withAttribute( RouteResult::class, RouteResult::fromRouteMatch('bar', 'foo', []) diff --git a/module/Rest/test/Service/RestTokenServiceTest.php b/module/Rest/test/Service/RestTokenServiceTest.php deleted file mode 100644 index d4487ff1..00000000 --- a/module/Rest/test/Service/RestTokenServiceTest.php +++ /dev/null @@ -1,93 +0,0 @@ -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()); - } -}