Skip to content

Commit 20e4d8b

Browse files
committed
reAuth implemented
1 parent 82ad241 commit 20e4d8b

File tree

8 files changed

+89
-65
lines changed

8 files changed

+89
-65
lines changed

src/Tqdev/PhpCrudApi/Api.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Tqdev\PhpCrudApi;
34

45
use Psr\Http\Message\ResponseInterface;
@@ -24,6 +25,7 @@
2425
use Tqdev\PhpCrudApi\Middleware\IpAddressMiddleware;
2526
use Tqdev\PhpCrudApi\Middleware\JoinLimitsMiddleware;
2627
use Tqdev\PhpCrudApi\Middleware\JwtAuthMiddleware;
28+
use Tqdev\PhpCrudApi\Middleware\ReAuthMiddleware;
2729
use Tqdev\PhpCrudApi\Middleware\MultiTenancyMiddleware;
2830
use Tqdev\PhpCrudApi\Middleware\PageLimitsMiddleware;
2931
use Tqdev\PhpCrudApi\Middleware\Router\SimpleRouter;
@@ -73,6 +75,9 @@ public function __construct(Config $config)
7375
case 'dbAuth':
7476
new DbAuthMiddleware($router, $responder, $properties, $reflection, $db);
7577
break;
78+
case 'reAuth':
79+
new ReAuthMiddleware($router, $responder, $properties, $reflection, $db);
80+
break;
7681
case 'validation':
7782
new ValidationMiddleware($router, $responder, $properties, $reflection);
7883
break;

src/Tqdev/PhpCrudApi/Column/ReflectionService.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Tqdev\PhpCrudApi\Column;
34

45
use Tqdev\PhpCrudApi\Cache\Cache;
@@ -19,10 +20,18 @@ public function __construct(GenericDB $db, Cache $cache, int $ttl)
1920
$this->db = $db;
2021
$this->cache = $cache;
2122
$this->ttl = $ttl;
22-
$this->database = $this->loadDatabase(true);
23+
$this->database = null;
2324
$this->tables = [];
2425
}
2526

