Browse Source

add passwordLength requirement

Maurits van der Schee 3 years ago
parent
commit
feddc70baf

+ 1
- 0
README.md View File

@@ -643,6 +643,7 @@ You can tune the middleware behavior using middleware specific configuration par
643 643
 - "dbAuth.passwordColumn": The users table column that holds passwords ("password")
644 644
 - "dbAuth.returnedColumns": The columns returned on successful login, empty means 'all' ("")
645 645
 - "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")
646 647
 - "dbAuth.sessionName": The name of the PHP session that is started ("")
647 648
 - "jwtAuth.mode": Set to "optional" if you want to allow anonymous access ("required")
648 649
 - "jwtAuth.header": Name of the header containing the JWT token ("X-Authorization")

+ 9
- 0
api.php View File

@@ -7568,6 +7568,7 @@ namespace Tqdev\PhpCrudApi\Middleware {
7568 7568
                 $usernameColumnName = $this->getProperty('usernameColumn', 'username');
7569 7569
                 $usernameColumn = $table->getColumn($usernameColumnName);
7570 7570
                 $passwordColumnName = $this->getProperty('passwordColumn', 'password');
7571
+                $passwordLength = $this->getProperty('passwordLength', '12');
7571 7572
                 $pkName = $table->getPk()->getName();
7572 7573
                 $registerUser = $this->getProperty('registerUser', '');
7573 7574
                 $condition = new ColumnCondition($usernameColumn, 'eq', $username);
@@ -7584,6 +7585,9 @@ namespace Tqdev\PhpCrudApi\Middleware {
7584 7585
                     if (!$registerUser) {
7585 7586
                         return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, $username);
7586 7587
                     }
7588
+                    if (strlen($password) < $passwordLength) {
7589
+                        return $this->responder->error(ErrorCode::PASSWORD_TOO_SHORT, $passwordLength);
7590
+                    }
7587 7591
                     $users = $this->db->selectAll($table, $columnNames, $condition, $columnOrdering, 0, 1);
7588 7592
                     if (!empty($users)) {
7589 7593
                         return $this->responder->error(ErrorCode::USER_ALREADY_EXIST, $username);
@@ -7618,6 +7622,9 @@ namespace Tqdev\PhpCrudApi\Middleware {
7618 7622
                     if ($username != ($_SESSION['user'][$usernameColumnName] ?? '')) {
7619 7623
                         return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, $username);
7620 7624
                     }
7625
+                    if (strlen($newPassword) < $passwordLength) {
7626
+                        return $this->responder->error(ErrorCode::PASSWORD_TOO_SHORT, $passwordLength);
7627
+                    }
7621 7628
                     $users = $this->db->selectAll($table, $columnNames, $condition, $columnOrdering, 0, 1);
7622 7629
                     foreach ($users as $user) {
7623 7630
                         if (password_verify($password, $user[$passwordColumnName]) == 1) {
@@ -9962,6 +9969,7 @@ namespace Tqdev\PhpCrudApi\Record {
9962 9969
         const ONLY_AJAX_REQUESTS_ALLOWED = 1018;
9963 9970
         const PAGINATION_FORBIDDEN = 1019;
9964 9971
         const USER_ALREADY_EXIST = 1020;
9972
+        const PASSWORD_TOO_SHORT = 1021;
9965 9973
 
9966 9974
         private $values = [
9967 9975
             9999 => ["%s", ResponseFactory::INTERNAL_SERVER_ERROR],
@@ -9986,6 +9994,7 @@ namespace Tqdev\PhpCrudApi\Record {
9986 9994
             1018 => ["Only AJAX requests allowed for '%s'", ResponseFactory::FORBIDDEN],
9987 9995
             1019 => ["Pagination forbidden", ResponseFactory::FORBIDDEN],
9988 9996
             1020 => ["User '%s' already exists", ResponseFactory::CONFLICT],
9997
+            1021 => ["Password too short (<%d characters)", ResponseFactory::UNPROCESSABLE_ENTITY],
9989 9998
         ];
9990 9999
 
9991 10000
         public function __construct(int $code)

+ 7
- 0
src/Tqdev/PhpCrudApi/Middleware/DbAuthMiddleware.php View File

@@ -52,6 +52,7 @@ class DbAuthMiddleware extends Middleware
52 52
             $usernameColumnName = $this->getProperty('usernameColumn', 'username');
53 53
             $usernameColumn = $table->getColumn($usernameColumnName);
54 54
             $passwordColumnName = $this->getProperty('passwordColumn', 'password');
55
+            $passwordLength = $this->getProperty('passwordLength', '12');
55 56
             $pkName = $table->getPk()->getName();
56 57
             $registerUser = $this->getProperty('registerUser', '');
57 58
             $condition = new ColumnCondition($usernameColumn, 'eq', $username);
@@ -68,6 +69,9 @@ class DbAuthMiddleware extends Middleware
68 69
                 if (!$registerUser) {
69 70
                     return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, $username);
70 71
                 }
72
+                if (strlen($password) < $passwordLength) {
73
+                    return $this->responder->error(ErrorCode::PASSWORD_TOO_SHORT, $passwordLength);
74
+                }
71 75
                 $users = $this->db->selectAll($table, $columnNames, $condition, $columnOrdering, 0, 1);
72 76
                 if (!empty($users)) {
73 77
                     return $this->responder->error(ErrorCode::USER_ALREADY_EXIST, $username);
@@ -102,6 +106,9 @@ class DbAuthMiddleware extends Middleware
102 106
                 if ($username != ($_SESSION['user'][$usernameColumnName] ?? '')) {
103 107
                     return $this->responder->error(ErrorCode::AUTHENTICATION_FAILED, $username);
104 108
                 }
109
+                if (strlen($newPassword) < $passwordLength) {
110
+                    return $this->responder->error(ErrorCode::PASSWORD_TOO_SHORT, $passwordLength);
111
+                }
105 112
                 $users = $this->db->selectAll($table, $columnNames, $condition, $columnOrdering, 0, 1);
106 113
                 foreach ($users as $user) {
107 114
                     if (password_verify($password, $user[$passwordColumnName]) == 1) {

+ 2
- 0
src/Tqdev/PhpCrudApi/Record/ErrorCode.php View File

@@ -32,6 +32,7 @@ class ErrorCode
32 32
     const ONLY_AJAX_REQUESTS_ALLOWED = 1018;
33 33
     const PAGINATION_FORBIDDEN = 1019;
34 34
     const USER_ALREADY_EXIST = 1020;
35
+    const PASSWORD_TOO_SHORT = 1021;
35 36
 
36 37
     private $values = [
37 38
         9999 => ["%s", ResponseFactory::INTERNAL_SERVER_ERROR],
@@ -56,6 +57,7 @@ class ErrorCode
56 57
         1018 => ["Only AJAX requests allowed for '%s'", ResponseFactory::FORBIDDEN],
57 58
         1019 => ["Pagination forbidden", ResponseFactory::FORBIDDEN],
58 59
         1020 => ["User '%s' already exists", ResponseFactory::CONFLICT],
60
+        1021 => ["Password too short (<%d characters)", ResponseFactory::UNPROCESSABLE_ENTITY],
59 61
     ];
60 62
 
61 63
     public function __construct(int $code)

+ 1
- 0
tests/config/base.php View File

@@ -8,6 +8,7 @@ $settings = [
8 8
     'dbAuth.mode' => 'optional',
9 9
     'dbAuth.returnedColumns' => 'id,username,password',
10 10
     'dbAuth.registerUser' => '1',
11
+    'dbAuth.passwordLength' => '4',
11 12
     'jwtAuth.mode' => 'optional',
12 13
     'jwtAuth.time' => '1538207605',
13 14
     'jwtAuth.secrets' => 'axpIrCGNGqxzx2R9dtXLIPUSqPo778uhb8CA0F4Hx',

+ 11
- 0
tests/functional/002_auth/003_db_auth.log View File

@@ -80,6 +80,17 @@ Content-Length: 49
80 80
 POST /register
81 81
 Content-Type: application/json; charset=utf-8
82 82
 
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
+
83 94
 {"username":"user2","password":"pass2"}
84 95
 ===
85 96
 409

Loading…
Cancel
Save