File Coverage

blib/lib/PlugAuth/Plugin/LDAP.pm
Criterion Covered Total %
statement 32 35 91.4
branch 7 14 50.0
condition 1 3 33.3
subroutine 7 7 100.0
pod 1 1 100.0
total 48 60 80.0


line stmt bran cond sub pod time code
1             package PlugAuth::Plugin::LDAP;
2              
3             # ABSTRACT: LDAP back end for PlugAuth
4             our $VERSION = '0.08'; # VERSION
5              
6              
7 2     2   133521 use strict;
  2         2  
  2         49  
8 2     2   6 use warnings;
  2         1  
  2         41  
9 2     2   16 use v5.10;
  2         4  
10 2     2   703 use Net::LDAP;
  2         104575  
  2         34  
11 2     2   750 use Log::Log4perl qw/:easy/;
  2         32192  
  2         31  
12 2     2   1281 use Role::Tiny::With;
  2         3238  
  2         410  
13              
14             with 'PlugAuth::Role::Plugin';
15             with 'PlugAuth::Role::Auth';
16              
17              
18             sub check_credentials {
19 3     3 1 75856 my ($class, $user,$pw) = @_;
20 3         7 $user = lc $user;
21              
22 3         11 my $ldap_config = $class->global_config->ldap(default => '');
23              
24 3 50 33     51 if (!$ldap_config or !$ldap_config->{authoritative}) {
25             # Check files first.
26 0 0       0 return 1 if $class->deligate_check_credentials($user, $pw);
27             }
28 3 50       7 return 0 unless $ldap_config;
29 3 50       10 my $server = $ldap_config->{server} or LOGDIE "Missing ldap server";
30 3 50       16 my $ldap = Net::LDAP->new($server, timeout => 5) or do {
31 0         0 ERROR "Could not connect to ldap server $server: $@";
32 0         0 return 0;
33             };
34 3         7 my $orig = $user;
35 3         9 my $extra = $user =~ tr/a-zA-Z0-9@._-//dc;
36 3 50       8 WARN "Invalid username '$orig', turned into $user" if $extra;
37 3         10 my $dn = sprintf($ldap_config->{dn},$user);
38 3         9 my $mesg = $ldap->bind($dn, password => $pw);
39 3 100       10 $mesg->code or return 1;
40 2         5 INFO "Ldap returned ".$mesg->code." : ".$mesg->error;
41 2         1391 return 0;
42             }
43              
44             1;
45              
46             __END__