From 66667cf54e29f4b9a0a72744b658a5cb6de79509 Mon Sep 17 00:00:00 2001 From: Arthur Suzuki Date: Sun, 5 Jul 2020 23:03:43 +0200 Subject: [PATCH] Bug 23011: QA follow-up --- C4/ILSDI/Services.pm | 5 ++--- Koha/AuthUtils.pm | 17 +++++++++++++++++ t/db_dependent/ILSDI_Services.t | 6 +++++- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/C4/ILSDI/Services.pm b/C4/ILSDI/Services.pm index 42c036cc3c..e97a2f516d 100644 --- a/C4/ILSDI/Services.pm +++ b/C4/ILSDI/Services.pm @@ -380,9 +380,8 @@ sub AuthenticatePatron { unless( $is_valid ) { $response->{code} = 'PatronPasswordNotSecure'; $response->{securePasswordLabel} = $error; - my $minPasswordLength = C4::Context->preference('minPasswordLength'); - $response->{securePasswordPattern} = '.{'.$minPasswordLength.'}' if( $error eq 'too_short'); - $response->{securePasswordPattern} = '(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{'.$minPasswordLength.'}' if( $error eq 'too_weak'); + my $minPasswordLength = C4::Context->preference('minPasswordLength') // 3; + $response->{securePasswordPattern} = Koha::AuthUtils::password_policy(); } return $response; diff --git a/Koha/AuthUtils.pm b/Koha/AuthUtils.pm index 7c4dcd1002..e41e570053 100644 --- a/Koha/AuthUtils.pm +++ b/Koha/AuthUtils.pm @@ -161,6 +161,23 @@ sub is_password_valid { return ( 1, undef ); } +=head2 password_policy + +my ( $regex) = password_policy(); + +return a regex matching the password policy in use + +=cut + +sub password_policy { + my $minPasswordLength = C4::Context->preference('minPasswordLength'); + $minPasswordLength = 3 if not $minPasswordLength or $minPasswordLength < 3; + if ( C4::Context->preference('RequireStrongPassword') ) { + return '(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{'.$minPasswordLength.'}'; + } + return '.{'.$minPasswordLength.'}'; +} + =head2 generate_password my password = generate_password(); diff --git a/t/db_dependent/ILSDI_Services.t b/t/db_dependent/ILSDI_Services.t index 4519a673d5..8d18a80414 100644 --- a/t/db_dependent/ILSDI_Services.t +++ b/t/db_dependent/ILSDI_Services.t @@ -19,7 +19,7 @@ use Modern::Perl; use CGI qw ( -utf8 ); -use Test::More tests => 10; +use Test::More tests => 11; use Test::MockModule; use t::lib::Mocks; use t::lib::TestBuilder; @@ -44,6 +44,8 @@ subtest 'AuthenticatePatron test' => sub { $schema->storage->txn_begin; my $plain_password = 'tomasito'; + t::lib::Mocks::mock_preference( 'minPasswordLength', 0 ); + t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 ); $builder->build({ source => 'Borrower', @@ -111,6 +113,8 @@ subtest 'AuthenticatePatron Secure Password test' => sub { $schema->storage->txn_begin; my $plain_password = 'tomasito'; + t::lib::Mocks::mock_preference( 'minPasswordLength', 0 ); + t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 ); $builder->build({ source => 'Borrower', -- 2.11.0