From ad4e22cedb94aa39b708d11bf7297276697fdf9b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gerg=C5=91=20Tisza?= Date: Mon, 18 Feb 2019 20:19:13 -0800 Subject: [PATCH] Fix password policy handling in temporary password provider Fix breakage caused by f15ecc60cd94. Also use correct check name. Bug: T216196 Change-Id: Id2567adf8334742ef18a59a6c7e74b2b780ab43a --- ...TemporaryPasswordAuthenticationRequest.php | 7 ++---- ...oraryPasswordAuthenticationRequestTest.php | 24 +++++++++++++++---- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/includes/auth/TemporaryPasswordAuthenticationRequest.php b/includes/auth/TemporaryPasswordAuthenticationRequest.php index bc7c779d90..1c87ea9ea2 100644 --- a/includes/auth/TemporaryPasswordAuthenticationRequest.php +++ b/includes/auth/TemporaryPasswordAuthenticationRequest.php @@ -69,11 +69,8 @@ class TemporaryPasswordAuthenticationRequest extends AuthenticationRequest { $minLength = $config->get( 'MinimalPasswordLength' ); $policy = $config->get( 'PasswordPolicy' ); foreach ( $policy['policies'] as $p ) { - if ( isset( $p['MinimalPasswordLength'] ) ) { - $minLength = max( $minLength, $p['MinimalPasswordLength'] ); - } - if ( isset( $p['MinimalPasswordLengthToLogin'] ) ) { - $minLength = max( $minLength, $p['MinimalPasswordLengthToLogin'] ); + foreach ( [ 'MinimalPasswordLength', 'MinimumPasswordLengthToLogin' ] as $check ) { + $minLength = max( $minLength, $p[$check]['value'] ?? $p[$check] ?? 0 ); } } diff --git a/tests/phpunit/includes/auth/TemporaryPasswordAuthenticationRequestTest.php b/tests/phpunit/includes/auth/TemporaryPasswordAuthenticationRequestTest.php index dc4ab6f497..1ee4a039d9 100644 --- a/tests/phpunit/includes/auth/TemporaryPasswordAuthenticationRequestTest.php +++ b/tests/phpunit/includes/auth/TemporaryPasswordAuthenticationRequestTest.php @@ -26,18 +26,32 @@ class TemporaryPasswordAuthenticationRequestTest extends AuthenticationRequestTe global $wgPasswordPolicy; $policy = $wgPasswordPolicy; - $policy['policies']['default'] += [ + unset( $policy['policies'] ); + $policy['policies']['default'] = [ 'MinimalPasswordLength' => 1, - 'MinimalPasswordLengthToLogin' => 1, + 'MinimumPasswordLengthToLogin' => 1, ]; - $this->setMwGlobals( 'wgPasswordPolicy', $policy ); + $this->setMwGlobals( [ + 'wgMinimalPasswordLength' => 10, + 'wgPasswordPolicy' => $policy, + ] ); $ret1 = TemporaryPasswordAuthenticationRequest::newRandom(); $ret2 = TemporaryPasswordAuthenticationRequest::newRandom(); - $this->assertNotSame( '', $ret1->password ); - $this->assertNotSame( '', $ret2->password ); + $this->assertEquals( 10, strlen( $ret1->password ) ); + $this->assertEquals( 10, strlen( $ret2->password ) ); $this->assertNotSame( $ret1->password, $ret2->password ); + + $policy['policies']['default']['MinimalPasswordLength'] = 15; + $this->setMwGlobals( 'wgPasswordPolicy', $policy ); + $ret = TemporaryPasswordAuthenticationRequest::newRandom(); + $this->assertEquals( 15, strlen( $ret->password ) ); + + $policy['policies']['default']['MinimalPasswordLength'] = [ 'value' => 20 ]; + $this->setMwGlobals( 'wgPasswordPolicy', $policy ); + $ret = TemporaryPasswordAuthenticationRequest::newRandom(); + $this->assertEquals( 20, strlen( $ret->password ) ); } public function testNewInvalid() { -- 2.20.1