6 Catalyst::Authentication::Store::LDAP::Backend
7 - LDAP authentication storage backend.
11 # you probably just want Store::LDAP under most cases,
12 # but if you insist you can instantiate your own store:
14 use Catalyst::Authentication::Store::LDAP::Backend;
18 Authentication::Credential::Password
22 'ldap_server' => 'ldap1.yourcompany.com',
23 'ldap_server_options' => {
26 'binddn' => 'anonymous',
27 'bindpw' => 'dontcarehow',
29 'start_tls_options' => {
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' => {
39 'user_results_filter' => sub { return shift->pop_entry },
40 'entry_class' => 'MyApp::LDAP::Entry',
41 'user_class' => 'MyUser',
43 'role_basedn' => 'ou=groups,dc=yourcompany,dc=com',
44 'role_filter' => '(&(objectClass=posixGroup)(member=%s))',
45 'role_scope' => 'one',
48 'role_search_options' => {
51 'role_search_as_user' => 0,
54 our $users = Catalyst::Authentication::Store::LDAP::Backend->new(\%config);
58 You probably want L<Catalyst::Authentication::Store::LDAP>.
60 Otherwise, this lets you create a store manually.
62 See the L<Catalyst::Authentication::Store::LDAP> documentation for
63 an explanation of the configuration options.
69 package Catalyst::Authentication::Store::LDAP::Backend;
70 use base qw( Class::Accessor::Fast );
75 our $VERSION = '0.1004';
77 use Catalyst::Authentication::Store::LDAP::User;
79 use Catalyst::Utils ();
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
89 user_results_filter user_class role_search_as_user
96 Creates a new L<Catalyst::Authentication::Store::LDAP::Backend> object.
97 $config should be a hashref, which should contain the configuration options
98 listed in L<Catalyst::Authentication::Store::LDAP>'s documentation.
100 Also sets a few sensible defaults.
105 my ( $class, $config ) = @_;
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."
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';
123 $config_hash{'user_class'} ||= 'Catalyst::Authentication::Store::LDAP::User';
124 $config_hash{'role_search_as_user'} ||= 0;
126 Catalyst::Utils::ensure_class_loaded($config_hash{'user_class'});
127 my $self = \%config_hash;
128 bless( $self, $class );
132 =head2 find_user( I<authinfo> )
134 Creates a L<Catalyst::Authentication::Store::LDAP::User> object
135 for the given User ID. This is the preferred mechanism for getting a
136 given User out of the Store.
138 I<authinfo> should be a hashref with a key of either C<id> or
139 C<username>. The value will be compared against the LDAP C<user_field> field.
144 my ( $self, $authinfo, $c ) = @_;
145 return $self->get_user( $authinfo->{id} || $authinfo->{username} );
150 Creates a L<Catalyst::Authentication::Store::LDAP::User> object
151 for the given User ID. This is the preferred mechanism for getting a
152 given User out of the Store.
157 my ( $self, $id ) = @_;
158 my $user = $self->user_class->new( $self,
159 $self->lookup_user($id) );
165 Returns a L<Net::LDAP> object, connected to your LDAP server. (According
166 to how you configured the Backend, of course)
173 if ( defined( $self->ldap_server_options() ) ) {
175 = Net::LDAP->new( $self->ldap_server,
176 %{ $self->ldap_server_options } )
177 or Catalyst::Exception->throw($@);
180 $ldap = Net::LDAP->new( $self->ldap_server )
181 or Catalyst::Exception->throw($@);
183 if ( defined( $self->start_tls ) && $self->start_tls =~ /(1|true)/i ) {
185 if ( defined( $self->start_tls_options ) ) {
186 $mesg = $ldap->start_tls( %{ $self->start_tls_options } );
189 $mesg = $ldap->start_tls;
191 if ( $mesg->is_error ) {
192 Catalyst::Exception->throw( "TLS Error: " . $mesg->error );
198 =head2 ldap_bind($ldap, $binddn, $bindpw)
200 Bind's to the directory. If $ldap is undef, it will connect to the
201 LDAP server first. $binddn should be the DN of the object you wish
202 to bind as, and $bindpw the password.
204 If $binddn is "anonymous", an anonymous bind will be performed.
209 my ( $self, $ldap, $binddn, $bindpw, $forauth ) = @_;
211 $ldap ||= $self->ldap_connect;
212 if ( !defined($ldap) ) {
213 Catalyst::Exception->throw("LDAP Server undefined!");
215 $binddn ||= $self->binddn;
216 $bindpw ||= $self->bindpw;
217 if ( $binddn eq "anonymous" ) {
218 $self->_ldap_bind_anon($ldap);
222 my $mesg = $ldap->bind( $binddn, 'password' => $bindpw );
223 if ( $mesg->is_error ) {
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 );
237 $self->_ldap_bind_anon($ldap, $binddn);
243 sub _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 );
251 =head2 lookup_user($id)
253 Given a User ID, this method will:
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.
258 C) Assuming we found the object, we will walk it's attributes
259 using L<Net::LDAP::Entry>'s get_value method. We store the
260 results in a hashref.
261 D) Return a hashref that looks like:
264 'ldap_entry' => $entry, # The Net::LDAP::Entry object
265 'attributes' => $attributes,
268 This method is usually only called by find_user().
273 my ( $self, $id ) = @_;
275 # No sneaking in wildcards!
277 Catalyst::Exception->throw("ID $id contains wildcards!");
279 # Trim trailing space or we confuse ourselves
281 my $ldap = $self->ldap_bind;
283 if ( defined( $self->user_basedn ) ) {
284 push( @searchopts, 'base' => $self->user_basedn );
287 Catalyst::Exception->throw(
288 "You must set user_basedn before looking up users!");
290 my $filter = $self->_replace_filter( $self->user_filter, $id );
291 push( @searchopts, 'filter' => $filter );
292 push( @searchopts, 'scope' => $self->user_scope );
293 if ( defined( $self->user_search_options ) ) {
294 push( @searchopts, %{ $self->user_search_options } );
296 my $usersearch = $ldap->search(@searchopts);
297 if ( $usersearch->is_error ) {
298 Catalyst::Exception->throw(
299 "LDAP Error while searching for user: " . $usersearch->error );
302 my $user_field = $self->user_field;
303 my $results_filter = $self->user_results_filter;
305 if ( defined($results_filter) ) {
306 $entry = &$results_filter($usersearch);
309 $entry = $usersearch->pop_entry;
311 if ( $usersearch->pop_entry ) {
312 Catalyst::Exception->throw(
313 "More than one entry matches user search.\n"
314 . "Consider defining a user_results_filter sub." );
317 # a little extra sanity check with the 'eq' since LDAP already
319 # NOTE that Net::LDAP returns exactly what you asked for, but
320 # because LDAP is often case insensitive, FoO can match foo
321 # and so we normalize with lc().
322 if ( defined($entry) ) {
323 unless ( lc( $entry->get_value($user_field) ) eq lc($id) ) {
324 Catalyst::Exception->throw(
325 "LDAP claims '$user_field' equals '$id' but results entry does not match."
333 unless ($userentry) {
337 foreach my $attr ( $userentry->attributes ) {
338 my @attrvalues = $userentry->get_value($attr);
339 if ( scalar(@attrvalues) == 1 ) {
340 $attrhash->{ lc($attr) } = $attrvalues[0];
343 $attrhash->{ lc($attr) } = \@attrvalues;
347 eval { Catalyst::Utils::ensure_class_loaded($self->entry_class) };
349 bless( $userentry, $self->entry_class );
350 $userentry->{_use_unicode}++;
353 'ldap_entry' => $userentry,
354 'attributes' => $attrhash,
359 =head2 lookup_roles($userobj, [$ldap])
361 This method looks up the roles for a given user. It takes a
362 L<Catalyst::Authentication::Store::LDAP::User> object
363 as it's first argument, and can optionally take a I<Net::LDAP> object which
364 is used rather than the default binding if supplied.
366 It returns an array containing the role_field attribute from all the
367 objects that match it's criteria.
372 my ( $self, $userobj, $ldap ) = @_;
373 if ( $self->use_roles == 0 || $self->use_roles =~ /^false$/i ) {
376 $ldap ||= $self->ldap_bind;
378 if ( defined( $self->role_basedn ) ) {
379 push( @searchopts, 'base' => $self->role_basedn );
382 Catalyst::Exception->throw(
383 "You must set up role_basedn before looking up roles!");
385 my $filter_value = $userobj->has_attribute( $self->role_value );
386 if ( !defined($filter_value) ) {
387 Catalyst::Exception->throw( "User object "
391 . " attribute, so I can't look up it's roles!" );
393 my $filter = $self->_replace_filter( $self->role_filter, $filter_value );
394 push( @searchopts, 'filter' => $filter );
395 push( @searchopts, 'scope' => $self->role_scope );
396 push( @searchopts, 'attrs' => [ $self->role_field ] );
397 if ( defined( $self->role_search_options ) ) {
398 push( @searchopts, %{ $self->role_search_options } );
400 my $rolesearch = $ldap->search(@searchopts);
402 RESULT: foreach my $entry ( $rolesearch->entries ) {
403 push( @roles, $entry->get_value( $self->role_field ) );
408 sub _replace_filter {
412 $filter =~ s/\%s/$replace/g;
419 Catalyst::Authentication::Store::LDAP::User->supports(@_).
426 # this can work as a class method
427 Catalyst::Authentication::Store::LDAP::User->supports(@_);
430 =head2 from_session( I<id> )
432 Returns get_user() for I<id>.
437 my ( $self, $c, $id ) = @_;
438 $self->get_user($id);
447 Adam Jacob <holoway@cpan.org>
449 Some parts stolen shamelessly and entirely from
450 L<Catalyst::Plugin::Authentication::Store::Htpasswd>.
452 Currently maintained by Peter Karman <karman@cpan.org>.
456 To nothingmuch, ghenry, castaway and the rest of #catalyst for the help. :)
460 L<Catalyst::Authentication::Store::LDAP>, L<Catalyst::Authentication::Store::LDAP::User>, L<Catalyst::Plugin::Authentication>, L<Net::LDAP>
462 =head1 COPYRIGHT & LICENSE
464 Copyright (c) 2005 the aforementioned authors. All rights
465 reserved. This program is free software; you can redistribute
466 it and/or modify it under the same terms as Perl itself.