Skip to content

FOUR-11415 Password Policy Configuration #5682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ProcessMaker/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class LoginController extends Controller
*/
protected $redirectTo = '/';

protected $maxAttempts;

/**
* Create a new controller instance.
*
Expand All @@ -46,6 +48,7 @@ class LoginController extends Controller
public function __construct()
{
$this->middleware('guest')->except(['logout', 'beforeLogout', 'keepAlive']);
$this->maxAttempts = (int) config('password-policies.login_attempts');
}

/**
Expand Down
27 changes: 21 additions & 6 deletions ProcessMaker/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Password;
use Laravel\Passport\HasApiTokens;
use ProcessMaker\Models\EmptyModel;
use ProcessMaker\Notifications\ResetPassword as ResetPasswordNotification;
use ProcessMaker\Query\Traits\PMQL;
use ProcessMaker\Rules\StringHasAtLeastOneUpperCaseCharacter;
use ProcessMaker\Rules\StringHasNumberOrSpecialCharacter;
use ProcessMaker\Traits\Exportable;
use ProcessMaker\Traits\HasAuthorization;
use ProcessMaker\Traits\HideSystemResources;
Expand Down Expand Up @@ -183,13 +183,28 @@ public static function rules(self $existing = null)
*/
public static function passwordRules(self $existing = null)
{
return array_filter([
// Mandatory policies
$passwordPolicies = [
'required',
$existing ? 'sometimes' : '',
'min:8',
new StringHasNumberOrSpecialCharacter(),
new StringHasAtLeastOneUpperCaseCharacter(),
]);
];
// Configurable policies
$passwordRules = Password::min(config('password-policies.minimum_length'));
if (config('password-policies.maximum_length')) {
$passwordPolicies[] = 'max:' . config('password-policies.maximum_length');
}
if (config('password-policies.numbers')) {
$passwordRules->numbers();
}
if (config('password-policies.uppercase')) {
$passwordPolicies[] = new StringHasAtLeastOneUpperCaseCharacter();
}
if (config('password-policies.special')) {
$passwordRules->symbols();
}
$passwordPolicies[] = $passwordRules;

return $passwordPolicies;
}

