7 use SL::Auth::Constants qw(:all);
11 $main::lxdebug->enter_sub();
16 $self->{auth} = shift;
20 $main::lxdebug->leave_sub();
26 $main::lxdebug->enter_sub();
32 my $dbh = $self->{auth}->dbconnect();
35 $main::lxdebug->leave_sub();
39 my $query = qq|SELECT password FROM auth."user" WHERE login = ?|;
40 my ($stored_password) = $dbh->selectrow_array($query, undef, $login);
42 my ($algorithm, $algorithm2);
44 # Empty password hashes in the database mean just that -- empty
45 # passwords. Hash it for easier comparison.
46 $stored_password = $self->hash_password(password => $stored_password) unless $stored_password;
47 ($algorithm, $stored_password) = $self->parse_password_entry($stored_password);
48 ($algorithm2, $password) = $self->parse_password_entry($self->hash_password(password => $password, algorithm => $algorithm, login => $login));
50 $main::lxdebug->leave_sub();
52 return $password eq $stored_password ? OK : ERR_PASSWORD;
55 sub can_change_password {
60 $main::lxdebug->enter_sub();
65 my $is_crypted = shift;
67 my $dbh = $self->{auth}->dbconnect();
70 $main::lxdebug->leave_sub();
74 $password = $self->hash_password(password => $password) unless $is_crypted;
76 do_query($main::form, $dbh, qq|UPDATE auth."user" SET password = ? WHERE login = ?|, $password, $login);
80 $main::lxdebug->leave_sub();
90 my ($self, %params) = @_;
92 if (!$params{algorithm}) {
93 $params{algorithm} = 'SHA1';
94 $params{fallback_algorithm} = 'MD5';
97 if ($params{algorithm} eq 'SHA1') {
98 if (eval { require Digest::SHA1; 1 }) {
99 return '{SHA1}' . Digest::SHA1::sha1_hex($params{password});
101 } elsif ($params{fallback_algorithm}) {
102 return $self->hash_password(%params, algorithm => $params{fallback_algorithm});
105 die 'Digest::SHA1 not available';
108 } elsif ($params{algorithm} eq 'MD5') {
110 return '{MD5}' . Digest::MD5::md5_hex($params{password});
112 } elsif ($params{algorithm} eq 'CRYPT') {
113 return '{CRYPT}' . crypt($params{password}, substr($params{login}, 0, 2));
116 croak 'Unsupported hash algorithm ' . $params{algorithm};
120 sub parse_password_entry {
121 my ($self, $password) = @_;
123 return ($1, $2) if $password =~ m/^\{ ([^\}]+) \} (.+)/x;
124 return ('CRYPT', $password);