Bump required Module::Install version in everything. janus++
[catagits/Catalyst-Authentication-Store-LDAP.git] / lib / Catalyst / Authentication / Store / LDAP / Backend.pm
CommitLineData
f66d606b 1
2=pod
3
4=head1 NAME
5
6Catalyst::Authentication::Store::LDAP::Backend
7 - LDAP authentication storage backend.
8
9=head1 SYNOPSIS
10
11 # you probably just want Store::LDAP under most cases,
12 # but if you insist you can instantiate your own store:
13
14 use Catalyst::Authentication::Store::LDAP::Backend;
15
16 use Catalyst qw/
17 Authentication
18 Authentication::Credential::Password
19 /;
20
21 my %config = (
22 'ldap_server' => 'ldap1.yourcompany.com',
23 'ldap_server_options' => {
24 'timeout' => 30,
25 },
26 'binddn' => 'anonymous',
27 'bindpw' => 'dontcarehow',
28 'start_tls' => 1,
29 'start_tls_options' => {
30 'verify' => 'none',
31 },
32 'user_basedn' => 'ou=people,dc=yourcompany,dc=com',
33 'user_filter' => '(&(objectClass=posixAccount)(uid=%s))',
34 'user_scope' => 'one',
35 'user_field' => 'uid',
36 'user_search_options' => {
37 'deref' => 'always',
38 },
1647b33a 39 'user_results_filter' => sub { return shift->pop_entry },
f66d606b 40 'entry_class' => 'MyApp::LDAP::Entry',
405489b5 41 'user_class' => 'MyUser',
f66d606b 42 'use_roles' => 1,
43 'role_basedn' => 'ou=groups,dc=yourcompany,dc=com',
44 'role_filter' => '(&(objectClass=posixGroup)(member=%s))',
45 'role_scope' => 'one',
46 'role_field' => 'cn',
47 'role_value' => 'dn',
48 'role_search_options' => {
49 'deref' => 'always',
50 },
405489b5 51 'role_search_as_user' => 0,
f66d606b 52 );
53
54 our $users = Catalyst::Authentication::Store::LDAP::Backend->new(\%config);
55
f66d606b 56=head1 DESCRIPTION
57
afb8e81c 58You probably want L<Catalyst::Authentication::Store::LDAP>.
f66d606b 59
afb8e81c 60Otherwise, this lets you create a store manually.
f66d606b 61
62See the L<Catalyst::Authentication::Store::LDAP> documentation for
63an explanation of the configuration options.
64
65=head1 METHODS
66
67=cut
68
69package Catalyst::Authentication::Store::LDAP::Backend;
70use base qw( Class::Accessor::Fast );
71
72use strict;
73use warnings;
74
d94851da 75our $VERSION = '0.1005';
f66d606b 76
77use Catalyst::Authentication::Store::LDAP::User;
78use Net::LDAP;
405489b5 79use Catalyst::Utils ();
f66d606b 80
81BEGIN {
82 __PACKAGE__->mk_accessors(
83 qw( ldap_server ldap_server_options binddn
84 bindpw entry_class user_search_options
85 user_filter user_basedn user_scope
86 user_attrs user_field use_roles role_basedn
87 role_filter role_scope role_field role_value
88 role_search_options start_tls start_tls_options
405489b5 89 user_results_filter user_class role_search_as_user
f66d606b 90 )
91 );
92}
93
94=head2 new($config)
95
96Creates a new L<Catalyst::Authentication::Store::LDAP::Backend> object.
97$config should be a hashref, which should contain the configuration options
98listed in L<Catalyst::Authentication::Store::LDAP>'s documentation.
99
100Also sets a few sensible defaults.
101
102=cut
103
104sub new {
105 my ( $class, $config ) = @_;
106
107 unless ( defined($config) && ref($config) eq "HASH" ) {
108 Catalyst::Exception->throw(
109 "Catalyst::Authentication::Store::LDAP::Backend needs to be configured with a hashref."
110 );
111 }
112 my %config_hash = %{$config};
113 $config_hash{'binddn'} ||= 'anonymous';
114 $config_hash{'user_filter'} ||= '(uid=%s)';
115 $config_hash{'user_scope'} ||= 'sub';
116 $config_hash{'user_field'} ||= 'uid';
117 $config_hash{'role_filter'} ||= '(memberUid=%s)';
118 $config_hash{'role_scope'} ||= 'sub';
119 $config_hash{'role_field'} ||= 'cn';
120 $config_hash{'use_roles'} ||= '1';
121 $config_hash{'start_tls'} ||= '0';
122 $config_hash{'entry_class'} ||= 'Catalyst::Model::LDAP::Entry';
405489b5 123 $config_hash{'user_class'} ||= 'Catalyst::Authentication::Store::LDAP::User';
124 $config_hash{'role_search_as_user'} ||= 0;
f66d606b 125
405489b5 126 Catalyst::Utils::ensure_class_loaded($config_hash{'user_class'});
f66d606b 127 my $self = \%config_hash;
128 bless( $self, $class );
129 return $self;
130}
131
132=head2 find_user( I<authinfo> )
133
134Creates a L<Catalyst::Authentication::Store::LDAP::User> object
135for the given User ID. This is the preferred mechanism for getting a
136given User out of the Store.
137
138I<authinfo> should be a hashref with a key of either C<id> or
139C<username>. The value will be compared against the LDAP C<user_field> field.
140
141=cut
142
143sub find_user {
144 my ( $self, $authinfo, $c ) = @_;
145 return $self->get_user( $authinfo->{id} || $authinfo->{username} );
146}
147
148=head2 get_user($id)
149
150Creates a L<Catalyst::Authentication::Store::LDAP::User> object
151for the given User ID. This is the preferred mechanism for getting a
152given User out of the Store.
153
154=cut
155
156sub get_user {
157 my ( $self, $id ) = @_;
405489b5 158 my $user = $self->user_class->new( $self,
f66d606b 159 $self->lookup_user($id) );
160 return $user;
161}
162
163=head2 ldap_connect
164
165Returns a L<Net::LDAP> object, connected to your LDAP server. (According
166to how you configured the Backend, of course)
167
168=cut
169
170sub ldap_connect {
171 my ($self) = shift;
172 my $ldap;
173 if ( defined( $self->ldap_server_options() ) ) {
174 $ldap
175 = Net::LDAP->new( $self->ldap_server,
176 %{ $self->ldap_server_options } )
177 or Catalyst::Exception->throw($@);
178 }
179 else {
180 $ldap = Net::LDAP->new( $self->ldap_server )
181 or Catalyst::Exception->throw($@);
182 }
183 if ( defined( $self->start_tls ) && $self->start_tls =~ /(1|true)/i ) {
184 my $mesg;
185 if ( defined( $self->start_tls_options ) ) {
186 $mesg = $ldap->start_tls( %{ $self->start_tls_options } );
187 }
188 else {
189 $mesg = $ldap->start_tls;
190 }
191 if ( $mesg->is_error ) {
192 Catalyst::Exception->throw( "TLS Error: " . $mesg->error );
193 }
194 }
195 return $ldap;
196}
197
198=head2 ldap_bind($ldap, $binddn, $bindpw)
199
200Bind's to the directory. If $ldap is undef, it will connect to the
201LDAP server first. $binddn should be the DN of the object you wish
202to bind as, and $bindpw the password.
203
204If $binddn is "anonymous", an anonymous bind will be performed.
205
206=cut
207
208sub ldap_bind {
209 my ( $self, $ldap, $binddn, $bindpw, $forauth ) = @_;
210 $forauth ||= 0;
211 $ldap ||= $self->ldap_connect;
212 if ( !defined($ldap) ) {
213 Catalyst::Exception->throw("LDAP Server undefined!");
214 }
215 $binddn ||= $self->binddn;
216 $bindpw ||= $self->bindpw;
217 if ( $binddn eq "anonymous" ) {
405489b5 218 $self->_ldap_bind_anon($ldap);
f66d606b 219 }
220 else {
221 if ($bindpw) {
222 my $mesg = $ldap->bind( $binddn, 'password' => $bindpw );
223 if ( $mesg->is_error ) {
224
225 # If we're not checking this bind for authentication purposes
226 # Go ahead an blow up if we fail.
227 if ( $forauth ne 'forauth' ) {
228 Catalyst::Exception->throw(
229 "Error on Initial Bind: " . $mesg->error );
230 }
231 else {
232 return undef;
233 }
234 }
235 }
236 else {
405489b5 237 $self->_ldap_bind_anon($ldap, $binddn);
f66d606b 238 }
239 }
240 return $ldap;
241}
242
405489b5 243sub _ldap_bind_anon {
244 my ($self, $ldap, $dn) = @_;
245 my $mesg = $ldap->bind($dn);
246 if ( $mesg->is_error ) {
247 Catalyst::Exception->throw( "Error on Bind: " . $mesg->error );
248 }
249}
250
f66d606b 251=head2 lookup_user($id)
252
253Given a User ID, this method will:
254
255 A) Bind to the directory using the configured binddn and bindpw
256 B) Perform a search for the User Object in the directory, using
257 user_basedn, user_filter, and user_scope.
d94851da 258 C) Assuming we found the object, we will walk it's attributes
f66d606b 259 using L<Net::LDAP::Entry>'s get_value method. We store the
d94851da 260 results in a hashref. If we do not find the object, then
261 undef is returned.
262 D) Return a hashref that looks like:
263
f66d606b 264 $results = {
265 'ldap_entry' => $entry, # The Net::LDAP::Entry object
266 'attributes' => $attributes,
267 }
268
1647b33a 269This method is usually only called by find_user().
f66d606b 270
271=cut
272
273sub lookup_user {
274 my ( $self, $id ) = @_;
275
276 # No sneaking in wildcards!
277 if ( $id =~ /\*/ ) {
278 Catalyst::Exception->throw("ID $id contains wildcards!");
279 }
280 my $ldap = $self->ldap_bind;
281 my @searchopts;
282 if ( defined( $self->user_basedn ) ) {
283 push( @searchopts, 'base' => $self->user_basedn );
284 }
285 else {
286 Catalyst::Exception->throw(
287 "You must set user_basedn before looking up users!");
288 }
289 my $filter = $self->_replace_filter( $self->user_filter, $id );
290 push( @searchopts, 'filter' => $filter );
291 push( @searchopts, 'scope' => $self->user_scope );
292 if ( defined( $self->user_search_options ) ) {
293 push( @searchopts, %{ $self->user_search_options } );
294 }
295 my $usersearch = $ldap->search(@searchopts);
d94851da 296
297 return if ( $usersearch->is_error );
298
f66d606b 299 my $userentry;
1647b33a 300 my $user_field = $self->user_field;
301 my $results_filter = $self->user_results_filter;
302 my $entry;
303 if ( defined($results_filter) ) {
304 $entry = &$results_filter($usersearch);
305 }
306 else {
307 $entry = $usersearch->pop_entry;
308 }
309 if ( $usersearch->pop_entry ) {
310 Catalyst::Exception->throw(
311 "More than one entry matches user search.\n"
312 . "Consider defining a user_results_filter sub." );
313 }
314
315 # a little extra sanity check with the 'eq' since LDAP already
316 # says it matches.
5772b468 317 # NOTE that Net::LDAP returns exactly what you asked for, but
318 # because LDAP is often case insensitive, FoO can match foo
319 # and so we normalize with lc().
1647b33a 320 if ( defined($entry) ) {
5772b468 321 unless ( lc( $entry->get_value($user_field) ) eq lc($id) ) {
1647b33a 322 Catalyst::Exception->throw(
323 "LDAP claims '$user_field' equals '$id' but results entry does not match."
324 );
f66d606b 325 }
1647b33a 326 $userentry = $entry;
f66d606b 327 }
1647b33a 328
f66d606b 329 $ldap->unbind;
330 $ldap->disconnect;
331 unless ($userentry) {
332 return undef;
333 }
334 my $attrhash;
335 foreach my $attr ( $userentry->attributes ) {
336 my @attrvalues = $userentry->get_value($attr);
337 if ( scalar(@attrvalues) == 1 ) {
338 $attrhash->{ lc($attr) } = $attrvalues[0];
339 }
340 else {
341 $attrhash->{ lc($attr) } = \@attrvalues;
342 }
343 }
405489b5 344
345 eval { Catalyst::Utils::ensure_class_loaded($self->entry_class) };
f66d606b 346 if ( !$@ ) {
347 bless( $userentry, $self->entry_class );
348 $userentry->{_use_unicode}++;
349 }
350 my $rv = {
351 'ldap_entry' => $userentry,
352 'attributes' => $attrhash,
353 };
354 return $rv;
355}
356
405489b5 357=head2 lookup_roles($userobj, [$ldap])
f66d606b 358
359This method looks up the roles for a given user. It takes a
360L<Catalyst::Authentication::Store::LDAP::User> object
405489b5 361as it's first argument, and can optionally take a I<Net::LDAP> object which
362is used rather than the default binding if supplied.
f66d606b 363
364It returns an array containing the role_field attribute from all the
365objects that match it's criteria.
366
367=cut
368
369sub lookup_roles {
405489b5 370 my ( $self, $userobj, $ldap ) = @_;
f66d606b 371 if ( $self->use_roles == 0 || $self->use_roles =~ /^false$/i ) {
372 return undef;
373 }
405489b5 374 $ldap ||= $self->ldap_bind;
f66d606b 375 my @searchopts;
376 if ( defined( $self->role_basedn ) ) {
377 push( @searchopts, 'base' => $self->role_basedn );
378 }
379 else {
380 Catalyst::Exception->throw(
381 "You must set up role_basedn before looking up roles!");
382 }
383 my $filter_value = $userobj->has_attribute( $self->role_value );
384 if ( !defined($filter_value) ) {
385 Catalyst::Exception->throw( "User object "
386 . $userobj->username
387 . " has no "
388 . $self->role_value
389 . " attribute, so I can't look up it's roles!" );
390 }
391 my $filter = $self->_replace_filter( $self->role_filter, $filter_value );
392 push( @searchopts, 'filter' => $filter );
393 push( @searchopts, 'scope' => $self->role_scope );
394 push( @searchopts, 'attrs' => [ $self->role_field ] );
395 if ( defined( $self->role_search_options ) ) {
396 push( @searchopts, %{ $self->role_search_options } );
397 }
398 my $rolesearch = $ldap->search(@searchopts);
399 my @roles;
ab62b426 400RESULT: foreach my $entry ( $rolesearch->entries ) {
401 push( @roles, $entry->get_value( $self->role_field ) );
f66d606b 402 }
403 return @roles;
404}
405
406sub _replace_filter {
407 my $self = shift;
408 my $filter = shift;
409 my $replace = shift;
410 $filter =~ s/\%s/$replace/g;
411 return $filter;
412}
413
414=head2 user_supports
415
416Returns the value of
417Catalyst::Authentication::Store::LDAP::User->supports(@_).
418
419=cut
420
421sub user_supports {
422 my $self = shift;
423
424 # this can work as a class method
425 Catalyst::Authentication::Store::LDAP::User->supports(@_);
426}
427
428=head2 from_session( I<id> )
429
430Returns get_user() for I<id>.
431
432=cut
433
434sub from_session {
435 my ( $self, $c, $id ) = @_;
436 $self->get_user($id);
437}
438
4391;
440
441__END__
442
443=head1 AUTHORS
444
445Adam Jacob <holoway@cpan.org>
446
447Some parts stolen shamelessly and entirely from
448L<Catalyst::Plugin::Authentication::Store::Htpasswd>.
449
450Currently maintained by Peter Karman <karman@cpan.org>.
451
452=head1 THANKS
453
454To nothingmuch, ghenry, castaway and the rest of #catalyst for the help. :)
455
456=head1 SEE ALSO
457
458L<Catalyst::Authentication::Store::LDAP>, L<Catalyst::Authentication::Store::LDAP::User>, L<Catalyst::Plugin::Authentication>, L<Net::LDAP>
459
460=head1 COPYRIGHT & LICENSE
461
462Copyright (c) 2005 the aforementioned authors. All rights
463reserved. This program is free software; you can redistribute
464it and/or modify it under the same terms as Perl itself.
465
466=cut
467