blib/lib/CGI/Session/Authorizor/LDAP.pm untested

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