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', # or 'sub' for Active Directory
35 'user_field' => 'uid',
36 'user_search_options' => {
38 'attrs' => [qw( distinguishedname name mail )],
40 'user_results_filter' => sub { return shift->pop_entry },
41 'entry_class' => 'MyApp::LDAP::Entry',
42 'user_class' => 'MyUser',
44 'role_basedn' => 'ou=groups,dc=yourcompany,dc=com',
45 'role_filter' => '(&(objectClass=posixGroup)(member=%s))',
46 'role_scope' => 'one',
49 'role_search_options' => {
52 'role_search_as_user' => 0,
53 'persist_in_session' => 'all',
56 our $users = Catalyst::Authentication::Store::LDAP::Backend->new(\%config);
60 You probably want L<Catalyst::Authentication::Store::LDAP>.
62 Otherwise, this lets you create a store manually.
64 See the L<Catalyst::Authentication::Store::LDAP> documentation for
65 an explanation of the configuration options.
71 package Catalyst::Authentication::Store::LDAP::Backend;
72 use base qw( Class::Accessor::Fast );
77 our $VERSION = '1.015';
79 use Catalyst::Authentication::Store::LDAP::User;
81 use Catalyst::Utils ();
84 __PACKAGE__->mk_accessors(
85 qw( ldap_server ldap_server_options binddn
86 bindpw entry_class user_search_options
87 user_filter user_basedn user_scope
88 user_attrs user_field use_roles role_basedn
89 role_filter role_scope role_field role_value
90 role_search_options start_tls start_tls_options
91 user_results_filter user_class role_search_as_user
99 Creates a new L<Catalyst::Authentication::Store::LDAP::Backend> object.
100 $config should be a hashref, which should contain the configuration options
101 listed in L<Catalyst::Authentication::Store::LDAP>'s documentation.
103 Also sets a few sensible defaults.
108 my ( $class, $config ) = @_;
110 unless ( defined($config) && ref($config) eq "HASH" ) {
111 Catalyst::Exception->throw(
112 "Catalyst::Authentication::Store::LDAP::Backend needs to be configured with a hashref."
115 my %config_hash = %{$config};
116 $config_hash{'binddn'} ||= 'anonymous';
117 $config_hash{'user_filter'} ||= '(uid=%s)';
118 $config_hash{'user_scope'} ||= 'sub';
119 $config_hash{'user_field'} ||= 'uid';
120 $config_hash{'role_filter'} ||= '(memberUid=%s)';
121 $config_hash{'role_scope'} ||= 'sub';
122 $config_hash{'role_field'} ||= 'cn';
123 $config_hash{'use_roles'} ||= '1';
124 $config_hash{'start_tls'} ||= '0';
125 $config_hash{'entry_class'} ||= 'Catalyst::Model::LDAP::Entry';
126 $config_hash{'user_class'}
127 ||= 'Catalyst::Authentication::Store::LDAP::User';
128 $config_hash{'role_search_as_user'} ||= 0;
129 $config_hash{'persist_in_session'} ||= 'username';
130 Catalyst::Exception->throw('persist_in_session must be either username or all')
131 unless $config_hash{'persist_in_session'} =~ /\A(?:username|all)\z/;
133 Catalyst::Utils::ensure_class_loaded( $config_hash{'user_class'} );
134 my $self = \%config_hash;
135 bless( $self, $class );
139 =head2 find_user( I<authinfo>, $c )
141 Creates a L<Catalyst::Authentication::Store::LDAP::User> object
142 for the given User ID. This is the preferred mechanism for getting a
143 given User out of the Store.
145 I<authinfo> should be a hashref with a key of either C<id> or
146 C<username>. The value will be compared against the LDAP C<user_field> field.
151 my ( $self, $authinfo, $c ) = @_;
152 return $self->get_user( $authinfo->{id} || $authinfo->{username}, $c );
155 =head2 get_user( I<id>, $c)
157 Creates a L<Catalyst::Authentication::Store::LDAP::User> object
158 for the given User ID, or calls C<new> on the class specified in
159 C<user_class>. This instance of the store object, the results of
160 C<lookup_user> and $c are passed as arguments (in that order) to C<new>.
161 This is the preferred mechanism for getting a given User out of the Store.
166 my ( $self, $id, $c ) = @_;
167 my $user = $self->user_class->new( $self, $self->lookup_user($id), $c );
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 ) = @_;
218 $ldap ||= $self->ldap_connect;
219 if ( !defined($ldap) ) {
220 Catalyst::Exception->throw("LDAP Server undefined!");
223 # if username is present, make sure password is present too.
224 # see https://rt.cpan.org/Ticket/Display.html?id=81908
225 if ( !defined $binddn ) {
226 $binddn = $self->binddn;
227 $bindpw = $self->bindpw;
230 if ( $binddn eq "anonymous" ) {
231 $self->_ldap_bind_anon($ldap);
235 my $mesg = $ldap->bind( $binddn, 'password' => $bindpw );
236 if ( $mesg->is_error ) {
237 Catalyst::Exception->throw(
238 "Error on Initial Bind: " . $mesg->error );
242 $self->_ldap_bind_anon( $ldap, $binddn );
248 sub _ldap_bind_anon {
249 my ( $self, $ldap, $dn ) = @_;
250 my $mesg = $ldap->bind($dn);
251 if ( $mesg->is_error ) {
252 Catalyst::Exception->throw( "Error on Bind: " . $mesg->error );
256 =head2 ldap_auth( $binddn, $bindpw )
258 Connect to the LDAP server and do an authenticated bind against the
259 directory. Throws an exception if connecting to the LDAP server fails.
260 Returns 1 if binding succeeds, 0 if it fails.
265 my ( $self, $binddn, $bindpw ) = @_;
266 my $ldap = $self->ldap_connect;
267 if ( !defined $ldap ) {
268 Catalyst::Exception->throw("LDAP server undefined!");
270 my $mesg = $ldap->bind( $binddn, password => $bindpw );
271 return $mesg->is_error ? 0 : 1;
274 =head2 lookup_user($id)
276 Given a User ID, this method will:
278 A) Bind to the directory using the configured binddn and bindpw
279 B) Perform a search for the User Object in the directory, using
280 user_basedn, user_filter, and user_scope.
281 C) Assuming we found the object, we will walk it's attributes
282 using L<Net::LDAP::Entry>'s get_value method. We store the
283 results in a hashref. If we do not find the object, then
285 D) Return a hashref that looks like:
288 'ldap_entry' => $entry, # The Net::LDAP::Entry object
289 'attributes' => $attributes,
292 This method is usually only called by find_user().
297 my ( $self, $id ) = @_;
299 # Trim trailing space or we confuse ourselves
301 my $ldap = $self->ldap_bind;
303 if ( defined( $self->user_basedn ) ) {
304 push( @searchopts, 'base' => $self->user_basedn );
307 Catalyst::Exception->throw(
308 "You must set user_basedn before looking up users!");
310 my $filter = $self->_replace_filter( $self->user_filter, $id );
311 push( @searchopts, 'filter' => $filter );
312 push( @searchopts, 'scope' => $self->user_scope );
313 if ( defined( $self->user_search_options ) ) {
314 push( @searchopts, %{ $self->user_search_options } );
316 my $usersearch = $ldap->search(@searchopts);
318 return undef if ( $usersearch->is_error );
321 my $user_field = $self->user_field;
322 my $results_filter = $self->user_results_filter;
324 if ( defined($results_filter) ) {
325 $entry = &$results_filter($usersearch);
328 $entry = $usersearch->pop_entry;
330 if ( $usersearch->pop_entry ) {
331 Catalyst::Exception->throw(
332 "More than one entry matches user search.\n"
333 . "Consider defining a user_results_filter sub." );
336 # a little extra sanity check with the 'eq' since LDAP already
338 # NOTE that Net::LDAP returns exactly what you asked for, but
339 # because LDAP is often case insensitive, FoO can match foo
340 # and so we normalize with lc().
341 if ( defined($entry) ) {
342 unless ( lc( $entry->get_value($user_field) ) eq lc($id) ) {
343 Catalyst::Exception->throw(
344 "LDAP claims '$user_field' equals '$id' but results entry does not match."
352 unless ($userentry) {
356 foreach my $attr ( $userentry->attributes ) {
357 my @attrvalues = $userentry->get_value($attr);
358 if ( scalar(@attrvalues) == 1 ) {
359 $attrhash->{ lc($attr) } = $attrvalues[0];
362 $attrhash->{ lc($attr) } = \@attrvalues;
366 eval { Catalyst::Utils::ensure_class_loaded( $self->entry_class ) };
368 bless( $userentry, $self->entry_class );
369 $userentry->{_use_unicode}++;
372 'ldap_entry' => $userentry,
373 'attributes' => $attrhash,
378 =head2 lookup_roles($userobj, [$ldap])
380 This method looks up the roles for a given user. It takes a
381 L<Catalyst::Authentication::Store::LDAP::User> object
382 as it's first argument, and can optionally take a I<Net::LDAP> object which
383 is used rather than the default binding if supplied.
385 It returns an array containing the role_field attribute from all the
386 objects that match it's criteria.
391 my ( $self, $userobj, $ldap ) = @_;
392 if ( $self->use_roles == 0 || $self->use_roles =~ /^false$/i ) {
395 $ldap ||= $self->role_search_as_user
396 ? $userobj->ldap_connection : $self->ldap_bind;
398 if ( defined( $self->role_basedn ) ) {
399 push( @searchopts, 'base' => $self->role_basedn );
402 Catalyst::Exception->throw(
403 "You must set up role_basedn before looking up roles!");
405 my $filter_value = $userobj->has_attribute( $self->role_value );
406 if ( !defined($filter_value) ) {
407 Catalyst::Exception->throw( "User object "
411 . " attribute, so I can't look up it's roles!" );
413 my $filter = $self->_replace_filter( $self->role_filter, $filter_value );
414 push( @searchopts, 'filter' => $filter );
415 push( @searchopts, 'scope' => $self->role_scope );
416 push( @searchopts, 'attrs' => [ $self->role_field ] );
417 if ( defined( $self->role_search_options ) ) {
418 push( @searchopts, %{ $self->role_search_options } );
420 my $rolesearch = $ldap->search(@searchopts);
422 RESULT: foreach my $entry ( $rolesearch->entries ) {
423 push( @roles, $entry->get_value( $self->role_field ) );
428 sub _replace_filter {
432 $replace =~ s/([*()\\\x{0}])/sprintf '\\%02x', ord($1)/ge;
433 $filter =~ s/\%s/$replace/g;
440 Catalyst::Authentication::Store::LDAP::User->supports(@_).
447 # this can work as a class method
448 Catalyst::Authentication::Store::LDAP::User->supports(@_);
451 =head2 from_session( I<id>, I<$c> )
453 Returns get_user() for I<id>.
458 my ( $self, $c, $frozenuser ) = @_;
460 if ( $self->persist_in_session eq 'all' ) {
461 return $self->user_class->new( $self, $frozenuser->{user}, $c, $frozenuser->{_roles} );
464 return $self->get_user( $frozenuser, $c );
473 Adam Jacob <holoway@cpan.org>
475 Some parts stolen shamelessly and entirely from
476 L<Catalyst::Plugin::Authentication::Store::Htpasswd>.
478 Currently maintained by Peter Karman <karman@cpan.org>.
482 To nothingmuch, ghenry, castaway and the rest of #catalyst for the help. :)
486 L<Catalyst::Authentication::Store::LDAP>, L<Catalyst::Authentication::Store::LDAP::User>, L<Catalyst::Plugin::Authentication>, L<Net::LDAP>
488 =head1 COPYRIGHT & LICENSE
490 Copyright (c) 2005 the aforementioned authors. All rights
491 reserved. This program is free software; you can redistribute
492 it and/or modify it under the same terms as Perl itself.