Skip to content

Commit 220a7d7

Browse files
authored
Allow authorization handler to disable openapi (#712)
Adds a new authorization middleware config option: 'authorization.allowOpenApiAccessHandler' => function () { return false; }, Returning false from this operation will disable the OpenAPI endpoint Fixes #672
1 parent 2122cf5 commit 220a7d7

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ You can tune the middleware behavior using middleware specific configuration par
665665
- "reconnect.passwordHandler": Handler to implement retrieval of the database password ("")
666666
- "authorization.tableHandler": Handler to implement table authorization rules ("")
667667
- "authorization.columnHandler": Handler to implement column authorization rules ("")
668+
- "authorization.pathHandler": Handler to implement path authorization rules ("")
668669
- "authorization.recordHandler": Handler to implement record authorization filter rules ("")
669670
- "validation.handler": Handler to implement validation rules for input values ("")
670671
- "validation.types": Types to enable type validation for, empty means 'none' ("all")
@@ -852,7 +853,7 @@ Add the "columns" controller in the configuration to enable this functionality.
852853

853854
### Authorizing tables, columns and records
854855

855-
By default all tables and columns are accessible. If you want to restrict access to some tables you may add the 'authorization' middleware
856+
By default all tables, columns and paths are accessible. If you want to restrict access to some tables you may add the 'authorization' middleware
856857
and define a 'authorization.tableHandler' function that returns 'false' for these tables.
857858

858859
'authorization.tableHandler' => function ($operation, $tableName) {
@@ -874,6 +875,12 @@ The above example will restrict access to the 'password' field of the 'users' ta
874875
The above example will disallow access to user records where the username is 'admin'.
875876
This construct adds a filter to every executed query.
876877

878+
'authorization.pathHandler' => function ($path) {
879+
return $path === 'openapi' ? false : true;
880+
},
881+
882+
The above example will disabled the `/openapi` route.
883+
877884
NB: You need to handle the creation of invalid records with a validation (or sanitation) handler.
878885

879886
### SQL GRANT authorization

api.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7164,6 +7164,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
71647164
use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
71657165
use Tqdev\PhpCrudApi\Middleware\Communication\VariableStore;
71667166
use Tqdev\PhpCrudApi\Middleware\Router\Router;
7167+
use Tqdev\PhpCrudApi\Record\ErrorCode;
71677168
use Tqdev\PhpCrudApi\Record\FilterInfo;
71687169
use Tqdev\PhpCrudApi\RequestUtils;
71697170

@@ -7225,9 +7226,20 @@ private function handleRecords(string $operation, string $tableName) /*: void*/
72257226
}
72267227
}
72277228

7229+
private function pathHandler(string $path) /*: bool*/
7230+
{
7231+
$pathHandler = $this->getProperty('pathHandler', '');
7232+
return $pathHandler ? call_user_func($pathHandler, $path) : true;
7233+
}
7234+
72287235
public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
72297236
{
72307237
$path = RequestUtils::getPathSegment($request, 1);
7238+
7239+
if (!$this->pathHandler($path)) {
7240+
return $this->responder->error(ErrorCode::ROUTE_NOT_FOUND, $request->getUri()->getPath());
7241+
}
7242+
72317243
$operation = RequestUtils::getOperation($request);
72327244
$tableNames = RequestUtils::getTableNames($request, $this->reflection);
72337245
foreach ($tableNames as $tableName) {

src/Tqdev/PhpCrudApi/Middleware/AuthorizationMiddleware.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
1111
use Tqdev\PhpCrudApi\Middleware\Communication\VariableStore;
1212
use Tqdev\PhpCrudApi\Middleware\Router\Router;
13+
use Tqdev\PhpCrudApi\Record\ErrorCode;
1314
use Tqdev\PhpCrudApi\Record\FilterInfo;
1415
use Tqdev\PhpCrudApi\RequestUtils;
1516

@@ -71,9 +72,20 @@ private function handleRecords(string $operation, string $tableName) /*: void*/
7172
}
7273
}
7374

75+
private function pathHandler(string $path) /*: bool*/
76+
{
77+
$pathHandler = $this->getProperty('pathHandler', '');
78+
return $pathHandler ? call_user_func($pathHandler, $path) : true;
79+
}
80+
7481
public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
7582
{
7683
$path = RequestUtils::getPathSegment($request, 1);
84+
85+
if (!$this->pathHandler($path)) {
86+
return $this->responder->error(ErrorCode::ROUTE_NOT_FOUND, $request->getUri()->getPath());
87+
}
88+
7789
$operation = RequestUtils::getOperation($request);
7890
$tableNames = RequestUtils::getTableNames($request, $this->reflection);
7991
foreach ($tableNames as $tableName) {

0 commit comments

Comments
 (0)