/**
Expand Down
11 changes: 11 additions & 0 deletions config/password-policies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

return [
'minimum_length' => env('PASSWORD_POLICY_MINIMUM_LENGTH', 8),
'maximum_length' => env('PASSWORD_POLICY_MAXIMUM_LENGTH', null),
'numbers' => env('PASSWORD_POLICY_NUMBERS', true),
'uppercase' => env('PASSWORD_POLICY_UPPERCASE', true),
'special' => env('PASSWORD_POLICY_SPECIAL', true),
//'expiration_days' => env('PASSWORD_POLICY_EXPIRATION_DAYS', 0), // 0 never expires
'login_attempts' => env('PASSWORD_POLICY_LOGIN_ATTEMPTS', 5),
];
6 changes: 0 additions & 6 deletions resources/views/admin/users/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,6 @@
delete this.formData.password;
return true
}
if (this.formData.password.trim().length > 0 && this.formData.password.trim().length < 8) {
this.errors.password = ['Password must be at least 8 characters']
this.password = ''
this.submitted = false
return false
}
if (this.formData.password !== this.formData.confPassword) {
this.errors.password = ['Passwords must match']
this.password = ''
Expand Down
13 changes: 1 addition & 12 deletions resources/views/auth/passwords/change.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
'v-bind:class' => '{\'form-control\':true, \'is-invalid\':errors.password}']) !!}
</div>
</vue-password>
<small v-if="errors && errors.password && errors.password.length" class="text-danger">@{{ errors.password[0] }}</small>
<small v-for="(error, index) in errors.password" class="text-danger">@{{ error }}</small>
</div>
<div class="form-group">
{!!Form::label('confpassword', __('Confirm Password'))!!}<small class="ml-1">*</small>
Expand Down Expand Up @@ -94,11 +94,6 @@
});
},
validatePassword() {
if (this.$refs.passwordStrength.strength.score < 2) {
this.errors.password = ['Password is too weak']
return false
}

if (!this.formData.password && !this.formData.confpassword) {
return false;
}
Expand All @@ -107,12 +102,6 @@
return false
}

if (this.formData.password.trim().length > 0 && this.formData.password.trim().length < 8) {
this.errors.password = ['Password must be at least 8 characters']
this.formData.password = ''
return false
}

if (this.formData.password !== this.formData.confpassword) {
this.errors.password = ['Passwords must match']
return false
Expand Down
6 changes: 0 additions & 6 deletions resources/views/profile/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,6 @@
delete this.formData.password;
return true
}
if (this.formData.password.trim().length > 0 && this.formData.password.trim().length < 8) {
this.errors.password = ['Password must be at least 8 characters']
this.password = ''
this.submitted = false
return false
}
if (this.formData.password !== this.formData.confPassword) {
this.errors.password = ['Passwords must match']
this.password = ''
Expand Down
7 changes: 4 additions & 3 deletions resources/views/shared/users/sidebar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
</div>
</div>
<div class="card card-body mt-3">
<fieldset :disabled="{{ \Auth::user()->hasPermission('edit-user-and-password') || \Auth::user()->is_administrator ? 'false' : 'true' }}">
<legend>
<fieldset :disabled="{{ \Auth::user()->hasPermission('edit-user-and-password') || \Auth::user()->is_administrator ? 'false' : 'true' }}">
<legend>
<h5 class="mb-3 font-weight-bold">{{__('Login Information')}}</h5>
</legend>
<div class="form-group">
Expand Down Expand Up @@ -54,7 +54,8 @@
{!! Form::label('confPassword', __('Confirm Password')) !!}
{!! Form::password('confPassword', ['id' => 'confPassword', 'rows' => 4, 'class'=> 'form-control', 'v-model'
=> 'formData.confPassword', 'autocomplete' => 'new-password', 'v-bind:class' => '{\'form-control\':true, \'is-invalid\':errors.password}']) !!}
<div class="invalid-feedback" :style="{display: (errors.password) ? 'block' : 'none' }" role="alert" v-if="errors.password">@{{errors.password[0]}}</div>
<div class="invalid-feedback" :style="{display: (errors.password) ? 'block' : 'none' }" role="alert"
v-for="(error, index) in errors.password">@{{error}}</div>
</div>
@cannot('edit-user-and-password')
<div class="form-group">
Expand Down
9 changes: 5 additions & 4 deletions tests/Feature/Api/ChangePasswordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ public function testUserPasswordChangeWithInvalidPassword()
'confpassword' => 'ProcessMaker',
]);

$response->assertStatus(422)
->assertSeeText('The password must contain either a number or a special character');
$response->assertStatus(422);
$json = $response->json();
$this->assertTrue(in_array('The Password field must contain at least one symbol.', $json['errors']['password']));

// Validate updated user password changed
$updatedUser = User::where('id', $user->id)->first();
Expand All @@ -72,8 +73,8 @@ public function testUserChangePasswordMustSetFlagToFalse()

// Post data with new password
$response = $this->apiCall('PUT', self::API_TEST_URL, [
'password' => 'ProcessMaker1',
'confpassword' => 'ProcessMaker1',
'password' => 'ProcessMaker1_',
'confpassword' => 'ProcessMaker1_',
]);

// Validate the header status code
Expand Down
14 changes: 7 additions & 7 deletions tests/Feature/Api/UsersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getUpdatedData()
'timezone' => $faker->timezone(),
'status' => $faker->randomElement(['ACTIVE', 'INACTIVE']),
'birthdate' => $faker->dateTimeThisCentury()->format('Y-m-d'),
'password' => $faker->sentence(10),
'password' => $faker->sentence(8) . 'A_' . '1',
];
}

Expand Down Expand Up @@ -100,7 +100,7 @@ public function testCreateUser()
'lastname' => 'name',
'email' => $faker->email(),
'status' => $faker->randomElement(['ACTIVE', 'INACTIVE']),
'password' => $faker->sentence(10),
'password' => $faker->password(8) . 'A_' . '1',
]);

// Validate the header status code
Expand Down Expand Up @@ -602,9 +602,9 @@ public function testCreateWithoutPassword()
$response = $this->apiCall('POST', self::API_TEST_URL, $payload);
$response->assertStatus(422);
$json = $response->json();
$this->assertEquals('The Password field must be at least 8 characters.', $json['errors']['password'][0]);
$this->assertTrue(in_array('The Password field must be at least 8 characters.', $json['errors']['password']));

$payload['password'] = 'Abc12345';
$payload['password'] = 'Abc12345_';
$response = $this->apiCall('POST', self::API_TEST_URL, $payload);
$response->assertStatus(201);
$json = $response->json();
Expand All @@ -616,9 +616,9 @@ public function testCreateWithoutPassword()
$response = $this->apiCall('PUT', route('api.users.update', $userId), $payload);
$response->assertStatus(422);
$json = $response->json();
$this->assertEquals('The Password field must be at least 8 characters.', $json['errors']['password'][0]);
$this->assertTrue(in_array('The Password field must be at least 8 characters.', $json['errors']['password']));

$payload['password'] = 'Abc12345';
$payload['password'] = 'Abc12345_';
$response = $this->apiCall('PUT', route('api.users.update', $userId), $payload);
$response->assertStatus(204);

Expand Down Expand Up @@ -673,7 +673,7 @@ public function testCreateUserValidateUsername()
'lastname' => $faker->lastName(),
'email' => $faker->email(),
'status' => $faker->randomElement(['ACTIVE', 'INACTIVE']),
'password' => $faker->sentence(10),
'password' => $faker->sentence(8) . 'A_' . '1',
]);
// Validate the header status code
$response->assertStatus(201);
Expand Down