blib/lib/CGI/Session/Authenticator/NetLDAP.pm untested

filter: Statementstmt 0.0% 0 / 59 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 112 CC 14 SCAR 36.8
CC
14
Cov
0%
authenticated
72.0
_param
56.0
set
2.0
CRAP 39.5
1 #!/usr/local/bin/perl5
2 #
3  
4 package CGI::Session::Authenticator::NetLDAP;
5  
6 use Carp;
7 use Net::LDAP qw(:all);
8  
9  
10  
11 my %_params = ( -host => '-host',
12 -port => '-port',
13 -bind => '-bind',
14 -cafile => '-cafile',
15 -capath => '-capath',
16 -ciphers => '-ciphers', );
17  
18 sub _param
19 {
20 my $self = shift;
21 if ( scalar @_ == 1 )
22 {
23 my $field = shift;
24  
25 # Hack for db types.
26 #
27 if ( $field eq '-use_mysql' ) { return $db_type eq $DB_MYSQL; }
28 #
29 my $slot = $_params{$field};
30 croak "Programmer Error: $field is not a known parameter" unless defined $slot;
31 return $self->{$slot};
32 }
33 else
34 {
35 while( my $field = shift )
36 {
37 my $slot = $_params{$field};
38  
39 # Hack for db types
40 #
41 if ( $field eq '-use_mysql' ) { $self->use_mysql if shift; return; }
42 #
43 croak "Programmer Error: $field is not a known parameter" unless defined $slot;
44 $self->{$slot} = shift;
45 }
46 }
47 }
48  
49 sub set { _param(shift,@_); }
50  
51 sub new
52 {
53 my $type = shift;
54 my $self = {};
55 bless $self, $type;
56 $self->set( @_ );
57 return $self;
58 }
59  
60 sub host { set( shift(), '-host', @_ ); }
61 sub port { set( shift(), '-port', @_ ); }
62 sub bind { set( shift(), '-bind', @_ ); }
63 sub cafile { set( shift(), '-cafile', @_ ); }
64 sub capath { set( shift(), '-capath', @_ ); }
65 sub ciphers { set( shift(), '-ciphers', @_ ); }
66  
67 sub authenticated
68 {
69 my ( $self, $params ) = @_;
70 my $password = $params->{-password};
71 my $username = $params->{-username};
72  
73 $username = defined $username ? $username : "";
74  
75 my $host = $self->host();
76 my $port = $self->port() ? $self->port : 389 ;
77 my $bind = $self->bind();
78  
79 $bind =~ s/\$username/$username/g;
80  
81 my $connection = new Net::LDAP( $host, port=> $port );
82  
83 # Handle SSL if neccessary.
84 #
85 my $cafile = $self->cafile();
86 my $capath = $self->capath();
87 my $ciphers = $self->ciphers();
88 if ( $cafile or $capath )
89 {
90 my %tls_args = ( verify=>'require' );
91 $tls_args{cafile} = $cafile if $cafile;
92 $tls_args{capath} = $capath if $capath;
93 $tls_args{ciphers} = $ciphers if $ciphers;
94 my $result = $connection->start_tls( %tls_args );
95 }
96  
97 $result = $connection->bind( $bind, password=>$password );
98  
99 if ( $result->code eq LDAP_SUCCESS )
100 {
101 $connection->unbind();
102 return 1;
103 }
104 $connection->unbind();
105 return 0;
106 }
107  
108  
109 =item CGI::Session::Authenticator::NetLDAP
110  
111 An LDAP authenticator. Uses Net::LDAP;
112  
113 $auth = new CGI::Session::Authenticator::NetLdap( -host => $host,
114 -port => $port,
115 -bind => 'uid=$username,ou=People,dc=mycorp,dc=com' );
116  
117 if ( $auth->authenticated( $param ) ) { ...success... }
118 else { ...failure... }
119  
120 =item CGI::Session::Authenticator::authenticated
121  
122 It accepts on argument which is a reference to a hash. This hash contains
123 variables which are used to attempt authentication.
124  
125 The standard values which are used for comparison. The basic ones are
126 '-username' and '-password'.
127  
128 It returns a true value if the user was successfully authenticated, and a
129 false value if the user was not successfully authenticated.
130  
131 =cut
132  
133  
134 1;