Browse Source

reAuth implemented

Maurits van der Schee 5 years ago
parent
commit
e6907e06bc
1 changed files with 110 additions and 34 deletions
  1. 110
    34
      api.php

+ 110
- 34
api.php View File

@@ -4102,10 +4102,18 @@ namespace Tqdev\PhpCrudApi\Column {
4102 4102
             $this->db = $db;
4103 4103
             $this->cache = $cache;
4104 4104
             $this->ttl = $ttl;
4105
-            $this->database = $this->loadDatabase(true);
4105
+            $this->database = null;
4106 4106
             $this->tables = [];
4107 4107
         }
4108 4108
 
4109
+        private function database(): ReflectedDatabase
4110
+        {
4111
+            if (!$this->database) {
4112
+                $this->database = $this->loadDatabase(true);
4113
+            }
4114
+            return $this->database;
4115
+        }
4116
+
4109 4117
         private function loadDatabase(bool $useCache): ReflectedDatabase
4110 4118
         {
4111 4119
             $data = $useCache ? $this->cache->get('ReflectedDatabase') : '';
@@ -4125,7 +4133,7 @@ namespace Tqdev\PhpCrudApi\Column {
4125 4133
             if ($data != '') {
4126 4134
                 $table = ReflectedTable::fromJson(json_decode(gzuncompress($data)));
4127 4135
             } else {
4128
-                $tableType = $this->database->getType($tableName);
4136
+                $tableType = $this->database()->getType($tableName);
4129 4137
                 $table = ReflectedTable::fromReflection($this->db->reflection(), $tableName, $tableType);
4130 4138
                 $data = gzcompress(json_encode($table, JSON_UNESCAPED_UNICODE));
4131 4139
                 $this->cache->set("ReflectedTable($tableName)", $data, $this->ttl);
@@ -4145,12 +4153,12 @@ namespace Tqdev\PhpCrudApi\Column {
4145 4153
 
4146 4154
         public function hasTable(string $tableName): bool
4147 4155
         {
4148
-            return $this->database->hasTable($tableName);
4156
+            return $this->database()->hasTable($tableName);
4149 4157
         }
4150 4158
 
4151 4159
         public function getType(string $tableName): string
4152 4160
         {
4153
-            return $this->database->getType($tableName);
4161
+            return $this->database()->getType($tableName);
4154 4162
         }
4155 4163
 
4156 4164
         public function getTable(string $tableName): ReflectedTable
@@ -4163,20 +4171,19 @@ namespace Tqdev\PhpCrudApi\Column {
4163 4171
 
4164 4172
         public function getTableNames(): array
4165 4173
         {
4166
-            return $this->database->getTableNames();
4174
+            return $this->database()->getTableNames();
4167 4175
         }
4168 4176
 
4169 4177
         public function getDatabaseName(): string
4170 4178
         {
4171
-            return $this->database->getName();
4179
+            return $this->database()->getName();
4172 4180
         }
4173 4181
 
4174 4182
         public function removeTable(string $tableName): bool
4175 4183
         {
4176 4184
             unset($this->tables[$tableName]);
4177
-            return $this->database->removeTable($tableName);
4185
+            return $this->database()->removeTable($tableName);
4178 4186
         }
4179
-
4180 4187
     }
4181 4188
 }
4182 4189
 
@@ -5175,25 +5182,30 @@ namespace Tqdev\PhpCrudApi\Database {
5175 5182
         private function getDsn(string $address, int $port, string $database): string
5176 5183
         {
5177 5184
             switch ($this->driver) {
5178
-                case 'mysql':return "$this->driver:host=$address;port=$port;dbname=$database;charset=utf8mb4";
5179
-                case 'pgsql':return "$this->driver:host=$address port=$port dbname=$database options='--client_encoding=UTF8'";
5180
-                case 'sqlsrv':return "$this->driver:Server=$address,$port;Database=$database";
5185
+                case 'mysql':
5186
+                    return "$this->driver:host=$address;port=$port;dbname=$database;charset=utf8mb4";
5187
+                case 'pgsql':
5188
+                    return "$this->driver:host=$address port=$port dbname=$database options='--client_encoding=UTF8'";
5189
+                case 'sqlsrv':
5190
+                    return "$this->driver:Server=$address,$port;Database=$database";
5181 5191
             }
5182 5192
         }
5183 5193
 
5184 5194
         private function getCommands(): array
5185 5195
         {
5186 5196
             switch ($this->driver) {
5187
-                case 'mysql':return [
5197
+                case 'mysql':
5198
+                    return [
5188 5199
                         'SET SESSION sql_warnings=1;',
5189 5200
                         'SET NAMES utf8mb4;',
5190 5201
                         'SET SESSION sql_mode = "ANSI,TRADITIONAL";',
5191 5202
                     ];
5192
-                case 'pgsql':return [
5203
+                case 'pgsql':
5204
+                    return [
5193 5205
                         "SET NAMES 'UTF8';",
5194 5206
                     ];
5195
-                case 'sqlsrv':return [
5196
-                    ];
5207
+                case 'sqlsrv':
5208
+                    return [];
5197 5209
             }
5198 5210
         }
5199 5211
 
@@ -5204,16 +5216,19 @@ namespace Tqdev\PhpCrudApi\Database {
5204 5216
                 \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
5205 5217
             );
5206 5218
             switch ($this->driver) {
5207
-                case 'mysql':return $options + [
5219
+                case 'mysql':
5220
+                    return $options + [
5208 5221
                         \PDO::ATTR_EMULATE_PREPARES => false,
5209 5222
                         \PDO::MYSQL_ATTR_FOUND_ROWS => true,
5210 5223
                         \PDO::ATTR_PERSISTENT => true,
5211 5224
                     ];
5212
-                case 'pgsql':return $options + [
5225
+                case 'pgsql':
5226
+                    return $options + [
5213 5227
                         \PDO::ATTR_EMULATE_PREPARES => false,
5214 5228
                         \PDO::ATTR_PERSISTENT => true,
5215 5229
                     ];
5216
-                case 'sqlsrv':return $options + [
5230
+                case 'sqlsrv':
5231
+                    return $options + [
5217 5232
                         \PDO::SQLSRV_ATTR_DIRECT_QUERY => false,
5218 5233
                         \PDO::SQLSRV_ATTR_FETCHES_NUMERIC_TYPE => true,
5219 5234
                     ];
@@ -5229,7 +5244,7 @@ namespace Tqdev\PhpCrudApi\Database {
5229 5244
             $this->pdo = new LazyPdo($dsn, $username, $password, $options);
5230 5245
             $commands = $this->getCommands();
5231 5246
             foreach ($commands as $command) {
5232
-                $this->pdo->query($command);
5247
+                $this->pdo->addInitCommand($command);
5233 5248
             }
5234 5249
             $this->reflection = new GenericReflection($this->pdo, $driver, $database);
5235 5250
             $this->definition = new GenericDefinition($this->pdo, $driver, $database);
@@ -5238,7 +5253,7 @@ namespace Tqdev\PhpCrudApi\Database {
5238 5253
             $this->converter = new DataConverter($driver);
5239 5254
         }
5240 5255
 
5241
-        public function pdo(): \PDO
5256
+        public function pdo(): LazyPdo
5242 5257
         {
5243 5258
             return $this->pdo;
5244 5259
         }
@@ -5990,7 +6005,8 @@ namespace Tqdev\PhpCrudApi\Database {
5990 6005
         private $dsn;
5991 6006
         private $user;
5992 6007
         private $password;
5993
-        private $options = array();
6008
+        private $options;
6009
+        private $commands;
5994 6010
 
5995 6011
         private $pdo = null;
5996 6012
 
@@ -6000,35 +6016,37 @@ namespace Tqdev\PhpCrudApi\Database {
6000 6016
             $this->user = $user;
6001 6017
             $this->password = $password;
6002 6018
             $this->options = $options;
6019
+            $this->commands = array();
6003 6020
             // explicitly NOT calling super::__construct
6004 6021
         }
6005 6022
 
6023
+        public function addInitCommand(string $command)/*: void*/
6024
+        {
6025
+            $this->commands[] = $command;
6026
+        }
6027
+
6006 6028
         private function pdo()
6007 6029
         {
6008 6030
             if (!$this->pdo) {
6009 6031
                 $this->pdo = new \PDO($this->dsn, $this->user, $this->password, $this->options);
6032
+                foreach ($this->commands as $command) {
6033
+                    $this->pdo->query($command);
6034
+                }
6010 6035
             }
6011 6036
             return $this->pdo;
6012 6037
         }
6013 6038
 
6014
-        public function setUser(/*?string*/ $user): bool
6039
+        public function reauthenticate(/*?string*/$user, /*?string*/ $password): bool
6015 6040
         {
6016
-            if ($this->pdo) {
6017
-                return false;
6018
-            }
6019 6041
             $this->user = $user;
6020
-            return true;
6021
-        }
6022
-
6023
-        public function setPassword(/*?string*/ $password): bool
6024
-        {
6042
+            $this->password = $password;
6025 6043
             if ($this->pdo) {
6044
+                $this->pdo = null;
6026 6045
                 return false;
6027 6046
             }
6028
-            $this->password = $password;
6029 6047
             return true;
6030 6048
         }
6031
-        
6049
+
6032 6050
         public function inTransaction(): bool
6033 6051
         {
6034 6052
             // Do not call parent method if there is no pdo object
@@ -6037,7 +6055,7 @@ namespace Tqdev\PhpCrudApi\Database {
6037 6055
 
6038 6056
         public function setAttribute($attribute, $value): bool
6039 6057
         {
6040
-            if ($this->pdo) { 
6058
+            if ($this->pdo) {
6041 6059
                 return $this->pdo()->setAttribute($attribute, $value);
6042 6060
             }
6043 6061
             $this->options[$attribute] = $value;
@@ -6096,7 +6114,7 @@ namespace Tqdev\PhpCrudApi\Database {
6096 6114
 
6097 6115
         public function query(string $statement): \PDOStatement
6098 6116
         {
6099
-            return call_user_func_array(array($this->pdo(), __FUNCTION__), func_get_args());
6117
+            return call_user_func_array(array($this->pdo(), 'query'), func_get_args());
6100 6118
         }
6101 6119
     }
6102 6120
 }
@@ -7721,6 +7739,60 @@ namespace Tqdev\PhpCrudApi\Middleware {
7721 7739
     }
7722 7740
 }
7723 7741
 
7742
+// file: src/Tqdev/PhpCrudApi/Middleware/ReAuthMiddleware.php
7743
+namespace Tqdev\PhpCrudApi\Middleware {
7744
+
7745
+    use Psr\Http\Message\ResponseInterface;
7746
+    use Psr\Http\Message\ServerRequestInterface;
7747
+    use Psr\Http\Server\RequestHandlerInterface;
7748
+    use Tqdev\PhpCrudApi\Column\ReflectionService;
7749
+    use Tqdev\PhpCrudApi\Controller\Responder;
7750
+    use Tqdev\PhpCrudApi\Database\GenericDB;
7751
+    use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
7752
+    use Tqdev\PhpCrudApi\Middleware\Router\Router;
7753
+
7754
+    class ReAuthMiddleware extends Middleware
7755
+    {
7756
+        private $reflection;
7757
+        private $db;
7758
+
7759
+        public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection, GenericDB $db)
7760
+        {
7761
+            parent::__construct($router, $responder, $properties);
7762
+            $this->reflection = $reflection;
7763
+            $this->db = $db;
7764
+        }
7765
+
7766
+        private function getUsername(): string
7767
+        {
7768
+            $usernameHandler = $this->getProperty('usernameHandler', '');
7769
+            if ($usernameHandler) {
7770
+                return call_user_func($usernameHandler);
7771
+            }
7772
+            return '';
7773
+        }
7774
+
7775
+        private function getPassword(): string
7776
+        {
7777
+            $passwordHandler = $this->getProperty('passwordHandler', '');
7778
+            if ($passwordHandler) {
7779
+                return call_user_func($passwordHandler);
7780
+            }
7781
+            return '';
7782
+        }
7783
+
7784
+        public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
7785
+        {
7786
+            $username = $this->getUsername();
7787
+            $password = $this->getPassword();
7788
+            if ($username && $password) {
7789
+                $this->db->pdo()->reauthenticate($username, $password);
7790
+            }
7791
+            return $next->handle($request);
7792
+        }
7793
+    }
7794
+}
7795
+
7724 7796
 // file: src/Tqdev/PhpCrudApi/Middleware/SanitationMiddleware.php
7725 7797
 namespace Tqdev\PhpCrudApi\Middleware {
7726 7798
 
@@ -9505,6 +9577,7 @@ namespace Tqdev\PhpCrudApi {
9505 9577
     use Tqdev\PhpCrudApi\Middleware\IpAddressMiddleware;
9506 9578
     use Tqdev\PhpCrudApi\Middleware\JoinLimitsMiddleware;
9507 9579
     use Tqdev\PhpCrudApi\Middleware\JwtAuthMiddleware;
9580
+    use Tqdev\PhpCrudApi\Middleware\ReAuthMiddleware;
9508 9581
     use Tqdev\PhpCrudApi\Middleware\MultiTenancyMiddleware;
9509 9582
     use Tqdev\PhpCrudApi\Middleware\PageLimitsMiddleware;
9510 9583
     use Tqdev\PhpCrudApi\Middleware\Router\SimpleRouter;
@@ -9554,6 +9627,9 @@ namespace Tqdev\PhpCrudApi {
9554 9627
                     case 'dbAuth':
9555 9628
                         new DbAuthMiddleware($router, $responder, $properties, $reflection, $db);
9556 9629
                         break;
9630
+                    case 'reAuth':
9631
+                        new ReAuthMiddleware($router, $responder, $properties, $reflection, $db);
9632
+                        break;
9557 9633
                     case 'validation':
9558 9634
                         new ValidationMiddleware($router, $responder, $properties, $reflection);
9559 9635
                         break;

Loading…
Cancel
Save