Passwort-Hashing in eigenes Modul ausgelagert
[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(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 change_password {
63   $main::lxdebug->enter_sub();
64
65   my $self       = shift;
66   my $login      = shift;
67   my $password   = shift;
68   my $is_crypted = shift;
69
70   my $dbh        = $self->{auth}->dbconnect();
71
72   if (!$dbh) {
73     $main::lxdebug->leave_sub();
74     return ERR_BACKEND;
75   }
76
77   $password = SL::Auth::Password->hash(password => $password) unless $is_crypted;
78
79   do_query($main::form, $dbh, qq|UPDATE auth."user" SET password = ? WHERE login = ?|, $password, $login);
80
81   $dbh->commit();
82
83   $main::lxdebug->leave_sub();
84
85   return 1;
86 }
87
88 sub verify_config {
89   return 1;
90 }
91
92 1;