blib/lib/Catalyst/Plugin/Authentication/LDAP.pm

filter: Statementstmt 13.1% 11 / 84 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 25.0% 3 / 12 Podpod 100% 9 / 9 totaltotal 13.9% 23 / 165 CC 31 SCAR 45.3
CC
31
Cov
8%
login
210.0
roles
72.0
process_permission
42.0
CRAP 92.4
↑ Summary
1 package Catalyst::Plugin::Authentication::LDAP;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use NEXT;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Net::LDAP;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.02';
8  
9 =head1 NAME
10  
11 Catalyst::Plugin::Authentication::LDAP - LDAP Authentication for Catalyst *DEPRECATED, use Store::LDAP instead*
12  
13 =head1 DEPRECATED
14  
15 WARNING: This module has been superseded by L<Catalyst::Plugin::Authentication::Store::LDAP>, and is therefore no longer
16 maintained.
17  
18 =head1 SYNOPSIS
19  
20 use Catalyst 'Authentication::LDAP';
21 __PACKAGE__->config->{authentication} = (
22 ldap_server => 'ldap://ldap.mycompany.com',
23 default_naming_context => 'dc=mycompany,dc=com',
24 user_context => 'cn=users',
25 user_append => '@mycompany.com',
26 user_filter => '(&(objectclass=user)(objectcategory=user)(samaccountname=__USER__*))',
27 group_attribute => 'memberOf',
28 );
29 $c->login( $user, $password );
30 $c->logout;
31 $c->session_login( $user, $password );
32 $c->session_logout;
33 $c->roles(qw/customer admin/);
34  
35 =head1 DESCRIPTION
36  
37 This plugin allows you to authenticate your web users using an LDAP server. See the L<Configuration>
38 section for more details on how to set it up. This module was designed with Active Directory in mind
39 and has not yet been tested using other LDAP servers. Patches are welcome that enable support for
40 other servers.
41  
42 Note that this plugin requires a session plugin like C<Catalyst::Plugin::Session::FastMmap>.
43  
44 =head1 CONFIGURATION
45  
46 This plugin is configured by passing an "authentication" hash reference to your application's
47 config method. The following keys are supported:
48  
49 ldap_server
50  
51 Required. Specify the full URI to your LDAP server. Some examples are: ldap://ldap.mycompany.com,
52 ldap://pdc:1234, ldaps://secure.ldap.mycompany.com
53  
54 default_naming_context => 'dc=mycompany,dc=com'
55  
56 Required. This is the base context for your server. In most cases, this is a string of two
57 or more "dc" values separated by commas.
58  
59 user_context => 'cn=users',
60  
61 Optional. The context to be used when querying a user's details. This value is prefixed to the
62 default_naming_context. The default value should be suitable for Active Directory servers. If you
63 do not intend to use role-based authentication, you can ignore this option.
64  
65 user_append = '@mycompany.com'
66
67 Optional. This string will be appended to a user's login name when authenticating to the server.
68 Active Directory servers require the user to be specified as "username@mycompany.com".
69  
70 user_filter => '(&(objectclass=user)(objectcategory=user)(samaccountname=__USER__*))'
71  
72 Optional. This filter is used to retrieve the user's account details, specifically the list of
73 groups the user is a member of. For Active Directory servers, the default value should be suitable.
74 The string __USER__ is replaced by the current username. If you do not intend to use role-based
75 authentication, you can ignore this option.
76  
77 group_attribute => 'memberOf'
78
79 Optional. Specify which attribute contains the list of groups/roles the user is a member of.
80  
81 =head2 METHODS
82  
83 =over 4
84  
85 =item login
86  
87 Attempt to authenticate a user. Takes username/password as arguments,
88  
89 $c->login( $user, $password );
90  
91 User remains authenticated until end of request.
92  
93 =cut
94  
95 0 ▶sub login {
Subroutine login: not called CC=14 SCAR=53.5
Pod documented
96 0 my ( $c, $user, $password ) = @_;
97 0 ▶ return 1 if $c->request->{user};
Branch: 1 branch
BranchTrueFalseTotal
if $c->request->{"user"} 0 0 0
98
99 0 my $ldap_server = $c->config->{authentication}->{ldap_server};
100 0 my $dnc = $c->config->{authentication}->{default_naming_context};
101 0 ▶ my $user_append = $c->config->{authentication}->{user_append} || '';
Condition0%0/2
$c->config->{'authentication'}{'user_append'} || ''
l!l
00
MC/DC0%0/1
$c->config->{'authentication'}{'user_append'} || ''
$c->config->{'authentication'}{'user_append'}
Truth table0%0/2
A
A= $c->config->{'authentication'}{'user_append'}
Aresult
11
00
102 0 ▶ my $user_context = $c->config->{authentication}->{user_context} || 'cn=users';
Condition0%0/2
$c->config->{'authentication'}{'user_context'} || 'cn=users'
l!l
00
MC/DC0%0/1
$c->config->{'authentication'}{'user_context'} || 'cn=users'
$c->config->{'authentication'}{'user_context'}
Truth table0%0/2
A
A= $c->config->{'authentication'}{'user_context'}
Aresult
11
00
103 my $user_filter = $c->config->{authentication}->{user_filter} ||
104 0 ▶ '(&(objectclass=user)(objectcategory=user)(samaccountname=__USER__*))';
Condition0%0/2
$c->config->{'authentication'}{'user_filter'} || '(&(objectclass=user)(objectcategory=user)(samaccountname=__USER__*))'
l!l
00
MC/DC0%0/1
$c->config->{'authentication'}{'user_filter'} || '(&(objectclass=user)(objectcategory=user)(samaccountname=__USER__*))'
$c->config->{'authentication'}{'user_filter'}
Truth table0%0/2
A
A= $c->config->{'authentication'}{'user_filter'}
Aresult
11
00
105 0 ▶ my $group_attribute = $c->config->{authentication}->{group_attribute} || 'memberOf';
Condition0%0/2
$c->config->{'authentication'}{'group_attribute'} || 'memberOf'
l!l
00
MC/DC0%0/1
$c->config->{'authentication'}{'group_attribute'} || 'memberOf'
$c->config->{'authentication'}{'group_attribute'}
Truth table0%0/2
A
A= $c->config->{'authentication'}{'group_attribute'}
Aresult
11
00
106
107 0 eval {
108 0 ▶ my $ldap = Net::LDAP->new( $ldap_server ) or die "Unable to connect to LDAP server $ldap_server: $@";
Branch: 1 branch
BranchTrueFalseTotal
my $ldap = "Net::LDAP"->new($ldap_server) or die "Unable to connect to LDAP server ${ldap_server}: $@" 0 0 0
109
110 0 my $rc = $ldap->bind( $user . $user_append, password => $password );
111 0 ▶ die "User authentication failed for $user: " . $rc->error if $rc->code;
Branch: 1 branch
BranchTrueFalseTotal
if $rc->code 0 0 0
112
113 # since most LDAP servers require a password on every connection, we have to query
114 # the user's groups/roles here and store them in the session, since we won't have the
115 # password available later
116 0 $user_filter =~ s/__USER__/$user/;
117 0 my $search = $ldap->search(
118 base => $user_context . "," . $dnc,
119 scope => "sub",
120 filter => $user_filter,
121 );
122 0 ▶ die "Unable to retrieve user details for $user: " . $search->error if $search->code;
Branch: 1 branch
BranchTrueFalseTotal
if $search->code 0 0 0
123  
124 0 foreach my $entry ($search->entries) {
125 0 my @groups = $entry->get_value( $group_attribute );
126 0 ▶ $c->log->debug( "LDAP: User is a member of: " . join ",", @groups ) if $c->debug && scalar @groups;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug and scalar @groups 0 0 0
Condition0%0/2
$c->debug and scalar @groups
!ll
00
MC/DC0%0/1
$c->debug and scalar @groups
$c->debug
Truth table0%0/2
A
A= $c->debug
Aresult
00
11
127 0 ▶ $c->session->{roles} = \@groups if scalar @groups;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @groups 0 0 0
128 }
129
130 0 $ldap->unbind;
131 };
132 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
133 0 $c->log->warn("LDAP: $@");
134 } else {
135 0 $c->request->{user} = $user;
136 0 return 1;
137 }
138 0 return 0;
139 }
140  
141 =item logout
142  
143 Log out the user. will not clear the session, so user will still remain
144 logged in at next request unless session_logout is called.
145  
146 =cut
147  
148 0 ▶sub logout {
Subroutine logout: not called CC=1 SCAR=6.9
Pod documented
149 0 my $c = shift;
150 0 $c->request->{user} = undef;
151 }
152  
153 =item process_permission
154  
155 check for permissions. used by the 'roles' function.
156  
157 =cut
158  
159 0 ▶sub process_permission {
Subroutine process_permission: not called CC=6 SCAR=37.4
Pod documented
160 0 my ( $c, $roles ) = @_;
161 0 ▶ if ($roles) {
Branch: 1 branch
BranchTrueFalseTotal
if ($roles) 0 0 0
162 0 ▶ return 1 if $#$roles < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $roles->$#* < 0 0 0 0
163 0 my $string = join ' ', @$roles;
164 0 ▶ if ( $c->process_roles($roles) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->process_roles($roles)) { } 0 0 0
165 0 ▶ $c->log->debug(qq/Permission granted "$string"/) if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
166 }
167 else {
168 0 ▶ $c->log->debug(qq/Permission denied "$string"/) if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
169 0 return 0;
170 }
171 }
172 0 return 1;
173 }
174  
175 =item roles
176  
177 Check permissions for roles and return true or false.
178  
179 $c->roles(qw/foo bar/);
180  
181 Returns an arrayref containing the verified roles.
182  
183 my @roles = @{ $c->roles };
184  
185 =cut
186  
187 0 ▶sub roles {
Subroutine roles: not called CC=8 SCAR=42.8
Pod documented
188 0 my $c = shift;
189 0 ▶ $c->{roles} ||= [];
Condition0%0/2
$c->{"roles"} ||= []
l!l
00
MC/DC0%0/1
$c->{"roles"} ||= []
$c->{"roles"}
Truth table0%0/2
A
A= $c->{"roles"}
Aresult
11
00
190 0 ▶ my $roles = ref $_[0] eq 'ARRAY' ? $_[0] : [@_];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 0 0
191 0 ▶ if ( $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) 0 0 0
192 0 my @roles;
193 0 foreach my $role (@$roles) {
194 0 ▶ push @roles, $role unless grep $_ eq $role, @{ $c->{roles} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep(($_ eq $role), @{$$c{"roles"};}) 0 0 0
195 }
196 0 ▶ return 1 unless @roles;
Branch: 1 branch
BranchTrueFalseTotal
unless @roles 0 0 0
197 0 ▶ if ( $c->process_permission( \@roles ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->process_permission(\@roles)) { } 0 0 0
198 0 $c->{roles} = [ @{ $c->{roles} }, @roles ];
199 0 return 1;
200 }
201 0 else { return 0 }
202 }
203 0 return $c->{roles};
204 }
205  
206 =item session_login
207  
208 Persistently login the user. The user will remain logged in
209 until he clears the session himself, or session_logout is
210 called.
211  
212 $c->session_login( $user, $password );
213  
214 =cut
215  
216 0 ▶sub session_login {
Subroutine session_login: not called CC=2 SCAR=17.9
Pod documented
217 0 my ( $c, $user, $password ) = @_;
218 0 ▶ return 0 unless $c->login( $user, $password );
Branch: 1 branch
BranchTrueFalseTotal
unless $c->login($user, $password) 0 0 0
219 0 $c->session->{user} = $user;
220 0 return 1;
221 }
222  
223 =item session_logout
224  
225 Session logout. will delete the user object from the session.
226  
227 =cut
228  
229 0 ▶sub session_logout {
Subroutine session_logout: not called CC=1 SCAR=6.9
Pod documented
230 0 my $c = shift;
231 0 $c->logout;
232 0 $c->session->{user} = undef;
233 0 $c->session->{roles} = undef;
234 }
235  
236 =back
237  
238 =head2 EXTENDED METHODS
239  
240 =over 4
241  
242 =item prepare_action
243  
244 sets $c->request->{user} from session.
245  
246 =cut
247  
248 0 ▶sub prepare_action {
Subroutine prepare_action: not called CC=1 SCAR=6.9
Pod documented
249 0 my $c = shift;
250 0 $c->NEXT::prepare_action(@_);
251 0 $c->request->{user} = $c->session->{user};
252 }
253  
254 =item setup
255  
256 sets up $c->config->{authentication}.
257  
258 =cut
259  
260 0 ▶sub setup {
Subroutine setup: not called CC=2 SCAR=17.9
Pod documented
261 0 my $c = shift;
262 0 my $conf = $c->config->{authentication};
263 0 ▶ $conf = ref $conf eq 'ARRAY' ? {@$conf} : $conf;
Branch: 1 branch
BranchTrueFalseTotal
ref $conf eq 'ARRAY' ? : 0 0 0
264 0 $c->config->{authentication} = $conf;
265 0 return $c->NEXT::setup(@_);
266 }
267  
268 =back
269  
270 =head2 OVERLOADED METHODS
271  
272 =over 4
273  
274 =item process_roles
275  
276 Takes an arrayref of roles and checks if user has the supplied roles.
277 Returns 1/0.
278  
279 =cut
280  
281 0 ▶sub process_roles {
Subroutine process_roles: not called CC=4 SCAR=30.0
Pod documented
282 0 my ( $c, $roles ) = @_;
283  
284 0 ▶ return 0 unless exists $c->{session}->{roles};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $c->{"session"}{"roles"} 0 0 0
285
286 0 for my $role (@$roles) {
287 0 ▶ return 1 if grep $_ =~ /$role/i, @{ $c->{session}->{roles} };
Branch: 1 branch
BranchTrueFalseTotal
if grep(($_ =~ /$role/i), @{$$c{"session"}{"roles"};}) 0 0 0
288 }
289 0 return 0;
290 }
291  
292 =back
293  
294 =head1 LIMITATIONS
295  
296 Because many LDAP servers require a password to query information, the user's group/role
297 data must be queried and stored at the time they login. This means that group/role data
298 updated on the LDAP server after a user logs in will not be reflected in their session until
299 they logout and log back in.
300  
301 =head1 SEE ALSO
302  
303 L<Catalyst>, L<Catalyst::Plugin::Authentication::CDBI>.
304  
305 =head1 AUTHOR
306  
307 Andy Grundman, C<andy@hybridized.org>
308  
309 Based on Catalyst::Plugin::Authentication::CDBI by:
310 Sebastian Riedel, C<sri@cpan.org>
311 Marcus Ramberg, C<mramberg@cpan.org>
312  
313 =head1 COPYRIGHT
314  
315 This program is free software, you can redistribute it and/or modify it under
316 the same terms as Perl itself.
317  
318 =cut
319  
320 1 1;
↑ Summary