]> BookStack Code Mirror - bookstack/commitdiff
Aligned password length requirements
authorDan Brown <redacted>
Sat, 18 Dec 2021 16:31:48 +0000 (16:31 +0000)
committerDan Brown <redacted>
Sat, 18 Dec 2021 16:33:40 +0000 (16:33 +0000)
Updated all password validation to use central password defaults
system while updating length requirements to now all match
at 8 characters minimum.

Some language text was technically correct (More than 7 characters)
but this has been updated for clarity and to prompt other translations
to be updated.

Closes #2237

app/Console/Commands/CreateAdmin.php
app/Http/Controllers/Auth/RegisterController.php
app/Http/Controllers/Auth/UserInviteController.php
app/Http/Controllers/UserController.php
app/Providers/AuthServiceProvider.php
resources/lang/en/auth.php
resources/lang/en/settings.php

index 1494444205c734c31d8f493484f690ec4e472180..48da87af4f379deaff2b16f09294769f2ad33297 100644 (file)
@@ -4,6 +4,9 @@ namespace BookStack\Console\Commands;
 
 use BookStack\Auth\UserRepo;
 use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Validator;
+use Illuminate\Validation\Rules\Password;
+use Illuminate\Validation\Rules\Unique;
 use Symfony\Component\Console\Command\Command as SymfonyCommand;
 
 class CreateAdmin extends Command
