Merge branch 'b-3.6.1' of ../kivitendo-erp_20220811
[kivitendo-erp.git] / SL / Auth / DB.pm
index 66d23cd..93e5cc0 100644 (file)
@@ -25,6 +25,10 @@ sub new {
   return $self;
 }
 
+sub reset {
+  # nothing to do here
+}
+
 sub authenticate {
   $main::lxdebug->enter_sub();
 
@@ -32,27 +36,17 @@ sub authenticate {
   my $login      = shift;
   my $password   = shift;
 
-  my $dbh        = $self->{auth}->dbconnect();
-
-  if (!$dbh) {
-    $main::lxdebug->leave_sub();
-    return ERR_BACKEND;
-  }
-
-  my $query             = qq|SELECT password FROM auth."user" WHERE login = ?|;
-  my ($stored_password) = $dbh->selectrow_array($query, undef, $login);
-
-  my ($algorithm, $algorithm2);
+  my $stored_password = $self->{auth}->get_stored_password($login);
 
   # Empty password hashes in the database mean just that -- empty
   # passwords. Hash it for easier comparison.
-  $stored_password               = SL::Auth::Password->hash(password => $stored_password) unless $stored_password;
-  ($algorithm, $stored_password) = SL::Auth::Password->parse($stored_password);
-  ($algorithm2, $password)       = SL::Auth::Password->parse(SL::Auth::Password->hash_if_unhashed(password => $password, algorithm => $algorithm, login => $login));
+  $stored_password    = SL::Auth::Password->hash(password => $stored_password) unless $stored_password;
+  my ($algorithm)     = SL::Auth::Password->parse($stored_password);
+  my $hashed_password = SL::Auth::Password->hash(password => $password, algorithm => $algorithm, login => $login, stored_password => $stored_password);
 
   $main::lxdebug->leave_sub();
 
-  return $password eq $stored_password ? OK : ERR_PASSWORD;
+  return $hashed_password eq $stored_password ? OK : ERR_PASSWORD;
 }
 
 sub can_change_password {
@@ -78,7 +72,7 @@ sub change_password {
     return ERR_BACKEND;
   }
 
-  $password = SL::Auth::Password->hash(password => $password) unless $is_crypted;
+  $password = SL::Auth::Password->hash(login => $login, password => $password) unless $is_crypted;
 
   do_query($main::form, $dbh, qq|UPDATE auth."user" SET password = ? WHERE login = ?|, $password, $login);