Skip to content

Commit 71b9d5a

Browse files
committed
Added dbAuth middleware
1 parent b267b08 commit 71b9d5a

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
namespace Tqdev\PhpCrudApi\Middleware;
3+
4+
use Psr\Http\Message\ResponseInterface;
5+
use Psr\Http\Message\ServerRequestInterface;
6+
use Psr\Http\Server\RequestHandlerInterface;
7+
use Tqdev\PhpCrudApi\Column\ReflectionService;
8+
use Tqdev\PhpCrudApi\Controller\Responder;
9+
use Tqdev\PhpCrudApi\Database\GenericDB;
10+
use Tqdev\PhpCrudApi\Middleware\Base\Middleware;
11+
use Tqdev\PhpCrudApi\Middleware\Router\Router;
12+
use Tqdev\PhpCrudApi\Record\Condition\ColumnCondition;
13+
use Tqdev\PhpCrudApi\Record\ErrorCode;
14+
use Tqdev\PhpCrudApi\RequestUtils;
15+
16+
class DbAuthMiddleware extends Middleware
17+
{
18+
private $reflection;
19+
private $db;
20+
21+
public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection, GenericDB $db)
22+
{
23+
parent::__construct($router, $responder, $properties);
24+
$this->reflection = $reflection;
25+
$this->db = $db;
26+
}
27+
28+
public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface
29+
{
30+
if (session_status() == PHP_SESSION_NONE) {
31+
if (!headers_sent()) {
32+
session_start();
33+
}
34+
}
35+
$path = RequestUtils::getPathSegment($request, 1);
36+
$method = $request->getMethod();
37+
if ($method == 'POST' && $path == 'login') {
38+
$body = $request->getParsedBody();
39+
$username = isset($body->username) ? $body->username : '';
40+
$password = isset($body->password) ? $body->password : '';
41+
$tableName = $this->getProperty('usersTable', 'users');
42+
$table = $this->reflection->getTable($tableName);
43+
$usernameColumnName = $this->getProperty('usernameColumn', 'username');
44+
$usernameColumn = $table->getColumn($usernameColumnName);
45+
$passwordColumnName = $this->getProperty('passwordColumn', 'password');
46+
$passwordColumn = $table->getColumn($passwordColumnName);
47+
$condition = new ColumnCondition($usernameColumn, 'eq', $username);
48+
$columnNames = $table->getColumnNames();
49+
$users = $this->db->selectAll($table, $columnNames, $condition, [], 0, -1);
50+
foreach ($users as $user) {
51+
if (password_verify($password, $user[$passwordColumnName]) == 1) {
52+
if (!headers_sent()) {
53+
session_regenerate_id(true);
54+
}
55+
unset($user[$passwordColumnName]);
56+
$_SESSION['user'] = $user;
57+
return $this->responder->success($user);
58+
}
59+
}
60+
return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, $username);
61+
}
62+
if ($method == 'POST' && $path == 'logout') {
63+
if (isset($_SESSION['user'])) {
64+
$user = $_SESSION['user'];
65+
unset($_SESSION['user']);
66+
session_destroy();
67+
return $this->responder->success($user);
68+
}
69+
return $this->responder->error(ErrorCode::AUTHENTICATION_REQUIRED, '');
70+
}
71+
if (!isset($_SESSION['user']) || !$_SESSION['user']) {
72+
$authenticationMode = $this->getProperty('mode', 'required');
73+
if ($authenticationMode == 'required') {
74+
return $this->responder->error(ErrorCode::AUTHENTICATION_REQUIRED, '');
75+
}
76+
}
77+
return $next->handle($request);
78+
}
79+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
GET /records/invisibles/e42c77c6-06a4-4502-816c-d112c7142e6d
2+
===
3+
404
4+
Content-Type: application/json
5+
Content-Length: 54
6+
7+
{"code":1001,"message":"Table 'invisibles' not found"}
8+
===
9+
POST /login
10+
Content-Type: application/json
11+
12+
{"username":"user2","password":"pass2"}
13+
===
14+
200
15+
Content-Type: application/json
16+
Content-Length: 43
17+
18+
{"id":2,"username":"user2","location":null}
19+
===
20+
GET /records/invisibles/e42c77c6-06a4-4502-816c-d112c7142e6d
21+
===
22+
200
23+
Content-Type: application/json
24+
Content-Length: 45
25+
26+
{"id":"e42c77c6-06a4-4502-816c-d112c7142e6d"}
27+
===
28+
POST /login
29+
Content-Type: application/json
30+
31+
{"username":"user2","password":"incorect password"}
32+
===
33+
403
34+
Content-Type: application/json
35+
Content-Length: 59
36+
37+
{"code":1012,"message":"Authentication failed for 'user2'"}
38+
===
39+
GET /records/invisibles/e42c77c6-06a4-4502-816c-d112c7142e6d
40+
===
41+
200
42+
Content-Type: application/json
43+
Content-Length: 45
44+
45+
{"id":"e42c77c6-06a4-4502-816c-d112c7142e6d"}
46+
===
47+
POST /logout
48+
===
49+
200
50+
Content-Type: application/json
51+
Content-Length: 43
52+
53+
{"id":2,"username":"user2","location":null}
54+
===
55+
GET /records/invisibles/e42c77c6-06a4-4502-816c-d112c7142e6d
56+
===
57+
404
58+
Content-Type: application/json
59+
Content-Length: 54
60+
61+
{"code":1001,"message":"Table 'invisibles' not found"}
62+
===
63+
POST /logout
64+
===
65+
401
66+
Content-Type: application/json
67+
Content-Length: 49
68+
69+
{"code":1011,"message":"Authentication required"}

0 commit comments

Comments
 (0)