Skip to content

Commit feddc70

Browse files
committed
add passwordLength requirement
1 parent 5bbb2fd commit feddc70

File tree

6 files changed

+31
-0
lines changed

6 files changed

+31
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,7 @@ You can tune the middleware behavior using middleware specific configuration par
643643
- "dbAuth.passwordColumn": The users table column that holds passwords ("password")
644644
- "dbAuth.returnedColumns": The columns returned on successful login, empty means 'all' ("")
645645
- "dbAuth.registerUser": JSON user data (or "1") in case you want the /register endpoint enabled ("")
646+
- "dbAuth.passwordLength": Minimum length that the password must have ("12")
646647
- "dbAuth.sessionName": The name of the PHP session that is started ("")
647648
- "jwtAuth.mode": Set to "optional" if you want to allow anonymous access ("required")
648649
- "jwtAuth.header": Name of the header containing the JWT token ("X-Authorization")

api.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7568,6 +7568,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
75687568
$usernameColumnName = $this->getProperty('usernameColumn', 'username');
75697569
$usernameColumn = $table->getColumn($usernameColumnName);
75707570
$passwordColumnName = $this->getProperty('passwordColumn', 'password');
7571+
$passwordLength = $this->getProperty('passwordLength', '12');
75717572
$pkName = $table->getPk()->getName();
75727573
$registerUser = $this->getProperty('registerUser', '');
75737574
$condition = new ColumnCondition($usernameColumn, 'eq', $username);
@@ -7584,6 +7585,9 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
75847585
if (!$registerUser) {
75857586
return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, $username);
75867587
}
7588+
if (strlen($password) < $passwordLength) {
7589+
return $this->responder->error(ErrorCode::PASSWORD_TOO_SHORT, $passwordLength);
7590+
}
75877591
$users = $this->db->selectAll($table, $columnNames, $condition, $columnOrdering, 0, 1);
75887592
if (!empty($users)) {
75897593
return $this->responder->error(ErrorCode::USER_ALREADY_EXIST, $username);
@@ -7618,6 +7622,9 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
76187622
if ($username != ($_SESSION['user'][$usernameColumnName] ?? '')) {
76197623
return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, $username);
76207624
}
7625+
if (strlen($newPassword) < $passwordLength) {
7626+
return $this->responder->error(ErrorCode::PASSWORD_TOO_SHORT, $passwordLength);
7627+
}
76217628
$users = $this->db->selectAll($table, $columnNames, $condition, $columnOrdering, 0, 1);
76227629
foreach ($users as $user) {
76237630
if (password_verify($password, $user[$passwordColumnName]) == 1) {
@@ -9962,6 +9969,7 @@ class ErrorCode
99629969
const ONLY_AJAX_REQUESTS_ALLOWED = 1018;
99639970
const PAGINATION_FORBIDDEN = 1019;
99649971
const USER_ALREADY_EXIST = 1020;
9972+
const PASSWORD_TOO_SHORT = 1021;
99659973

99669974
private $values = [
99679975
9999 => ["%s", ResponseFactory::INTERNAL_SERVER_ERROR],
@@ -9986,6 +9994,7 @@ class ErrorCode
99869994
1018 => ["Only AJAX requests allowed for '%s'", ResponseFactory::FORBIDDEN],
99879995
1019 => ["Pagination forbidden", ResponseFactory::FORBIDDEN],
99889996
1020 => ["User '%s' already exists", ResponseFactory::CONFLICT],
9997+
1021 => ["Password too short (<%d characters)", ResponseFactory::UNPROCESSABLE_ENTITY],
99899998
];
99909999

999110000
public function __construct(int $code)

src/Tqdev/PhpCrudApi/Middleware/DbAuthMiddleware.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
5252
$usernameColumnName = $this->getProperty('usernameColumn', 'username');
5353
$usernameColumn = $table->getColumn($usernameColumnName);
5454
$passwordColumnName = $this->getProperty('passwordColumn', 'password');
55+
$passwordLength = $this->getProperty('passwordLength', '12');
5556
$pkName = $table->getPk()->getName();
5657
$registerUser = $this->getProperty('registerUser', '');
5758
$condition = new ColumnCondition($usernameColumn, 'eq', $username);
@@ -68,6 +69,9 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
6869
if (!$registerUser) {
6970
return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, $username);
7071
}
72+
if (strlen($password) < $passwordLength) {
73+
return $this->responder->error(ErrorCode::PASSWORD_TOO_SHORT, $passwordLength);
74+
}
7175
$users = $this->db->selectAll($table, $columnNames, $condition, $columnOrdering, 0, 1);
7276
if (!empty($users)) {
7377
return $this->responder->error(ErrorCode::USER_ALREADY_EXIST, $username);
@@ -102,6 +106,9 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
102106
if ($username != ($_SESSION['user'][$usernameColumnName] ?? '')) {
103107
return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, $username);
104108
}
109+
if (strlen($newPassword) < $passwordLength) {
110+
return $this->responder->error(ErrorCode::PASSWORD_TOO_SHORT, $passwordLength);
111+
}
105112
$users = $this->db->selectAll($table, $columnNames, $condition, $columnOrdering, 0, 1);
106113
foreach ($users as $user) {
107114
if (password_verify($password, $user[$passwordColumnName]) == 1) {

src/Tqdev/PhpCrudApi/Record/ErrorCode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class ErrorCode
3232
const ONLY_AJAX_REQUESTS_ALLOWED = 1018;
3333
const PAGINATION_FORBIDDEN = 1019;
3434
const USER_ALREADY_EXIST = 1020;
35+
const PASSWORD_TOO_SHORT = 1021;
3536

3637
private $values = [
3738
9999 => ["%s", ResponseFactory::INTERNAL_SERVER_ERROR],
@@ -56,6 +57,7 @@ class ErrorCode
5657
1018 => ["Only AJAX requests allowed for '%s'", ResponseFactory::FORBIDDEN],
5758
1019 => ["Pagination forbidden", ResponseFactory::FORBIDDEN],
5859
1020 => ["User '%s' already exists", ResponseFactory::CONFLICT],
60+
1021 => ["Password too short (<%d characters)", ResponseFactory::UNPROCESSABLE_ENTITY],
5961
];
6062

6163
public function __construct(int $code)

tests/config/base.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
'dbAuth.mode' => 'optional',
99
'dbAuth.returnedColumns' => 'id,username,password',
1010
'dbAuth.registerUser' => '1',
11+
'dbAuth.passwordLength' => '4',
1112
'jwtAuth.mode' => 'optional',
1213
'jwtAuth.time' => '1538207605',
1314
'jwtAuth.secrets' => 'axpIrCGNGqxzx2R9dtXLIPUSqPo778uhb8CA0F4Hx',

tests/functional/002_auth/003_db_auth.log

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ Content-Length: 49
8080
POST /register
8181
Content-Type: application/json; charset=utf-8
8282

83+
{"username":"user2","password":""}
84+
===
85+
422
86+
Content-Type: application/json; charset=utf-8
87+
Content-Length: 60
88+
89+
{"code":1021,"message":"Password too short (<4 characters)"}
90+
===
91+
POST /register
92+
Content-Type: application/json; charset=utf-8
93+
8394
{"username":"user2","password":"pass2"}
8495
===
8596
409

0 commit comments

Comments
 (0)