Bei Passwortänderung Login für Salzen übergeben
[kivitendo-erp.git] / SL / Auth / DB.pm
1 package SL::Auth::DB;
2
3 use strict;
4
5 use Carp;
6 use Scalar::Util qw(weaken);
7
8 use SL::Auth::Constants qw(:all);
9 use SL::Auth::Password;
10 use SL::DBUtils;
11
12 sub new {
13   $main::lxdebug->enter_sub();
14
15   my $type = shift;
16   my $self = {};
17
18   $self->{auth} = shift;
19   weaken $self->{auth};
20
21   bless $self, $type;
22
23   $main::lxdebug->leave_sub();
24
25   return $self;
26 }
27
28 sub authenticate {
29   $main::lxdebug->enter_sub();
30
31   my $self       = shift;
32   my $login      = shift;
33   my $password   = shift;
34
35   my $stored_password = $self->{auth}->get_stored_password($login);
36
37   my ($algorithm, $algorithm2);
38
39   # Empty password hashes in the database mean just that -- empty
40   # passwords. Hash it for easier comparison.
41   $stored_password               = SL::Auth::Password->hash(password => $stored_password) unless $stored_password;
42   ($algorithm, $stored_password) = SL::Auth::Password->parse($stored_password);
43   ($algorithm2, $password)       = SL::Auth::Password->parse(SL::Auth::Password->hash_if_unhashed(password => $password, algorithm => $algorithm, login => $login));
44
45   $main::lxdebug->leave_sub();
46
47   return $password eq $stored_password ? OK : ERR_PASSWORD;
48 }
49
50 sub can_change_password {
51   return 1;
52 }
53
54 sub requires_cleartext_password {
55   return 0;
56 }
57
58 sub change_password {
59   $main::lxdebug->enter_sub();
60
61   my $self       = shift;
62   my $login      = shift;
63   my $password   = shift;
64   my $is_crypted = shift;
65
66   my $dbh        = $self->{auth}->dbconnect();
67
68   if (!$dbh) {
69     $main::lxdebug->leave_sub();
70     return ERR_BACKEND;
71   }
72
73   $password = SL::Auth::Password->hash(login => $login, password => $password) unless $is_crypted;
74
75   do_query($main::form, $dbh, qq|UPDATE auth."user" SET password = ? WHERE login = ?|, $password, $login);
76
77   $dbh->commit();
78
79   $main::lxdebug->leave_sub();
80
81   return 1;
82 }
83
84 sub verify_config {
85   return 1;
86 }
87
88 1;