X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FAuth%2FDB.pm;h=93e5cc0b36d280fe497e6e1db7f3c72ea1aecec3;hb=53593baa211863fbf66540cf1bcc36c8fb37257f;hp=0168896f9f4ef2bba9c34f4547613324740f6a66;hpb=c6d0de695657e132931e883c08f1436974bc22f2;p=kivitendo-erp.git diff --git a/SL/Auth/DB.pm b/SL/Auth/DB.pm index 0168896f9..93e5cc0b3 100644 --- a/SL/Auth/DB.pm +++ b/SL/Auth/DB.pm @@ -3,8 +3,10 @@ package SL::Auth::DB; use strict; use Carp; +use Scalar::Util qw(weaken); use SL::Auth::Constants qw(:all); +use SL::Auth::Password; use SL::DBUtils; sub new { @@ -14,6 +16,7 @@ sub new { my $self = {}; $self->{auth} = shift; + weaken $self->{auth}; bless $self, $type; @@ -22,6 +25,10 @@ sub new { return $self; } +sub reset { + # nothing to do here +} + sub authenticate { $main::lxdebug->enter_sub(); @@ -29,33 +36,27 @@ 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 = $self->hash_password(password => $stored_password) unless $stored_password; - ($algorithm, $stored_password) = $self->parse_password_entry($stored_password); - ($algorithm2, $password) = $self->parse_password_entry($self->hash_password(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 { return 1; } +sub requires_cleartext_password { + return 0; +} + sub change_password { $main::lxdebug->enter_sub(); @@ -71,7 +72,7 @@ sub change_password { return ERR_BACKEND; } - $password = $self->hash_password(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); @@ -86,42 +87,4 @@ sub verify_config { return 1; } -sub hash_password { - my ($self, %params) = @_; - - if (!$params{algorithm}) { - $params{algorithm} = 'SHA1'; - $params{fallback_algorithm} = 'MD5'; - } - - if ($params{algorithm} eq 'SHA1') { - if (eval { require Digest::SHA1; 1 }) { - return '{SHA1}' . Digest::SHA1::sha1_hex($params{password}); - - } elsif ($params{fallback_algorithm}) { - return $self->hash_password(%params, algorithm => $params{fallback_algorithm}); - - } else { - die 'Digest::SHA1 not available'; - } - - } elsif ($params{algorithm} eq 'MD5') { - require Digest::MD5; - return '{MD5}' . Digest::MD5::md5_hex($params{password}); - - } elsif ($params{algorithm} eq 'CRYPT') { - return '{CRYPT}' . crypt($params{password}, substr($params{login}, 0, 2)); - - } else { - croak 'Unsupported hash algorithm ' . $params{algorithm}; - } -} - -sub parse_password_entry { - my ($self, $password) = @_; - - return ($1, $2) if $password =~ m/^\{ ([^\}]+) \} (.+)/x; - return ('CRYPT', $password); -} - 1;