From 07c9264b7ad71630cc10e37aa2a2736981e09173 Mon Sep 17 00:00:00 2001 From: Lari Taskula Date: Tue, 22 Oct 2019 10:36:35 +0000 Subject: [PATCH] Bug 23865: Throw exceptions in Koha::AuthUtils::is_valid_password Exceptions are currently thrown in Koha::Patron->set_password. We need to validate password (with exceptions) without wanting to set the password. To test: 1. Make sure is_valid_password is not used anywhere in codebase apart from Koha::Patron::set_password (grep -rn 'is_valid_password') 2. prove t/db_dependent/Koha/Patrons.t Observe ok 35 - ->set_password --- Koha/AuthUtils.pm | 13 ++++++++++--- Koha/Patron.pm | 17 ----------------- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/Koha/AuthUtils.pm b/Koha/AuthUtils.pm index d834536357..6ec034415e 100644 --- a/Koha/AuthUtils.pm +++ b/Koha/AuthUtils.pm @@ -151,13 +151,20 @@ sub is_password_valid { my $minPasswordLength = C4::Context->preference('minPasswordLength'); $minPasswordLength = 3 if not $minPasswordLength or $minPasswordLength < 3; if ( length($password) < $minPasswordLength ) { - return ( 0, 'too_short' ); + my $min_length = C4::Context->preference('minPasswordLength'); + $min_length = 3 if not $min_length or $min_length < 3; + + my $password_length = length($password); + Koha::Exceptions::Password::TooShort->throw( + length => $password_length, min_length => $min_length + ); } elsif ( C4::Context->preference('RequireStrongPassword') ) { - return ( 0, 'too_weak' ) + Koha::Exceptions::Password::TooWeak->throw() if $password !~ m|(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{$minPasswordLength,}|; } - return ( 0, 'has_whitespaces' ) if $password =~ m[^\s|\s$]; + Koha::Exceptions::Password::WhitespaceCharacters->throw() + if $password =~ m[^\s|\s$]; return ( 1, undef ); } diff --git a/Koha/Patron.pm b/Koha/Patron.pm index fca796ff28..12d123bee0 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -693,23 +693,6 @@ sub set_password { unless ( $args->{skip_validation} ) { my ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $password ); - - if ( !$is_valid ) { - if ( $error eq 'too_short' ) { - my $min_length = C4::Context->preference('minPasswordLength'); - $min_length = 3 if not $min_length or $min_length < 3; - - my $password_length = length($password); - Koha::Exceptions::Password::TooShort->throw( - length => $password_length, min_length => $min_length ); - } - elsif ( $error eq 'has_whitespaces' ) { - Koha::Exceptions::Password::WhitespaceCharacters->throw(); - } - elsif ( $error eq 'too_weak' ) { - Koha::Exceptions::Password::TooWeak->throw(); - } - } } my $digest = Koha::AuthUtils::hash_password($password); -- 2.17.1