Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 28 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| OpenAPISchemaValidateRequestMiddleware | |
0.00% |
0 / 28 |
|
0.00% |
0 / 4 |
110 | |
0.00% |
0 / 1 |
| process | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| addSchema | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| removeSchema | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
42 | |||
| getApplicableSchemas | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2021 (original work) Open Assessment Technologies SA |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\Middleware; |
| 24 | |
| 25 | use League\OpenAPIValidation\PSR7\ValidatorBuilder; |
| 26 | use oat\oatbox\service\ConfigurableService; |
| 27 | use oat\tao\model\Context\ContextInterface; |
| 28 | use oat\tao\model\Middleware\Context\OpenApiMiddlewareContext; |
| 29 | use Psr\Http\Message\RequestInterface; |
| 30 | use Psr\Http\Message\ResponseInterface; |
| 31 | use Psr\Http\Message\ServerRequestInterface; |
| 32 | use Psr\Http\Server\MiddlewareInterface; |
| 33 | use Psr\Http\Server\RequestHandlerInterface; |
| 34 | |
| 35 | class OpenAPISchemaValidateRequestMiddleware extends ConfigurableService implements MiddlewareInterface |
| 36 | { |
| 37 | public const OPTION_SCHEMA_MAP = 'schema_map'; |
| 38 | public const SERVICE_ID = 'tao/OpenAPISchemaValidateRequestMiddleware'; |
| 39 | |
| 40 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
| 41 | { |
| 42 | foreach ($this->getApplicableSchemas($request) as $schema) { |
| 43 | $validator = (new ValidatorBuilder())->fromYamlFile($schema)->getServerRequestValidator(); |
| 44 | $validator->validate($request); |
| 45 | } |
| 46 | |
| 47 | return $handler->handle($request); |
| 48 | } |
| 49 | |
| 50 | public function addSchema(ContextInterface $context): self |
| 51 | { |
| 52 | $map = array_merge_recursive( |
| 53 | $this->getOption(self::OPTION_SCHEMA_MAP, []), |
| 54 | [ |
| 55 | $context->getParameter(OpenApiMiddlewareContext::PARAM_ROUTE) => [ |
| 56 | $context->getParameter(OpenApiMiddlewareContext::PARAM_SCHEMA_PATH) |
| 57 | ] |
| 58 | ] |
| 59 | ); |
| 60 | |
| 61 | $this->setOption(self::OPTION_SCHEMA_MAP, $map); |
| 62 | |
| 63 | return $this; |
| 64 | } |
| 65 | |
| 66 | public function removeSchema(ContextInterface $context): self |
| 67 | { |
| 68 | $map = $this->getOption(self::OPTION_SCHEMA_MAP); |
| 69 | |
| 70 | $path = $context->getParameter(OpenApiMiddlewareContext::PARAM_SCHEMA_PATH); |
| 71 | $route = $context->getParameter(OpenApiMiddlewareContext::PARAM_ROUTE); |
| 72 | |
| 73 | if ($route && !$path) { |
| 74 | unset($map[$route]); |
| 75 | } |
| 76 | if ($path && $route) { |
| 77 | $routed = $map[$route]; |
| 78 | $key = array_search($path, $routed); |
| 79 | |
| 80 | if ($key !== false) { |
| 81 | unset($routed[$key]); |
| 82 | $map[$route] = $routed; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | $this->setOption(OpenAPISchemaValidateRequestMiddleware::OPTION_SCHEMA_MAP, array_filter($map)); |
| 87 | |
| 88 | return $this; |
| 89 | } |
| 90 | |
| 91 | private function getApplicableSchemas(RequestInterface $request): array |
| 92 | { |
| 93 | return $this->getOption(self::OPTION_SCHEMA_MAP, [])[$request->getUri()->getPath()] ?? []; |
| 94 | } |
| 95 | } |