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,
55 our $users = Catalyst::Authentication::Store::LDAP::Backend->new(\%config);
59 You probably want L<Catalyst::Authentication::Store::LDAP>.
61 Otherwise, this lets you create a store manually.
63 See the L<Catalyst::Authentication::Store::LDAP> documentation for
64 an explanation of the configuration options.
70 package Catalyst::Authentication::Store::LDAP::Backend;
71 use base qw( Class::Accessor::Fast );
76 our $VERSION = '1.015';
78 use Catalyst::Authentication::Store::LDAP::User;
80 use Catalyst::Utils ();
83 __PACKAGE__->mk_accessors(
84 qw( ldap_server ldap_server_options binddn
85 bindpw entry_class user_search_options
86 user_filter user_basedn user_scope
87 user_attrs user_field use_roles role_basedn
88 role_filter role_scope role_field role_value
89 role_search_options start_tls start_tls_options
90 user_results_filter user_class role_search_as_user
97 Creates a new L<Catalyst::Authentication::Store::LDAP::Backend> object.
98 $config should be a hashref, which should contain the configuration options
99 listed in L<Catalyst::Authentication::Store::LDAP>'s documentation.
101 Also sets a few sensible defaults.
106 my ( $class, $config ) = @_;
108 unless ( defined($config) && ref($config) eq "HASH" ) {
109 Catalyst::Exception->throw(
110 "Catalyst::Authentication::Store::LDAP::Backend needs to be configured with a hashref."
113 my %config_hash = %{$config};
114 $config_hash{'binddn'} ||= 'anonymous';
115 $config_hash{'user_filter'} ||= '(uid=%s)';
116 $config_hash{'user_scope'} ||= 'sub';
117 $config_hash{'user_field'} ||= 'uid';
118 $config_hash{'role_filter'} ||= '(memberUid=%s)';
119 $config_hash{'role_scope'} ||= 'sub';
120 $config_hash{'role_field'} ||= 'cn';
121 $config_hash{'use_roles'} ||= '1';
122 $config_hash{'start_tls'} ||= '0';
123 $config_hash{'entry_class'} ||= 'Catalyst::Model::LDAP::Entry';
124 $config_hash{'user_class'}
125 ||= 'Catalyst::Authentication::Store::LDAP::User';
126 $config_hash{'role_search_as_user'} ||= 0;
128 Catalyst::Utils::ensure_class_loaded( $config_hash{'user_class'} );
129 my $self = \%config_hash;
130 bless( $self, $class );
134 =head2 find_user( I<authinfo>, $c )
136 Creates a L<Catalyst::Authentication::Store::LDAP::User> object
137 for the given User ID. This is the preferred mechanism for getting a
138 given User out of the Store.
140 I<authinfo> should be a hashref with a key of either C<id> or
141 C<username>. The value will be compared against the LDAP C<user_field> field.
146 my ( $self, $authinfo, $c ) = @_;
147 return $self->get_user( $authinfo->{id} || $authinfo->{username}, $c );
150 =head2 get_user( I<id>, $c)
152 Creates a L<Catalyst::Authentication::Store::LDAP::User> object
153 for the given User ID, or calls C<new> on the class specified in
154 C<user_class>. This instance of the store object, the results of
155 C<lookup_user> and $c are passed as arguments (in that order) to C<new>.
156 This is the preferred mechanism for getting a given User out of the Store.
161 my ( $self, $id, $c ) = @_;
162 my $user = $self->user_class->new( $self, $self->lookup_user($id), $c );
168 Returns a L<Net::LDAP> object, connected to your LDAP server. (According
169 to how you configured the Backend, of course)
176 if ( defined( $self->ldap_server_options() ) ) {
178 = Net::LDAP->new( $self->ldap_server,
179 %{ $self->ldap_server_options } )
180 or Catalyst::Exception->throw($@);
183 $ldap = Net::LDAP->new( $self->ldap_server )
184 or Catalyst::Exception->throw($@);
186 if ( defined( $self->start_tls ) && $self->start_tls =~ /(1|true)/i ) {
188 if ( defined( $self->start_tls_options ) ) {
189 $mesg = $ldap->start_tls( %{ $self->start_tls_options } );
192 $mesg = $ldap->start_tls;
194 if ( $mesg->is_error ) {
195 Catalyst::Exception->throw( "TLS Error: " . $mesg->error );
201 =head2 ldap_bind($ldap, $binddn, $bindpw)
203 Bind's to the directory. If $ldap is undef, it will connect to the
204 LDAP server first. $binddn should be the DN of the object you wish
205 to bind as, and $bindpw the password.
207 If $binddn is "anonymous", an anonymous bind will be performed.
212 my ( $self, $ldap, $binddn, $bindpw ) = @_;
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);
230 my $mesg = $ldap->bind( $binddn, 'password' => $bindpw );
231 if ( $mesg->is_error ) {
232 Catalyst::Exception->throw(
233 "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 ldap_auth( $binddn, $bindpw )
253 Connect to the LDAP server and do an authenticated bind against the
254 directory. Throws an exception if connecting to the LDAP server fails.
255 Returns 1 if binding succeeds, 0 if it fails.
260 my ( $self, $binddn, $bindpw ) = @_;
261 my $ldap = $self->ldap_connect;
262 if ( !defined $ldap ) {
263 Catalyst::Exception->throw("LDAP server undefined!");
265 my $mesg = $ldap->bind( $binddn, password => $bindpw );
266 return $mesg->is_error ? 0 : 1;
269 =head2 lookup_user($id)
271 Given a User ID, this method will:
273 A) Bind to the directory using the configured binddn and bindpw
274 B) Perform a search for the User Object in the directory, using
275 user_basedn, user_filter, and user_scope.
276 C) Assuming we found the object, we will walk it's attributes
277 using L<Net::LDAP::Entry>'s get_value method. We store the
278 results in a hashref. If we do not find the object, then
280 D) Return a hashref that looks like:
283 'ldap_entry' => $entry, # The Net::LDAP::Entry object
284 'attributes' => $attributes,
287 This method is usually only called by find_user().
292 my ( $self, $id ) = @_;
294 # Trim trailing space or we confuse ourselves
296 my $ldap = $self->ldap_bind;
298 if ( defined( $self->user_basedn ) ) {
299 push( @searchopts, 'base' => $self->user_basedn );
302 Catalyst::Exception->throw(
303 "You must set user_basedn before looking up users!");
305 my $filter = $self->_replace_filter( $self->user_filter, $id );
306 push( @searchopts, 'filter' => $filter );
307 push( @searchopts, 'scope' => $self->user_scope );
308 if ( defined( $self->user_search_options ) ) {
309 push( @searchopts, %{ $self->user_search_options } );
311 my $usersearch = $ldap->search(@searchopts);
313 return undef if ( $usersearch->is_error );
316 my $user_field = $self->user_field;
317 my $results_filter = $self->user_results_filter;
319 if ( defined($results_filter) ) {
320 $entry = &$results_filter($usersearch);
323 $entry = $usersearch->pop_entry;
325 if ( $usersearch->pop_entry ) {
326 Catalyst::Exception->throw(
327 "More than one entry matches user search.\n"
328 . "Consider defining a user_results_filter sub." );
331 # a little extra sanity check with the 'eq' since LDAP already
333 # NOTE that Net::LDAP returns exactly what you asked for, but
334 # because LDAP is often case insensitive, FoO can match foo
335 # and so we normalize with lc().
336 if ( defined($entry) ) {
337 unless ( lc( $entry->get_value($user_field) ) eq lc($id) ) {
338 Catalyst::Exception->throw(
339 "LDAP claims '$user_field' equals '$id' but results entry does not match."
347 unless ($userentry) {
351 foreach my $attr ( $userentry->attributes ) {
352 my @attrvalues = $userentry->get_value($attr);
353 if ( scalar(@attrvalues) == 1 ) {
354 $attrhash->{ lc($attr) } = $attrvalues[0];
357 $attrhash->{ lc($attr) } = \@attrvalues;
361 eval { Catalyst::Utils::ensure_class_loaded( $self->entry_class ) };
363 bless( $userentry, $self->entry_class );
364 $userentry->{_use_unicode}++;
367 'ldap_entry' => $userentry,
368 'attributes' => $attrhash,
373 =head2 lookup_roles($userobj, [$ldap])
375 This method looks up the roles for a given user. It takes a
376 L<Catalyst::Authentication::Store::LDAP::User> object
377 as it's first argument, and can optionally take a I<Net::LDAP> object which
378 is used rather than the default binding if supplied.
380 It returns an array containing the role_field attribute from all the
381 objects that match it's criteria.
386 my ( $self, $userobj, $ldap ) = @_;
387 if ( $self->use_roles == 0 || $self->use_roles =~ /^false$/i ) {
390 $ldap ||= $self->role_search_as_user
391 ? $userobj->ldap_connection : $self->ldap_bind;
393 if ( defined( $self->role_basedn ) ) {
394 push( @searchopts, 'base' => $self->role_basedn );
397 Catalyst::Exception->throw(
398 "You must set up role_basedn before looking up roles!");
400 my $filter_value = $userobj->has_attribute( $self->role_value );
401 if ( !defined($filter_value) ) {
402 Catalyst::Exception->throw( "User object "
406 . " attribute, so I can't look up it's roles!" );
408 my $filter = $self->_replace_filter( $self->role_filter, $filter_value );
409 push( @searchopts, 'filter' => $filter );
410 push( @searchopts, 'scope' => $self->role_scope );
411 push( @searchopts, 'attrs' => [ $self->role_field ] );
412 if ( defined( $self->role_search_options ) ) {
413 push( @searchopts, %{ $self->role_search_options } );
415 my $rolesearch = $ldap->search(@searchopts);
417 RESULT: foreach my $entry ( $rolesearch->entries ) {
418 push( @roles, $entry->get_value( $self->role_field ) );
423 sub _replace_filter {
427 $replace =~ s/([*()\\\x{0}])/sprintf '\\%02x', ord($1)/ge;
428 $filter =~ s/\%s/$replace/g;
435 Catalyst::Authentication::Store::LDAP::User->supports(@_).
442 # this can work as a class method
443 Catalyst::Authentication::Store::LDAP::User->supports(@_);
446 =head2 from_session( I<id>, I<$c> )
448 Returns get_user() for I<id>.
453 my ( $self, $c, $id ) = @_;
454 $self->get_user( $id, $c );
463 Adam Jacob <holoway@cpan.org>
465 Some parts stolen shamelessly and entirely from
466 L<Catalyst::Plugin::Authentication::Store::Htpasswd>.
468 Currently maintained by Peter Karman <karman@cpan.org>.
472 To nothingmuch, ghenry, castaway and the rest of #catalyst for the help. :)
476 L<Catalyst::Authentication::Store::LDAP>, L<Catalyst::Authentication::Store::LDAP::User>, L<Catalyst::Plugin::Authentication>, L<Net::LDAP>
478 =head1 COPYRIGHT & LICENSE
480 Copyright (c) 2005 the aforementioned authors. All rights
481 reserved. This program is free software; you can redistribute
482 it and/or modify it under the same terms as Perl itself.