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' => {
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 = '1.014';
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'}
124 ||= 'Catalyst::Authentication::Store::LDAP::User';
125 $config_hash{'role_search_as_user'} ||= 0;
127 Catalyst::Utils::ensure_class_loaded( $config_hash{'user_class'} );
128 my $self = \%config_hash;
129 bless( $self, $class );
133 =head2 find_user( I<authinfo>, $c )
135 Creates a L<Catalyst::Authentication::Store::LDAP::User> object
136 for the given User ID. This is the preferred mechanism for getting a
137 given User out of the Store.
139 I<authinfo> should be a hashref with a key of either C<id> or
140 C<username>. The value will be compared against the LDAP C<user_field> field.
145 my ( $self, $authinfo, $c ) = @_;
146 return $self->get_user( $authinfo->{id} || $authinfo->{username}, $c );
149 =head2 get_user( I<id>, $c)
151 Creates a L<Catalyst::Authentication::Store::LDAP::User> object
152 for the given User ID, or calls C<new> on the class specified in
153 C<user_class>. This instance of the store object, the results of
154 C<lookup_user> and $c are passed as arguments (in that order) to C<new>.
155 This is the preferred mechanism for getting a given User out of the Store.
160 my ( $self, $id, $c ) = @_;
161 my $user = $self->user_class->new( $self, $self->lookup_user($id), $c );
167 Returns a L<Net::LDAP> object, connected to your LDAP server. (According
168 to how you configured the Backend, of course)
175 if ( defined( $self->ldap_server_options() ) ) {
177 = Net::LDAP->new( $self->ldap_server,
178 %{ $self->ldap_server_options } )
179 or Catalyst::Exception->throw($@);
182 $ldap = Net::LDAP->new( $self->ldap_server )
183 or Catalyst::Exception->throw($@);
185 if ( defined( $self->start_tls ) && $self->start_tls =~ /(1|true)/i ) {
187 if ( defined( $self->start_tls_options ) ) {
188 $mesg = $ldap->start_tls( %{ $self->start_tls_options } );
191 $mesg = $ldap->start_tls;
193 if ( $mesg->is_error ) {
194 Catalyst::Exception->throw( "TLS Error: " . $mesg->error );
200 =head2 ldap_bind($ldap, $binddn, $bindpw)
202 Bind's to the directory. If $ldap is undef, it will connect to the
203 LDAP server first. $binddn should be the DN of the object you wish
204 to bind as, and $bindpw the password.
206 If $binddn is "anonymous", an anonymous bind will be performed.
211 my ( $self, $ldap, $binddn, $bindpw, $forauth ) = @_;
213 $ldap ||= $self->ldap_connect;
214 if ( !defined($ldap) ) {
215 Catalyst::Exception->throw("LDAP Server undefined!");
218 # if username is present, make sure password is present too.
219 # see https://rt.cpan.org/Ticket/Display.html?id=81908
220 if ( !defined $binddn ) {
221 $binddn = $self->binddn;
222 $bindpw = $self->bindpw;
225 if ( $binddn eq "anonymous" ) {
226 $self->_ldap_bind_anon($ldap);
229 # Don't fall back to unauthenticated bind when authenticating
230 if ($bindpw or $forauth eq 'forauth') {
231 my $mesg = $ldap->bind( $binddn, 'password' => $bindpw );
232 if ( $mesg->is_error ) {
234 # If we're not checking this bind for authentication purposes
235 # Go ahead an blow up if we fail.
236 if ( $forauth ne 'forauth' ) {
237 Catalyst::Exception->throw(
238 "Error on Initial Bind: " . $mesg->error );
246 $self->_ldap_bind_anon( $ldap, $binddn );
252 sub _ldap_bind_anon {
253 my ( $self, $ldap, $dn ) = @_;
254 my $mesg = $ldap->bind($dn);
255 if ( $mesg->is_error ) {
256 Catalyst::Exception->throw( "Error on Bind: " . $mesg->error );
260 =head2 lookup_user($id)
262 Given a User ID, this method will:
264 A) Bind to the directory using the configured binddn and bindpw
265 B) Perform a search for the User Object in the directory, using
266 user_basedn, user_filter, and user_scope.
267 C) Assuming we found the object, we will walk it's attributes
268 using L<Net::LDAP::Entry>'s get_value method. We store the
269 results in a hashref. If we do not find the object, then
271 D) Return a hashref that looks like:
274 'ldap_entry' => $entry, # The Net::LDAP::Entry object
275 'attributes' => $attributes,
278 This method is usually only called by find_user().
283 my ( $self, $id ) = @_;
285 # Trim trailing space or we confuse ourselves
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);
304 return undef if ( $usersearch->is_error );
307 my $user_field = $self->user_field;
308 my $results_filter = $self->user_results_filter;
310 if ( defined($results_filter) ) {
311 $entry = &$results_filter($usersearch);
314 $entry = $usersearch->pop_entry;
316 if ( $usersearch->pop_entry ) {
317 Catalyst::Exception->throw(
318 "More than one entry matches user search.\n"
319 . "Consider defining a user_results_filter sub." );
322 # a little extra sanity check with the 'eq' since LDAP already
324 # NOTE that Net::LDAP returns exactly what you asked for, but
325 # because LDAP is often case insensitive, FoO can match foo
326 # and so we normalize with lc().
327 if ( defined($entry) ) {
328 unless ( lc( $entry->get_value($user_field) ) eq lc($id) ) {
329 Catalyst::Exception->throw(
330 "LDAP claims '$user_field' equals '$id' but results entry does not match."
338 unless ($userentry) {
342 foreach my $attr ( $userentry->attributes ) {
343 my @attrvalues = $userentry->get_value($attr);
344 if ( scalar(@attrvalues) == 1 ) {
345 $attrhash->{ lc($attr) } = $attrvalues[0];
348 $attrhash->{ lc($attr) } = \@attrvalues;
352 eval { Catalyst::Utils::ensure_class_loaded( $self->entry_class ) };
354 bless( $userentry, $self->entry_class );
355 $userentry->{_use_unicode}++;
358 'ldap_entry' => $userentry,
359 'attributes' => $attrhash,
364 =head2 lookup_roles($userobj, [$ldap])
366 This method looks up the roles for a given user. It takes a
367 L<Catalyst::Authentication::Store::LDAP::User> object
368 as it's first argument, and can optionally take a I<Net::LDAP> object which
369 is used rather than the default binding if supplied.
371 It returns an array containing the role_field attribute from all the
372 objects that match it's criteria.
377 my ( $self, $userobj, $ldap ) = @_;
378 if ( $self->use_roles == 0 || $self->use_roles =~ /^false$/i ) {
381 $ldap ||= $self->ldap_bind;
383 if ( defined( $self->role_basedn ) ) {
384 push( @searchopts, 'base' => $self->role_basedn );
387 Catalyst::Exception->throw(
388 "You must set up role_basedn before looking up roles!");
390 my $filter_value = $userobj->has_attribute( $self->role_value );
391 if ( !defined($filter_value) ) {
392 Catalyst::Exception->throw( "User object "
396 . " attribute, so I can't look up it's roles!" );
398 my $filter = $self->_replace_filter( $self->role_filter, $filter_value );
399 push( @searchopts, 'filter' => $filter );
400 push( @searchopts, 'scope' => $self->role_scope );
401 push( @searchopts, 'attrs' => [ $self->role_field ] );
402 if ( defined( $self->role_search_options ) ) {
403 push( @searchopts, %{ $self->role_search_options } );
405 my $rolesearch = $ldap->search(@searchopts);
407 RESULT: foreach my $entry ( $rolesearch->entries ) {
408 push( @roles, $entry->get_value( $self->role_field ) );
413 sub _replace_filter {
417 $replace =~ s/([*()\\\x{0}])/sprintf '\\%02x', ord($1)/ge;
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>, I<$c> )
438 Returns get_user() for I<id>.
443 my ( $self, $c, $id ) = @_;
444 $self->get_user( $id, $c );
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.