Das Benutzer-Passwort nicht im Klartext in Session-Tabelle ablegen
[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 $dbh        = $self->{auth}->dbconnect();
36
37   if (!$dbh) {
38     $main::lxdebug->leave_sub();
39     return ERR_BACKEND;
40   }
41
42   my $query             = qq|SELECT password FROM auth."user" WHERE login = ?|;
43   my ($stored_password) = $dbh->selectrow_array($query, undef, $login);
44
45   my ($algorithm, $algorithm2);
46
47   # Empty password hashes in the database mean just that -- empty
48   # passwords. Hash it for easier comparison.
49   $stored_password               = SL::Auth::Password->hash(password => $stored_password) unless $stored_password;
50   ($algorithm, $stored_password) = SL::Auth::Password->parse($stored_password);
51   ($algorithm2, $password)       = SL::Auth::Password->parse(SL::Auth::Password->hash_if_unhashed(password => $password, algorithm => $algorithm, login => $login));
52
53   $main::lxdebug->leave_sub();
54
55   return $password eq $stored_password ? OK : ERR_PASSWORD;
56 }
57
58 sub can_change_password {
59   return 1;
60 }
61
62 sub requires_cleartext_password {
63   return 0;
64 }
65
66 sub change_password {
67   $main::lxdebug->enter_sub();
68
69   my $self       = shift;
70   my $login      = shift;
71   my $password   = shift;
72   my $is_crypted = shift;
73
74   my $dbh        = $self->{auth}->dbconnect();
75
76   if (!$dbh) {
77     $main::lxdebug->leave_sub();
78     return ERR_BACKEND;
79   }
80
81   $password = SL::Auth::Password->hash(password => $password) unless $is_crypted;
82
83   do_query($main::form, $dbh, qq|UPDATE auth."user" SET password = ? WHERE login = ?|, $password, $login);
84
85   $dbh->commit();
86
87   $main::lxdebug->leave_sub();
88
89   return 1;
90 }
91
92 sub verify_config {
93   return 1;
94 }
95
96 1;