Session/blib/lib/CGI/Session/Authenticator/LDAP.pm

filter: Statementstmt 66.6% 4 / 6 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 75.0% 6 / 8 CC 1 SCAR 1.4
CC
1
Cov
67%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« Session/blib/lib/CGI/Session/CookieJar.pm ↑ Summary Session/blib/lib/CGI/Session/Authorizor/NetLDAP.pm »
1 #!/usr/local/bin/perl5
2 #
3  
4 package CGI::Session::Authenticator::LDAP;
5  
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use Mozilla::LDAP::Conn;
Subroutine BEGIN: called CC=1 SCAR=2.6
8  
9 my %_params = ( -host => '-host',
10 -port => '-port',
11 -bind => '-bind', );
12  
13 sub _param
14 {
15 my $self = shift;
16 if ( scalar @_ == 1 )
17 {
18 my $field = shift;
19  
20 # Hack for db types.
21 #
22 if ( $field eq '-use_mysql' ) { return $db_type eq $DB_MYSQL; }
23 #
24 my $slot = $_params{$field};
25 croak "Programmer Error: $field is not a known parameter" unless defined $slot;
26 return $self->{$slot};
27 }
28 else
29 {
30 while( my $field = shift )
31 {
32 my $slot = $_params{$field};
33  
34 # Hack for db types
35 #
36 if ( $field eq '-use_mysql' ) { $self->use_mysql if shift; return; }
37 #
38 croak "Programmer Error: $field is not a known parameter" unless defined $slot;
39 $self->{$slot} = shift;
40 }
41 }
42 }
43  
44 sub set { _param(shift,@_); }
45  
46 sub new
47 {
48 my $type = shift;
49 my $self = {};
50 bless $self, $type;
51 $self->set( @_ );
52 return $self;
53 }
54  
55 sub host { set( shift(), '-host', @_ ); }
56 sub port { set( shift(), '-port', @_ ); }
57 sub bind { set( shift(), '-bind', @_ ); }
58  
59 sub authenticated
60 {
61 my ( $self, $params ) = @_;
62 my $password = $params->{-password};
63 my $username = $params->{-username};
64  
65 $username = defined $username ? $username : "";
66  
67 my %ld = Mozilla::LDAP::Utils::ldapArgs();
68 my $host = $self->host();
69 my $port = $self->port();
70 my $bind = $self->bind();
71 $bind =~ s/\$username/$username/g;
72  
73 my $connection = new Mozilla::LDAP::Conn( $host, $port, $bind, $password );
74 if ( $connection )
75 {
76 $connection->close();
77 return 1;
78 }
79 return 0;
80 }
81  
82  
83 =item CGI::Session::Authenticator::LDAP
84  
85 An LDAP authenticator. Use Mozilla::LDAP.
86  
87 $auth = new CGI::Session::Authenticator( -host => $host,
88 -port => $port,
89 -bind => 'uid=$username,ou=People,dc=mycorp,dc=com' );
90  
91 if ( $auth->authenticated( $param ) ) { ...success... }
92 else { ...failure... }
93  
94 =item CGI::Session::Authenticator::authenticated
95  
96 It accepts on argument which is a reference to a hash. This hash contains
97 variables which are used to attempt authentication.
98  
99 The standard values which are used for comparison. The basic ones are
100 '-username' and '-password'.
101  
102 It returns a true value if the user was successfully authenticated, and a
103 false value if the user was not successfully authenticated.
104  
105 =cut
106  
107  
108 1;
« Session/blib/lib/CGI/Session/CookieJar.pm ↑ Summary Session/blib/lib/CGI/Session/Authorizor/NetLDAP.pm »