Skip to content

Commit

Permalink
Move request parsing from middleware into service
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusklocke committed Dec 19, 2023
1 parent e78f6e7 commit ce4f614
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 62 deletions.
8 changes: 7 additions & 1 deletion src/Infrastructure/API/ActionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@

interface ActionInterface
{
/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param array $args
* @return ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface;
}
}
1 change: 0 additions & 1 deletion src/Infrastructure/API/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public function __construct()

$this->add(new LoggingMiddleware($container));
$this->add(new TrailingSlash());
$this->add(new JsonParserMiddleware());
$this->add(new AuthenticationMiddleware($container));
$this->add(new MaintenanceModeMiddleware());

Expand Down
14 changes: 8 additions & 6 deletions src/Infrastructure/API/GraphQL/QueryAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,43 @@
use HexagonalPlayground\Application\TypeAssert;
use HexagonalPlayground\Infrastructure\API\ActionInterface;
use HexagonalPlayground\Infrastructure\API\JsonResponseWriter;
use HexagonalPlayground\Infrastructure\API\RequestParser;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;

class QueryAction implements ActionInterface
{
/** @var ContainerInterface */
private ContainerInterface $container;

/** @var JsonResponseWriter */
private JsonResponseWriter $responseWriter;
private RequestParser $requestParser;

/**
* @param ContainerInterface $container
* @param JsonResponseWriter $responseWriter
* @param RequestParser $requestParser
*/
public function __construct(ContainerInterface $container, JsonResponseWriter $responseWriter)
public function __construct(ContainerInterface $container, JsonResponseWriter $responseWriter, RequestParser $requestParser)
{
$this->container = $container;
$this->responseWriter = $responseWriter;
$this->requestParser = $requestParser;
}

/**
* @inheritDoc
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
{
$parsedBody = $request->getParsedBody();
$parsedBody = $this->requestParser->parseJson($request);
$query = $parsedBody['query'] ?? null;
$variables = $parsedBody['variables'] ?? [];

TypeAssert::assertString($query, 'query');
TypeAssert::assertArray($variables, 'variables');

$request = $request->withParsedBody($parsedBody);
$context = new AppContext($request, $this->container);
$errorHandler = new ErrorHandler($this->container->get(LoggerInterface::class), $request);
$schema = $this->container->get(Schema::class);
Expand All @@ -53,4 +55,4 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res

return $this->responseWriter->write($response, $result->toArray());
}
}
}
28 changes: 0 additions & 28 deletions src/Infrastructure/API/JsonParserMiddleware.php

This file was deleted.

29 changes: 29 additions & 0 deletions src/Infrastructure/API/RequestParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);

namespace HexagonalPlayground\Infrastructure\API;

use HexagonalPlayground\Domain\Exception\InvalidInputException;
use Psr\Http\Message\RequestInterface;
use Throwable;

class RequestParser
{
/**
* @param RequestInterface $request
* @return mixed
* @throws InvalidInputException
*/
public function parseJson(RequestInterface $request): mixed
{
if (!in_array('application/json', $request->getHeader('Content-Type'))) {
throw new InvalidInputException('Missing expected Content-Type header "application/json"');
}

try {
return json_decode((string)$request->getBody(), true, 64, JSON_THROW_ON_ERROR);
} catch (Throwable $throwable) {
throw new InvalidInputException('Failed to decode JSON from request body', 0, $throwable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use HexagonalPlayground\Application\TypeAssert;
use HexagonalPlayground\Infrastructure\API\ActionInterface;
use HexagonalPlayground\Infrastructure\API\JsonResponseWriter;
use HexagonalPlayground\Infrastructure\API\RequestParser;
use HexagonalPlayground\Infrastructure\API\Security\WebAuthn\FakeCredentialDescriptorFactory;
use HexagonalPlayground\Infrastructure\API\Security\WebAuthn\OptionsStoreInterface;
use HexagonalPlayground\Infrastructure\API\Security\WebAuthn\RequestOptionsFactory;
Expand All @@ -18,23 +19,13 @@

class GetLoginOptionsAction implements ActionInterface
{
/** @var PublicKeyCredentialSourceRepository */
private PublicKeyCredentialSourceRepository $credentialRepository;

/** @var RequestOptionsFactory */
private RequestOptionsFactory $requestOptionsFactory;

/** @var OptionsStoreInterface */
private OptionsStoreInterface $optionsStore;

/** @var FakeCredentialDescriptorFactory */
private FakeCredentialDescriptorFactory $fakeCredentialDescriptorFactory;

/** @var UserRepositoryInterface */
private UserRepositoryInterface $userRepository;

/** @var JsonResponseWriter */
private JsonResponseWriter $responseWriter;
private RequestParser $requestParser;

/**
* @param PublicKeyCredentialSourceRepository $credentialRepository
Expand All @@ -43,23 +34,25 @@ class GetLoginOptionsAction implements ActionInterface
* @param FakeCredentialDescriptorFactory $fakeCredentialDescriptorFactory
* @param UserRepositoryInterface $userRepository
* @param JsonResponseWriter $responseWriter
* @param RequestParser $requestParser
*/
public function __construct(PublicKeyCredentialSourceRepository $credentialRepository, RequestOptionsFactory $requestOptionsFactory, OptionsStoreInterface $optionsStore, FakeCredentialDescriptorFactory $fakeCredentialDescriptorFactory, UserRepositoryInterface $userRepository, JsonResponseWriter $responseWriter)
public function __construct(PublicKeyCredentialSourceRepository $credentialRepository, RequestOptionsFactory $requestOptionsFactory, OptionsStoreInterface $optionsStore, FakeCredentialDescriptorFactory $fakeCredentialDescriptorFactory, UserRepositoryInterface $userRepository, JsonResponseWriter $responseWriter, RequestParser $requestParser)
{
$this->credentialRepository = $credentialRepository;
$this->requestOptionsFactory = $requestOptionsFactory;
$this->optionsStore = $optionsStore;
$this->fakeCredentialDescriptorFactory = $fakeCredentialDescriptorFactory;
$this->userRepository = $userRepository;
$this->responseWriter = $responseWriter;
$this->requestParser = $requestParser;
}

/**
* @inheritDoc
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
{
$parsedBody = $request->getParsedBody();
$parsedBody = $this->requestParser->parseJson($request);
$email = $parsedBody['email'] ?? null;

/** @var string $email */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use HexagonalPlayground\Application\TypeAssert;
use HexagonalPlayground\Infrastructure\API\ActionInterface;
use HexagonalPlayground\Infrastructure\API\JsonResponseWriter;
use HexagonalPlayground\Infrastructure\API\RequestParser;
use HexagonalPlayground\Infrastructure\API\Security\WebAuthn\OptionsStoreInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -28,6 +29,7 @@ class PerformLoginAction implements ActionInterface
private UserRepositoryInterface $userRepository;
private TokenServiceInterface $tokenService;
private JsonResponseWriter $responseWriter;
private RequestParser $requestParser;

/**
* @param OptionsStoreInterface $optionsStore
Expand All @@ -36,23 +38,25 @@ class PerformLoginAction implements ActionInterface
* @param UserRepositoryInterface $userRepository
* @param TokenServiceInterface $tokenService
* @param JsonResponseWriter $responseWriter
* @param RequestParser $requestParser
*/
public function __construct(OptionsStoreInterface $optionsStore, PublicKeyCredentialLoader $credentialLoader, AuthenticatorAssertionResponseValidator $authenticatorAssertionResponseValidator, UserRepositoryInterface $userRepository, TokenServiceInterface $tokenService, JsonResponseWriter $responseWriter)
public function __construct(OptionsStoreInterface $optionsStore, PublicKeyCredentialLoader $credentialLoader, AuthenticatorAssertionResponseValidator $authenticatorAssertionResponseValidator, UserRepositoryInterface $userRepository, TokenServiceInterface $tokenService, JsonResponseWriter $responseWriter, RequestParser $requestParser)
{
$this->optionsStore = $optionsStore;
$this->credentialLoader = $credentialLoader;
$this->authenticatorAssertionResponseValidator = $authenticatorAssertionResponseValidator;
$this->userRepository = $userRepository;
$this->tokenService = $tokenService;
$this->responseWriter = $responseWriter;
$this->requestParser = $requestParser;
}

/**
* @inheritDoc
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
{
$parsedBody = $request->getParsedBody();
$parsedBody = $this->requestParser->parseJson($request);
$email = $parsedBody['email'] ?? null;

/** @var string $email */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use HexagonalPlayground\Domain\Exception\InvalidInputException;
use HexagonalPlayground\Application\TypeAssert;
use HexagonalPlayground\Infrastructure\API\ActionInterface;
use HexagonalPlayground\Infrastructure\API\RequestParser;
use HexagonalPlayground\Infrastructure\API\Security\AuthReader;
use HexagonalPlayground\Infrastructure\API\Security\WebAuthn\OptionsStoreInterface;
use HexagonalPlayground\Infrastructure\API\Security\WebAuthn\PublicKeyCredential;
Expand All @@ -19,43 +20,37 @@

class RegisterCredentialAction implements ActionInterface
{
/** @var PublicKeyCredentialSourceRepository */
private PublicKeyCredentialSourceRepository $credentialRepository;

/** @var PublicKeyCredentialLoader */
private PublicKeyCredentialLoader $credentialLoader;

/** @var AuthenticatorAttestationResponseValidator */
private AuthenticatorAttestationResponseValidator $authenticatorAttestationResponseValidator;

/** @var OptionsStoreInterface */
private OptionsStoreInterface $creationOptionsStore;

/** @var AuthReader */
private AuthReader $authReader;
private RequestParser $requestParser;

/**
* @param PublicKeyCredentialSourceRepository $credentialRepository
* @param PublicKeyCredentialLoader $credentialLoader
* @param AuthenticatorAttestationResponseValidator $authenticatorAttestationResponseValidator
* @param OptionsStoreInterface $creationOptionsStore
* @param AuthReader $authReader
* @param RequestParser $requestParser
*/
public function __construct(PublicKeyCredentialSourceRepository $credentialRepository, PublicKeyCredentialLoader $credentialLoader, AuthenticatorAttestationResponseValidator $authenticatorAttestationResponseValidator, OptionsStoreInterface $creationOptionsStore, AuthReader $authReader)
public function __construct(PublicKeyCredentialSourceRepository $credentialRepository, PublicKeyCredentialLoader $credentialLoader, AuthenticatorAttestationResponseValidator $authenticatorAttestationResponseValidator, OptionsStoreInterface $creationOptionsStore, AuthReader $authReader, RequestParser $requestParser)
{
$this->credentialRepository = $credentialRepository;
$this->credentialLoader = $credentialLoader;
$this->authenticatorAttestationResponseValidator = $authenticatorAttestationResponseValidator;
$this->creationOptionsStore = $creationOptionsStore;
$this->authReader = $authReader;
$this->requestParser = $requestParser;
}

/**
* @inheritDoc
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
{
$parsedBody = $request->getParsedBody();
$parsedBody = $this->requestParser->parseJson($request);
$name = $parsedBody['name'] ?? null;

/** @var string $name */
Expand Down

0 comments on commit ce4f614

Please sign in to comment.