blib/lib/ClearPress/authenticator/db.pm

filter: Statementstmt 67.3% 62 / 92 Branchbran 59.0% 13 / 22 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 63.1% 12 / 19 Podpod 100% 5 / 5 totaltotal 68.7% 101 / 147 CC 14 SCAR 11.4
CC
14
Cov
68%
authen_credentials
6.0
table
3.6
username_field
3.6
CRAP 3.1
« blib/lib/ClearPress/authenticator/ldap.pm ↑ Summary blib/lib/ClearPress/driver.pm »
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 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use base qw(ClearPress::authenticator Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 our $VERSION = q[2026.07.25];
15  
16 3 __PACKAGE__->mk_accessors(qw(dbh));
17  
18 our $SUPPORTED_CIPHERS = {
19 0 ▶ mysql => sub { my ($self, $str) = @_; $self->dyn_use('Crypt::MySQL'); return Crypt::MySQL::password($str); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
20 0 ▶ mysql41 => sub { my ($self, $str) = @_; $self->dyn_use('Crypt::MySQL'); return Crypt::MySQL::password41($str); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 3 ▶ sha1 => sub { my ($self, $str) = @_; $self->dyn_use('Digest::SHA'); return Digest::SHA::sha1_hex($str); },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 0 ▶ sha128 => sub { my ($self, $str) = @_; $self->dyn_use('Digest::SHA'); return Digest::SHA::sha128_hex($str); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 0 ▶ sha256 => sub { my ($self, $str) = @_; $self->dyn_use('Digest::SHA'); return Digest::SHA::sha256_hex($str); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 0 ▶ sha384 => sub { my ($self, $str) = @_; $self->dyn_use('Digest::SHA'); return Digest::SHA::sha384_hex($str); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 0 ▶ sha512 => sub { my ($self, $str) = @_; $self->dyn_use('Digest::SHA'); return Digest::SHA::sha512_hex($str); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 0 ▶ md5 => sub { my ($self, $str) = @_; $self->dyn_use('Digest::MD5'); return Digest::MD5::md5_hex($str); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 3 };
28 3 Readonly::Scalar our $DEFAULT_TABLE => 'user';
29 3 Readonly::Scalar our $DEFAULT_USERNAME_FIELD => 'username';
30 3 Readonly::Scalar our $DEFAULT_PASSWORD_FIELD => 'pass';
31 3 Readonly::Scalar our $DEFAULT_CIPHER => 'sha1';
32  
33 3 ▶sub table {
Subroutine table: called CC=3 SCAR=12.7
Pod documented
34 3 my ($self, $v) = @_;
35  
36 3 ▶ if($v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v) 0 3 3
37 0 $self->{table} = $v;
38 }
39  
40 3 ▶ if($self->{table}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"table"}) 0 3 3
41 0 return $self->{table};
42 }
43  
44 3 return $DEFAULT_TABLE;
45 }
46  
47 3 ▶sub username_field {
Subroutine username_field: called CC=3 SCAR=12.7
Pod documented
48 3 my ($self, $v) = @_;
49  
50 3 ▶ if($v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v) 0 3 3
51 0 $self->{username_field} = $v;
52 }
53  
54 3 ▶ if($self->{username_field}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"username_field"}) 0 3 3
55 0 return $self->{username_field};
56 }
57  
58 3 return $DEFAULT_USERNAME_FIELD;
59 }
60  
61 3 ▶sub password_field {
Subroutine password_field: called CC=3 SCAR=12.7
Pod documented
62 3 my ($self, $v) = @_;
63  
64 3 ▶ if($v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v) 0 3 3
65 0 $self->{password_field} = $v;
66 }
67  
68 3 ▶ if($self->{password_field}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"password_field"}) 0 3 3
69 0 return $self->{password_field};
70 }
71  
72 3 return $DEFAULT_PASSWORD_FIELD;
73 }
74  
75 3 ▶sub cipher {
Subroutine cipher: called CC=3 SCAR=12.7
Pod documented
76 3 my ($self, $v) = @_;
77  
78 3 ▶ if($v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v) 0 3 3
79 0 $self->{cipher} = $v;
80 }
81  
82 3 ▶ if($self->{cipher}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cipher"}) 0 3 3
83 0 return $self->{cipher};
84 }
85  
86 3 return $DEFAULT_CIPHER;
87 }
88  
89 6 ▶sub authen_credentials {
Subroutine authen_credentials: called CC=6 SCAR=17.9
Pod documented
90 6 my ($self, $ref) = @_;
91  
92 6 ▶ if(!$ref ||
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref and $ref->{"username"} and $ref->{"password"}) 3 3 6
Condition100%6/6
$ref and $ref->{"username"} and $ref->{"password"}
!ll&&!rl&&r
213
$ref and $ref->{"username"}
!ll&&!rl&&r
114
MC/DC100%3/3
$ref and $ref->{"username"} and $ref->{"password"}
$ref $ref->{"username"} $ref->{"password"}
Truth table100%4/4
A and B and C
A= $refB= $ref->{"username"}C= $ref->{"password"}
ABCresult
0XX0
10X0
1100
1111
93 !$ref->{username} ||
94 !$ref->{password} ) {
95 3 return;
96 }
97  
98 3 my $dbh = $self->dbh();
99 3 my $table = $self->table;
100 3 my $user_f = $self->username_field;
101 3 my $pass_f = $self->password_field;
102 3 my $c_type = $self->cipher;
103 3 my $cipher = $SUPPORTED_CIPHERS->{$c_type};
104  
105 3 ▶ if(!$cipher) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cipher) 0 3 3
106 0 croak qq[Unsupported cipher: $c_type];
107 }
108  
109 3 my $digest = $cipher->($self, $ref->{password});
110 3 my $query = qq[SELECT $user_f FROM $table WHERE $user_f=? AND $pass_f=?];
111 3 my $results = $dbh->selectall_arrayref($query, {}, $ref->{username}, $digest);
112  
113 3 ▶ if(!scalar @{$results}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$results;}) 2 1 3
114 2 return;
115 }
116  
117 1 return $ref;
118 }
119  
120 3 1;
121 __END__
« blib/lib/ClearPress/authenticator/ldap.pm ↑ Summary blib/lib/ClearPress/driver.pm »