LDAP.pm

filter: Statementstmt 35.0% 21 / 60 Branchbran 0.0% 0 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 6 / 7 Podpod 100% 1 / 1 totaltotal 28.0% 28 / 100 CC 17 SCAR 54.2
CC
17
Cov
23%
check
306.0
BEGIN
1.0
BEGIN
1.0
CRAP 226.4
↑ Summary ActiveDirectory.pm »
1 package Authen::Simple::LDAP;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base 'Authen::Simple::Adapter';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Net::LDAP qw[];
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Net::LDAP::Constant qw[LDAP_INVALID_CREDENTIALS];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Params::Validate qw[];
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = 0.3;
12  
13 1 __PACKAGE__->options({
14 host => {
15 type => Params::Validate::SCALAR | Params::Validate::ARRAYREF,
16 default => 'localhost',
17 optional => 1
18 },
19 port => {
20 type => Params::Validate::SCALAR,
21 default => 389,
22 optional => 1
23 },
24 timeout => {
25 type => Params::Validate::SCALAR,
26 default => 60,
27 optional => 1
28 },
29 version => {
30 type => Params::Validate::SCALAR,
31 default => 3,
32 optional => 1
33 },
34 binddn => {
35 type => Params::Validate::SCALAR,
36 depends => [ 'bindpw' ],
37 optional => 1
38 },
39 bindpw => {
40 type => Params::Validate::SCALAR,
41 depends => [ 'binddn' ],
42 optional => 1
43 },
44 basedn => {
45 type => Params::Validate::SCALAR,
46 optional => 1
47 },
48 scope => {
49 type => Params::Validate::SCALAR,
50 default => 'sub',
51 optional => 1
52 },
53 filter => {
54 type => Params::Validate::SCALAR,
55 default => '(uid=%s)',
56 optional => 1
57 }
58 });
59  
60 0 ▶sub check {
Subroutine check: not called CC=17 SCAR=57.2
Pod documented
61 0 my ( $self, $username, $password ) = @_;
62  
63 0 my $connection = Net::LDAP->new( $self->host,
64 port => $self->port,
65 timeout => $self->timeout,
66 version => $self->version
67 );
68  
69 0 ▶ unless ( defined $connection ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $connection) 0 0 0
70  
71 0 my $host = $self->host;
72  
73 0 ▶ $self->log->error( qq/Failed to connect to '$host'. Reason: '$@'/ )
Branch: 1 branch
BranchTrueFalseTotal
if $self->log 0 0 0
74 if $self->log;
75  
76 0 return 0;
77 }
78  
79 0 my ( @credentials, $message, $search, $entry, $filter, $dn );
80  
81 0 ▶ @credentials = $self->binddn ? ( $self->binddn, password => $self->bindpw ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->binddn ? : 0 0 0
82 0 $message = $connection->bind(@credentials);
83  
84 0 ▶ if ( $message->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message->is_error) 0 0 0
85  
86 0 my $error = $message->error;
87 0 my $binddn = $self->binddn;
88 0 ▶ my $bind = $binddn ? qq/with dn '$binddn'/ : "Anonymously";
Branch: 1 branch
BranchTrueFalseTotal
$binddn ? : 0 0 0
89  
90 0 ▶ $self->log->error( qq/Failed to bind $bind. Reason: '$error'/ )
Branch: 1 branch
BranchTrueFalseTotal
if $self->log 0 0 0
91 if $self->log;
92  
93 0 return 0;
94 }
95  
96 0 $filter = sprintf( $self->filter, ($username) x 10 );
97 0 $search = $connection->search(
98 base => $self->basedn,
99 scope => $self->scope,
100 filter => $filter,
101 attrs => ['1.1']
102 );
103  
104 0 ▶ if ( $search->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($search->is_error) 0 0 0
105  
106 0 my $error = $search->error;
107 0 my $basedn = $self->basedn;
108 0 my $options = qq/basedn '$basedn' with filter '$filter'/;
109  
110 0 ▶ $self->log->error( qq/Failed to search $options. Reason: '$error'/ )
Branch: 1 branch
BranchTrueFalseTotal
if $self->log 0 0 0
111 if $self->log;
112  
113 0 return 0;
114 }
115  
116 0 ▶ if ( $search->count == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($search->count == 0) 0 0 0
117  
118 0 ▶ $self->log->debug( qq/User '$username' was not found with filter '$filter'./ )
Branch: 1 branch
BranchTrueFalseTotal
if $self->log 0 0 0
119 if $self->log;
120  
121 0 return 0;
122 }
123  
124 0 ▶ if ( $search->count > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($search->count > 1) 0 0 0
125  
126 0 my $count = $search->count;
127  
128 0 ▶ $self->log->warn( qq/Found $count matching entries for '$username' with filter '$filter'./ )
Branch: 1 branch
BranchTrueFalseTotal
if $self->log 0 0 0
129 if $self->log;
130 }
131  
132 0 $entry = $search->entry(0);
133 0 $message = $connection->bind( $entry->dn, password => $password );
134 0 $dn = $entry->dn;
135  
136 0 ▶ if ( $message->is_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message->is_error) 0 0 0
137  
138 0 my $error = $message->error;
139 0 ▶ my $level = $message->code == LDAP_INVALID_CREDENTIALS ? 'debug' : 'error';
Branch: 1 branch
BranchTrueFalseTotal
$message->code == 49 ? : 0 0 0
140  
141 0 ▶ $self->log->$level( qq/Failed to authenticate user '$username' with dn '$dn'. Reason: '$error'/ )
Branch: 1 branch
BranchTrueFalseTotal
if $self->log 0 0 0
142 if $self->log;
143  
144 0 return 0;
145 }
146  
147 0 ▶ $self->log->debug( qq/Successfully authenticated user '$username' with dn '$dn'./ )
Branch: 1 branch
BranchTrueFalseTotal
if $self->log 0 0 0
148 if $self->log;
149  
150 0 return $dn;
151 }
152  
153 1 1;
154  
155 __END__
↑ Summary ActiveDirectory.pm »