27+
private function database(): ReflectedDatabase
28+
{
29+
if (!$this->database) {
30+
$this->database = $this->loadDatabase(true);
31+
}
32+
return $this->database;
33+
}
34+
2635
private function loadDatabase(bool $useCache): ReflectedDatabase
2736
{
2837
$data = $useCache ? $this->cache->get('ReflectedDatabase') : '';
@@ -42,7 +51,7 @@ private function loadTable(string $tableName, bool $useCache): ReflectedTable
4251
if ($data != '') {
4352
$table = ReflectedTable::fromJson(json_decode(gzuncompress($data)));
4453
} else {
45-
$tableType = $this->database->getType($tableName);
54+
$tableType = $this->database()->getType($tableName);
4655
$table = ReflectedTable::fromReflection($this->db->reflection(), $tableName, $tableType);
4756
$data = gzcompress(json_encode($table, JSON_UNESCAPED_UNICODE));
4857
$this->cache->set("ReflectedTable($tableName)", $data, $this->ttl);
@@ -62,12 +71,12 @@ public function refreshTable(string $tableName)
6271

6372
public function hasTable(string $tableName): bool
6473
{
65-
return $this->database->hasTable($tableName);
74+
return $this->database()->hasTable($tableName);
6675
}
6776

6877
public function getType(string $tableName): string
6978
{
70-
return $this->database->getType($tableName);
79+
return $this->database()->getType($tableName);
7180
}
7281

7382
public function getTable(string $tableName): ReflectedTable
@@ -80,18 +89,17 @@ public function getTable(string $tableName): ReflectedTable
8089

8190
public function getTableNames(): array
8291
{
83-
return $this->database->getTableNames();
92+
return $this->database()->getTableNames();
8493
}
8594

8695
public function getDatabaseName(): string
8796
{
88-
return $this->database->getName();
97+
return $this->database()->getName();
8998
}
9099

91100
public function removeTable(string $tableName): bool
92101
{
93102
unset($this->tables[$tableName]);
94-
return $this->database->removeTable($tableName);
103+
return $this->database()->removeTable($tableName);
95104
}
96-
97105
}

src/Tqdev/PhpCrudApi/Database/GenericDB.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Tqdev\PhpCrudApi\Database;
34

45
use Tqdev\PhpCrudApi\Column\Reflection\ReflectedTable;
@@ -20,25 +21,30 @@ class GenericDB
2021
private function getDsn(string $address, int $port, string $database): string
2122
{
2223
switch ($this->driver) {
23-
case 'mysql':return "$this->driver:host=$address;port=$port;dbname=$database;charset=utf8mb4";
24-
case 'pgsql':return "$this->driver:host=$address port=$port dbname=$database options='--client_encoding=UTF8'";
25-
case 'sqlsrv':return "$this->driver:Server=$address,$port;Database=$database";
24+
case 'mysql':
25+
return "$this->driver:host=$address;port=$port;dbname=$database;charset=utf8mb4";
26+
case 'pgsql':
27+
return "$this->driver:host=$address port=$port dbname=$database options='--client_encoding=UTF8'";
28+
case 'sqlsrv':
29+
return "$this->driver:Server=$address,$port;Database=$database";
2630
}
2731
}
2832

2933
private function getCommands(): array
3034
{
3135
switch ($this->driver) {
32-
case 'mysql':return [
36+
case 'mysql':
37+
return [
3338
'SET SESSION sql_warnings=1;',
3439
'SET NAMES utf8mb4;',
3540
'SET SESSION sql_mode = "ANSI,TRADITIONAL";',
3641
];
37-
case 'pgsql':return [
42+
case 'pgsql':
43+
return [
3844
"SET NAMES 'UTF8';",
3945
];
40-
case 'sqlsrv':return [
41-
];
46+
case 'sqlsrv':
47+
return [];
4248
}
4349
}
4450

@@ -49,16 +55,19 @@ private function getOptions(): array
4955
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
5056
);
5157
switch ($this->driver) {
52-
case 'mysql':return $options + [
58+
case 'mysql':
59+
return $options + [
5360
\PDO::ATTR_EMULATE_PREPARES => false,
5461
\PDO::MYSQL_ATTR_FOUND_ROWS => true,
5562
\PDO::ATTR_PERSISTENT => true,
5663
];
57-
case 'pgsql':return $options + [
64+
case 'pgsql':
65+
return $options + [
5866
\PDO::ATTR_EMULATE_PREPARES => false,
5967
\PDO::ATTR_PERSISTENT => true,
6068
];
61-
case 'sqlsrv':return $options + [
69+
case 'sqlsrv':
70+
return $options + [
6271
\PDO::SQLSRV_ATTR_DIRECT_QUERY => false,
6372
\PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE => true,
6473
];
@@ -74,7 +83,7 @@ public function __construct(string $driver, string $address, int $port, string $
7483
$this->pdo = new LazyPdo($dsn, $username, $password, $options);
7584
$commands = $this->getCommands();
7685
foreach ($commands as $command) {
77-
$this->pdo->query($command);
86+
$this->pdo->addInitCommand($command);
7887
}
7988
$this->reflection = new GenericReflection($this->pdo, $driver, $database);
8089
$this->definition = new GenericDefinition($this->pdo, $driver, $database);

src/Tqdev/PhpCrudApi/Database/LazyPdo.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
<?php
2+
23
namespace Tqdev\PhpCrudApi\Database;
34

45
class LazyPdo extends \PDO
56
{
67
private $dsn;
78
private $user;
89
private $password;
9-
private $options = array();
10+
private $options;
11+
private $commands;
1012

1113
private $pdo = null;
1214

@@ -16,28 +18,37 @@ public function __construct(string $dsn, /*?string*/ $user = null, /*?string*/ $
1618
$this->user = $user;
1719
$this->password = $password;
1820
$this->options = $options;
21+
$this->commands = array();
1922
// explicitly NOT calling super::__construct
2023
}
2124

25+
public function addInitCommand(string $command)/*: void*/
26+
{
27+
$this->commands[] = $command;
28+
}
29+
2230
private function pdo()
2331
{
2432
if (!$this->pdo) {
2533
$this->pdo = new \PDO($this->dsn, $this->user, $this->password, $this->options);
34+
foreach ($this->commands as $command) {
35+
$this->pdo->query($command);
36+
}
2637
}
2738
return $this->pdo;
2839
}
2940

30-
public function reauthenticate(/*?string*/ $user, /*?string*/ $password): bool
41+
public function reauthenticate(/*?string*/$user, /*?string*/ $password): bool
3142
{
3243
$this->user = $user;
3344
$this->password = $password;
3445
if ($this->pdo) {
35-
$this->pdo = new \PDO($this->dsn, $this->user, $this->password, $this->options);
36-
return false;
46+
$this->pdo = null;
47+
return false;
3748
}
3849
return true;
3950
}
40-
51+
4152
public function inTransaction(): bool
4253
{
4354
// Do not call parent method if there is no pdo object
@@ -46,7 +57,7 @@ public function inTransaction(): bool
4657

4758
public function setAttribute($attribute, $value): bool
4859
{
49-
if ($this->pdo) {
60+
if ($this->pdo) {
5061
return $this->pdo()->setAttribute($attribute, $value);
5162
}
5263
$this->options[$attribute] = $value;
@@ -107,4 +118,4 @@ public function query(string $statement): \PDOStatement
107118
{
108119
return call_user_func_array(array($this->pdo(), 'query'), func_get_args());
109120
}
110-
}
121+
}

src/Tqdev/PhpCrudApi/Middleware/DbAuthMiddleware.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Tqdev\PhpCrudApi\Middleware;
34

45
use Psr\Http\Message\ResponseInterface;

src/Tqdev/PhpCrudApi/Middleware/ReauthMiddleware.php

Lines changed: 0 additions & 34 deletions
This file was deleted.

test.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
use Tqdev\PhpCrudApi\Api;
34
use Tqdev\PhpCrudApi\Config;
45
use Tqdev\PhpCrudApi\Database\GenericDB;
@@ -76,6 +77,23 @@ function runTest(Config $config, string $file, string $category): int
7677
return $success;
7778
}
7879

80+
function getUsername(Config $config)
81+
{
82+
if (!isset($config->getMiddlewares()['reAuth']['usernameHandler'])) {
83+
return $config->getUsername();
84+
}
85+
return $config->getMiddlewares()['reAuth']['usernameHandler']();
86+
}
87+
88+
function getPassword(Config $config)
89+
{
90+
if (!isset($config->getMiddlewares()['reAuth']['passwordHandler'])) {
91+
return $config->getPassword();
92+
}
93+
return $config->getMiddlewares()['reAuth']['passwordHandler']();
94+
}
95+
96+
7997
function loadFixture(string $dir, Config $config)
8098
{
8199
$driver = $config->getDriver();
@@ -86,8 +104,8 @@ function loadFixture(string $dir, Config $config)
86104
$config->getAddress(),
87105
$config->getPort(),
88106
$config->getDatabase(),
89-
$config->getUsername(),
90-
$config->getPassword()
107+
getUsername($config),
108+
getPassword($config)
91109
);
92110
$pdo = $db->pdo();
93111
$file = preg_replace('/--.*$/m', '', $file);

tests/config/base.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
<?php
22
$settings = [
33
'database' => 'php-crud-api',
4-
'username' => 'php-crud-api',
5-
'password' => 'php-crud-api',
4+
'username' => 'incorrect_username',
5+
'password' => 'incorrect_password',
66
'controllers' => 'records,columns,cache,openapi,geojson',
7-
'middlewares' => 'cors,dbAuth,jwtAuth,basicAuth,authorization,validation,ipAddress,sanitation,multiTenancy,pageLimits,joinLimits,customization',
7+
'middlewares' => 'cors,reAuth,dbAuth,jwtAuth,basicAuth,authorization,validation,ipAddress,sanitation,multiTenancy,pageLimits,joinLimits,customization',
88
'dbAuth.mode' => 'optional',
99
'dbAuth.returnedColumns' => 'id,username,password',
1010
'jwtAuth.mode' => 'optional',
1111
'jwtAuth.time' => '1538207605',
1212
'jwtAuth.secret' => 'axpIrCGNGqxzx2R9dtXLIPUSqPo778uhb8CA0F4Hx',
1313
'basicAuth.mode' => 'optional',
1414
'basicAuth.passwordFile' => __DIR__ . DIRECTORY_SEPARATOR . '.htpasswd',
15+
'reAuth.usernameHandler' => function () {
16+
return 'php-crud-api';
17+
},
18+
'reAuth.passwordHandler' => function () {
19+
return 'php-crud-api';
20+
},
1521
'authorization.tableHandler' => function ($operation, $tableName) {
1622
return !($tableName == 'invisibles' && !isset($_SESSION['claims']['name']) && empty($_SESSION['username']) && empty($_SESSION['user']));
1723
},

0 commit comments

Comments
 (0)