Skip to content

Commit

Permalink
refactor: Adjustment due to the immutable times and authentication check
Browse files Browse the repository at this point in the history
  • Loading branch information
BibaltiK committed Jul 3, 2024
1 parent ec43d98 commit 18ed462
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 19 deletions.
15 changes: 15 additions & 0 deletions src/App/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Handler\Authentication\UserPasswordForgottonHandlerFactory;
use App\Hydrator\ClassMethodsHydratorFactory;
use App\Hydrator\DateTimeFormatterStrategyFactory;
use App\Hydrator\DateTimeImmutableFormatterStrategyFactory;
use App\Hydrator\NullableStrategyFactory;
use App\Hydrator\ReflectionHydrator;
use App\Middleware\Event\EventCreateMiddlewareFactory;
Expand Down Expand Up @@ -54,6 +55,7 @@
use Envms\FluentPDO\Query;
use Laminas\Hydrator\ClassMethodsHydrator;
use Laminas\Hydrator\Strategy\DateTimeFormatterStrategy;
use Laminas\Hydrator\Strategy\DateTimeImmutableFormatterStrategy;
use Laminas\Hydrator\Strategy\NullableStrategy;
use Laminas\ServiceManager\AbstractFactory\ConfigAbstractFactory;
use Ramsey\Uuid\Uuid;
Expand All @@ -73,7 +75,19 @@ public function getDependencies(): array
{
return [
'invokables' => [
LoginAuthenticationService::class,

EmailInput::class,
EventDescriptionInput::class,
EventDurationInput::class,
EventStartTimeInput::class,
EventTextInput::class,
EventTitleInput::class,
PasswordInput::class,
ReflectionHydrator::class,
TopicDescriptionInput::class,
TopicInput::class,
UsernameInput::class,
],
'aliases' => [
EventRepository::class => EventTable::class,
Expand All @@ -85,6 +99,7 @@ public function getDependencies(): array
'factories' => [
ClassMethodsHydrator::class => ClassMethodsHydratorFactory::class,
DateTimeFormatterStrategy::class => DateTimeFormatterStrategyFactory::class,
DateTimeImmutableFormatterStrategy::class => DateTimeImmutableFormatterStrategyFactory::class,
NullableStrategy::class => NullableStrategyFactory::class,

Handler\Authentication\LoginHandler::class => LoginHandlerFactory::class,
Expand Down
14 changes: 5 additions & 9 deletions src/App/Handler/Authentication/LoginHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,11 @@ public function handle(ServerRequestInterface $request): ResponseInterface
{
$user = $request->getAttribute(User::AUTHENTICATED_USER);

$token = null;

if ($user instanceof User) {
$token = $this->generateToken(
$user->uuid->getHex()->toString(),
$this->tokenSecret,
$this->tokenDuration,
);
}
$token = $this->generateToken(
$user->uuid->getHex()->toString(),
$this->tokenSecret,
$this->tokenDuration,
);

return new JsonResponse(new LoginTokenDto($token), HTTP::STATUS_OK);
}
Expand Down
15 changes: 15 additions & 0 deletions src/App/Hydrator/DateTimeImmutableFormatterStrategyFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);

namespace App\Hydrator;

use Laminas\Hydrator\Strategy\DateTimeFormatterStrategy;
use Laminas\Hydrator\Strategy\DateTimeImmutableFormatterStrategy;
use Psr\Container\ContainerInterface;

readonly class DateTimeImmutableFormatterStrategyFactory
{
public function __invoke(ContainerInterface $container): DateTimeImmutableFormatterStrategy
{
return new DateTimeImmutableFormatterStrategy(new DateTimeFormatterStrategy('Y-m-d H:i:s'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,12 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
{
$data = $request->getParsedBody();

$validationMessages = $request->getAttribute('validationMessages');

if (null !== $validationMessages) {
return $handler->handle($request);
}

$name = $data['username'];
$password = $data['password'];

$user = $this->userService->findByName($name);

if (!$this->authService->isUserDataCorrect($user, $password)) {
if (!($user instanceof User) || !$this->authService->isUserDataCorrect($user, $password)) {
return new JsonResponse(new SimpleMessageDto('Login failed'), HTTP::STATUS_UNAUTHORIZED);
}

Expand Down
5 changes: 3 additions & 2 deletions src/App/Service/User/UserServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\System\Hydrator\Strategy\UuidStrategy;
use Laminas\Hydrator\Strategy\BackedEnumStrategy;
use Laminas\Hydrator\Strategy\DateTimeFormatterStrategy;
use Laminas\Hydrator\Strategy\DateTimeImmutableFormatterStrategy;
use Psr\Container\ContainerInterface;
use Ramsey\Uuid\Uuid;

Expand All @@ -21,8 +22,8 @@ public function __invoke(ContainerInterface $container): UserService
/** @var ReflectionHydrator $hydrator */
$hydrator = clone $container->get(ReflectionHydrator::class);

/** @var DateTimeFormatterStrategy $dateTimeFormatterStrategy */
$dateTimeFormatterStrategy = $container->get(DateTimeFormatterStrategy::class);
/** @var DateTimeImmutableFormatterStrategy $dateTimeFormatterStrategy */
$dateTimeFormatterStrategy = $container->get(DateTimeImmutableFormatterStrategy::class);

/** @var Uuid $uuid */
$uuid = $container->get(Uuid::class);
Expand Down
33 changes: 33 additions & 0 deletions tests/Unit/App/Handler/Authentication/LoginHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types=1);

namespace Test\Unit\App\Handler\Authentication;

use App\Entity\User;
use App\Handler\Authentication\LoginHandler;
use Laminas\Diactoros\Response\JsonResponse;
use Test\Data\Entity\UserTestEntity;
use Test\Unit\App\Handler\AbstractHandler;

use function json_decode;

class LoginHandlerTest extends AbstractHandler
{
public function testUserIsLoggedIn(): void
{
$handler = new LoginHandler('abcd', 100);

$response = $handler->handle(
$this->request->withAttribute(User::AUTHENTICATED_USER, new User(...UserTestEntity::getDefaultUserValue()))
);

$responseData = $response->getBody()->getContents();

$responseDataAsArray = json_decode($responseData, true);

self::assertInstanceOf(JsonResponse::class, $response);
self::assertIsString($responseData);
self::assertJson($responseData);
self::assertIsArray($responseDataAsArray);
self::assertArrayHasKey('token', $responseDataAsArray);
}
}
3 changes: 3 additions & 0 deletions tests/Unit/App/Service/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Hydrator\ReflectionHydrator;
use App\System\Hydrator\Strategy\UuidStrategy;
use Laminas\Hydrator\Strategy\DateTimeFormatterStrategy;
use Laminas\Hydrator\Strategy\DateTimeImmutableFormatterStrategy;
use Laminas\Hydrator\Strategy\NullableStrategy;
use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Uuid;
Expand All @@ -14,6 +15,7 @@ abstract class AbstractService extends TestCase
{
protected ReflectionHydrator $hydrator;
protected DateTimeFormatterStrategy $dateTimeFormatterStrategy;
protected DateTimeImmutableFormatterStrategy $dateTimeImmutableFormatterStrategy;
protected NullableStrategy $nullableStrategy;
protected UuidStrategy $uuidStrategy;
protected UuidInterface $uuid;
Expand All @@ -22,6 +24,7 @@ protected function setUp(): void
{
$this->hydrator = new ReflectionHydrator();
$this->dateTimeFormatterStrategy = new DateTimeFormatterStrategy();
$this->dateTimeImmutableFormatterStrategy = new DateTimeImmutableFormatterStrategy(new DateTimeFormatterStrategy('Y-m-d H:i:s'));
$this->nullableStrategy = new NullableStrategy($this->dateTimeFormatterStrategy);
$this->uuidStrategy = new UuidStrategy();
$this->uuid = Uuid::uuid4();
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/App/Service/UserServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Service\User\UserService;
use App\Service\User\UserServiceFactory;
use Laminas\Hydrator\Strategy\DateTimeFormatterStrategy;
use Laminas\Hydrator\Strategy\DateTimeImmutableFormatterStrategy;
use Laminas\Hydrator\Strategy\NullableStrategy;
use Ramsey\Uuid\Uuid;
use Test\Unit\Mock\MockContainer;
Expand All @@ -20,7 +21,7 @@ public function testCanCreateUserService(): void
UserRepository::class => new MockUserTable(),
ReflectionHydrator::class => $this->hydrator,
NullableStrategy::class => $this->nullableStrategy,
DateTimeFormatterStrategy::class => $this->dateTimeFormatterStrategy,
DateTimeImmutableFormatterStrategy::class => $this->dateTimeFormatterStrategy,
Uuid::class => Uuid::uuid4(),
]);

Expand Down

0 comments on commit 18ed462

Please sign in to comment.