line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*- |
2
|
|
|
|
|
|
|
# vim:ts=8:sw=2:et:sta:sts=2 |
3
|
|
|
|
|
|
|
######### |
4
|
|
|
|
|
|
|
# Author: rmp |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
package ClearPress::authenticator::db; |
7
|
2
|
|
|
2
|
|
38368
|
use strict; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
62
|
|
8
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
57
|
|
9
|
2
|
|
|
2
|
|
11
|
use base qw(ClearPress::authenticator Class::Accessor); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
886
|
|
10
|
2
|
|
|
2
|
|
14
|
use Readonly; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
92
|
|
11
|
2
|
|
|
2
|
|
13
|
use Carp; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
107
|
|
12
|
2
|
|
|
2
|
|
14
|
use English qw(-no_match_vars); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
12
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = q[477.1.2]; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw(dbh)); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $SUPPORTED_CIPHERS = { |
19
|
|
|
|
|
|
|
mysql => sub { my ($self, $str) = @_; $self->dyn_use('Crypt::MySQL'); return Crypt::MySQL::password($str); }, |
20
|
|
|
|
|
|
|
mysql41 => sub { my ($self, $str) = @_; $self->dyn_use('Crypt::MySQL'); return Crypt::MySQL::password41($str); }, |
21
|
|
|
|
|
|
|
sha1 => sub { my ($self, $str) = @_; $self->dyn_use('Digest::SHA'); return Digest::SHA::sha1_hex($str); }, |
22
|
|
|
|
|
|
|
sha128 => sub { my ($self, $str) = @_; $self->dyn_use('Digest::SHA'); return Digest::SHA::sha128_hex($str); }, |
23
|
|
|
|
|
|
|
sha256 => sub { my ($self, $str) = @_; $self->dyn_use('Digest::SHA'); return Digest::SHA::sha256_hex($str); }, |
24
|
|
|
|
|
|
|
sha384 => sub { my ($self, $str) = @_; $self->dyn_use('Digest::SHA'); return Digest::SHA::sha384_hex($str); }, |
25
|
|
|
|
|
|
|
sha512 => sub { my ($self, $str) = @_; $self->dyn_use('Digest::SHA'); return Digest::SHA::sha512_hex($str); }, |
26
|
|
|
|
|
|
|
md5 => sub { my ($self, $str) = @_; $self->dyn_use('Digest::MD5'); return Digest::MD5::md5_hex($str); }, |
27
|
|
|
|
|
|
|
}; |
28
|
|
|
|
|
|
|
Readonly::Scalar our $DEFAULT_TABLE => 'user'; |
29
|
|
|
|
|
|
|
Readonly::Scalar our $DEFAULT_USERNAME_FIELD => 'username'; |
30
|
|
|
|
|
|
|
Readonly::Scalar our $DEFAULT_PASSWORD_FIELD => 'pass'; |
31
|
|
|
|
|
|
|
Readonly::Scalar our $DEFAULT_CIPHER => 'sha1'; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub table { |
34
|
3
|
|
|
3
|
1
|
7
|
my ($self, $v) = @_; |
35
|
|
|
|
|
|
|
|
36
|
3
|
50
|
|
|
|
7
|
if($v) { |
37
|
0
|
|
|
|
|
0
|
$self->{table} = $v; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
3
|
50
|
|
|
|
7
|
if($self->{table}) { |
41
|
0
|
|
|
|
|
0
|
return $self->{table}; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
3
|
|
|
|
|
8
|
return $DEFAULT_TABLE; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub username_field { |
48
|
3
|
|
|
3
|
1
|
6
|
my ($self, $v) = @_; |
49
|
|
|
|
|
|
|
|
50
|
3
|
50
|
|
|
|
7
|
if($v) { |
51
|
0
|
|
|
|
|
0
|
$self->{username_field} = $v; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
3
|
50
|
|
|
|
8
|
if($self->{username_field}) { |
55
|
0
|
|
|
|
|
0
|
return $self->{username_field}; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
3
|
|
|
|
|
5
|
return $DEFAULT_USERNAME_FIELD; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub password_field { |
62
|
3
|
|
|
3
|
1
|
4
|
my ($self, $v) = @_; |
63
|
|
|
|
|
|
|
|
64
|
3
|
50
|
|
|
|
7
|
if($v) { |
65
|
0
|
|
|
|
|
0
|
$self->{password_field} = $v; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
3
|
50
|
|
|
|
6
|
if($self->{password_field}) { |
69
|
0
|
|
|
|
|
0
|
return $self->{password_field}; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
3
|
|
|
|
|
6
|
return $DEFAULT_PASSWORD_FIELD; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub cipher { |
76
|
3
|
|
|
3
|
1
|
8
|
my ($self, $v) = @_; |
77
|
|
|
|
|
|
|
|
78
|
3
|
50
|
|
|
|
9
|
if($v) { |
79
|
0
|
|
|
|
|
0
|
$self->{cipher} = $v; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
3
|
50
|
|
|
|
7
|
if($self->{cipher}) { |
83
|
0
|
|
|
|
|
0
|
return $self->{cipher}; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
3
|
|
|
|
|
7
|
return $DEFAULT_CIPHER; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub authen_credentials { |
90
|
6
|
|
|
6
|
1
|
62
|
my ($self, $ref) = @_; |
91
|
|
|
|
|
|
|
|
92
|
6
|
100
|
100
|
|
|
57
|
if(!$ref || |
|
|
|
100
|
|
|
|
|
93
|
|
|
|
|
|
|
!$ref->{username} || |
94
|
|
|
|
|
|
|
!$ref->{password} ) { |
95
|
3
|
|
|
|
|
21
|
return; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
3
|
|
|
|
|
12
|
my $dbh = $self->dbh(); |
99
|
3
|
|
|
|
|
56
|
my $table = $self->table; |
100
|
3
|
|
|
|
|
7
|
my $user_f = $self->username_field; |
101
|
3
|
|
|
|
|
9
|
my $pass_f = $self->password_field; |
102
|
3
|
|
|
|
|
22
|
my $c_type = $self->cipher; |
103
|
3
|
|
|
|
|
7
|
my $cipher = $SUPPORTED_CIPHERS->{$c_type}; |
104
|
|
|
|
|
|
|
|
105
|
3
|
50
|
|
|
|
8
|
if(!$cipher) { |
106
|
0
|
|
|
|
|
0
|
croak qq[Unsupported cipher: $c_type]; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
3
|
|
|
|
|
9
|
my $digest = $cipher->($self, $ref->{password}); |
110
|
3
|
|
|
|
|
18
|
my $query = qq[SELECT $user_f FROM $table WHERE $user_f=? AND $pass_f=?]; |
111
|
3
|
|
|
|
|
40
|
my $results = $dbh->selectall_arrayref($query, {}, $ref->{username}, $digest); |
112
|
|
|
|
|
|
|
|
113
|
3
|
100
|
|
|
|
371
|
if(!scalar @{$results}) { |
|
3
|
|
|
|
|
12
|
|
114
|
2
|
|
|
|
|
12
|
return; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
1
|
|
|
|
|
5
|
return $ref; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
1; |
121
|
|
|
|
|
|
|
__END__ |