@@ -45,43 +48,33 @@ class CreateAdmin extends Command
      */
     public function handle()
     {
-        $email = trim($this->option('email'));
-        if (empty($email)) {
-            $email = $this->ask('Please specify an email address for the new admin user');
-        }
-        if (mb_strlen($email) < 5 || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
-            $this->error('Invalid email address provided');
 
-            return SymfonyCommand::FAILURE;
-        }
+        $details = $this->options();
 
-        if ($this->userRepo->getByEmail($email) !== null) {
-            $this->error('A user with the provided email already exists!');
-
-            return SymfonyCommand::FAILURE;
+        if (empty($details['email'])) {
+            $details['email'] = $this->ask('Please specify an email address for the new admin user');
         }
-
-        $name = trim($this->option('name'));
-        if (empty($name)) {
-            $name = $this->ask('Please specify an name for the new admin user');
+        if (empty($details['name'])) {
+            $details['name'] = $this->ask('Please specify a name for the new admin user');
         }
-        if (mb_strlen($name) < 2) {
-            $this->error('Invalid name provided');
-
-            return SymfonyCommand::FAILURE;
+        if (empty($details['password'])) {
+            $details['password'] = $this->ask('Please specify a password for the new admin user (8 characters min)');
         }
 
-        $password = trim($this->option('password'));
-        if (empty($password)) {
-            $password = $this->secret('Please specify a password for the new admin user');
-        }
-        if (mb_strlen($password) < 5) {
-            $this->error('Invalid password provided, Must be at least 5 characters');
+        $validator = Validator::make($details, [
+            'email' => ['required', 'email', 'min:5', new Unique('users', 'email')],
+            'name' => ['required', 'min:2'],
+            'password' => ['required', Password::default()],
+        ]);
 
+        if ($validator->fails()) {
+            foreach ($validator->errors()->all() as $error) {
+                $this->error($error);
+            }
             return SymfonyCommand::FAILURE;
         }
 
-        $user = $this->userRepo->create(['email' => $email, 'name' => $name, 'password' => $password]);
+        $user = $this->userRepo->create($validator->validated());
         $this->userRepo->attachSystemRole($user, 'admin');
         $this->userRepo->downloadAndAssignUserAvatar($user);
         $user->email_confirmed = true;
index d4e7fcb8e90281c4506926ab267b0f37de61b5b2..9399e8b7f53339c373278742800604a3247b5a48 100644 (file)
@@ -13,6 +13,7 @@ use Illuminate\Foundation\Auth\RegistersUsers;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Hash;
 use Illuminate\Support\Facades\Validator;
+use Illuminate\Validation\Rules\Password;
 
 class RegisterController extends Controller
 {
@@ -70,7 +71,7 @@ class RegisterController extends Controller
         return Validator::make($data, [
             'name'     => ['required', 'min:2', 'max:255'],
             'email'    => ['required', 'email', 'max:255', 'unique:users'],
-            'password' => ['required', 'min:8'],
+            'password' => ['required', Password::default()],
         ]);
     }
 
index df8262e222e0ddd19ce2afaeff6e5096e2bd3c53..27b20f831b956d67b6501a4c17a8ee902e645a85 100644 (file)
@@ -11,6 +11,7 @@ use Exception;
 use Illuminate\Http\RedirectResponse;
 use Illuminate\Http\Request;
 use Illuminate\Routing\Redirector;
+use Illuminate\Validation\Rules\Password;
 
 class UserInviteController extends Controller
 {
@@ -55,7 +56,7 @@ class UserInviteController extends Controller
     public function setPassword(Request $request, string $token)
     {
         $this->validate($request, [
-            'password' => ['required', 'min:8'],
+            'password' => ['required', Password::default()],
         ]);
 
         try {
index 414bfefeb3e3db5fab5ad825511f97b600ac8a4d..a78f921f21e2ea187b906be6e3b4783d77d70e23 100644 (file)
@@ -13,6 +13,7 @@ use BookStack\Uploads\ImageRepo;
 use Exception;
 use Illuminate\Http\Request;
 use Illuminate\Support\Str;
+use Illuminate\Validation\Rules\Password;
 use Illuminate\Validation\ValidationException;
 
 class UserController extends Controller
@@ -82,7 +83,7 @@ class UserController extends Controller
         $sendInvite = ($request->get('send_invite', 'false') === 'true');
 
         if ($authMethod === 'standard' && !$sendInvite) {
-            $validationRules['password'] = ['required', 'min:6'];
+            $validationRules['password'] = ['required', Password::default()];
             $validationRules['password-confirm'] = ['required', 'same:password'];
         } elseif ($authMethod === 'ldap' || $authMethod === 'saml2' || $authMethod === 'openid') {
             $validationRules['external_auth_id'] = ['required'];
@@ -155,11 +156,11 @@ class UserController extends Controller
         $this->checkPermissionOrCurrentUser('users-manage', $id);
 
         $this->validate($request, [
-            'name'             => 'min:2',
+            'name'             => ['min:2'],
             'email'            => ['min:2', 'email', 'unique:users,email,' . $id],
-            'password'         => ['min:6', 'required_with:password_confirm'],
+            'password'         => ['required_with:password_confirm', Password::default()],
             'password-confirm' => ['same:password', 'required_with:password'],
-            'setting'          => 'array',
+            'setting'          => ['array'],
             'profile_image'    => array_merge(['nullable'], $this->getImageValidationRules()),
         ]);
 
index 4a626e4fadd49e49d697967c496aad433f6de488..b301604a519e9b95e5bf5881f7a93c0f78cfb88e 100644 (file)
@@ -11,6 +11,7 @@ use BookStack\Auth\Access\LoginService;
 use BookStack\Auth\Access\RegistrationService;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\ServiceProvider;
+use Illuminate\Validation\Rules\Password;
 
 class AuthServiceProvider extends ServiceProvider
 {
@@ -21,6 +22,12 @@ class AuthServiceProvider extends ServiceProvider
      */
     public function boot()
     {
+        // Password Configuration
+        Password::defaults(function () {
+            return Password::min(8);
+        });
+
+        // Custom guards
         Auth::extend('api-token', function ($app, $name, array $config) {
             return new ApiTokenGuard($app['request'], $app->make(LoginService::class));
         });
index 107585bf0da5496aa23d58f0273ec9835bca9c8e..ad0d516bb1d5e2f6a9d7006d422cf1ad9b910141 100644 (file)
@@ -21,7 +21,7 @@ return [
     'email' => 'Email',
     'password' => 'Password',
     'password_confirm' => 'Confirm Password',
-    'password_hint' => 'Must be over 7 characters',
+    'password_hint' => 'Must be at least 8 characters',
     'forgot_password' => 'Forgot Password?',
     'remember_me' => 'Remember Me',
     'ldap_email_hint' => 'Please enter an email to use for this account.',
index 1cca516c85d6bef1b0f539d7760094475f76c84f..08d4e7294acb6082a1178935c34b8851a0fe8e8c 100755 (executable)
@@ -174,7 +174,7 @@ return [
     'users_role' => 'User Roles',
     'users_role_desc' => 'Select which roles this user will be assigned to. If a user is assigned to multiple roles the permissions from those roles will stack and they will receive all abilities of the assigned roles.',
     'users_password' => 'User Password',
-    'users_password_desc' => 'Set a password used to log-in to the application. This must be at least 6 characters long.',
+    'users_password_desc' => 'Set a password used to log-in to the application. This must be at least 8 characters long.',
     'users_send_invite_text' => 'You can choose to send this user an invitation email which allows them to set their own password otherwise you can set their password yourself.',
     'users_send_invite_option' => 'Send user invite email',
     'users_external_auth_id' => 'External Authentication ID',