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);
57 my ( $self, $c ) = @_;
59 $c->login( $users->get_user( $c->req->param("login") ),
60 $c->req->param("password") );
65 You probably want L<Catalyst::Authentication::Store::LDAP>, unless
66 you are mixing several stores in a single app and one of them is LDAP.
68 Otherwise, this lets you create a store manually.
70 See the L<Catalyst::Authentication::Store::LDAP> documentation for
71 an explanation of the configuration options.
77 package Catalyst::Authentication::Store::LDAP::Backend;
78 use base qw( Class::Accessor::Fast );
83 our $VERSION = '0.1004';
85 use Catalyst::Authentication::Store::LDAP::User;
87 use Catalyst::Utils ();
90 __PACKAGE__->mk_accessors(
91 qw( ldap_server ldap_server_options binddn
92 bindpw entry_class user_search_options
93 user_filter user_basedn user_scope
94 user_attrs user_field use_roles role_basedn
95 role_filter role_scope role_field role_value
96 role_search_options start_tls start_tls_options
97 user_results_filter user_class role_search_as_user
104 Creates a new L<Catalyst::Authentication::Store::LDAP::Backend> object.
105 $config should be a hashref, which should contain the configuration options
106 listed in L<Catalyst::Authentication::Store::LDAP>'s documentation.
108 Also sets a few sensible defaults.
113 my ( $class, $config ) = @_;
115 unless ( defined($config) && ref($config) eq "HASH" ) {
116 Catalyst::Exception->throw(
117 "Catalyst::Authentication::Store::LDAP::Backend needs to be configured with a hashref."
120 my %config_hash = %{$config};
121 $config_hash{'binddn'} ||= 'anonymous';
122 $config_hash{'user_filter'} ||= '(uid=%s)';
123 $config_hash{'user_scope'} ||= 'sub';
124 $config_hash{'user_field'} ||= 'uid';
125 $config_hash{'role_filter'} ||= '(memberUid=%s)';
126 $config_hash{'role_scope'} ||= 'sub';
127 $config_hash{'role_field'} ||= 'cn';
128 $config_hash{'use_roles'} ||= '1';
129 $config_hash{'start_tls'} ||= '0';
130 $config_hash{'entry_class'} ||= 'Catalyst::Model::LDAP::Entry';
131 $config_hash{'user_class'} ||= 'Catalyst::Authentication::Store::LDAP::User';
132 $config_hash{'role_search_as_user'} ||= 0;
134 Catalyst::Utils::ensure_class_loaded($config_hash{'user_class'});
135 my $self = \%config_hash;
136 bless( $self, $class );
140 =head2 find_user( I<authinfo> )
142 Creates a L<Catalyst::Authentication::Store::LDAP::User> object
143 for the given User ID. This is the preferred mechanism for getting a
144 given User out of the Store.
146 I<authinfo> should be a hashref with a key of either C<id> or
147 C<username>. The value will be compared against the LDAP C<user_field> field.
152 my ( $self, $authinfo, $c ) = @_;
153 return $self->get_user( $authinfo->{id} || $authinfo->{username} );
158 Creates a L<Catalyst::Authentication::Store::LDAP::User> object
159 for the given User ID. This is the preferred mechanism for getting a
160 given User out of the Store.
165 my ( $self, $id ) = @_;
166 my $user = $self->user_class->new( $self,
167 $self->lookup_user($id) );
173 Returns a L<Net::LDAP> object, connected to your LDAP server. (According
174 to how you configured the Backend, of course)
181 if ( defined( $self->ldap_server_options() ) ) {
183 = Net::LDAP->new( $self->ldap_server,
184 %{ $self->ldap_server_options } )
185 or Catalyst::Exception->throw($@);
188 $ldap = Net::LDAP->new( $self->ldap_server )
189 or Catalyst::Exception->throw($@);
191 if ( defined( $self->start_tls ) && $self->start_tls =~ /(1|true)/i ) {
193 if ( defined( $self->start_tls_options ) ) {
194 $mesg = $ldap->start_tls( %{ $self->start_tls_options } );
197 $mesg = $ldap->start_tls;
199 if ( $mesg->is_error ) {
200 Catalyst::Exception->throw( "TLS Error: " . $mesg->error );
206 =head2 ldap_bind($ldap, $binddn, $bindpw)
208 Bind's to the directory. If $ldap is undef, it will connect to the
209 LDAP server first. $binddn should be the DN of the object you wish
210 to bind as, and $bindpw the password.
212 If $binddn is "anonymous", an anonymous bind will be performed.
217 my ( $self, $ldap, $binddn, $bindpw, $forauth ) = @_;
219 $ldap ||= $self->ldap_connect;
220 if ( !defined($ldap) ) {
221 Catalyst::Exception->throw("LDAP Server undefined!");
223 $binddn ||= $self->binddn;
224 $bindpw ||= $self->bindpw;
225 if ( $binddn eq "anonymous" ) {
226 $self->_ldap_bind_anon($ldap);
230 my $mesg = $ldap->bind( $binddn, 'password' => $bindpw );
231 if ( $mesg->is_error ) {
233 # If we're not checking this bind for authentication purposes
234 # Go ahead an blow up if we fail.
235 if ( $forauth ne 'forauth' ) {
236 Catalyst::Exception->throw(
237 "Error on Initial Bind: " . $mesg->error );
245 $self->_ldap_bind_anon($ldap, $binddn);
251 sub _ldap_bind_anon {
252 my ($self, $ldap, $dn) = @_;
253 my $mesg = $ldap->bind($dn);
254 if ( $mesg->is_error ) {
255 Catalyst::Exception->throw( "Error on Bind: " . $mesg->error );
259 =head2 lookup_user($id)
261 Given a User ID, this method will:
263 A) Bind to the directory using the configured binddn and bindpw
264 B) Perform a search for the User Object in the directory, using
265 user_basedn, user_filter, and user_scope.
266 C) Assuming we found the object, we will walk it's attributes
267 using L<Net::LDAP::Entry>'s get_value method. We store the
268 results in a hashref.
269 D) Return a hashref that looks like:
272 'ldap_entry' => $entry, # The Net::LDAP::Entry object
273 'attributes' => $attributes,
276 This method is usually only called by find_user().
281 my ( $self, $id ) = @_;
283 # No sneaking in wildcards!
285 Catalyst::Exception->throw("ID $id contains wildcards!");
287 my $ldap = $self->ldap_bind;
289 if ( defined( $self->user_basedn ) ) {
290 push( @searchopts, 'base' => $self->user_basedn );
293 Catalyst::Exception->throw(
294 "You must set user_basedn before looking up users!");
296 my $filter = $self->_replace_filter( $self->user_filter, $id );
297 push( @searchopts, 'filter' => $filter );
298 push( @searchopts, 'scope' => $self->user_scope );
299 if ( defined( $self->user_search_options ) ) {
300 push( @searchopts, %{ $self->user_search_options } );
302 my $usersearch = $ldap->search(@searchopts);
303 if ( $usersearch->is_error ) {
304 Catalyst::Exception->throw(
305 "LDAP Error while searching for user: " . $usersearch->error );
308 my $user_field = $self->user_field;
309 my $results_filter = $self->user_results_filter;
311 if ( defined($results_filter) ) {
312 $entry = &$results_filter($usersearch);
315 $entry = $usersearch->pop_entry;
317 if ( $usersearch->pop_entry ) {
318 Catalyst::Exception->throw(
319 "More than one entry matches user search.\n"
320 . "Consider defining a user_results_filter sub." );
323 # a little extra sanity check with the 'eq' since LDAP already
325 # NOTE that Net::LDAP returns exactly what you asked for, but
326 # because LDAP is often case insensitive, FoO can match foo
327 # and so we normalize with lc().
328 if ( defined($entry) ) {
329 unless ( lc( $entry->get_value($user_field) ) eq lc($id) ) {
330 Catalyst::Exception->throw(
331 "LDAP claims '$user_field' equals '$id' but results entry does not match."
339 unless ($userentry) {
343 foreach my $attr ( $userentry->attributes ) {
344 my @attrvalues = $userentry->get_value($attr);
345 if ( scalar(@attrvalues) == 1 ) {
346 $attrhash->{ lc($attr) } = $attrvalues[0];
349 $attrhash->{ lc($attr) } = \@attrvalues;
353 eval { Catalyst::Utils::ensure_class_loaded($self->entry_class) };
355 bless( $userentry, $self->entry_class );
356 $userentry->{_use_unicode}++;
359 'ldap_entry' => $userentry,
360 'attributes' => $attrhash,
365 =head2 lookup_roles($userobj, [$ldap])
367 This method looks up the roles for a given user. It takes a
368 L<Catalyst::Authentication::Store::LDAP::User> object
369 as it's first argument, and can optionally take a I<Net::LDAP> object which
370 is used rather than the default binding if supplied.
372 It returns an array containing the role_field attribute from all the
373 objects that match it's criteria.
378 my ( $self, $userobj, $ldap ) = @_;
379 if ( $self->use_roles == 0 || $self->use_roles =~ /^false$/i ) {
382 $ldap ||= $self->ldap_bind;
384 if ( defined( $self->role_basedn ) ) {
385 push( @searchopts, 'base' => $self->role_basedn );
388 Catalyst::Exception->throw(
389 "You must set up role_basedn before looking up roles!");
391 my $filter_value = $userobj->has_attribute( $self->role_value );
392 if ( !defined($filter_value) ) {
393 Catalyst::Exception->throw( "User object "
397 . " attribute, so I can't look up it's roles!" );
399 my $filter = $self->_replace_filter( $self->role_filter, $filter_value );
400 push( @searchopts, 'filter' => $filter );
401 push( @searchopts, 'scope' => $self->role_scope );
402 push( @searchopts, 'attrs' => [ $self->role_field ] );
403 if ( defined( $self->role_search_options ) ) {
404 push( @searchopts, %{ $self->role_search_options } );
406 my $rolesearch = $ldap->search(@searchopts);
408 RESULT: foreach my $entry ( $rolesearch->entries ) {
409 push( @roles, $entry->get_value( $self->role_field ) );
414 sub _replace_filter {
418 $filter =~ s/\%s/$replace/g;
425 Catalyst::Authentication::Store::LDAP::User->supports(@_).
432 # this can work as a class method
433 Catalyst::Authentication::Store::LDAP::User->supports(@_);
436 =head2 from_session( I<id> )
438 Returns get_user() for I<id>.
443 my ( $self, $c, $id ) = @_;
444 $self->get_user($id);
453 Adam Jacob <holoway@cpan.org>
455 Some parts stolen shamelessly and entirely from
456 L<Catalyst::Plugin::Authentication::Store::Htpasswd>.
458 Currently maintained by Peter Karman <karman@cpan.org>.
462 To nothingmuch, ghenry, castaway and the rest of #catalyst for the help. :)
466 L<Catalyst::Authentication::Store::LDAP>, L<Catalyst::Authentication::Store::LDAP::User>, L<Catalyst::Plugin::Authentication>, L<Net::LDAP>
468 =head1 COPYRIGHT & LICENSE
470 Copyright (c) 2005 the aforementioned authors. All rights
471 reserved. This program is free software; you can redistribute
472 it and/or modify it under the same terms as Perl itself.